Skip to content

Commit 9643a16

Browse files
authored
feat(mistral): add async batch job chat completion example (#329)
* feat(mistral): add async batch job chat completion example Adds a new example demonstrating how to use Mistral's async batch job API for chat completion. The example creates a batch job with multiple requests, monitors its status, and prints the results once completed. This provides a practical implementation of asynchronous batch processing with Mistral's API. * fix
1 parent eb85b6c commit 9643a16

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from mistralai import Mistral, BatchRequest, UserMessage
2+
import os
3+
import asyncio
4+
5+
6+
async def main():
7+
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
8+
9+
requests = [BatchRequest(
10+
custom_id=str(i),
11+
body=dict(
12+
model="mistral-medium-latest",
13+
messages=[UserMessage(
14+
content=f"What's i + {i}"
15+
)]
16+
)
17+
) for i in range(5)
18+
]
19+
20+
job = await client.batch.jobs.create_async(
21+
requests=requests,
22+
model="mistral-small-latest",
23+
endpoint="/v1/chat/completions",
24+
metadata={"job_type": "testing"}
25+
)
26+
27+
print(f"Created job with ID: {job.id}")
28+
29+
while job.status not in ["SUCCESS", "FAILED"]:
30+
await asyncio.sleep(1)
31+
job = await client.batch.jobs.get_async(job_id=job.id)
32+
print(f"Job status: {job.status}")
33+
34+
print(f"Job is done, status {job.status}")
35+
for res in job.outputs:
36+
print(res["response"]["body"])
37+
38+
if __name__ == "__main__":
39+
asyncio.run(main())
40+

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mistralai"
3-
version = "1.10.0"
3+
version = "1.10.1"
44
description = "Python Client SDK for the Mistral AI API."
55
authors = [{ name = "Mistral" }]
66
requires-python = ">=3.10"

0 commit comments

Comments
 (0)