Skip to content

Commit 057363b

Browse files
committed
[Feature] Structured Outputs
1 parent f2d0bc1 commit 057363b

21 files changed

+647
-13
lines changed

.genignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pyproject.toml
2-
examples/*
2+
examples/*
3+
src/mistral/extra/*
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Linting Python custom files
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
lint:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v3
18+
19+
- name: Set up Python
20+
uses: actions/setup-python@v4
21+
with:
22+
python-version: '3.12'
23+
24+
- name: Install ruff
25+
run: pip install ruff
26+
27+
- name: Lint with ruff
28+
# No need to lint the automatically generated Speakeasy code
29+
run: |
30+
ruff check examples/
31+
ruff check src/mistralai/_hooks/ --exclude __init__.py --exclude sdkhooks.py --exclude types.py
32+
ruff check src/mistralai/extra/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Testing Python custom files
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
lint:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v3
18+
19+
- name: Set up Python
20+
uses: actions/setup-python@v4
21+
with:
22+
python-version: '3.12'
23+
24+
- name: Install Poetry
25+
uses: snok/install-poetry@76e04a911780d5b312d89783f7b1cd627778900a # v1.4.1
26+
27+
- name: Install dependencies
28+
run: poetry install
29+
30+
- name: Run the 'src/mistralai/extra' package unit tests
31+
run: python3.12 -m unittest discover -s src/mistralai/extra/tests -t src

.speakeasy/gen.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ python:
2323
clientServerStatusCodesAsErrors: true
2424
defaultErrorName: SDKError
2525
description: Python Client SDK for the Mistral AI API.
26-
enableCustomCodeRegions: false
26+
enableCustomCodeRegions: true
2727
enumFormat: union
2828
envVarPrefix: MISTRAL
2929
fixFlags:

.vscode/settings.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

examples/async_agents_no_streaming.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99

1010
async def main():
1111
api_key = os.environ["MISTRAL_API_KEY"]
12+
agent_id = os.environ["MISTRAL_AGENT_ID"]
1213

1314
client = Mistral(api_key=api_key)
1415

1516
chat_response = await client.agents.complete_async(
16-
agent_id="<your_agent_id>",
17+
agent_id=agent_id,
1718
messages=[UserMessage(content="What is the best French cheese?")],
1819
)
1920

examples/async_chat_with_image_no_streaming.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import asyncio
44
import os
55

6-
import httpx
76

87
from mistralai import Mistral
9-
from mistralai.models import ImageURLChunk, TextChunk, UserMessage
8+
from mistralai.models import UserMessage
109

1110

1211
async def main():
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
3+
import asyncio
4+
import os
5+
from pydantic import BaseModel
6+
7+
from mistralai import Mistral
8+
9+
async def main():
10+
11+
api_key = os.environ["MISTRAL_API_KEY"]
12+
client = Mistral(api_key=api_key)
13+
14+
class Explanation(BaseModel):
15+
explanation: str
16+
output: str
17+
18+
class MathDemonstration(BaseModel):
19+
steps: list[Explanation]
20+
final_answer: str
21+
22+
chat_response = await client.chat.parse_async(
23+
model="mistral-large-2411",
24+
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+
{"role": "user", "content": "How can I solve 8x + 7 = -23"},
27+
],
28+
response_format = MathDemonstration
29+
)
30+
print(chat_response.choices[0].message.parsed)
31+
32+
if __name__ == "__main__":
33+
asyncio.run(main())

examples/chat_with_streaming.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
def main():
1010
api_key = os.environ["MISTRAL_API_KEY"]
11-
model = "mistral-tiny"
11+
model = "mistral-large-latest"
1212

1313
client = Mistral(api_key=api_key)
1414

@@ -17,7 +17,7 @@ def main():
1717
messages=[UserMessage(content="What is the best French cheese?")],
1818
):
1919

20-
print(chunk.data.choices[0].delta.content)
20+
print(chunk.data.choices[0].delta.content, end="")
2121

2222

2323
if __name__ == "__main__":

examples/function_calling.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ def retrieve_payment_date(data: Dict[str, List], transaction_id: str) -> str:
117117
tool_call_id=tool_call.id,
118118
)
119119
)
120+
print(messages)
121+
120122
response = client.chat.complete(model=model, messages=messages, tools=tools)
121123

122124
print(f"{response.choices[0].message.content}")

0 commit comments

Comments
 (0)