RunnableParallel: run chain for each element in a list in parralel #25521
Replies: 3 comments 9 replies
-
Hey there, @VolodymyrGnennyi! I'm a bot designed to help you with bugs, questions, and becoming a contributor. Let's squash this bug together! 🤖 To run a from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables.base import RunnableSequence
from langchain_core.runnables import RunnableParallel
sentences = ["The USA is the fourth largest country in the world in area", "CA, constituent state of the United States of America."]
prompt = PromptTemplate(template="Rephrase input sentence resolving abbreviations. {sentence}")
llm = ChatOpenAI(temperature=0, model="gpt-4o")
chain = prompt | llm | StrOutputParser()
# Create a list of chains with different input parameters
runnable_list = [chain.bind(sentence=s) for s in sentences]
parallel_chain = RunnableParallel(runnable_list)
results = parallel_chain.batch([{} for _ in sentences])
print(results) In this example, |
Beta Was this translation helpful? Give feedback.
-
@eyurtsev , in addition to above here is a full error message and it looks like a bug somewhere in runnable: |
Beta Was this translation helpful? Give feedback.
-
Got the same problem.... |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
I'm trying to run a Runnable for each element in a list. My example contains list of sentenses with abbreviations (e.g. USA, CA). I would like LLM to rephrase each sentense to get full meaning for each abbreviation.
I would like to run this in parallel. So I created a chain definition. Then I created a map { key : chain_with_invokation_parameter } which is required argument for RunnableParallel
Unfortunately, when I run code it complains: "RunnableParallel.invoke() missing 1 required positional argument: 'input'"
If I modify list line of the code as the following:
paralel_chain.invoke({'input': 'bla-bla-bla'})
then I get error as the following: "TypeError: BasePromptTemplate.invoke() got an unexpected keyword argument 'sentence'"
System Info
System Information
Package Information
Packages not installed (Not Necessarily a Problem)
The following packages were not found:
Beta Was this translation helpful? Give feedback.
All reactions