langchain_experimental.autonomous_agents.hugginggpt.task_planner.TaskPlaningChain

注意

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

Runnable 接口 具有在可运行对象上可用的其他方法,例如 with_types, with_retry, assign, bind, get_graph, 等等。

class langchain_experimental.autonomous_agents.hugginggpt.task_planner.TaskPlaningChain[source]

基类: LLMChain

用于执行任务的链。

param callback_manager: Optional[BaseCallbackManager] = None

[已弃用] 请使用 callbacks 代替。

param callbacks: Callbacks = None

回调处理程序(或回调管理器)的可选列表。默认为 None。回调处理程序在调用链的整个生命周期中被调用,从 on_chain_start 开始,到 on_chain_end 或 on_chain_error 结束。每个自定义链可以选择性地调用其他回调方法,详情请参阅回调文档。

param llm: Union[Runnable[LanguageModelInput, str], Runnable[LanguageModelInput, BaseMessage]] [Required]

要调用的语言模型。

param llm_kwargs: dict [Optional]
param memory: Optional[BaseMemory] = None

可选的内存对象。默认为 None。Memory 是一个在每个链的开始和结束时被调用的类。在开始时,Memory 加载变量并将它们传递到链中。在结束时,它保存任何返回的变量。有许多不同类型的 Memory - 请参阅 Memory 文档以获取完整目录。

param metadata: Optional[Dict[str, Any]] = None

与链关联的可选元数据。默认为 None。此元数据将与对此链的每次调用关联,并作为参数传递给 callbacks 中定义的处理程序。您可以使用这些来识别链的特定实例及其用例。

param output_parser: BaseLLMOutputParser

Output parser to use. Defaults to one that takes the most likely string but does not change it otherwise.

param prompt: BasePromptTemplate [Required]

Prompt object to use.

param return_final_only: bool = True

Whether to return only the final parsed result. Defaults to True. If false, will return a bunch of extra information about the generation.

param tags: Optional[List[str]] = None

Optional list of tags associated with the chain. Defaults to None. These tags will be associated with each call to this chain, and passed as arguments to the handlers defined in callbacks. You can use these to eg identify a specific instance of a chain with its use case.

param verbose: bool [Optional]

Whether or not run in verbose mode. In verbose mode, some intermediate logs will be printed to the console. Defaults to the global verbose value, accessible via langchain.globals.get_verbose().

__call__(inputs: Union[Dict[str, Any], Any], return_only_outputs: bool = False, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, *, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, run_name: Optional[str] = None, include_run_info: bool = False) Dict[str, Any]

Deprecated since version langchain==0.1.0: Use invoke instead.

Execute the chain.

Parameters
  • inputs (Union[Dict[str, Any], Any]) – Dictionary of inputs, or single input if chain expects only one param. Should contain all inputs specified in Chain.input_keys except for inputs that will be set by the chain’s memory.

  • return_only_outputs (bool) – Whether to return only outputs in the response. If True, only new keys generated by this chain will be returned. If False, both input keys and new keys generated by this chain will be returned. Defaults to False.

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – Callbacks to use for this chain run. These will be called in addition to callbacks passed to the chain during construction, but only these runtime callbacks will propagate to calls to other objects.

  • tags (Optional[List[str]]) – List of string tags to pass to all callbacks. These will be passed in addition to tags passed to the chain during construction, but only these runtime tags will propagate to calls to other objects.

  • metadata (Optional[Dict[str, Any]]) – Optional metadata associated with the chain. Defaults to None

  • include_run_info (bool) – Whether to include run info in the response. Defaults to False.

  • run_name (Optional[str]) –

Returns

A dict of named outputs. Should contain all outputs specified in

Chain.output_keys.

Return type

Dict[str, Any]

async aapply(input_list: List[Dict[str, Any]], callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None) List[Dict[str, str]]

Utilize the LLM generate method for speed gains.

Parameters
Return type

List[Dict[str, str]]

async aapply_and_parse(input_list: List[Dict[str, Any]], callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None) Sequence[Union[str, List[str], Dict[str, str]]]

Call apply and then parse the results.

Parameters
Return type

Sequence[Union[str, List[str], Dict[str, str]]]

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

Default implementation runs ainvoke in parallel using asyncio.gather.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters
  • inputs (List[Input]) – A list of inputs to the Runnable.

  • config (Optional[Union[RunnableConfig, List[RunnableConfig]]]) – A config to use when invoking the Runnable. The config supports standard keys like ‘tags’, ‘metadata’ for tracing purposes, ‘max_concurrency’ for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

  • return_exceptions (bool) – Whether to return exceptions instead of raising them. Defaults to False.

  • kwargs (Optional[Any]) – Additional keyword arguments to pass to the Runnable.

Returns

