Skip to content

Commit 4e30549

Browse files
committed
add support for video_url in chat.completions
1 parent 9e6120f commit 4e30549

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,61 @@ from together import Together
5757

5858
client = Together(api_key=os.environ.get("TOGETHER_API_KEY"))
5959

60+
# Simple text message
6061
response = client.chat.completions.create(
6162
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
6263
messages=[{"role": "user", "content": "tell me about new york"}],
6364
)
6465
print(response.choices[0].message.content)
66+
67+
# Multi-modal message with text and image
68+
response = client.chat.completions.create(
69+
model="meta-llama/Llama-3.2-11B-Vision-Instruct-Turbo",
70+
messages=[{
71+
"role": "user",
72+
"content": [
73+
{
74+
"type": "text",
75+
"text": "What's in this image?"
76+
},
77+
{
78+
"type": "image_url",
79+
"image_url": {
80+
"url": "https://example.com/image.jpg"
81+
}
82+
}
83+
]
84+
}]
85+
)
86+
87+
# Multi-modal message with text and video
88+
response = client.chat.completions.create(
89+
model="Qwen/Qwen2.5-VL-72B-Instruct",
90+
messages=[{
91+
"role": "user",
92+
"content": [
93+
{
94+
"type": "text",
95+
"text": "What's happening in this video?"
96+
},
97+
{
98+
"type": "video_url",
99+
"video_url": {
100+
"url": "https://example.com/video.mp4"
101+
}
102+
}
103+
]
104+
}]
105+
)
65106
```
66107

108+
The chat completions API supports three types of content:
109+
- Plain text messages using the `content` field directly
110+
- Multi-modal messages with images using `type: "image_url"`
111+
- Multi-modal messages with videos using `type: "video_url"`
112+
113+
When using multi-modal content, the `content` field becomes an array of content objects, each with its own type and corresponding data.
114+
67115
#### Streaming
68116

69117
```python

src/together/types/chat_completions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,22 @@ class ToolCalls(BaseModel):
4444
class ChatCompletionMessageContentType(str, Enum):
4545
TEXT = "text"
4646
IMAGE_URL = "image_url"
47+
VIDEO_URL = "video_url"
4748

4849

4950
class ChatCompletionMessageContentImageURL(BaseModel):
5051
url: str
5152

5253

54+
class ChatCompletionMessageContentVideoURL(BaseModel):
55+
url: str
56+
57+
5358
class ChatCompletionMessageContent(BaseModel):
5459
type: ChatCompletionMessageContentType
5560
text: str | None = None
5661
image_url: ChatCompletionMessageContentImageURL | None = None
62+
video_url: ChatCompletionMessageContentVideoURL | None = None
5763

5864

5965
class ChatCompletionMessage(BaseModel):

tests/unit/test_video_url.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from together.types.chat_completions import (
2+
ChatCompletionMessage,
3+
ChatCompletionMessageContent,
4+
ChatCompletionMessageContentType,
5+
ChatCompletionMessageContentVideoURL,
6+
MessageRole,
7+
)
8+
9+
10+
def test_video_url_message():
11+
# Test creating a message with video_url content
12+
message = ChatCompletionMessage(
13+
role=MessageRole.USER,
14+
content=[
15+
ChatCompletionMessageContent(
16+
type=ChatCompletionMessageContentType.TEXT, text="What's in this video?"
17+
),
18+
ChatCompletionMessageContent(
19+
type=ChatCompletionMessageContentType.VIDEO_URL,
20+
video_url=ChatCompletionMessageContentVideoURL(
21+
url="https://example.com/video.mp4"
22+
),
23+
),
24+
],
25+
)
26+
27+
# Verify the message structure
28+
assert message.role == MessageRole.USER
29+
assert isinstance(message.content, list)
30+
assert len(message.content) == 2
31+
32+
# Verify text content
33+
assert message.content[0].type == ChatCompletionMessageContentType.TEXT
34+
assert message.content[0].text == "What's in this video?"
35+
assert message.content[0].video_url is None
36+
37+
# Verify video_url content
38+
assert message.content[1].type == ChatCompletionMessageContentType.VIDEO_URL
39+
assert message.content[1].text is None
40+
assert message.content[1].video_url.url == "https://example.com/video.mp4"

0 commit comments

Comments
 (0)