Skip to content

Instantly share code, notes, and snippets.

@yookoala
Last active February 14, 2025 03:14
Show Gist options
  • Save yookoala/85be287f29d90219fa5b904b003cc48b to your computer and use it in GitHub Desktop.
Save yookoala/85be287f29d90219fa5b904b003cc48b to your computer and use it in GitHub Desktop.
A very simple mistral example
# API Key
MISTRAL_API_KEY="some-api-key"
# Model used
#MISTRAL_MODEL="mistral-large-latest"
MISTRAL_MODEL="mistral-tiny"

Example for using Mistral AI

This is a very simple example using Mistral AI with their official Mistral Python Client.

The main.py passes STDIN to Mistral AI and then stream out the response text to STDOUT until finished.

Prequesities

  1. Have a Mistral AI account.
  2. Have setup a Mistral Le Platforme subscription.
  3. Create an API key at Le Platforme > API Key
  4. With pip, install mistralai and python-dotenv (you may use the requirements.txt here)

Usage

  1. Save main.py to your machine.
  2. Create a .env (reference .env.example above) in the same folder.
  3. Assuming you're using macos or Linux, run echo "Why is the sky blue?" | main.py.
    (If you're using Windows, try to use WSL and do the same).
#!/usr/bin/env python
from dotenv import load_dotenv
from mistralai import Mistral
import os
import sys
load_dotenv()
api_key = os.environ["MISTRAL_API_KEY"]
model = os.environ["MISTRAL_MODEL"]
prompt = sys.stdin.read().strip()
with Mistral(api_key=api_key) as client:
res = client.chat.stream(
model = model,
stream = True,
messages = [
{
"role": "user",
"content": prompt,
},
]
)
with res as event_stream:
for event in event_stream:
print(event.data.choices[0].delta.content, flush=True, end="")
annotated-types==0.7.0
anyio==4.8.0
certifi==2025.1.31
eval_type_backport==0.2.2
h11==0.14.0
httpcore==1.0.7
httpx==0.28.1
idna==3.10
jsonpath-python==1.0.6
mistralai==1.5.0
mypy-extensions==1.0.0
pydantic==2.10.6
pydantic_core==2.27.2
python-dateutil==2.9.0.post0
python-dotenv==1.0.1
six==1.17.0
sniffio==1.3.1
typing-inspect==0.9.0
typing_extensions==4.12.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment