这将帮助您开始使用 DeepSeek 托管的 聊天模型。有关所有 ChatDeepSeek 功能和配置的详细文档,请访问 API 参考
DeepSeek 的模型是开源的,可以在本地运行(例如在 Ollama 中)或在其他推理提供商上运行(例如 FireworksTogether)。

概述

集成详情

ClassPackageLocalSerializableJS supportDownloadsVersion
ChatDeepSeeklangchain-deepseekbetaPyPI - DownloadsPyPI - Version

模型功能

Tool callingStructured outputJSON modeImage inputAudio inputVideo inputToken-level streamingNative asyncToken usageLogprobs
DeepSeek-R1(通过 model="deepseek-reasoner" 指定)不支持工具调用或结构化输出。这些功能由 DeepSeek-V3(通过 model="deepseek-chat" 指定)支持

设置

要访问 DeepSeek 模型,您需要创建 DeepSeek 帐户、获取 API 密钥并安装 langchain-deepseek 集成包。

凭证

前往 DeepSeek 的 API 密钥页面 注册 DeepSeek 并生成 API 密钥。完成后,设置 DEEPSEEK_API_KEY 环境变量:
import getpass
import os

if not os.getenv("DEEPSEEK_API_KEY"):
    os.environ["DEEPSEEK_API_KEY"] = getpass.getpass("Enter your DeepSeek API key: ")
要启用模型调用的自动跟踪,请设置您的 LangSmith API 密钥:
os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")

安装

LangChain DeepSeek 集成位于 langchain-deepseek 包中:
pip install -qU langchain-deepseek

实例化

现在我们可以实例化模型对象并生成聊天完成:
from langchain_deepseek import ChatDeepSeek

llm = ChatDeepSeek(
    model="deepseek-chat",
    temperature=0,
    max_tokens=None,
    timeout=None,
    max_retries=2,
    # other params...
)

调用

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.content

API 参考

有关所有 ChatDeepSeek 功能和配置的详细文档,请访问 API 参考
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.