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"), } ) # Use the chain output = chain.invoke("What's your name?") print(output["result"]) # Output: "hello" print(output["context"]) # Output: "What's your name?" print(output["input"]) # Output: "What's your name?
方法
__init__
()create_scope
(scope, /)创建上下文作用域。
getter
(key, /)setter
([_key, _value])- __init__()¶
- static create_scope(scope: str, /) PrefixContext [source]¶
创建上下文作用域。
- 参数
scope (str) – 作用域。
- 返回
上下文作用域。
- 返回类型
- static getter(key: Union[str, List[str]], /) ContextGet [source]¶
- 参数
key (Union[str, List[str]]) –
- 返回类型
- 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]¶