A list of outputs from the Runnable.

Return type

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]]]

Run ainvoke in parallel on a list of inputs, yielding results as they complete.

Parameters
  • inputs (Sequence[Input]) – A list of inputs to the Runnable.

  • config (Optional[Union[RunnableConfig, Sequence[RunnableConfig]]]) – A config to use when invoking the Runnable. The config supports standard keys like ‘tags’, ‘metadata’ for tracing purposes, ‘max_concurrency’ for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None. Defaults to None.

  • return_exceptions (bool) – Whether to return exceptions instead of raising them. Defaults to False.

  • kwargs (Optional[Any]) – Additional keyword arguments to pass to the Runnable.

Yields

A tuple of the index of the input and the output from the Runnable.

Return type

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

async acall(inputs: Union[Dict[str, Any], Any], return_only_outputs: bool = False, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, *, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, run_name: Optional[str] = None, include_run_info: bool = False) Dict[str, Any]

Deprecated since version langchain==0.1.0: 自 langchain==0.1.0 版本起已弃用: 请使用 ainvoke 代替。

异步执行链。

Parameters
  • inputs (Union[Dict[str, Any], Any]) – Dictionary of inputs, or single input if chain expects only one param. Should contain all inputs specified in Chain.input_keys except for inputs that will be set by the chain’s memory.

  • return_only_outputs (bool) – Whether to return only outputs in the response. If True, only new keys generated by this chain will be returned. If False, both input keys and new keys generated by this chain will be returned. Defaults to False.

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – Callbacks to use for this chain run. These will be called in addition to callbacks passed to the chain during construction, but only these runtime callbacks will propagate to calls to other objects.

  • tags (Optional[List[str]]) – List of string tags to pass to all callbacks. These will be passed in addition to tags passed to the chain during construction, but only these runtime tags will propagate to calls to other objects.

  • metadata (Optional[Dict[str, Any]]) – Optional metadata associated with the chain. Defaults to None

  • include_run_info (bool) – Whether to include run info in the response. Defaults to False.

  • run_name (Optional[str]) –

Returns

A dict of named outputs. Should contain all outputs specified in

Chain.output_keys.

Return type

Dict[str, Any]

async agenerate(input_list: List[Dict[str, Any]], run_manager: Optional[AsyncCallbackManagerForChainRun] = None) LLMResult

从输入生成 LLM 结果。

Parameters
Return type

LLMResult

async ainvoke(input: Dict[str, Any], config: Optional[RunnableConfig] = None, **kwargs: Any) Dict[str, Any]

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

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

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

Parameters
  • input (Dict[str, Any]) –

  • config (Optional[RunnableConfig]) –

  • kwargs (Any) –

Return type

Dict[str, Any]

apply(input_list: List[Dict[str, Any]], callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None) List[Dict[str, str]]

Utilize the LLM generate method for speed gains.

Parameters
Return type

List[Dict[str, str]]

apply_and_parse(input_list: List[Dict[str, Any]], callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None) Sequence[Union[str, List[str], Dict[str, str]]]

Call apply and then parse the results.

Parameters
Return type

Sequence[Union[str, List[str], Dict[str, str]]]

async apredict(callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, **kwargs: Any) str

使用 kwargs 格式化提示并传递给 LLM。

Parameters
Returns

来自 LLM 的补全。

Return type

str

示例

completion = llm.predict(adjective="funny")
async apredict_and_parse(callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, **kwargs: Any) Union[str, List[str], Dict[str, str]]

调用 apredict 然后解析结果。

Parameters
Return type

Union[str, List[str], Dict[str, str]]

async aprep_inputs(inputs: Union[Dict[str, Any], Any]) Dict[str, str]

准备链的输入,包括从内存中添加输入。

Parameters

inputs (Union[Dict[str, Any], Any]) – 原始输入的字典,或者如果链只期望一个参数,则为单个输入。应包含 Chain.input_keys 中指定的所有输入,但链的内存将设置的输入除外。

Returns

所有输入的字典,包括链的内存添加的输入。

Return type

Dict[str, str]

async aprep_outputs(inputs: Dict[str, str], outputs: Dict[str, str], return_only_outputs: bool = False) Dict[str, str]

验证并准备链的输出,并将有关此运行的信息保存到内存中。

Parameters
  • inputs (Dict[str, str]) – 链输入的字典,包括链内存添加的任何输入。

  • outputs (Dict[str, str]) – 初始链输出的字典。

  • return_only_outputs (bool) – 是否仅返回链输出。如果为 False,则输入也会添加到最终输出中。

Returns

最终链输出的字典。

Return type

Dict[str, str]

async aprep_prompts(input_list: List[Dict[str, Any]], run_manager: Optional[AsyncCallbackManagerForChainRun] = None) Tuple[List[PromptValue], Optional[List[str]]]

