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