Skip to content

Commit a7d02c3

Browse files
authored
Add examples for using raw json schema without pydantic (#210)
1 parent fde481b commit a7d02c3

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
client = Mistral(api_key=api_key)
11+
12+
print("Using the .complete method to input a raw json schema to the API:\n")
13+
# When providing raw JSON Schema to the SDK you need to have 'additionalProperties': False in the schema definition
14+
# This is because the API is only accepting a strict JSON Schema
15+
chat_response = client.chat.complete(
16+
model="mistral-large-latest",
17+
messages=[
18+
{
19+
"role": "system",
20+
"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.",
21+
},
22+
{"role": "user", "content": "How can I solve 8x + 7 = -23"},
23+
],
24+
response_format={
25+
"type": "json_schema",
26+
"json_schema": {
27+
"name": "MathDemonstration",
28+
"schema_definition": {
29+
"$defs": {
30+
"Explanation": {
31+
"properties": {
32+
"explanation": {
33+
"title": "Explanation",
34+
"type": "string",
35+
},
36+
"output": {"title": "Output", "type": "string"},
37+
},
38+
"required": ["explanation", "output"],
39+
"title": "Explanation",
40+
"type": "object",
41+
"additionalProperties": False,
42+
}
43+
},
44+
"properties": {
45+
"steps": {
46+
"items": {"$ref": "#/$defs/Explanation"},
47+
"title": "Steps",
48+
"type": "array",
49+
},
50+
"final_answer": {"title": "Final Answer", "type": "string"},
51+
},
52+
"required": ["steps", "final_answer"],
53+
"title": "MathDemonstration",
54+
"type": "object",
55+
"additionalProperties": False,
56+
},
57+
"description": None,
58+
"strict": True,
59+
},
60+
},
61+
)
62+
print(chat_response.choices[0].message.content)
63+
64+
# Or with the streaming API
65+
with client.chat.stream(
66+
model="mistral-large-latest",
67+
messages=[
68+
{
69+
"role": "system",
70+
"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.",
71+
},
72+
{"role": "user", "content": "How can I solve 8x + 7 = -23"},
73+
],
74+
response_format={
75+
"type": "json_schema",
76+
"json_schema": {
77+
"name": "MathDemonstration",
78+
"schema_definition": {
79+
"$defs": {
80+
"Explanation": {
81+
"properties": {
82+
"explanation": {
83+
"title": "Explanation",
84+
"type": "string",
85+
},
86+
"output": {"title": "Output", "type": "string"},
87+
},
88+
"required": ["explanation", "output"],
89+
"title": "Explanation",
90+
"type": "object",
91+
"additionalProperties": False,
92+
}
93+
},
94+
"properties": {
95+
"steps": {
96+
"items": {"$ref": "#/$defs/Explanation"},
97+
"title": "Steps",
98+
"type": "array",
99+
},
100+
"final_answer": {"title": "Final Answer", "type": "string"},
101+
},
102+
"required": ["steps", "final_answer"],
103+
"title": "MathDemonstration",
104+
"type": "object",
105+
"additionalProperties": False,
106+
},
107+
"description": None,
108+
"strict": True,
109+
},
110+
},
111+
) as stream:
112+
for chunk in stream:
113+
print(chunk.data.choices[0].delta.content, end="")
114+
115+
116+
if __name__ == "__main__":
117+
main()

0 commit comments

Comments
 (0)