从输入准备提示。

Parameters
Return type

Tuple[List[PromptValue], Optional[List[str]]]

async arun(*args: Any, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any) Any

Deprecated since version langchain==0.1.0: 自 langchain==0.1.0 版本起已弃用: 请使用 ainvoke 代替。

用于执行链的便捷方法。

此方法与 Chain.__call__ 之间的主要区别在于,此方法期望输入作为位置参数或关键字参数直接传入,而 Chain.__call__ 期望单个输入字典包含所有输入

Parameters
  • *args (Any) – 如果链期望单个输入,则可以作为唯一的位置参数传入。

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – Callbacks to use for this chain run. These will be called in addition to callbacks passed to the chain during construction, but only these runtime callbacks will propagate to calls to other objects.

  • tags (Optional[List[str]]) – List of string tags to pass to all callbacks. These will be passed in addition to tags passed to the chain during construction, but only these runtime tags will propagate to calls to other objects.

  • **kwargs (Any) – 如果链期望多个输入,则可以直接作为关键字参数传入。

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

  • **kwargs

Returns

链的输出。

Return type

Any

示例

# Suppose we have a single-input chain that takes a 'question' string:
await chain.arun("What's the temperature in Boise, Idaho?")
# -> "The temperature in Boise is..."

# Suppose we have a multi-input chain that takes a 'question' string
# and 'context' string:
question = "What's the temperature in Boise, Idaho?"
context = "Weather report for Boise, Idaho on 07/03/23..."
await chain.arun(question=question, context=context)
# -> "The temperature in Boise is..."
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 接受字典作为输入,并且未键入特定字典键),则可以使用 args_schema 直接指定模式。您还可以传递 arg_types 以仅指定必需的参数及其类型。

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

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

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

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

Returns

BaseTool 实例。

Return type

BaseTool

类型化字典输入

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: Input, config: Optional[RunnableConfig] = None, **kwargs: Optional[Any]) AsyncIterator[Output]

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

Parameters
  • input (Input) – Runnable 的输入。

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

  • kwargs (Optional[Any]) – Additional keyword arguments to pass to the Runnable.

Yields

Runnable 的输出。

Return type

AsyncIterator[Output]

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 将被分配其自己的唯一 ID。

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

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

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

    事件的标签。

  • metadata: Optional[Dict[str, Any]] - Runnable 的元数据

    生成事件的 Runnable 的元数据。

  • data: Dict[str, Any]

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

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

事件

名称

输入

输出

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 版本中显示!

自定义事件具有以下格式

属性

类型

描述

名称

str

事件的用户定义名称。

数据

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}

提示:

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)
Parameters
  • 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,则引发此错误。

Return type

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。

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters
  • inputs (List[Input]) –

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

  • return_exceptions (bool) –

  • kwargs (Optional[Any]) –

Return type

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,并在完成时产生结果。

Parameters
  • inputs (Sequence[Input]) –

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

  • return_exceptions (bool) –

  • kwargs (Optional[Any]) –

Return type

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

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 的备选项。

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

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

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

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

Returns

配置了备选项的新 Runnable。

Return type

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 字段。

Parameters

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

Returns

配置了字段的新 Runnable。

Return type

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
)
create_outputs(llm_result: LLMResult) List[Dict[str, Any]]

从响应创建输出。

Parameters

llm_result (LLMResult) –

Return type

List[Dict[str, Any]]

classmethod from_llm(llm: BaseLanguageModel, demos: List[Dict] = [{'role': 'user', 'content': "please show me a video and an image of (based on the text) 'a boy is running' and dub it"}, {'role': 'assistant', 'content': '[{{"task": "video_generator", "id": 0, "dep": [-1], "args": {{"prompt": "a boy is running" }}}}, {{"task": "text_reader", "id": 1, "dep": [-1], "args": {{"text": "a boy is running" }}}}, {{"task": "image_generator", "id": 2, "dep": [-1], "args": {{"prompt": "a boy is running" }}}}]'}, {'role': 'user', 'content': 'Give you some pictures e1.jpg, e2.png, e3.jpg, help me count the number of sheep?'}, {'role': 'assistant', 'content': '[ {{"task": "image_qa", "id": 0, "dep": [-1], "args": {{"image": "e1.jpg", "question": "How many sheep in the picture"}}}}, {{"task": "image_qa", "id": 1, "dep": [-1], "args": {{"image": "e2.jpg", "question": "How many sheep in the picture"}}}}, {{"task": "image_qa", "id": 2, "dep": [-1], "args": {{"image": "e3.jpg", "question": "How many sheep in the picture"}}}}]'}], verbose: bool = True) LLMChain[source]

获取响应解析器。

