Skip to main content
Open In Colab在 GitHub 上打开

聊天深度seek

这将帮助您开始使用 DeepSeek 的托管聊天模型。有关所有 ChatDeepSeek 功能和配置的详细文档,请访问 API 参考

提示

DeepSeek 的模型是开源的,可以在本地运行(例如在 Ollama 中)或其他推理提供商(例如 FireworksTogether)。

概述

集成详细信息

本地化序列 化JS 支持软件包下载最新包装
ChatDeepSeeklangchain-deepseekbetaPyPI - DownloadsPyPI - Version

模型特点

工具调用结构化输出JSON 模式图像输入音频输入视频输入令牌级流式处理本机异步Token 使用情况日志
注意

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

实例

现在我们可以实例化我们的 Model 对象并生成聊天补全:

from langchain_deepseek import ChatDeepSeek

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

调用

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

链接

我们可以用 prompt 模板链接我们的模型,如下所示:

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate(
[
(
"system",
"You are a helpful assistant that translates {input_language} to {output_language}.",
),
("human", "{input}"),
]
)

chain = prompt | llm
chain.invoke(
{
"input_language": "English",
"output_language": "German",
"input": "I love programming.",
}
)
API 参考:ChatPromptTemplate

API 参考

有关所有 ChatDeepSeek 功能和配置的详细文档,请前往 API 参考