langchain_openai.chat_models.base.ChatOpenAI

注意

ChatOpenAI 实现了标准的 Runnable 接口。 🏃

Runnable 接口 具有 runnable 上可用的其他方法,例如 with_typeswith_retryassignbindget_graph 等。

class langchain_openai.chat_models.base.ChatOpenAI[source]

基类: BaseChatOpenAI

OpenAI 聊天模型集成。

设置

安装 langchain-openai 并设置环境变量 OPENAI_API_KEY

pip install -U langchain-openai
export OPENAI_API_KEY="your-api-key"
主要初始化参数 — 完成参数
model: str

要使用的 OpenAI 模型名称。

temperature: float

采样温度。

max_tokens: Optional[int]

要生成的最大 token 数。

logprobs: Optional[bool]

是否返回 logprobs。

stream_options: Dict

配置流式输出,例如是否在流式传输时返回 token 使用情况 ({"include_usage": True})。

主要初始化参数 — 客户端参数
timeout: Union[float, Tuple[float, float], Any, None]

请求超时时间。

max_retries: int

最大重试次数。

api_key: Optional[str]

OpenAI API 密钥。如果未传入,将从环境变量 OPENAI_API_KEY 中读取。

base_url: Optional[str]

API 请求的基本 URL。仅在使用代理或服务模拟器时指定。

organization: Optional[str]

OpenAI 组织 ID。如果未传入,将从环境变量 OPENAI_ORG_ID 中读取。

请参阅参数部分中受支持的初始化参数及其描述的完整列表。

实例化
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-4o",
    temperature=0,
    max_tokens=None,
    timeout=None,
    max_retries=2,
    # api_key="...",
    # base_url="...",
    # organization="...",
    # other params...
)

注意:任何未显式支持的参数都将在每次调用模型时直接传递给 openai.OpenAI.chat.completions.create(...) API。 例如

from langchain_openai import ChatOpenAI
import openai

ChatOpenAI(..., frequency_penalty=0.2).invoke(...)

# results in underlying API call of:

openai.OpenAI(..).chat.completions.create(..., frequency_penalty=0.2)

# which is also equivalent to:

ChatOpenAI(...).invoke(..., frequency_penalty=0.2)
调用
messages = [
    (
        "system",
        "You are a helpful translator. Translate the user sentence to French.",
    ),
    ("human", "I love programming."),
]
llm.invoke(messages)
AIMessage(
    content="J'adore la programmation.",
    response_metadata={
        "token_usage": {
            "completion_tokens": 5,
            "prompt_tokens": 31,
            "total_tokens": 36,
        },
        "model_name": "gpt-4o",
        "system_fingerprint": "fp_43dfabdef1",
        "finish_reason": "stop",
        "logprobs": None,
    },
    id="run-012cffe2-5d3d-424d-83b5-51c6d4a593d1-0",
    usage_metadata={"input_tokens": 31, "output_tokens": 5, "total_tokens": 36},
)
流式处理
for chunk in llm.stream(messages):
    print(chunk)
AIMessageChunk(content="", id="run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0")
AIMessageChunk(content="J", id="run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0")
AIMessageChunk(content="'adore", id="run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0")
AIMessageChunk(content=" la", id="run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0")
AIMessageChunk(
    content=" programmation", id="run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0"
)
AIMessageChunk(content=".", id="run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0")
AIMessageChunk(
    content="",
    response_metadata={"finish_reason": "stop"},
    id="run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0",
)
stream = llm.stream(messages)
full = next(stream)
for chunk in stream:
    full += chunk
full
AIMessageChunk(
    content="J'adore la programmation.",
    response_metadata={"finish_reason": "stop"},
    id="run-bf917526-7f58-4683-84f7-36a6b671d140",
)
异步
await llm.ainvoke(messages)

# stream:
# async for chunk in (await llm.astream(messages))

# batch:
# await llm.abatch([messages])
AIMessage(
    content="J'adore la programmation.",
    response_metadata={
        "token_usage": {
            "completion_tokens": 5,
            "prompt_tokens": 31,
            "total_tokens": 36,
        },
        "model_name": "gpt-4o",
        "system_fingerprint": "fp_43dfabdef1",
        "finish_reason": "stop",
        "logprobs": None,
    },
    id="run-012cffe2-5d3d-424d-83b5-51c6d4a593d1-0",
    usage_metadata={"input_tokens": 31, "output_tokens": 5, "total_tokens": 36},
)
工具调用
from langchain_core.pydantic_v1 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"
    )


llm_with_tools = llm.bind_tools([GetWeather, GetPopulation])
ai_msg = llm_with_tools.invoke(
    "Which city is hotter today and which is bigger: LA or NY?"
)
ai_msg.tool_calls
[
    {
        "name": "GetWeather",
        "args": {"location": "Los Angeles, CA"},
        "id": "call_6XswGD5Pqk8Tt5atYr7tfenU",
    },
    {
        "name": "GetWeather",
        "args": {"location": "New York, NY"},
        "id": "call_ZVL15vA8Y7kXqOy3dtmQgeCi",
    },
    {
        "name": "GetPopulation",
        "args": {"location": "Los Angeles, CA"},
        "id": "call_49CFW8zqC9W7mh7hbMLSIrXw",
    },
    {
        "name": "GetPopulation",
        "args": {"location": "New York, NY"},
        "id": "call_6ghfKxV264jEfe1mRIkS3PE7",
    },
]

请注意,openai >= 1.32 支持 parallel_tool_calls 参数,该参数默认为 True。 可以将此参数设置为 False 以禁用并行工具调用

ai_msg = llm_with_tools.invoke(
    "What is the weather in LA and NY?", parallel_tool_calls=False
)
ai_msg.tool_calls
[
    {
        "name": "GetWeather",
        "args": {"location": "Los Angeles, CA"},
        "id": "call_4OoY0ZR99iEvC7fevsH8Uhtz",
    }
]

与其他运行时参数一样,parallel_tool_calls 可以使用 llm.bind(parallel_tool_calls=False) 绑定到模型,或者在实例化期间通过设置 model_kwargs 来绑定。

有关更多信息,请参阅 ChatOpenAI.bind_tools() 方法。

结构化输出
from typing import Optional

from langchain_core.pydantic_v1 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: Optional[int] = Field(description="How funny the joke is, from 1 to 10")


structured_llm = llm.with_structured_output(Joke)
structured_llm.invoke("Tell me a joke about cats")
Joke(
    setup="Why was the cat sitting on the computer?",
    punchline="To keep an eye on the mouse!",
    rating=None,
)

有关更多信息,请参阅 ChatOpenAI.with_structured_output()

JSON 模式
json_llm = llm.bind(response_format={"type": "json_object"})
ai_msg = json_llm.invoke(
    "Return a JSON object with key 'random_ints' and a value of 10 random ints in [0-99]"
)
ai_msg.content
'\n{\n  "random_ints": [23, 87, 45, 12, 78, 34, 56, 90, 11, 67]\n}'
图像输入
import base64
import httpx
from langchain_core.messages import HumanMessage

image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
image_data = base64.b64encode(httpx.get(image_url).content).decode("utf-8")
message = HumanMessage(
    content=[
        {"type": "text", "text": "describe the weather in this image"},
        {
            "type": "image_url",
            "image_url": {"url": f"data:image/jpeg;base64,{image_data}"},
        },
    ]
)
ai_msg = llm.invoke([message])
ai_msg.content
"The weather in the image appears to be clear and pleasant. The sky is mostly blue with scattered, light clouds, suggesting a sunny day with minimal cloud cover. There is no indication of rain or strong winds, and the overall scene looks bright and calm. The lush green grass and clear visibility further indicate good weather conditions."
Token 使用情况
ai_msg = llm.invoke(messages)
ai_msg.usage_metadata
{"input_tokens": 28, "output_tokens": 5, "total_tokens": 33}

流式传输时,设置 stream_usage kwarg

stream = llm.stream(messages, stream_usage=True)
full = next(stream)
for chunk in stream:
    full += chunk
full.usage_metadata
{"input_tokens": 28, "output_tokens": 5, "total_tokens": 33}

或者,在实例化模型时设置 stream_usage 在将 ChatOpenAI 合并到 LCEL 链中时可能很有用,或者在使用 .with_structured_output 等方法时也很有用,这些方法在底层生成链。

llm = ChatOpenAI(model="gpt-4o", stream_usage=True)
structured_llm = llm.with_structured_output(...)
Logprobs
logprobs_llm = llm.bind(logprobs=True)
ai_msg = logprobs_llm.invoke(messages)
ai_msg.response_metadata["logprobs"]
{
    "content": [
        {
            "token": "J",
            "bytes": [74],
            "logprob": -4.9617593e-06,
            "top_logprobs": [],
        },
        {
            "token": "'adore",
            "bytes": [39, 97, 100, 111, 114, 101],
            "logprob": -0.25202933,
            "top_logprobs": [],
        },
        {
            "token": " la",
            "bytes": [32, 108, 97],
            "logprob": -0.20141791,
            "top_logprobs": [],
        },
        {
            "token": " programmation",
            "bytes": [
                32,
                112,
                114,
                111,
                103,
                114,
                97,
                109,
                109,
                97,
                116,
                105,
                111,
                110,
            ],
            "logprob": -1.9361265e-07,
            "top_logprobs": [],
        },
        {
            "token": ".",
            "bytes": [46],
            "logprob": -1.2233183e-05,
            "top_logprobs": [],
        },
    ]
}
响应元数据
ai_msg = llm.invoke(messages)
ai_msg.response_metadata
{
    "token_usage": {
        "completion_tokens": 5,
        "prompt_tokens": 28,
        "total_tokens": 33,
    },
    "model_name": "gpt-4o",
    "system_fingerprint": "fp_319be4768e",
    "finish_reason": "stop",
    "logprobs": None,
}
param cache: Union[BaseCache, bool, None] = None

是否缓存响应。

  • 如果为 true,将使用全局缓存。

  • 如果为 false,将不使用缓存

  • 如果为 None,如果已设置全局缓存,将使用全局缓存,否则不使用缓存。

  • 如果是 BaseCache 的实例,将使用提供的缓存。

当前模型的流式处理方法不支持缓存。

param callback_manager: Optional[/]

[已弃用] 要添加到运行跟踪的回调管理器。

param callbacks: Callbacks = None

要添加到运行跟踪的回调。

param custom_get_token_ids: Optional[/]

用于计算 token 的可选编码器。

param default_headers: Union[Mapping[/], None] = None
param default_query: Union[Mapping[/], None] = None
param extra_body: Optional[/] = None

可选的其他 JSON 属性,用于在向 OpenAI 兼容 API(如 vLLM)发出请求时包含在请求参数中。

param frequency_penalty: Optional[/]

根据频率惩罚重复的 token。

param http_async_client: Union[Any, None] = None

可选的 httpx.AsyncClient。 仅用于异步调用。 如果你想要用于同步调用的自定义客户端,则还必须指定 http_client。

param http_client: Union[Any, None] = None

可选的 httpx.Client。 仅用于同步调用。 如果你想要用于异步调用的自定义客户端,则还必须指定 http_async_client。

param include_response_headers: bool = False

是否在输出消息 response_metadata 中包含响应标头。

param logit_bias: Optional[/]

修改指定 token 出现在补全中的可能性。

param logprobs: Optional[/]

是否返回 logprobs。

param max_retries: int = 2

生成时要进行的最大重试次数。

param max_tokens: Optional[/]

要生成的最大 token 数。

param metadata: Optional[/]

要添加到运行跟踪的元数据。

param model_kwargs: Dict[/] [可选]

包含对 create 调用有效但未显式指定的任何模型参数。

param model_name: str = 'gpt-3.5-turbo' (别名 'model')

要使用的模型名称。

param n: int = 1

为每个提示生成的聊天补全数。

param openai_api_base: Optional[/] (别名 'base_url')

API 请求的基本 URL 路径,如果不使用代理或服务模拟器,请留空。

param openai_api_key: Optional[SecretStr] = None (别名 'api_key')

如果未提供,则自动从环境变量 OPENAI_API_KEY 推断。

约束
  • type = string

  • writeOnly = True

  • format = password

param openai_organization: Optional[/] (别名 'organization')

如果未提供,则自动从环境变量 OPENAI_ORG_ID 推断。

param openai_proxy: Optional[/]
param presence_penalty: Optional[/]

惩罚重复的 token。

param rate_limiter: Optional[/]

可选的速率限制器,用于限制请求数。

param request_timeout: Union[float, Tuple[float, float], Any, None] = None (别名 'timeout')

对 OpenAI 补全 API 的请求超时时间。 可以是 float、httpx.Timeout 或 None。

param seed: Optional[/]

生成种子

param stop: Optional[Union[List[/], str]] = None (别名 'stop_sequences')

默认停止序列。

param stream_usage: bool = False

是否在流式输出中包含使用情况元数据。 如果为 True,则将在流式处理期间生成包含使用情况元数据的其他消息块。

param streaming: bool = False

是否流式传输结果。

param tags: Optional[/]

要添加到运行跟踪的标签。

param temperature: float = 0.7

要使用的采样温度。

param tiktoken_model_name: Optional[/]

使用此类时要传递给 tiktoken 的模型名称。 Tiktoken 用于计算文档中的 token 数,以将其限制在某个限制之下。 默认情况下,当设置为 None 时,这将与嵌入模型名称相同。 但是,在某些情况下,你可能希望将此 Embedding 类与 tiktoken 不支持的模型名称一起使用。 这可能包括使用 Azure 嵌入或使用许多模型提供商之一,这些提供商公开了类似 OpenAI 的 API,但模型不同。 在这些情况下,为了避免在调用 tiktoken 时出错,你可以在此处指定要使用的模型名称。

param top_logprobs: Optional[/]

要在每个 token 位置返回的最有可能的 token 数,每个 token 都有一个关联的对数概率。 如果使用此参数,则必须将 logprobs 设置为 true。

param top_p: Optional[/]

在每个步骤中要考虑的 token 的总概率质量。

param verbose [可选]

是否打印响应文本。

__call__(messages: List[BaseMessage], stop: Optional[List[^str]] = None, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, **kwargs: Any) BaseMessage

Deprecated since version langchain-core==0.1.7: 使用 invoke 代替。

参数
返回类型

BaseMessage

async abatch(inputs: List[Input], config: Optional[Union[RunnableConfig, List[RunnableConfig]]] = None, *, return_exceptions: bool = False, **kwargs: Optional[Any]) List[Output]

默认实现使用 asyncio.gather 并行运行 ainvoke。

batch 的默认实现非常适合 IO 绑定的 Runnable。

如果子类可以更有效地进行批量处理,则应覆盖此方法;例如,如果底层 Runnable 使用支持批量模式的 API。

参数
  • inputs (List[Input]) – Runnable 的输入列表。

  • config (Optional[Union[RunnableConfig, List[RunnableConfig]]]) – 调用 Runnable 时要使用的配置。该配置支持标准键,如用于跟踪目的的 ‘tags’、‘metadata’,用于控制并行执行多少工作的 ‘max_concurrency’,以及其他键。有关更多详细信息,请参阅 RunnableConfig。默认为 None。

  • return_exceptions (bool) – 是否返回异常而不是引发异常。默认为 False。

  • kwargs (Optional[Any]) – 传递给 Runnable 的其他关键字参数。

返回值

来自 Runnable 的输出列表。

返回类型

List[Output]

async abatch_as_completed(inputs: Sequence[Input], config: Optional[Union[RunnableConfig, Sequence[RunnableConfig]]] = None, *, return_exceptions: bool = False, **kwargs: Optional[Any]) AsyncIterator[Tuple[int, Union[Output, Exception]]]

并行运行列表中输入的 ainvoke,并在完成后生成结果。

参数
  • inputs (Sequence[Input]) – Runnable 的输入列表。

  • config (Optional[Union[RunnableConfig, Sequence[RunnableConfig]]]) – 调用 Runnable 时要使用的配置。该配置支持标准键,如用于跟踪目的的 ‘tags’、‘metadata’,用于控制并行执行多少工作的 ‘max_concurrency’,以及其他键。有关更多详细信息,请参阅 RunnableConfig。默认为 None。默认为 None。

  • return_exceptions (bool) – 是否返回异常而不是引发异常。默认为 False。

  • kwargs (Optional[Any]) – 传递给 Runnable 的其他关键字参数。

Yields

输入索引和 Runnable 输出的元组。

返回类型

AsyncIterator[Tuple[int, Union[Output, Exception]]]

async agenerate(messages: List[List[BaseMessage]], stop: Optional[List[str]] = None, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, *, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, run_name: Optional[str] = None, run_id: Optional[UUID] = None, **kwargs: Any) LLMResult

异步地将一系列提示传递给模型并返回生成结果。

此方法应利用模型的批量调用,以公开批处理 API。

当您想要以下操作时,请使用此方法
  1. 利用批量调用,

  2. 需要从模型获得比仅仅是顶部生成值更多的输出,

  3. 正在构建对底层语言模型不可知的链

    类型(例如,纯文本完成模型与聊天模型)。

参数
  • messages (List[List[BaseMessage]]) – 消息列表的列表。

  • stop (Optional[List[str]]) – 生成时要使用的停止词。模型输出在首次出现任何这些子字符串时被截断。

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – 要传递的回调。用于在整个生成过程中执行额外的功能,例如日志记录或流式传输。

  • **kwargs (Any) – 任意附加关键字参数。这些通常传递给模型提供商 API 调用。

  • tags (Optional[List[str]]) –

  • metadata (Optional[Dict[str, Any]]) –

  • run_name (Optional[str]) –

  • run_id (Optional[UUID]) –

  • **kwargs

返回值

一个 LLMResult,其中包含每个输入提示的候选 Generations 列表以及其他模型提供商特定的输出。

prompt 和其他模型提供商特定的输出。

返回类型

LLMResult

async agenerate_prompt(prompts: List[PromptValue], stop: Optional[List[str]] = None, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, **kwargs: Any) LLMResult

异步地传递一系列提示并返回模型生成结果。

此方法应利用模型的批量调用,以公开批处理 API。

当您想要以下操作时,请使用此方法
  1. 利用批量调用,

  2. 需要从模型获得比仅仅是顶部生成值更多的输出,

  3. 正在构建对底层语言模型不可知的链

    类型(例如,纯文本完成模型与聊天模型)。

参数
  • prompts (List[PromptValue]) – PromptValue 列表。PromptValue 是一个可以转换为匹配任何语言模型格式的对象(纯文本生成模型的字符串和聊天模型的 BaseMessages)。

  • stop (Optional[List[str]]) – 生成时要使用的停止词。模型输出在首次出现任何这些子字符串时被截断。

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – 要传递的回调。用于在整个生成过程中执行额外的功能,例如日志记录或流式传输。

  • **kwargs (Any) – 任意附加关键字参数。这些通常传递给模型提供商 API 调用。

返回值

一个 LLMResult,其中包含每个输入提示的候选 Generations 列表以及其他模型提供商特定的输出。

prompt 和其他模型提供商特定的输出。

返回类型

LLMResult

async ainvoke(input: LanguageModelInput, config: Optional[RunnableConfig] = None, *, stop: Optional[List[str]] = None, **kwargs: Any) BaseMessage

ainvoke 的默认实现,从线程调用 invoke。

即使 Runnable 没有实现 invoke 的原生异步版本,默认实现也允许使用异步代码。

如果子类可以异步运行,则应覆盖此方法。

参数
  • input (LanguageModelInput) –

  • config (Optional[RunnableConfig]) –

  • stop (Optional[List[str]]) –

  • kwargs (Any) –

返回类型

BaseMessage

async apredict(text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any) str

Deprecated since version langchain-core==0.1.7: 使用 ainvoke 代替。

参数
  • text (str) –

  • stop (Optional[Sequence[str]]) –

  • kwargs (Any) –

返回类型

str

async apredict_messages(messages: List[BaseMessage], *, stop: Optional[Sequence[str]] = None, **kwargs: Any) BaseMessage

Deprecated since version langchain-core==0.1.7: 使用 ainvoke 代替。

参数
  • messages (List[BaseMessage]) –

  • stop (Optional[Sequence[str]]) –

  • kwargs (Any) –

返回类型

BaseMessage

as_tool(args_schema: Optional[Type[BaseModel]] = None, *, name: Optional[str] = None, description: Optional[str] = None, arg_types: Optional[Dict[str, Type]] = None) BaseTool

Beta

此 API 处于 Beta 阶段,将来可能会发生变化。

从 Runnable 创建 BaseTool。

as_tool 将从 Runnable 实例化一个带有名称、描述和 args_schema 的 BaseTool。在可能的情况下,模式是从 runnable.get_input_schema 推断出来的。或者(例如,如果 Runnable 接受 dict 作为输入,并且未键入特定的 dict 键),可以使用 args_schema 直接指定模式。您还可以传递 arg_types 以仅指定必需的参数及其类型。

参数
  • args_schema (Optional[Type[BaseModel]]) – 工具的模式。默认为 None。

  • name (Optional[str]) – 工具的名称。默认为 None。

  • description (Optional[str]) – 工具的描述。默认为 None。

  • arg_types (Optional[Dict[str, Type]]) – 参数名称到类型的字典。默认为 None。

返回值

BaseTool 实例。

返回类型

BaseTool

类型化的 dict 输入

from typing import List
from typing_extensions import TypedDict
from langchain_core.runnables import RunnableLambda

class Args(TypedDict):
    a: int
    b: List[int]

def f(x: Args) -> str:
    return str(x["a"] * max(x["b"]))

runnable = RunnableLambda(f)
as_tool = runnable.as_tool()
as_tool.invoke({"a": 3, "b": [1, 2]})

dict 输入,通过 args_schema 指定模式

from typing import Any, Dict, List
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.runnables import RunnableLambda

def f(x: Dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))

class FSchema(BaseModel):
    """Apply a function to an integer and list of integers."""

    a: int = Field(..., description="Integer")
    b: List[int] = Field(..., description="List of ints")

runnable = RunnableLambda(f)
as_tool = runnable.as_tool(FSchema)
as_tool.invoke({"a": 3, "b": [1, 2]})

dict 输入,通过 arg_types 指定模式

from typing import Any, Dict, List
from langchain_core.runnables import RunnableLambda

def f(x: Dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))

runnable = RunnableLambda(f)
as_tool = runnable.as_tool(arg_types={"a": int, "b": List[int]})
as_tool.invoke({"a": 3, "b": [1, 2]})

字符串输入

from langchain_core.runnables import RunnableLambda

def f(x: str) -> str:
    return x + "a"

def g(x: str) -> str:
    return x + "z"

runnable = RunnableLambda(f) | g
as_tool = runnable.as_tool()
as_tool.invoke("b")

0.2.14 版本新增功能。

async astream(input: LanguageModelInput, config: Optional[RunnableConfig] = None, *, stop: Optional[List[str]] = None, **kwargs: Any) AsyncIterator[BaseMessageChunk]

astream 的默认实现,它调用 ainvoke。如果子类支持流式输出,则应覆盖此方法。

参数
  • input (LanguageModelInput) – Runnable 的输入。

  • config (Optional[RunnableConfig]) – 用于 Runnable 的配置。默认为 None。

  • kwargs (Any) – 传递给 Runnable 的其他关键字参数。

  • stop (Optional[List[str]]) –

Yields

Runnable 的输出。

返回类型

AsyncIterator[BaseMessageChunk]

astream_events(input: Any, config: Optional[RunnableConfig] = None, *, version: Literal['v1', 'v2'], include_names: Optional[Sequence[str]] = None, include_types: Optional[Sequence[str]] = None, include_tags: Optional[Sequence[str]] = None, exclude_names: Optional[Sequence[str]] = None, exclude_types: Optional[Sequence[str]] = None, exclude_tags: Optional[Sequence[str]] = None, **kwargs: Any) AsyncIterator[Union[StandardStreamEvent, CustomStreamEvent]]

Beta

此 API 处于 Beta 阶段,将来可能会发生变化。

生成事件流。

使用此方法创建一个迭代器,遍历 StreamEvents,这些事件提供关于 Runnable 进度的实时信息,包括来自中间结果的 StreamEvents。

StreamEvent 是一个字典,具有以下模式

  • event: str - 事件名称的格式为

    格式:on_[runnable_type]_(start|stream|end)。

  • name: str - 生成事件的 Runnable 的名称。

  • run_id: str - 随机生成的 ID,与给定 Runnable 的执行相关联,该 Runnable

    发出了事件。作为父 Runnable 执行的一部分而被调用的子 Runnable 将被分配其自己唯一的 ID。

  • parent_ids: List[str] - 生成事件的父 runnables 的 ID 列表。

    根 Runnable 将具有一个空列表。父 ID 的顺序是从根到直接父级。仅适用于 API 的 v2 版本。API 的 v1 版本将返回一个空列表。

  • tags: Optional[List[str]] - 生成事件的 Runnable 的标签。

    事件。

  • metadata: Optional[Dict[str, Any]] - 生成事件的 Runnable 的元数据。

    事件。

  • data: Dict[str, Any]

下面是一个表格,说明了各种链可能发出的一些事件。为了简洁起见,表格中省略了元数据字段。链的定义已包含在表格之后。

注意 此参考表适用于 V2 版本的模式。

event

name

chunk

input

output

on_chat_model_start

[模型名称]

{“messages”: [[SystemMessage, HumanMessage]]}

on_chat_model_stream

[模型名称]

AIMessageChunk(content=”hello”)

on_chat_model_end

[模型名称]

{“messages”: [[SystemMessage, HumanMessage]]}

AIMessageChunk(content=”hello world”)

on_llm_start

[模型名称]

{‘input’: ‘hello’}

on_llm_stream

[模型名称]

‘Hello’

on_llm_end

[模型名称]

‘Hello human!’

on_chain_start

format_docs

on_chain_stream

format_docs

“hello world!, goodbye world!”

on_chain_end

format_docs

[Document(…)]

“hello world!, goodbye world!”

on_tool_start

some_tool

{“x”: 1, “y”: “2”}

on_tool_end

some_tool

{“x”: 1, “y”: “2”}

on_retriever_start

[检索器名称]

{“query”: “hello”}

on_retriever_end

[检索器名称]

{“query”: “hello”}

[Document(…), ..]

on_prompt_start

[模板名称]

{“question”: “hello”}

on_prompt_end

[模板名称]

{“question”: “hello”}

ChatPromptValue(messages: [SystemMessage, …])

除了标准事件之外,用户还可以调度自定义事件(请参见下面的示例)。

自定义事件将仅在 API 的 v2 版本中显示!

自定义事件具有以下格式

属性

类型

描述

name

str

用户定义的事件名称。

data

Any

与事件关联的数据。这可以是任何内容,但我们建议使其可 JSON 序列化。

以下是与上面显示的标准事件关联的声明

format_docs:

def format_docs(docs: List[Document]) -> str:
    '''Format the docs.'''
    return ", ".join([doc.page_content for doc in docs])

format_docs = RunnableLambda(format_docs)

some_tool:

@tool
def some_tool(x: int, y: str) -> dict:
    '''Some_tool.'''
    return {"x": x, "y": y}

prompt:

template = ChatPromptTemplate.from_messages(
    [("system", "You are Cat Agent 007"), ("human", "{question}")]
).with_config({"run_name": "my_template", "tags": ["my_template"]})

示例

from langchain_core.runnables import RunnableLambda

async def reverse(s: str) -> str:
    return s[::-1]

chain = RunnableLambda(func=reverse)

events = [
    event async for event in chain.astream_events("hello", version="v2")
]

# will produce the following events (run_id, and parent_ids
# has been omitted for brevity):
[
    {
        "data": {"input": "hello"},
        "event": "on_chain_start",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"chunk": "olleh"},
        "event": "on_chain_stream",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"output": "olleh"},
        "event": "on_chain_end",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
]

示例:调度自定义事件

from langchain_core.callbacks.manager import (
    adispatch_custom_event,
)
from langchain_core.runnables import RunnableLambda, RunnableConfig
import asyncio


async def slow_thing(some_input: str, config: RunnableConfig) -> str:
    """Do something that takes a long time."""
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 1 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 2 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    return "Done"

slow_thing = RunnableLambda(slow_thing)

async for event in slow_thing.astream_events("some_input", version="v2"):
    print(event)
参数
  • input (Any) – Runnable 的输入。

  • config (Optional[RunnableConfig]) – 用于 Runnable 的配置。

  • version (Literal['v1', 'v2']) – 要使用的模式版本,v2v1。用户应使用 v2v1 用于向后兼容,将在 0.4.0 版本中弃用。在 API 稳定之前,不会分配默认值。自定义事件将仅在 v2 中显示。

  • include_names (Optional[Sequence[str]]) – 仅包括来自具有匹配名称的 runnables 的事件。

  • include_types (Optional[Sequence[str]]) – 仅包括来自具有匹配类型的 runnables 的事件。

  • include_tags (Optional[Sequence[str]]) – 仅包括来自具有匹配标签的 runnables 的事件。

  • exclude_names (Optional[Sequence[str]]) – 排除来自具有匹配名称的 runnables 的事件。

  • exclude_types (Optional[Sequence[str]]) – 排除来自具有匹配类型的 runnables 的事件。

  • exclude_tags (Optional[Sequence[str]]) – 排除来自具有匹配标签的 runnables 的事件。

  • kwargs (Any) – 传递给 Runnable 的其他关键字参数。这些参数将传递给 astream_log,因为此 astream_events 的实现是基于 astream_log 构建的。

Yields

StreamEvents 的异步流。

Raises

NotImplementedError – 如果版本不是 v1v2

返回类型

AsyncIterator[Union[StandardStreamEvent, CustomStreamEvent]]

batch(inputs: List[Input], config: Optional[Union[RunnableConfig, List[RunnableConfig]]] = None, *, return_exceptions: bool = False, **kwargs: Optional[Any]) List[Output]

默认实现使用线程池执行器并行运行 invoke。

batch 的默认实现非常适合 IO 绑定的 Runnable。

如果子类可以更有效地进行批量处理,则应覆盖此方法;例如,如果底层 Runnable 使用支持批量模式的 API。

参数
  • inputs (List[Input]) –

  • config (Optional[Union[RunnableConfig, List[RunnableConfig]]]) –

  • return_exceptions (bool) –

  • kwargs (Optional[Any]) –

返回类型

List[Output]

batch_as_completed(inputs: Sequence[Input], config: Optional[Union[RunnableConfig, Sequence[RunnableConfig]]] = None, *, return_exceptions: bool = False, **kwargs: Optional[Any]) Iterator[Tuple[int, Union[Output, Exception]]]

并行运行列表中输入的 invoke,并在完成时产生结果。

参数
  • inputs (Sequence[Input]) –

  • config (Optional[Union[RunnableConfig, Sequence[RunnableConfig]]]) –

  • return_exceptions (bool) –

  • kwargs (Optional[Any]) –

返回类型

Iterator[Tuple[int, Union[Output, Exception]]]

bind_functions(functions: Sequence[Union[Dict[str, Any], Type, Callable, BaseTool]], function_call: Optional[Union[_FunctionCall, str, Literal['auto', 'none']]] = None, **kwargs: Any) Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List, Tuple, str, Dict[str, Any]]]], BaseMessage]

将函数(和其他对象)绑定到此聊天模型。

假设模型与 OpenAI 函数调用 API 兼容。

注意:建议改用 bind_tools,因为 functions

function_call 请求参数已被 OpenAI 官方标记为已弃用。

参数
  • functions (Sequence[Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]]) – 要绑定到此聊天模型的函数定义列表。可以是字典、pydantic 模型或可调用对象。Pydantic 模型和可调用对象将自动转换为其模式字典表示形式。

  • function_call (Optional[Union[_FunctionCall, str, Literal['auto', 'none']]]) – 需要模型调用的函数。必须是提供的单个函数的名称,或者 “auto” 以自动确定要调用哪个函数(如果有)。

  • **kwargs (Any) – 传递给 Runnable 构造函数的任何其他参数。

返回类型

Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], BaseMessage]

bind_tools(tools: Sequence[Union[Dict[str, Any], Type, Callable, BaseTool]], *, tool_choice: Optional[Union[dict, str, Literal['auto', 'none', 'any', 'required'], bool]] = None, **kwargs: Any) Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List, Tuple, str, Dict[str, Any]]]], BaseMessage]

将类似工具的对象绑定到此聊天模型。

假设模型与 OpenAI 工具调用 API 兼容。

参数
  • tools (Sequence[Union[Dict[str, Any], Type, Callable, BaseTool]]) – 要绑定到此聊天模型的工具定义列表。支持 langchain_core.utils.function_calling.convert_to_openai_tool() 处理的任何工具定义。

  • tool_choice (Optional[Union[dict, str, Literal['auto', 'none', 'any', 'required'], bool]]) –

    需要模型调用的工具。选项包括:

    • 形如 "<<tool_name>>" 的 str:调用 <<tool_name>> 工具。

    • "auto":自动选择工具(包括不选择工具)。

    • "none":不调用工具。

    • "any""required"True:强制至少调用一个工具。

    • 形如 {"type": "function", "function": {"name": <<tool_name>>}} 的 dict:调用 <<tool_name>> 工具。

    • FalseNone:无效果,默认 OpenAI 行为。

  • kwargs (Any) – 任何其他参数都将直接传递给 self.bind(**kwargs)

返回类型

Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], BaseMessage]

call_as_llm(message: str, stop: Optional[List[str]] = None, **kwargs: Any) str

Deprecated since version langchain-core==0.1.7: 使用 invoke 代替。

参数
  • message (str) –

  • stop (Optional[List[str]]) –

  • kwargs (Any) –

返回类型

str

configurable_alternatives(which: ConfigurableField, *, default_key: str = 'default', prefix_keys: bool = False, **kwargs: Union[Runnable[Input, Output], Callable[[], Runnable[Input, Output]]]) RunnableSerializable[Input, Output]

配置可在运行时设置的 Runnables 的备选项。

参数
  • which (ConfigurableField) – 将用于选择备选项的 ConfigurableField 实例。

  • default_key (str) – 如果未选择备选项,则使用的默认键。默认为 “default”。

  • prefix_keys (bool) – 是否使用 ConfigurableField id 作为键的前缀。默认为 False。

  • **kwargs (Union[Runnable[Input, Output], Callable[[], Runnable[Input, Output]]]) – 键到 Runnable 实例或返回 Runnable 实例的可调用对象的字典。

返回值

配置了备选项的新 Runnable。

返回类型

RunnableSerializable[Input, Output]

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables.utils import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatAnthropic(
    model_name="claude-3-sonnet-20240229"
).configurable_alternatives(
    ConfigurableField(id="llm"),
    default_key="anthropic",
    openai=ChatOpenAI()
)

# uses the default model ChatAnthropic
print(model.invoke("which organization created you?").content)

# uses ChatOpenAI
print(
    model.with_config(
        configurable={"llm": "openai"}
    ).invoke("which organization created you?").content
)
configurable_fields(**kwargs: Union[ConfigurableField, ConfigurableFieldSingleOption, ConfigurableFieldMultiOption]) RunnableSerializable[Input, Output]

在运行时配置特定的 Runnable 字段。

参数

**kwargs (Union[ConfigurableField, ConfigurableFieldSingleOption, ConfigurableFieldMultiOption]) – 要配置的 ConfigurableField 实例的字典。

返回值

配置了字段的新 Runnable。

返回类型

RunnableSerializable[Input, Output]

from langchain_core.runnables import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatOpenAI(max_tokens=20).configurable_fields(
    max_tokens=ConfigurableField(
        id="output_token_number",
        name="Max tokens in the output",
        description="The maximum number of tokens in the output",
    )
)

# max_tokens = 20
print(
    "max_tokens_20: ",
    model.invoke("tell me something about chess").content
)

# max_tokens = 200
print("max_tokens_200: ", model.with_config(
    configurable={"output_token_number": 200}
    ).invoke("tell me something about chess").content
)
generate(messages: List[List[BaseMessage]], stop: Optional[List[str]] = None, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, *, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, run_name: Optional[str] = None, run_id: Optional[UUID] = None, **kwargs: Any) LLMResult

将一系列提示传递给模型并返回模型生成结果。

此方法应利用模型的批量调用,以公开批处理 API。

当您想要以下操作时,请使用此方法
  1. 利用批量调用,

  2. 需要从模型获得比仅仅是顶部生成值更多的输出,

  3. 正在构建对底层语言模型不可知的链

    类型(例如,纯文本完成模型与聊天模型)。

参数
  • messages (List[List[BaseMessage]]) – 消息列表的列表。

  • stop (Optional[List[str]]) – 生成时要使用的停止词。模型输出在首次出现任何这些子字符串时被截断。

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – 要传递的回调。用于在整个生成过程中执行额外的功能,例如日志记录或流式传输。

  • **kwargs (Any) – 任意附加关键字参数。这些通常传递给模型提供商 API 调用。

  • tags (Optional[List[str]]) –

  • metadata (Optional[Dict[str, Any]]) –

  • run_name (Optional[str]) –

  • run_id (Optional[UUID]) –

  • **kwargs

返回值

一个 LLMResult,其中包含每个输入提示的候选 Generations 列表以及其他模型提供商特定的输出。

prompt 和其他模型提供商特定的输出。

返回类型

LLMResult

generate_prompt(prompts: List[PromptValue], stop: Optional[List[str]] = None, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, **kwargs: Any) LLMResult

将一系列提示传递给模型并返回模型生成结果。

此方法应利用模型的批量调用,以公开批处理 API。

当您想要以下操作时,请使用此方法
  1. 利用批量调用,

  2. 需要从模型获得比仅仅是顶部生成值更多的输出,

  3. 正在构建对底层语言模型不可知的链

    类型(例如,纯文本完成模型与聊天模型)。

参数
  • prompts (List[PromptValue]) – PromptValue 列表。PromptValue 是一个可以转换为匹配任何语言模型格式的对象(纯文本生成模型的字符串和聊天模型的 BaseMessages)。

  • stop (Optional[List[str]]) – 生成时要使用的停止词。模型输出在首次出现任何这些子字符串时被截断。

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – 要传递的回调。用于在整个生成过程中执行额外的功能,例如日志记录或流式传输。

  • **kwargs (Any) – 任意附加关键字参数。这些通常传递给模型提供商 API 调用。

返回值

一个 LLMResult,其中包含每个输入提示的候选 Generations 列表以及其他模型提供商特定的输出。

prompt 和其他模型提供商特定的输出。

返回类型

LLMResult

get_num_tokens(text: str) int

获取文本中存在的 token 数量。

用于检查输入是否适合模型的上下文窗口。

参数

text (str) – 要进行 token 化的字符串输入。

返回值

文本中的 token 整数数量。

返回类型

int

get_num_tokens_from_messages(messages: List[BaseMessage]) int

使用 tiktoken 包计算 gpt-3.5-turbo 和 gpt-4 的 token 数量。

Requirements: 如果你想计算图像 token 数量(如果你将图像指定为 base64 字符串),则必须安装 pillow;如果你将图像指定为 URL,则必须同时安装 pillowhttpx。 如果未安装这些库,则图像输入将在 token 计数中被忽略。

OpenAI 参考: https://github.com/openai/openai-cookbook/blob/ main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb

参数

messages (List[BaseMessage]) –

返回类型

int

get_token_ids(text: str) List[int]

使用 tiktoken 包获取文本中存在的 token。

参数

text (str) –

返回类型

List[int]

invoke(input: LanguageModelInput, config: Optional[RunnableConfig] = None, *, stop: Optional[List[str]] = None, **kwargs: Any) BaseMessage

将单个输入转换为输出。重写以实现。

参数
  • input (LanguageModelInput) – Runnable 的输入。

  • config (Optional[RunnableConfig]) – 调用 Runnable 时使用的配置。该配置支持标准键,如用于跟踪目的的 ‘tags’、‘metadata’,用于控制并行执行量的 ‘max_concurrency’ 以及其他键。请参阅 RunnableConfig 以获取更多详细信息。

  • stop (Optional[List[str]]) –

  • kwargs (Any) –

返回值

Runnable 的输出。

返回类型

BaseMessage

predict(text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any) str

Deprecated since version langchain-core==0.1.7: 使用 invoke 代替。

参数
  • text (str) –

  • stop (Optional[Sequence[str]]) –

  • kwargs (Any) –

返回类型

str

predict_messages(messages: List[BaseMessage], *, stop: Optional[Sequence[str]] = None, **kwargs: Any) BaseMessage

Deprecated since version langchain-core==0.1.7: 使用 invoke 代替。

参数
  • messages (List[BaseMessage]) –

  • stop (Optional[Sequence[str]]) –

  • kwargs (Any) –

返回类型

BaseMessage

stream(input: LanguageModelInput, config: Optional[RunnableConfig] = None, *, stop: Optional[List[str]] = None, **kwargs: Any) Iterator[BaseMessageChunk]

流式传输的默认实现,它调用 invoke。如果子类支持流式输出,则应重写此方法。

参数
  • input (LanguageModelInput) – Runnable 的输入。

  • config (Optional[RunnableConfig]) – 用于 Runnable 的配置。默认为 None。

  • kwargs (Any) – 传递给 Runnable 的其他关键字参数。

  • stop (Optional[List[str]]) –

Yields

Runnable 的输出。

返回类型

Iterator[BaseMessageChunk]

to_json() Union[SerializedConstructor, SerializedNotImplemented]

将 Runnable 序列化为 JSON。

返回值

Runnable 的 JSON 可序列化表示形式。

返回类型

Union[SerializedConstructor, SerializedNotImplemented]

with_structured_output(schema: Optional[Union[Dict[str, Any], Type[_BM], Type]] = None, *, method: Literal['function_calling', 'json_mode'] = 'function_calling', include_raw: bool = False, **kwargs: Any) Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], Union[Dict, _BM]]

模型包装器,返回根据给定模式格式化的输出。

Args
schema
输出模式。可以作为以下形式传入:
  • OpenAI 函数/工具模式,

  • JSON Schema,

  • TypedDict 类 (在 0.1.20 版本中添加支持),

  • 或 Pydantic 类。

