Mistral AI API Guide 2026: Mixtral, Codestral, Embeddings

Build cost-efficient AI applications with Mistral AI models

返回教程列表
进阶30 分钟

Mistral AI API Guide 2026: Mixtral, Codestral, Embeddings

Build cost-efficient AI applications with Mistral AI models

Complete Mistral AI API guide: Mixtral 8x22B, Mistral Large, Codestral for code, embeddings for RAG, function calling, JSON mode, and local deployment with Ollama.

mistralmixtralllmapicodestralrag

Mistral AI API Guide 2026: Mixtral, Codestral, Embeddings

Mistral AI offers strong multilingual models at significantly lower cost.

Models

ModelContextBest ForCost/1M

Mistral Small32KSimple tasks$0.20/$0.60 Mistral Large 2128KComplex reasoning$3/$9 Mixtral 8x22B64KCost-efficient$2/$6 Codestral32KCode generation$1/$3

Basic Chat

python
from mistralai import Mistral

client = Mistral(api_key='your-api-key')

response = client.chat.complete( model='mistral-large-latest', messages=[ {'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': 'Explain transformer attention'} ] ) print(response.choices[0].message.content)

Streaming

for chunk in client.chat.stream( model='mistral-large-latest', messages=[{'role': 'user', 'content': 'Write a FastAPI tutorial'}] ): print(chunk.data.choices[0].delta.content, end='', flush=True)

Function Calling

python
import json

tools = [{'type': 'function', 'function': { 'name': 'get_weather', 'description': 'Get weather for a city', 'parameters': { 'type': 'object', 'properties': {'city': {'type': 'string'}}, 'required': ['city'] } }}]

r = client.chat.complete( model='mistral-large-latest', messages=[{'role': 'user', 'content': 'Weather in Paris?'}], tools=tools, tool_choice='auto' ) tc = r.choices[0].message.tool_calls[0] print(json.loads(tc.function.arguments))

Embeddings for RAG

python
import numpy as np

def embed(texts): r = client.embeddings.create(model='mistral-embed', inputs=texts) return [item.embedding for item in r.data]

def cosine(a, b): return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

docs = ['LangChain is a framework', 'Mistral builds open LLMs'] embs = embed(docs) q_emb = embed(['What is Mistral?'])[0] best = max(zip(docs, [cosine(q_emb, e) for e in embs]), key=lambda x: x[1]) print(best[0]) # Most relevant doc

Codestral

python

Best for 80+ programming languages

r = client.chat.complete( model='codestral-latest', messages=[{'role': 'user', 'content': 'Write async Python with aiohttp'}] )

Fill-in-the-middle completion

r = client.fim.complete( model='codestral-latest', prompt='def sort_by_date(items):', suffix=' return sorted_items' )

Local with Ollama

bash
ollama pull mistral:7b

python
from openai import OpenAI
c = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')
r = c.chat.completions.create(model='mistral:7b', messages=[{'role':'user','content':'Hello'}])

Conclusion

Mistral is the top choice for European data residency and cost-optimized multilingual AI. Codestral competes with GitHub Copilot at lower cost.

相关工具

mistralollamapython