From d4b025c812a406642981a52b60beb4f33266eb74 Mon Sep 17 00:00:00 2001 From: Guangdong Liu Date: Fri, 15 Mar 2024 03:41:19 +0800 Subject: [PATCH] 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 --- libs/core/langchain_core/runnables/base.py | 30 +++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/libs/core/langchain_core/runnables/base.py b/libs/core/langchain_core/runnables/base.py index 7624828c3f1..e41ef5877d7 100644 --- a/libs/core/langchain_core/runnables/base.py +++ b/libs/core/langchain_core/runnables/base.py @@ -459,7 +459,35 @@ class Runnable(Generic[Input, Output], ABC): ], ) -> RunnableSerializable[Any, Any]: """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 return self | RunnableAssign(RunnableParallel(kwargs))