mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-13 06:40:04 +00:00
core: Updated docstring for Context class (#19079)
- **Description:** Improves the docstring for `class Context` by providing an overview and an example. - **Issue:** #18803
This commit is contained in:
parent
044bc22acc
commit
bc648f6cfc
@ -307,7 +307,46 @@ class ContextSet(RunnableSerializable):
|
|||||||
|
|
||||||
|
|
||||||
class Context:
|
class Context:
|
||||||
"""Context for a runnable."""
|
"""
|
||||||
|
Context for a runnable.
|
||||||
|
|
||||||
|
The `Context` class provides methods for creating context scopes,
|
||||||
|
getters, and setters within a runnable. It allows for managing
|
||||||
|
and accessing contextual information throughout the execution
|
||||||
|
of a program.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
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?
|
||||||
|
"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_scope(scope: str, /) -> "PrefixContext":
|
def create_scope(scope: str, /) -> "PrefixContext":
|
||||||
|
Loading…
Reference in New Issue
Block a user