> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# ChatMistralAI 集成

> 使用 LangChain Python 与 ChatMistralAI 聊天模型集成。

这将帮助您开始使用 Mistral [聊天模型](/oss/python/langchain/models)。有关所有 `ChatMistralAI` 功能和配置的详细文档，请访问 [API 参考](https://reference.langchain.com/python/langchain-mistralai/chat_models/ChatMistralAI)。`ChatMistralAI` 类构建在 [Mistral API](https://docs.mistral.ai/api/) 之上。有关 Mistral 支持的所有模型列表，请查看[此页面](https://docs.mistral.ai/getting-started/models/)。

## 概述

### 集成详情

| 类                                                                                                       | 包                                                                                   | 可序列化 | [JS 支持](https://js.langchain.com/docs/integrations/chat/mistral) |                                                  下载量                                                 |                                                 版本                                                |
| :------------------------------------------------------------------------------------------------------ | :---------------------------------------------------------------------------------- | :--: | :--------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------: |
| [`ChatMistralAI`](https://reference.langchain.com/python/langchain-mistralai/chat_models/ChatMistralAI) | [`langchain-mistralai`](https://reference.langchain.com/python/langchain-mistralai) | beta |                                 ✅                                | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain_mistralai?style=flat-square\&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain_mistralai?style=flat-square\&label=%20) |

### 模型特性

| [工具调用](/oss/python/langchain/tools) | [结构化输出](/oss/python/langchain/structured-output) | [图像输入](/oss/python/langchain/messages#multimodal) | 音频输入 | 视频输入 | [令牌级流式传输](/oss/python/langchain/streaming/) | 原生异步 | [令牌使用量](/oss/python/langchain/models#token-usage) | [对数概率](/oss/python/langchain/models#log-probabilities) |
| :---------------------------------: | :----------------------------------------------: | :-----------------------------------------------: | :--: | :--: | :-----------------------------------------: | :--: | :-----------------------------------------------: | :----------------------------------------------------: |
|                  ✅                  |                         ✅                        |                         ❌                         |   ❌  |   ❌  |                      ✅                      |   ✅  |                         ✅                         |                            ❌                           |

## 设置

要访问 `ChatMistralAI` 模型，您需要创建一个 Mistral 账户，获取 API 密钥，并安装 `langchain-mistralai` 集成包。

### 凭证

与 API 通信需要有效的 [API 密钥](https://console.mistral.ai/api-keys/)。完成此操作后，设置 MISTRAL\_API\_KEY 环境变量：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import getpass
import os

if "MISTRAL_API_KEY" not in os.environ:
    os.environ["MISTRAL_API_KEY"] = getpass.getpass("Enter your Mistral API key: ")
```

要启用模型调用的自动跟踪，请设置您的 [LangSmith](/langsmith/home) API 密钥：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"
```

### 安装

LangChain Mistral 集成位于 `langchain-mistralai` 包中：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-mistralai
```

## 实例化

现在我们可以实例化模型对象并生成聊天补全：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_mistralai import ChatMistralAI

llm = ChatMistralAI(
    model="mistral-large-latest",
    temperature=0,
    max_retries=2,
    # other params...
)
```

## 调用

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
messages = [
    (
        "system",
        "You are a helpful assistant that translates English to French. Translate the user sentence.",
    ),
    ("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
AIMessage(content='Sure, I\'d be happy to help you translate that sentence into French! The English sentence "I love programming" translates to "J\'aime programmer" in French. Let me know if you have any other questions or need further assistance!', response_metadata={'token_usage': {'prompt_tokens': 32, 'total_tokens': 84, 'completion_tokens': 52}, 'model': 'mistral-small', 'finish_reason': 'stop'}, id='run-64bac156-7160-4b68-b67e-4161f63e021f-0', usage_metadata={'input_tokens': 32, 'output_tokens': 52, 'total_tokens': 84})
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
print(ai_msg.content)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
Sure, I'd be happy to help you translate that sentence into French! The English sentence "I love programming" translates to "J'aime programmer" in French. Let me know if you have any other questions or need further assistance!
```

***

## API 参考

访问 [API 参考](https://reference.langchain.com/python/langchain-mistralai/chat_models/ChatMistralAI) 获取所有属性和方法的详细文档。

***

<div className="source-links">
  <Callout icon="terminal-2">
    [连接这些文档](/use-these-docs) 到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/chat/mistralai.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