Parameters
Return type

LLMChain

classmethod from_string(llm: BaseLanguageModel, template: str) LLMChain

从 LLM 和模板创建 LLMChain。

Parameters
Return type

LLMChain

generate(input_list: List[Dict[str, Any]], run_manager: Optional[CallbackManagerForChainRun] = None) LLMResult

从输入生成 LLM 结果。

Parameters
Return type

LLMResult

invoke(input: Dict[str, Any], config: Optional[RunnableConfig] = None, **kwargs: Any) Dict[str, Any]

将单个输入转换为输出。覆盖此方法以实现。

Parameters
  • input (Dict[str, Any]) – Runnable 的输入。

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

  • kwargs (Any) –

Returns

Runnable 的输出。

Return type

Dict[str, Any]

predict(callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, **kwargs: Any) str

使用 kwargs 格式化提示并传递给 LLM。

Parameters
Returns

来自 LLM 的补全。

Return type

str

示例

completion = llm.predict(adjective="funny")
predict_and_parse(callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, **kwargs: Any) Union[str, List[str], Dict[str, Any]]

调用 predict,然后解析结果。

Parameters
Return type

Union[str, List[str], Dict[str, Any]]

prep_inputs(inputs: Union[Dict[str, Any], Any]) Dict[str, str]

准备链的输入,包括从内存中添加输入。

Parameters

inputs (Union[Dict[str, Any], Any]) – 原始输入的字典,或者如果链只期望一个参数,则为单个输入。应包含 Chain.input_keys 中指定的所有输入,但链的内存将设置的输入除外。

Returns

所有输入的字典,包括链的内存添加的输入。

Return type

Dict[str, str]

prep_outputs(inputs: Dict[str, str], outputs: Dict[str, str], return_only_outputs: bool = False) Dict[str, str]

验证并准备链的输出,并将有关此运行的信息保存到内存中。

Parameters
  • inputs (Dict[str, str]) – 链输入的字典,包括链内存添加的任何输入。

  • outputs (Dict[str, str]) – 初始链输出的字典。

  • return_only_outputs (bool) – 是否仅返回链输出。如果为 False,则输入也会添加到最终输出中。

Returns

最终链输出的字典。

Return type

Dict[str, str]

prep_prompts(input_list: List[Dict[str, Any]], run_manager: Optional[CallbackManagerForChainRun] = None) Tuple[List[PromptValue], Optional[List[str]]]

从输入准备提示。

Parameters
Return type

Tuple[List[PromptValue], Optional[List[str]]]

run(*args: Any, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any) Any

Deprecated since version langchain==0.1.0: Use invoke instead.

用于执行链的便捷方法。

此方法与 Chain.__call__ 之间的主要区别在于,此方法期望输入作为位置参数或关键字参数直接传入,而 Chain.__call__ 期望单个输入字典包含所有输入

Parameters
  • *args (Any) – 如果链期望单个输入,则可以作为唯一的位置参数传入。

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – Callbacks to use for this chain run. These will be called in addition to callbacks passed to the chain during construction, but only these runtime callbacks will propagate to calls to other objects.

  • tags (Optional[List[str]]) – List of string tags to pass to all callbacks. These will be passed in addition to tags passed to the chain during construction, but only these runtime tags will propagate to calls to other objects.

  • **kwargs (Any) – 如果链期望多个输入,则可以直接作为关键字参数传入。

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

  • **kwargs

Returns

链的输出。

Return type

Any

示例

# Suppose we have a single-input chain that takes a 'question' string:
chain.run("What's the temperature in Boise, Idaho?")
# -> "The temperature in Boise is..."

# Suppose we have a multi-input chain that takes a 'question' string
# and 'context' string:
question = "What's the temperature in Boise, Idaho?"
context = "Weather report for Boise, Idaho on 07/03/23..."
chain.run(question=question, context=context)
# -> "The temperature in Boise is..."
save(file_path: Union[Path, str]) None

保存链。

期望实现 Chain._chain_type 属性并且内存为空。

null.

Parameters

file_path (Union[Path, str]) – 保存链到文件的路径。

Return type

None

示例

chain.save(file_path="path/chain.yaml")
stream(input: Input, config: Optional[RunnableConfig] = None, **kwargs: Optional[Any]) Iterator[Output]

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

Parameters
  • input (Input) – Runnable 的输入。

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

  • kwargs (Optional[Any]) – Additional keyword arguments to pass to the Runnable.

Yields

Runnable 的输出。

Return type

Iterator[Output]

to_json() Union[SerializedConstructor, SerializedNotImplemented]

将 Runnable 序列化为 JSON。

Returns

Runnable 的 JSON 可序列化表示。

Return type

Union[SerializedConstructor, SerializedNotImplemented]