From e03d38bfa4e2146f2bb6b84f2d40c34ae4db8a42 Mon Sep 17 00:00:00 2001 From: Alexandre Menasria Date: Mon, 10 Mar 2025 10:24:01 +0100 Subject: [PATCH 1/2] [CI] Fix the CI for Python 3.8 --- examples/async_structured_outputs.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/examples/async_structured_outputs.py b/examples/async_structured_outputs.py index 4fafc991..560934e9 100644 --- a/examples/async_structured_outputs.py +++ b/examples/async_structured_outputs.py @@ -5,9 +5,10 @@ from pydantic import BaseModel from mistralai import Mistral +from typing import List -async def main(): +async def main(): api_key = os.environ["MISTRAL_API_KEY"] client = Mistral(api_key=api_key) @@ -16,18 +17,22 @@ class Explanation(BaseModel): output: str class MathDemonstration(BaseModel): - steps: list[Explanation] + steps: List[Explanation] final_answer: str chat_response = await client.chat.parse_async( model="mistral-large-2411", messages=[ - {"role": "system", "content": "You are a helpful math tutor. You will be provided with a math problem, and your goal will be to output a step by step solution, along with a final answer. For each step, just provide the output as an equation use the explanation field to detail the reasoning."}, + { + "role": "system", + "content": "You are a helpful math tutor. You will be provided with a math problem, and your goal will be to output a step by step solution, along with a final answer. For each step, just provide the output as an equation use the explanation field to detail the reasoning.", + }, {"role": "user", "content": "How can I solve 8x + 7 = -23"}, ], - response_format = MathDemonstration + response_format=MathDemonstration, ) print(chat_response.choices[0].message.parsed) + if __name__ == "__main__": asyncio.run(main()) From 7e82ba5558bc140030950ac9398d1880361c3d18 Mon Sep 17 00:00:00 2001 From: Alexandre Menasria Date: Mon, 10 Mar 2025 10:37:24 +0100 Subject: [PATCH 2/2] Fix the non async example as well --- examples/structured_outputs.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/examples/structured_outputs.py b/examples/structured_outputs.py index 15dc1bff..299f7509 100644 --- a/examples/structured_outputs.py +++ b/examples/structured_outputs.py @@ -5,6 +5,9 @@ from mistralai import Mistral +from typing import List + + def main(): api_key = os.environ["MISTRAL_API_KEY"] client = Mistral(api_key=api_key) @@ -14,32 +17,41 @@ class Explanation(BaseModel): output: str class MathDemonstration(BaseModel): - steps: list[Explanation] + steps: List[Explanation] final_answer: str print("Using the .parse method to parse the response into a Pydantic model:\n") chat_response = client.chat.parse( model="mistral-large-latest", messages=[ - {"role": "system", "content": "You are a helpful math tutor. You will be provided with a math problem, and your goal will be to output a step by step solution, along with a final answer. For each step, just provide the output as an equation use the explanation field to detail the reasoning."}, + { + "role": "system", + "content": "You are a helpful math tutor. You will be provided with a math problem, and your goal will be to output a step by step solution, along with a final answer. For each step, just provide the output as an equation use the explanation field to detail the reasoning.", + }, {"role": "user", "content": "How can I solve 8x + 7 = -23"}, ], - response_format = MathDemonstration + response_format=MathDemonstration, ) print(chat_response.choices[0].message.parsed) # Or with the streaming API - print("\nUsing the .parse_stream method to stream back the response into a JSON Schema:\n") + print( + "\nUsing the .parse_stream method to stream back the response into a JSON Schema:\n" + ) with client.chat.parse_stream( model="mistral-large-latest", messages=[ - {"role": "system", "content": "You are a helpful math tutor. You will be provided with a math problem, and your goal will be to output a step by step solution, along with a final answer. For each step, just provide the output as an equation use the explanation field to detail the reasoning."}, + { + "role": "system", + "content": "You are a helpful math tutor. You will be provided with a math problem, and your goal will be to output a step by step solution, along with a final answer. For each step, just provide the output as an equation use the explanation field to detail the reasoning.", + }, {"role": "user", "content": "How can I solve 8x + 7 = -23"}, ], - response_format=MathDemonstration + response_format=MathDemonstration, ) as stream: for chunk in stream: print(chunk.data.choices[0].delta.content, end="") + if __name__ == "__main__": - main() \ No newline at end of file + main()