Skip to content

Commit c77d50f

Browse files
committed
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.
1 parent eb85b6c commit c77d50f

1 file changed

Lines changed: 40 additions & 0 deletions

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+

0 commit comments

Comments
 (0)