langchain_core.beta.runnables.context
.Context¶
- class langchain_core.beta.runnables.context.Context[source]¶
用于可运行对象的上下文。
Context 类提供了在可运行对象中创建上下文范围、获取器和设置器的方法。它允许在程序执行过程中管理和访问上下文信息。
- 示例:
from langchain_core.beta.runnables.context import Context from langchain_core.runnables.passthrough import RunnablePassthrough from langchain_core.prompts.prompt import PromptTemplate from langchain_core.output_parsers.string import StrOutputParser from tests.unit_tests.fake.llm import FakeListLLM chain = ( Context.setter("input") | { "context": RunnablePassthrough() | Context.setter("context"), "question": RunnablePassthrough(), } | PromptTemplate.from_template("{context} {question}") | FakeListLLM(responses=["hello"]) | StrOutputParser() | { "result": RunnablePassthrough(), "context": Context.getter("context"), "input": Context.getter("input"), } ) # 使用链 output = chain.invoke("What's your name?") print(output["result"]) # 输出: "hello" print(output["context"]) # 输出: "What's your name?" print(output["input"]) # 输出: "What's your name?
Methods
__init__
()create_scope
(scope, /)创建一个上下文范围。
getter
(key, /)setter
([_key, _value])- __init__()¶
- static create_scope(scope: str, /) PrefixContext [source]¶
创建一个上下文范围。
- 参数:
scope: 范围。
- 返回:
上下文范围。
- Parameters
scope (str) –
- Return type
- static getter(key: Union[str, List[str]], /) ContextGet [source]¶
- Parameters
key (Union[str, List[str]]) –
- Return type
- static setter(_key: Optional[str] = None, _value: Optional[Union[Runnable[Input, Output], Callable[[Input], Output], Callable[[Input], Awaitable[Output]], Any]] = None, /, **kwargs: Union[Runnable[Input, Output], Callable[[Input], Output], Callable[[Input], Awaitable[Output]], Any]) ContextSet [source]¶
- Parameters
- Return type