You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/async_structured_outputs.py
+9-4Lines changed: 9 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -5,9 +5,10 @@
5
5
frompydanticimportBaseModel
6
6
7
7
frommistralaiimportMistral
8
+
fromtypingimportList
8
9
9
-
asyncdefmain():
10
10
11
+
asyncdefmain():
11
12
api_key=os.environ["MISTRAL_API_KEY"]
12
13
client=Mistral(api_key=api_key)
13
14
@@ -16,18 +17,22 @@ class Explanation(BaseModel):
16
17
output: str
17
18
18
19
classMathDemonstration(BaseModel):
19
-
steps: list[Explanation]
20
+
steps: List[Explanation]
20
21
final_answer: str
21
22
22
23
chat_response=awaitclient.chat.parse_async(
23
24
model="mistral-large-2411",
24
25
messages=[
25
-
{"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."},
26
+
{
27
+
"role": "system",
28
+
"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.",
29
+
},
26
30
{"role": "user", "content": "How can I solve 8x + 7 = -23"},
Copy file name to clipboardExpand all lines: examples/structured_outputs.py
+19-7Lines changed: 19 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,9 @@
5
5
6
6
frommistralaiimportMistral
7
7
8
+
fromtypingimportList
9
+
10
+
8
11
defmain():
9
12
api_key=os.environ["MISTRAL_API_KEY"]
10
13
client=Mistral(api_key=api_key)
@@ -14,32 +17,41 @@ class Explanation(BaseModel):
14
17
output: str
15
18
16
19
classMathDemonstration(BaseModel):
17
-
steps: list[Explanation]
20
+
steps: List[Explanation]
18
21
final_answer: str
19
22
20
23
print("Using the .parse method to parse the response into a Pydantic model:\n")
21
24
chat_response=client.chat.parse(
22
25
model="mistral-large-latest",
23
26
messages=[
24
-
{"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."},
27
+
{
28
+
"role": "system",
29
+
"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.",
30
+
},
25
31
{"role": "user", "content": "How can I solve 8x + 7 = -23"},
26
32
],
27
-
response_format=MathDemonstration
33
+
response_format=MathDemonstration,
28
34
)
29
35
print(chat_response.choices[0].message.parsed)
30
36
31
37
# Or with the streaming API
32
-
print("\nUsing the .parse_stream method to stream back the response into a JSON Schema:\n")
38
+
print(
39
+
"\nUsing the .parse_stream method to stream back the response into a JSON Schema:\n"
40
+
)
33
41
withclient.chat.parse_stream(
34
42
model="mistral-large-latest",
35
43
messages=[
36
-
{"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."},
44
+
{
45
+
"role": "system",
46
+
"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.",
47
+
},
37
48
{"role": "user", "content": "How can I solve 8x + 7 = -23"},
0 commit comments