mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-18 18:53:10 +00:00
code[patch]: Add in code documentation to core Runnable assign method (docs only) (#18951)
**PR message**: ***Delete this entire checklist*** and replace with - **Description:** [a description of the change](docs: Add in code documentation to core Runnable assign method) - **Issue:** the issue #18804
This commit is contained in:
parent
688a5bd106
commit
d4b025c812
@ -459,7 +459,35 @@ class Runnable(Generic[Input, Output], ABC):
|
|||||||
],
|
],
|
||||||
) -> RunnableSerializable[Any, Any]:
|
) -> RunnableSerializable[Any, Any]:
|
||||||
"""Assigns new fields to the dict output of this runnable.
|
"""Assigns new fields to the dict output of this runnable.
|
||||||
Returns a new runnable."""
|
Returns a new runnable.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
from langchain_community.llms.fake import FakeStreamingListLLM
|
||||||
|
from langchain_core.output_parsers import StrOutputParser
|
||||||
|
from langchain_core.prompts import SystemMessagePromptTemplate
|
||||||
|
from langchain_core.runnables import Runnable
|
||||||
|
from operator import itemgetter
|
||||||
|
|
||||||
|
prompt = (
|
||||||
|
SystemMessagePromptTemplate.from_template("You are a nice assistant.")
|
||||||
|
+ "{question}"
|
||||||
|
)
|
||||||
|
llm = FakeStreamingListLLM(responses=["foo-lish"])
|
||||||
|
|
||||||
|
chain: Runnable = prompt | llm | {"str": StrOutputParser()}
|
||||||
|
|
||||||
|
chain_with_assign = chain.assign(hello=itemgetter("str") | llm)
|
||||||
|
|
||||||
|
print(chain_with_assign.input_schema.schema())
|
||||||
|
# {'title': 'PromptInput', 'type': 'object', 'properties':
|
||||||
|
{'question': {'title': 'Question', 'type': 'string'}}}
|
||||||
|
print(chain_with_assign.output_schema.schema()) #
|
||||||
|
{'title': 'RunnableSequenceOutput', 'type': 'object', 'properties':
|
||||||
|
{'str': {'title': 'Str',
|
||||||
|
'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}}
|
||||||
|
|
||||||
|
"""
|
||||||
from langchain_core.runnables.passthrough import RunnableAssign
|
from langchain_core.runnables.passthrough import RunnableAssign
|
||||||
|
|
||||||
return self | RunnableAssign(RunnableParallel(kwargs))
|
return self | RunnableAssign(RunnableParallel(kwargs))
|
||||||
|
Loading…
Reference in New Issue
Block a user