Skip to content

Commit fac88af

Browse files
committed
fix: add examples
1 parent de2d19d commit fac88af

10 files changed

+223
-0
lines changed

examples/async_batch_jobs.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import asyncio
2+
3+
from mistralai import Mistral
4+
5+
client = Mistral()
6+
7+
async def main():
8+
await client.batch.jobs.create_async(
9+
endpoint="/v1/ocr"
10+
)
11+
print("Job created")
12+
13+
asyncio.run(main())

examples/audio_chat_base64.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
import base64
3+
import os
4+
5+
from mistralai import Mistral
6+
from mistralai.models import UserMessage
7+
8+
9+
10+
def main():
11+
api_key = os.environ["MISTRAL_API_KEY"]
12+
model = "voxtral-small-latest"
13+
14+
client = Mistral(api_key=api_key)
15+
with open("examples/files/bcn_weather.mp3", "rb") as f:
16+
content = f.read()
17+
chat_response = client.chat.complete(
18+
model=model,
19+
messages=[UserMessage(content=[
20+
{"type": "text", "text": "What's in this audio file?"},
21+
{
22+
"type": "input_audio",
23+
"input_audio": base64.b64encode(content).decode('utf-8'),
24+
},
25+
])],
26+
)
27+
print(chat_response.choices[0].message.content)
28+
29+
30+
if __name__ == "__main__":
31+
main()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
5+
from mistralai import Mistral
6+
from mistralai.models import UserMessage
7+
8+
9+
10+
def main():
11+
api_key = os.environ["MISTRAL_API_KEY"]
12+
model = "voxtral-small-latest"
13+
14+
client = Mistral(api_key=api_key)
15+
16+
chat_response = client.chat.complete(
17+
model=model,
18+
messages=[UserMessage(content=[
19+
{"type": "text", "text": "What is this audio about?"},
20+
{
21+
"type": "input_audio",
22+
"input_audio": "https://docs.mistral.ai/audio/bcn_weather.mp3",
23+
},
24+
])],
25+
)
26+
print(chat_response.choices[0].message.content)
27+
28+
29+
if __name__ == "__main__":
30+
main()

examples/audio_chat_streaming.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
5+
from mistralai import Mistral, File
6+
from mistralai.models import UserMessage
7+
8+
9+
10+
def main():
11+
api_key = os.environ["MISTRAL_API_KEY"]
12+
model = "voxtral-small-latest"
13+
14+
client = Mistral(api_key=api_key)
15+
with open("examples/files/bcn_weather.mp3", "rb") as f:
16+
file = client.files.upload(file=File(content=f, file_name=f.name), purpose="audio")
17+
print(f"Uploaded audio file, id={file.id}")
18+
signed_url = client.files.get_signed_url(file_id=file.id)
19+
try:
20+
chat_response = client.chat.stream(
21+
model=model,
22+
messages=[UserMessage(content=[
23+
{"type": "text", "text": "What is this audio about?"},
24+
{
25+
"type": "input_audio",
26+
"input_audio": signed_url.url,
27+
},
28+
])],
29+
)
30+
for chunk in chat_response:
31+
print(chunk.data.choices[0].delta.content)
32+
finally:
33+
client.files.delete(file_id=file.id)
34+
print(f"Deleted audio file, id={file.id}")
35+
36+
if __name__ == "__main__":
37+
main()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import asyncio
5+
from mistralai import Mistral, File
6+
7+
8+
async def main():
9+
api_key = os.environ["MISTRAL_API_KEY"]
10+
model = "voxtral-mini-latest"
11+
12+
client = Mistral(api_key=api_key)
13+
with open("examples/files/bcn_weather.mp3", "rb") as f:
14+
response = await client.audio.transcriptions.complete_async(
15+
model=model,
16+
file=File(content=f, file_name=f.name),
17+
)
18+
print(response.text)
19+
20+
21+
if __name__ == "__main__":
22+
asyncio.run(main())
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
5+
from mistralai import Mistral
6+
7+
8+
def main():
9+
api_key = os.environ["MISTRAL_API_KEY"]
10+
model = "voxtral-mini-latest"
11+
12+
client = Mistral(api_key=api_key)
13+
response = client.audio.transcriptions.complete(
14+
model=model,
15+
file_url="https://docs.mistral.ai/audio/bcn_weather.mp3",
16+
timestamp_granularities=["segment"],
17+
)
18+
print(response.text)
19+
print(response.segments)
20+
21+
22+
if __name__ == "__main__":
23+
main()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
5+
from mistralai import Mistral
6+
7+
8+
def main():
9+
api_key = os.environ["MISTRAL_API_KEY"]
10+
model = "voxtral-mini-latest"
11+
12+
client = Mistral(api_key=api_key)
13+
response = client.audio.transcriptions.stream(
14+
model=model,
15+
file_url="https://docs.mistral.ai/audio/bcn_weather.mp3",
16+
timestamp_granularities=["segment"],
17+
)
18+
for chunk in response:
19+
print(chunk)
20+
21+
22+
if __name__ == "__main__":
23+
main()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
import asyncio
3+
import os
4+
5+
from mistralai import Mistral, File
6+
7+
8+
async def main():
9+
api_key = os.environ["MISTRAL_API_KEY"]
10+
model = "voxtral-mini-2507"
11+
12+
client = Mistral(api_key=api_key)
13+
with open("examples/files/bcn_weather.mp3", "rb") as f:
14+
response = await client.audio.transcriptions.stream_async(
15+
model=model,
16+
file=File(content=f, file_name=f.name),
17+
)
18+
async for chunk in response:
19+
print(chunk.event, chunk.data)
20+
21+
22+
if __name__ == "__main__":
23+
asyncio.run(main())
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
5+
from mistralai import Mistral
6+
7+
8+
def main():
9+
api_key = os.environ["MISTRAL_API_KEY"]
10+
model = "voxtral-mini-latest"
11+
12+
client = Mistral(api_key=api_key)
13+
response = client.audio.transcriptions.complete(
14+
model=model,
15+
file_url="https://docs.mistral.ai/audio/bcn_weather.mp3",
16+
)
17+
print(response.text)
18+
19+
20+
if __name__ == "__main__":
21+
main()

examples/files/bcn_weather.mp3

345 KB
Binary file not shown.

0 commit comments

Comments
 (0)