The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
Change RunnableMap to RunnableParallel for consistency (#14142)
Alex Kira0caef3cde754
diff --git a/docs/docs/expression_language/cookbook/multiple_chains.ipynb b/docs/docs/expression_language/cookbook/multiple_chains.ipynbindex c9713ac11a..c380d29f1b 100644--- a/docs/docs/expression_language/cookbook/multiple_chains.ipynb+++ b/docs/docs/expression_language/cookbook/multiple_chains.ipynb@@ -146,7 +146,7 @@"source": ["### Branching and Merging\n","\n",- "You may want the output of one component to be processed by 2 or more other components. [RunnableMaps](https://api.python.langchain.com/en/latest/schema/langchain.schema.runnable.base.RunnableMap.html) let you split or fork the chain so multiple components can process the input in parallel. Later, other components can join or merge the results to synthesize a final response. This type of chain creates a computation graph that looks like the following:\n",+ "You may want the output of one component to be processed by 2 or more other components. [RunnableParallels](https://api.python.langchain.com/en/latest/runnables/langchain_core.runnables.base.RunnableParallel.html#langchain_core.runnables.base.RunnableParallel) let you split or fork the chain so multiple components can process the input in parallel. Later, other components can join or merge the results to synthesize a final response. This type of chain creates a computation graph that looks like the following:\n","\n","```text\n"," Input\n",diff --git a/docs/docs/expression_language/cookbook/prompt_llm_parser.ipynb b/docs/docs/expression_language/cookbook/prompt_llm_parser.ipynbindex 657e7ddac7..81fd0ec057 100644--- a/docs/docs/expression_language/cookbook/prompt_llm_parser.ipynb+++ b/docs/docs/expression_language/cookbook/prompt_llm_parser.ipynb@@ -317,7 +317,7 @@"source": ["## Simplifying input\n","\n",- "To make invocation even simpler, we can add a `RunnableMap` to take care of creating the prompt input dict for us:"+ "To make invocation even simpler, we can add a `RunnableParallel` to take care of creating the prompt input dict for us:"]},{@@ -327,9 +327,9 @@"metadata": {},"outputs": [],"source": [- "from langchain.schema.runnable import RunnableMap, RunnablePassthrough\n",+ "from langchain.schema.runnable import RunnableParallel, RunnablePassthrough\n","\n",- "map_ = RunnableMap(foo=RunnablePassthrough())\n",+ "map_ = RunnableParallel(foo=RunnablePassthrough())\n","chain = (\n"," map_\n"," | prompt\n",diff --git a/docs/docs/expression_language/cookbook/retrieval.ipynb b/docs/docs/expression_language/cookbook/retrieval.ipynbindex 27470a4a56..2740ef626a 100644--- a/docs/docs/expression_language/cookbook/retrieval.ipynb+++ b/docs/docs/expression_language/cookbook/retrieval.ipynb@@ -171,7 +171,7 @@"outputs": [],"source": ["from langchain.schema import format_document\n",- "from langchain.schema.runnable import RunnableMap"+ "from langchain.schema.runnable import RunnableParallel"]},{@@ -256,7 +256,7 @@"metadata": {},"outputs": [],"source": [- "_inputs = RunnableMap(\n",+ "_inputs = RunnableParallel(\n"," standalone_question=RunnablePassthrough.assign(\n"," chat_history=lambda x: _format_chat_history(x[\"chat_history\"])\n"," )\n",diff --git a/docs/docs/expression_language/how_to/map.ipynb b/docs/docs/expression_language/how_to/map.ipynbindex efab160347..72df445bc1 100644--- a/docs/docs/expression_language/how_to/map.ipynb+++ b/docs/docs/expression_language/how_to/map.ipynb@@ -104,7 +104,7 @@"source": ["Here the input to prompt is expected to be a map with keys \"context\" and \"question\". The user input is just the question. So we need to get the context using our retriever and passthrough the user input under the \"question\" key.\n","\n",- "Note that when composing a RunnableMap with another Runnable we don't even need to wrap our dictionary in the RunnableMap class — the type conversion is handled for us."+ "Note that when composing a RunnableParallel with another Runnable we don't even need to wrap our dictionary in the RunnableParallel class — the type conversion is handled for us."]},{@@ -114,7 +114,7 @@"source": ["## Parallelism\n","\n",- "RunnableMaps are also useful for running independent processes in parallel, since each Runnable in the map is executed in parallel. For example, we can see our earlier `joke_chain`, `poem_chain` and `map_chain` all have about the same runtime, even though `map_chain` executes both of the other two."+ "RunnableParallel are also useful for running independent processes in parallel, since each Runnable in the map is executed in parallel. For example, we can see our earlier `joke_chain`, `poem_chain` and `map_chain` all have about the same runtime, even though `map_chain` executes both of the other two."]},{diff --git a/docs/docs/expression_language/how_to/message_history.ipynb b/docs/docs/expression_language/how_to/message_history.ipynbindex 1538802bad..4a9218b328 100644--- a/docs/docs/expression_language/how_to/message_history.ipynb+++ b/docs/docs/expression_language/how_to/message_history.ipynb@@ -290,9 +290,9 @@],"source": ["from langchain.schema.messages import HumanMessage\n",- "from langchain.schema.runnable import RunnableMap\n",+ "from langchain.schema.runnable import RunnableParallel\n","\n",- "chain = RunnableMap({\"output_message\": ChatAnthropic(model=\"claude-2\")})\n",+ "chain = RunnableParallel({\"output_message\": ChatAnthropic(model=\"claude-2\")})\n","chain_with_history = RunnableWithMessageHistory(\n"," chain,\n"," lambda session_id: RedisChatMessageHistory(session_id, url=REDIS_URL),\n",diff --git a/docs/docs/guides/privacy/presidio_data_anonymization/qa_privacy_protection.ipynb b/docs/docs/guides/privacy/presidio_data_anonymization/qa_privacy_protection.ipynbindex f6fb901e04..2de1e36c79 100644--- a/docs/docs/guides/privacy/presidio_data_anonymization/qa_privacy_protection.ipynb+++ b/docs/docs/guides/privacy/presidio_data_anonymization/qa_privacy_protection.ipynb@@ -60,7 +60,7 @@"\n"," Firstly, the wallet contains my credit card with number 4111 1111 1111 1111, which is registered under my name and linked to my bank account, PL61109010140000071219812874.\n","\n",- " Additionally, the wallet had a driver's license - DL No: 999000680 issued to my name. It also houses my Social Security Number, 602-76-4532. \n",+ " Additionally, the wallet had a driver's license - DL No: 999000680 issued to my name. It also houses my Social Security Number, 602-76-4532.\n","\n"," What's more, I had my polish identity card there, with the number ABC123456.\n","\n",@@ -68,7 +68,7 @@"\n"," In case any information arises regarding my wallet, please reach out to me on my phone number, 999-888-7777, or through my personal email, [email protected].\n","\n",- " Please consider this information to be highly confidential and respect my privacy. \n",+ " Please consider this information to be highly confidential and respect my privacy.\n","\n"," The bank has been informed about the stolen credit card and necessary actions have been taken from their end. They will be reachable at their official email, [email protected].\n"," My representative there is Victoria Cherry (her business phone: 987-654-3210).\n",@@ -667,7 +667,11 @@"from langchain.chat_models.openai import ChatOpenAI\n","from langchain.prompts import ChatPromptTemplate\n","from langchain.schema.output_parser import StrOutputParser\n",- "from langchain.schema.runnable import RunnableLambda, RunnableMap, RunnablePassthrough\n",+ "from langchain.schema.runnable import (\n",+ " RunnableLambda,\n",+ " RunnableParallel,\n",+ " RunnablePassthrough,\n",+ ")\n","\n","# 6. Create anonymizer chain\n","template = \"\"\"Answer the question based only on the following context:\n",@@ -680,7 +684,7 @@"model = ChatOpenAI(temperature=0.3)\n","\n","\n",- "_inputs = RunnableMap(\n",+ "_inputs = RunnableParallel(\n"," question=RunnablePassthrough(),\n"," # It is important to remember about question anonymization\n"," anonymized_question=RunnableLambda(anonymizer.anonymize),\n",@@ -882,7 +886,7 @@"\n","\n","chain_with_deanonymization = (\n",- " RunnableMap({\"question\": RunnablePassthrough()})\n",+ " RunnableParallel({\"question\": RunnablePassthrough()})\n"," | {\n"," \"context\": itemgetter(\"question\")\n"," | retriever\n",diff --git a/docs/docs/use_cases/question_answering/index.ipynb b/docs/docs/use_cases/question_answering/index.ipynbindex b1135f693c..b1264e1195 100644--- a/docs/docs/use_cases/question_answering/index.ipynb+++ b/docs/docs/use_cases/question_answering/index.ipynb@@ -776,10 +776,10 @@"source": ["from langchain.prompts import PromptTemplate\n","\n",- "template = \"\"\"Use the following pieces of context to answer the question at the end. \n",- "If you don't know the answer, just say that you don't know, don't try to make up an answer. \n",- "Use three sentences maximum and keep the answer as concise as possible. \n",- "Always say \"thanks for asking!\" at the end of the answer. \n",+ "template = \"\"\"Use the following pieces of context to answer the question at the end.\n",+ "If you don't know the answer, just say that you don't know, don't try to make up an answer.\n",+ "Use three sentences maximum and keep the answer as concise as possible.\n",+ "Always say \"thanks for asking!\" at the end of the answer.\n","{context}\n","Question: {question}\n","Helpful Answer:\"\"\"\n",@@ -846,7 +846,7 @@"source": ["from operator import itemgetter\n","\n",- "from langchain.schema.runnable import RunnableMap\n",+ "from langchain.schema.runnable import RunnableParallel\n","\n","rag_chain_from_docs = (\n"," {\n",@@ -857,7 +857,7 @@" | llm\n"," | StrOutputParser()\n",")\n",- "rag_chain_with_source = RunnableMap(\n",+ "rag_chain_with_source = RunnableParallel(\n"," {\"documents\": retriever, \"question\": RunnablePassthrough()}\n",") | {\n"," \"documents\": lambda input: [doc.metadata for doc in input[\"documents\"]],\n",diff --git a/templates/anthropic-iterative-search/anthropic_iterative_search/retriever_agent.py b/templates/anthropic-iterative-search/anthropic_iterative_search/retriever_agent.pyindex 81d4fb3cbb..0c373bfc95 100644--- a/templates/anthropic-iterative-search/anthropic_iterative_search/retriever_agent.py+++ b/templates/anthropic-iterative-search/anthropic_iterative_search/retriever_agent.py@@ -2,7 +2,7 @@ from langchain.agents import AgentExecutorfrom langchain.chat_models import ChatAnthropicfrom langchain.prompts import ChatPromptTemplatefrom langchain.schema.output_parser import StrOutputParser-from langchain.schema.runnable import RunnableMap, RunnablePassthrough+from langchain.schema.runnable import RunnableParallel, RunnablePassthroughfrom .agent_scratchpad import format_agent_scratchpadfrom .output_parser import parse_output@@ -29,7 +29,7 @@ chain = ()agent_chain = (- RunnableMap(+ RunnableParallel({"partial_completion": chain,"intermediate_steps": lambda x: x["intermediate_steps"],diff --git a/templates/rag-conversation-zep/rag_conversation_zep/chain.py b/templates/rag-conversation-zep/rag_conversation_zep/chain.pyindex 52d871dd71..7da815f2ad 100644--- a/templates/rag-conversation-zep/rag_conversation_zep/chain.py+++ b/templates/rag-conversation-zep/rag_conversation_zep/chain.py@@ -14,7 +14,7 @@ from langchain.schema.runnable import (ConfigurableField,RunnableBranch,RunnableLambda,- RunnableMap,+ RunnableParallel,RunnablePassthrough,)from langchain.schema.runnable.utils import ConfigurableFieldSingleOption@@ -133,7 +133,7 @@ class ChatHistory(BaseModel):question: str-_inputs = RunnableMap(+_inputs = RunnableParallel({"question": lambda x: x["question"],"chat_history": lambda x: _format_chat_history(x["chat_history"]),diff --git a/templates/rag-conversation/rag_conversation/chain.py b/templates/rag-conversation/rag_conversation/chain.pyindex fee8d94694..3c12583caf 100644--- a/templates/rag-conversation/rag_conversation/chain.py+++ b/templates/rag-conversation/rag_conversation/chain.py@@ -11,7 +11,7 @@ from langchain.schema.output_parser import StrOutputParserfrom langchain.schema.runnable import (RunnableBranch,RunnableLambda,- RunnableMap,+ RunnableParallel,RunnablePassthrough,)from langchain.vectorstores import Pinecone@@ -108,7 +108,7 @@ _search_query = RunnableBranch(RunnableLambda(itemgetter("question")),)-_inputs = RunnableMap(+_inputs = RunnableParallel({"question": lambda x: x["question"],"chat_history": lambda x: _format_chat_history(x["chat_history"]),diff --git a/templates/rag-elasticsearch/rag_elasticsearch/chain.py b/templates/rag-elasticsearch/rag_elasticsearch/chain.pyindex 60358c3567..07f37300b0 100644--- a/templates/rag-elasticsearch/rag_elasticsearch/chain.py+++ b/templates/rag-elasticsearch/rag_elasticsearch/chain.py@@ -5,7 +5,7 @@ from langchain.chat_models import ChatOpenAIfrom langchain.embeddings import HuggingFaceEmbeddingsfrom langchain.schema import BaseMessage, format_documentfrom langchain.schema.output_parser import StrOutputParser-from langchain.schema.runnable import RunnableMap, RunnablePassthrough+from langchain.schema.runnable import RunnableParallel, RunnablePassthroughfrom langchain.vectorstores.elasticsearch import ElasticsearchStorefrom pydantic import BaseModel, Field@@ -49,7 +49,7 @@ class ChainInput(BaseModel):question: str = Field(..., description="The question to answer.")-_inputs = RunnableMap(+_inputs = RunnableParallel(standalone_question=RunnablePassthrough.assign(chat_history=lambda x: _format_chat_history(x["chat_history"]))diff --git a/templates/rag-google-cloud-sensitive-data-protection/rag_google_cloud_sensitive_data_protection/chain.py b/templates/rag-google-cloud-sensitive-data-protection/rag_google_cloud_sensitive_data_protection/chain.pyindex f5e8cee611..aeb6033e51 100644--- a/templates/rag-google-cloud-sensitive-data-protection/rag_google_cloud_sensitive_data_protection/chain.py+++ b/templates/rag-google-cloud-sensitive-data-protection/rag_google_cloud_sensitive_data_protection/chain.py@@ -7,7 +7,7 @@ from langchain.prompts import ChatPromptTemplate, MessagesPlaceholderfrom langchain.pydantic_v1 import BaseModel, Fieldfrom langchain.schema.messages import AIMessage, HumanMessagefrom langchain.schema.output_parser import StrOutputParser-from langchain.schema.runnable import RunnableLambda, RunnableMap+from langchain.schema.runnable import RunnableLambda, RunnableParallel# Formatting for chat history@@ -96,7 +96,7 @@ class ChatHistory(BaseModel):chat_history: List[Tuple[str, str]] = Field(..., extra={"widget": {"type": "chat"}})-_inputs = RunnableMap(+_inputs = RunnableParallel({"question": RunnableLambda(lambda x: _deidentify_with_replace(diff --git a/templates/rag-timescale-conversation/rag_timescale_conversation/chain.py b/templates/rag-timescale-conversation/rag_timescale_conversation/chain.pyindex 6ff6c5b042..ba9551db80 100644--- a/templates/rag-timescale-conversation/rag_timescale_conversation/chain.py+++ b/templates/rag-timescale-conversation/rag_timescale_conversation/chain.py@@ -13,7 +13,7 @@ from langchain.schema.output_parser import StrOutputParserfrom langchain.schema.runnable import (RunnableBranch,RunnableLambda,- RunnableMap,+ RunnableParallel,RunnablePassthrough,)from langchain.vectorstores.timescalevector import TimescaleVector@@ -136,7 +136,7 @@ def get_retriever_with_metadata(x):_retriever = RunnableLambda(get_retriever_with_metadata)-_inputs = RunnableMap(+_inputs = RunnableParallel({"question": lambda x: x["question"],"chat_history": lambda x: _format_chat_history(x["chat_history"]),