如果 schema 是 Pydantic 类,则模型输出将是该类的 Pydantic 实例,并且模型生成的字段将由 Pydantic 类验证。 否则,模型输出将是一个 dict,并且不会被验证。 有关如何正确指定模式字段的类型和描述的更多信息,请参阅 langchain_core.utils.function_calling.convert_to_openai_tool()

Changed in version 0.1.20: 添加了对 TypedDict 类的支持。

method

用于指导模型生成的方法,可以是 “function_calling” 或 “json_mode”。 如果是 “function_calling”,则模式将转换为 OpenAI 函数,并且返回的模型将使用函数调用 API。 如果是 “json_mode”,则将使用 OpenAI 的 JSON 模式。 请注意,如果使用 “json_mode”,则必须在模型调用中包含将输出格式化为所需模式的说明。

include_raw

如果为 False,则仅返回解析后的结构化输出。 如果在模型输出解析期间发生错误,则会引发错误。 如果为 True,则将返回原始模型响应 (BaseMessage) 和解析后的模型响应。 如果在输出解析期间发生错误,则会捕获该错误并一同返回。 最终输出始终是一个带有键 “raw”、“parsed” 和 “parsing_error” 的 dict。

返回值

一个 Runnable,它接受与 langchain_core.language_models.chat.BaseChatModel 相同的输入。

如果 include_raw 为 False 且 schema 是 Pydantic 类,则 Runnable 输出 schema 的实例(即,Pydantic 对象)。

否则,如果 include_raw 为 False,则 Runnable 输出一个 dict。

如果 include_raw 为 True,则 Runnable 输出一个带有以下键的 dict:
  • "raw": BaseMessage

  • "parsed": 如果存在解析错误,则为 None,否则类型取决于上面描述的 schema

  • "parsing_error": Optional[BaseException]

示例: schema=Pydantic class, method=”function_calling”, include_raw=False
from typing import Optional

from langchain_openai import ChatOpenAI
from langchain_core.pydantic_v1 import BaseModel, Field


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    # If we provide default values and/or descriptions for fields, these will be passed
    # to the model. This is an important part of improving a model's ability to
    # correctly return structured outputs.
    justification: Optional[str] = Field(
        default=None, description="A justification for the answer."
    )


llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
structured_llm = llm.with_structured_output(AnswerWithJustification)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)

# -> AnswerWithJustification(
#     answer='They weigh the same',
#     justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
# )
示例: schema=Pydantic class, method=”function_calling”, include_raw=True
from langchain_openai import ChatOpenAI
from langchain_core.pydantic_v1 import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
structured_llm = llm.with_structured_output(
    AnswerWithJustification, include_raw=True
)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
#     'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
#     'parsing_error': None
# }
示例: schema=TypedDict class, method=”function_calling”, include_raw=False
# IMPORTANT: If you are using Python <=3.8, you need to import Annotated
# from typing_extensions, not from typing.
from typing_extensions import Annotated, TypedDict

from langchain_openai import ChatOpenAI


class AnswerWithJustification(TypedDict):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: Annotated[
        Optional[str], None, "A justification for the answer."
    ]


llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
structured_llm = llm.with_structured_output(AnswerWithJustification)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'answer': 'They weigh the same',
#     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
# }
示例: schema=OpenAI function schema, method=”function_calling”, include_raw=False
 from langchain_openai import ChatOpenAI

 oai_schema = {
     'name': 'AnswerWithJustification',
     'description': 'An answer to the user question along with justification for the answer.',
     'parameters': {
         'type': 'object',
         'properties': {
             'answer': {'type': 'string'},
             'justification': {'description': 'A justification for the answer.', 'type': 'string'}
         },
        'required': ['answer']
    }
}

 llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
 structured_llm = llm.with_structured_output(oai_schema)

 structured_llm.invoke(
     "What weighs more a pound of bricks or a pound of feathers"
 )
 # -> {
 #     'answer': 'They weigh the same',
 #     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
 # }
示例: schema=Pydantic class, method=”json_mode”, include_raw=True
from langchain_openai import ChatOpenAI
from langchain_core.pydantic_v1 import BaseModel

class AnswerWithJustification(BaseModel):
    answer: str
    justification: str

llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
structured_llm = llm.with_structured_output(
    AnswerWithJustification,
    method="json_mode",
    include_raw=True
)

structured_llm.invoke(
    "Answer the following question. "
    "Make sure to return a JSON blob with keys 'answer' and 'justification'.

“砖块的一磅重还是羽毛的一磅重?”

) # -> { # ‘raw’: AIMessage(content=’{

“answer”: “它们重量相同。”, “justification”: “砖块的一磅和羽毛的一磅都重一磅。 区别在于材料的体积和密度,而不是重量。”

}’),

# ‘parsed’: AnswerWithJustification(answer=’它们重量相同。’, justification=’砖块的一磅和羽毛的一磅都重一磅。 区别在于材料的体积和密度,而不是重量。’), # ‘parsing_error’: None # }

示例: schema=None, method=”json_mode”, include_raw=True
structured_llm = llm.with_structured_output(method="json_mode", include_raw=True)

structured_llm.invoke(
    "Answer the following question. "
    "Make sure to return a JSON blob with keys 'answer' and 'justification'.

“砖块的一磅重还是羽毛的一磅重?”

) # -> { # ‘raw’: AIMessage(content=’{

“answer”: “它们重量相同。”, “justification”: “砖块的一磅和羽毛的一磅都重一磅。 区别在于材料的体积和密度,而不是重量。”

}’),

# ‘parsed’: { # ‘answer’: ‘它们重量相同。’, # ‘justification’: ‘砖块的一磅和羽毛的一磅都重一磅。 区别在于材料的体积和密度,而不是重量。’ # }, # ‘parsing_error’: None # }

参数
  • schema (Optional[Union[Dict[str, Any], Type[_BM], Type]]) –

  • method (Literal['function_calling', 'json_mode']) –

  • include_raw (bool) –

  • kwargs (Any) –

返回类型

Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], Union[Dict, _BM]]

ChatOpenAI 使用示例