- Resources
Cookbooks
Mistral-medium-3-5
Hands-on notebooks and guides for building with the Mistral API.
Mistral Medium 3.5, a new flagship model that merges instruction-following, reasoning, and coding into a single 128B dense model. Released as open weights, under a modified MIT license. https://docs.mistral.ai/models/model-cards/mistral-medium-3-5-26-04
In this notebook, we will show how some basics on how to work with Mistral Medium 3.5 in Microsoft Foundry, with a focus on multi-modal and financial data, to ask questions about the pdfs.
import base64
import os
from dotenv import load_dotenv
from typing import Any
from IPython.display import Image, display
load_dotenv()
MISTRAL_MODEL_NAME = os.getenv("MISTRAL_MODEL_NAME")
AMISTRAL_MODEL_KEY = os.getenv("MISTRAL_MODEL_KEY")
from mistralai.client import Mistral
client = Mistral(api_key=AMISTRAL_MODEL_KEY)
def encode_file(file_path: str) -> str:
try:
with open(file_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
except FileNotFoundError:
print(f"Error: The file {file_path} was not found.")
return None
pdf_path = "cvna_2021_annual_report.png"
cvna_2021_annual_report = encode_file(pdf_path)
Image(pdf_path)
def documentPayload(encoded_document: str, question: str) -> dict[str, Any]:
## This is a sample payload for sending a document to the Mistral Document AI API. The "content" field contains a list of message parts, which can include text and images. In this example, we are sending a text prompt along with an image encoded in base64 format. The model specified in the payload will process the input and generate a response based on the content of the document.
payload = {
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": question
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{encoded_document}"
}
}
]
}
]
}
return payload
def textPayload(question: str) -> dict[str, Any]:
## This is a sample payload for sending a text prompt to the Mistral Document AI API. The "content" field contains a list of message parts, which in this case includes only a text prompt. The model specified in the payload will process the input and generate a response based on the provided text.
payload = {
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": question
}
]
}
]
}
return payload
def send_document_ai_request(encoded_document: str, question: str, reasoning_effort="none") -> dict:
if not encoded_document:
payload = textPayload(question)
else:
payload = documentPayload(encoded_document, question)
response = client.chat.complete(
model=MISTRAL_MODEL_NAME,
messages=payload["messages"],
temperature=0.7,
max_tokens=2048,
top_p=1,
reasoning_effort=reasoning_effort,
)
return response
documentResponse = send_document_ai_request(cvna_2021_annual_report, "What's in this document? Answer in a single sentence?")
display(documentResponse.choices[0].message.content)
questions = [
"What was CVNA revenue in 2020?",
"How many additional markets has Carvana added since 2014?",
"What was 2016 revenue per retail unit sold?",
]
for index, question in enumerate(questions):
documentResponse = send_document_ai_request(cvna_2021_annual_report, question)
display(documentResponse.choices[0].message.content)
Multi-lingual- First question in Spanish, Second one in Chinese and the last one in Arabic
questions = [
"¿Cuáles fueron los ingresos de CVNA en 2020?",
"自2014年以来,Carvana新增了多少个市场?",
"ما هو الإيراد لكل وحدة بيع بالتجزئة في عام 2016",
]
for index, question in enumerate(questions):
documentResponse = send_document_ai_request(cvna_2021_annual_report, question)
display(documentResponse.choices[0].message.content)
Reasoning 'none' vs 'high'
content = """In Alice's Wonderland, a secret bit manipulation rule transforms 8-bit binary numbers. The transformation involves operations like bit shifts, rotations, XOR, AND, OR, NOT, and possibly majority or choice functions.
Here are some examples of input -> output:
01010001 -> 10100010
00001001 -> 00010010
00010101 -> 00101010
Now, determine the output for: 00110100"""
## correct answer: 01101000
documentResponse = send_document_ai_request("", content)
print(documentResponse.choices[0].message.content)
documentResponse = send_document_ai_request("", content, "high")
content_items = documentResponse.choices[0].message.content
print(content_items)
print(content_items[0].thinking[0].text)