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

# ChatMistralAI 集成

> 使用 LangChain Python 集成 ChatMistralAI 对话模型。

这将帮助您快速入门Mistral [对话模型](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md)。如需了解所有 `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://langchain-docs-zh.mintlify.site/langchain/oss/javascript/integrations/chat/mistral.md) | 下载量 | 版本 |
| :- | :- | :-: | :-: | :-: | :-: |
| [`ChatMistralAI`](https://reference.langchain.com/python/langchain-mistralai/chat_models/ChatMistralAI) | [`langchain-mistralai`](https://reference.langchain.com/python/langchain-mistralai) | beta | ✅ | ![PyPI - 下载量](https://img.shields.io/pypi/dm/langchain_mistralai?style=flat-square\&label=%20) | ![PyPI - 版本](https://img.shields.io/pypi/v/langchain_mistralai?style=flat-square\&label=%20) |

### 模型特性

| [工具调用](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/tools.md) | [结构化输出](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/structured-output.md) | [图像输入](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/messages.md#多模态) | 音频输入 | 视频输入 | [Token级流式输出](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/streaming.md) | 原生异步 | [Token使用量](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md#token-使用) | [对数概率](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md#对数概率) |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ |

## 配置

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

### 凭据

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

```python theme={null}
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](https://docs.langchain.com/langsmith/observability) API密钥：

```python theme={null}
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"
```

### 安装

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

```python theme={null}
pip install -qU langchain-mistralai
```

## 实例化

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

```python theme={null}
from langchain_mistralai import ChatMistralAI

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

## 调用

```python theme={null}
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={null}
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={null}
print(ai_msg.content)
```

```text theme={null}
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)。

***

> 官方原文：[src/oss/python/integrations/chat/mistralai.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/chat/mistralai.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.