EN

Mistral AI API 指南 2026:Mixtral、Mistral Large 与边缘部署

2026 年 Mistral AI 模型完整开发者指南,涵盖 Mistral Large、Mixtral 8x22B 以及本地部署 Mistral 模型以构建隐私优先应用

返回教程列表🌐 Read in English
进阶25 分钟

Mistral AI API 指南 2026:Mixtral、Mistral Large 与边缘部署

2026 年 Mistral AI 模型完整开发者指南,涵盖 Mistral Large、Mixtral 8x22B 以及本地部署 Mistral 模型以构建隐私优先应用

2026 年 Mistral AI API 与模型的全面指南。涵盖 Mistral Large 与 Mixtral 模型选择、Python 和 TypeScript 的 API 使用、基于 Ollama 的本地部署、函数调用,以及构建符合欧洲数据驻留要求的生产级应用。

Mistral AI API 指南 2026:Mixtral、Mistral Large 与边缘部署

Mistral AI 已将自己定位为 OpenAI 和 Anthropic 的欧洲替代方案——具备有竞争力的模型质量、欧洲数据驻留以及真正开源的模型权重。到 2026 年,Mistral 的模型因其高效性和隐私友好的许可协议而被广泛使用。

Mistral 模型阵容 2026

模型参数上下文最佳用途价格/百万 tokens

mistral-large-2123B128K复杂推理$2/$6 mistral-small-222B32K高性价比通用$0.2/$0.6 codestral22B32K代码生成$0.2/$0.6 mistral-embed-8K嵌入$0.1/百万 open-mixtral-8x22b141B MoE64K开源大模型自托管 open-mistral-7b7B32K本地部署免费

快速开始

python
from mistralai import Mistral

client = Mistral(api_key="your-mistral-api-key")

基础补全

response = client.chat.complete( model="mistral-large-2", messages=[{"role": "user", "content": "解释混合专家架构"}] )

print(response.choices[0].message.content) print(f"Tokens: {response.usage.total_tokens}")

流式输出

python

流式响应

with client.chat.stream( model="mistral-large-2", messages=[{"role": "user", "content": "写一篇关于 2026 年 AI 的博客文章"}] ) as stream: for event in stream: if event.data.choices[0].delta.content: print(event.data.choices[0].delta.content, end="", flush=True)

函数调用

python
import json

tools = [ { "type": "function", "function": { "name": "get_customer_data", "description": "检索客户账户信息", "parameters": { "type": "object", "properties": { "customer_id": {"type": "string"}, "include_orders": {"type": "boolean", "default": False} }, "required": ["customer_id"] } } } ]

response = client.chat.complete( model="mistral-large-2", messages=[{"role": "user", "content": "获取客户 CUS-12345 的订单历史"}], tools=tools, tool_choice="auto" )

处理工具调用

if response.choices[0].message.tool_calls: for tool_call in response.choices[0].message.tool_calls: func_name = tool_call.function.name args = json.loads(tool_call.function.arguments) print(f"调用 {func_name},参数为 {args}")

JSON 模式

python
response = client.chat.complete(
    model="mistral-large-2",
    messages=[{
        "role": "user",
        "content": "列出 2026 年排名前 5 的编程语言及其主要用例。以 JSON 格式返回。"
    }],
    response_format={"type": "json_object"}
)

data = json.loads(response.choices[0].message.content) print(data)

Codestral:专用代码模型

python

FIM(中间填充)- 用于代码补全

response = client.fim.complete( model="codestral-2405", prompt="def fibonacci(n: int) -> int:\n ", suffix="\n return result", max_tokens=200 )

print(response.choices[0].message.content)

代码生成

code_response = client.chat.complete( model="codestral-2405", messages=[{ "role": "user", "content": "编写一个 Python 异步函数,并发获取多个 URL,并返回 URL 到响应时间的字典" }] ) print(code_response.choices[0].message.content)

使用 Ollama 本地部署

bash

安装 Ollama

brew install ollama # macOS

或:curl https://ollama.ai/install.sh | sh

拉取 Mistral 模型

ollama pull mistral # 7B 模型 (4.1GB) ollama pull mixtral # 8x7B (26GB) ollama pull mistral-large # 123B(如果硬件支持)

交互式运行

ollama run mistral

作为 API 服务器运行(兼容 OpenAI SDK)

OLLAMA_HOST=0.0.0.0 ollama serve

python

通过兼容 OpenAI 的 API 本地使用 Mistral

from openai import OpenAI

local_client = OpenAI( base_url="http://localhost:11434/v1", api_key="not-needed" )

response = local_client.chat.completions.create( model="mistral", # 或 "mixtral" messages=[{"role": "user", "content": "分析这份机密文档:..."}] )

完全本地 - 数据不会离开你的机器

用于 RAG 的嵌入

python

使用 Mistral 生成嵌入

embeddings_response = client.embeddings.create( model="mistral-embed", inputs=["要嵌入的文本", "另一段文本"] )

vectors = [item.embedding for item in embeddings_response.data] print(f"嵌入维度:{len(vectors[0])}") # 1024

相似度搜索

import numpy as np

def cosine_similarity(vec1, vec2): return np.dot(vec1, vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2))

query_embedding = client.embeddings.create( model="mistral-embed", inputs=["什么是 RAG?"] ).data[0].embedding

从语料库中找到最相似的

similarities = [cosine_similarity(query_embedding, v) for v in vectors] best_match = np.argmax(similarities)

欧洲数据驻留

对于 GDPR 敏感的应用:

python

Mistral 默认在欧盟处理所有数据

如需显式控制,使用欧盟端点

client = Mistral( api_key="your-api-key", server_url="https://api.eu.mistral.ai" # 显式欧盟路由 )

或使用开源模型完全本地部署:

- Mistral 7B:完全开源 (Apache 2.0)

- Mixtral 8x7B:完全开源 (Apache 2.0)

- Mistral Large:可用于企业本地部署

成本对比

每月处理 1000 万 tokens:

模型月成本

Mistral Large$20-60 Mistral Small$2-6 OpenAI GPT-4o$30-150 Claude 3.5 Sonnet$30-150 Mistral 7B(自托管)仅计算成本(约 $5)

何时选择 Mistral

  • 需要欧洲数据驻留:Mistral 总部位于巴黎
  • 偏好开源权重:7B 和 Mixtral 完全开源
  • 成本优化:小型模型极具竞争力
  • 代码生成:Codestral 专注于代码
  • 本地部署:Small 和 7B 可在消费级硬件上运行
  • 结论

    Mistral AI 为美国 AI 提供商提供了一个有吸引力的替代方案,具备有竞争力的模型质量、欧洲数据驻留以及真正开源的模型权重。对于有欧洲数据要求或希望自托管的组织,Mistral 的栈在 2026 年已经成熟并可用于生产环境。

    相关工具

    mistralollamapython
    所属主题:API 与集成开发