Documentation Index
Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
Use this file to discover all available pages before exploring further.
本文将帮助您开始使用 Mistral 聊天模型。有关 ChatMistralAI 所有功能和配置的详细文档,请参阅 API 参考。ChatMistralAI 类构建于 Mistral API 之上。有关 Mistral 支持的所有模型列表,请查看 此页面。
集成详情
模型特性
要访问 ChatMistralAI 模型,您需要创建一个 Mistral 账户,获取 API 密钥,并安装 langchain-mistralai 集成包。
需要有效的 API 密钥 才能与 API 通信。获取密钥后,请设置 MISTRAL_API_KEY 环境变量:
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 API 密钥:
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"
LangChain Mistral 集成位于 langchain-mistralai 包中:
pip install -qU langchain-mistralai
实例化
现在我们可以实例化模型对象并生成聊天补全:
from langchain_mistralai import ChatMistralAI
llm = ChatMistralAI(
model="mistral-large-latest",
temperature=0,
max_retries=2,
# 其他参数...
)
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
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})
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 参考 查看所有属性和方法的详细文档。