-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathchat_streaming.py
More file actions
executable file
·45 lines (38 loc) · 1.29 KB
/
chat_streaming.py
File metadata and controls
executable file
·45 lines (38 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
import os
from mistralai.client import Mistral
from mistralai.client.models import File
from mistralai.client.models import UserMessage
def main():
api_key = os.environ["MISTRAL_API_KEY"]
model = "voxtral-small-latest"
client = Mistral(api_key=api_key)
with open("examples/fixtures/bcn_weather.mp3", "rb") as f:
file = client.files.upload(
file=File(content=f, file_name=f.name), purpose="audio"
)
print(f"Uploaded audio file, id={file.id}")
signed_url = client.files.get_signed_url(file_id=file.id)
try:
chat_response = client.chat.complete(
stream=True,
model=model,
messages=[
UserMessage(
content=[
{"type": "text", "text": "What is this audio about?"},
{
"type": "input_audio",
"input_audio": signed_url.url,
},
]
)
],
)
for chunk in chat_response:
print(chunk.data.choices[0].delta.content)
finally:
client.files.delete(file_id=file.id)
print(f"Deleted audio file, id={file.id}")
if __name__ == "__main__":
main()