langchain-deepseek¶
注意
此软件包参考尚未完全迁移到 v1。
langchain_deepseek ¶
LangChain DeepSeek 集成。
ChatDeepSeek ¶
Bases: BaseChatOpenAI
DeepSeek 聊天模型集成,用于访问 DeepSeek API 托管的模型。
设置
安装 langchain-deepseek 并设置环境变量 DEEPSEEK_API_KEY。
关键初始化参数 — 补全参数:model:要使用的 DeepSeek 模型名称,例如 "deepseek-chat"。temperature:采样温度。max_tokens:要生成的最大 token 数。
关键初始化参数 — 客户端参数:timeout:请求超时时间。max_retries:最大重试次数。api_key:DeepSeek API 密钥。如果未传入,将从环境变量 DEEPSEEK_API_KEY 中读取。
有关支持的初始化参数及其描述的完整列表,请参见参数部分。
实例化
调用
流
异步
工具调用
from pydantic import BaseModel, Field
class GetWeather(BaseModel):
'''Get the current weather in a given location'''
location: str = Field(..., description="The city and state, e.g. San Francisco, CA")
class GetPopulation(BaseModel):
'''Get the current population in a given location'''
location: str = Field(..., description="The city and state, e.g. San Francisco, CA")
model_with_tools = model.bind_tools([GetWeather, GetPopulation])
ai_msg = model_with_tools.invoke("Which city is hotter today and which is bigger: LA or NY?")
ai_msg.tool_calls
更多信息请参阅 ChatDeepSeek.bind_tools() 方法。
结构化输出
from typing import Optional
from pydantic import BaseModel, Field
class Joke(BaseModel):
'''Joke to tell user.'''
setup: str = Field(description="The setup of the joke")
punchline: str = Field(description="The punchline to the joke")
rating: int | None = Field(description="How funny the joke is, from 1 to 10")
structured_model = model.with_structured_output(Joke)
structured_model.invoke("Tell me a joke about cats")
更多信息请参阅 ChatDeepSeek.with_structured_output()。
令牌使用情况
| 方法 | 描述 |
|---|---|
validate_environment |
验证必要的环境变量和客户端参数。 |
with_structured_output |
返回与给定模式匹配的格式化输出的模型包装器。 |
api_key class-attribute instance-attribute ¶
api_key: SecretStr | None = Field(
default_factory=secret_from_env("DEEPSEEK_API_KEY", default=None)
)
DeepSeek API 密钥
api_base class-attribute instance-attribute ¶
DeepSeek API 基础 URL
with_structured_output ¶
with_structured_output(
schema: _DictOrPydanticClass | None = None,
*,
method: Literal[
"function_calling", "json_mode", "json_schema"
] = "function_calling",
include_raw: bool = False,
strict: bool | None = None,
**kwargs: Any,
) -> Runnable[LanguageModelInput, _DictOrPydantic]
返回与给定模式匹配的格式化输出的模型包装器。
| 参数 | 描述 |
|---|---|
模式
|
输出模式。可以作为以下形式传入:
如果 有关在指定 Pydantic 或
TYPE: |
method
|
类型: |
包含原始数据
|
如果为 最终输出总是一个带有键
类型: |
strict
|
是否在生成函数调用时启用严格的模式遵循。此参数是为了与其他聊天模型兼容而包含的,如果指定,将根据 OpenAI API 规范传递给聊天补全 API。但是,DeepSeek API 可能会忽略该参数。
类型: |
kwargs
|
不支持额外的关键字参数。
类型: |
| 返回 | 描述 |
|---|---|
Runnable[LanguageModelInput, _DictOrPydantic]
|
一个接受与 如果
|