-
Checked other resources
Commit to Help
Example Codeimport asyncio
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
# Define your LangChain components
llm = ChatOpenAI(model_name='openai/gpt-4o-mini')
template = "Translate the following English text to French: {text}"
prompt = PromptTemplate(template=template, input_variables=["text"])
chain = llm | prompt
# Define an async function to run the chain
async def run_chain(text):
return await chain.run_async({"text": text})
# Define a function to run multiple chains concurrently
async def run_multiple_chains(texts):
tasks = [run_chain(text) for text in texts]
results = await asyncio.gather(*tasks)
return results
# List of texts to process
texts = [
"Hello, how are you?",
"What is your name?",
"Where are you from?",
"What do you do?"
]
# Run the chains concurrently
results = asyncio.run(run_multiple_chains(texts))
# Print the results
for result in results:
print(result) DescriptionI am trying to use the Langchain library to send multiple requests at the same time to increase the speed of my program.
AttributeError: 'RunnableSequence' object has no attribute 'run_async'the System InfoSystem Information
Package Information
Packages not installed (Not Necessarily a Problem)The following packages were not found:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
To send multiple requests concurrently using the LangChain library, you should use the import asyncio
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain_core.runnables import RunnableSequence
# Define your LangChain components
llm = ChatOpenAI(model_name='openai/gpt-4o-mini')
template = "Translate the following English text to French: {text}"
prompt = PromptTemplate(template=template, input_variables=["text"])
# Combine them into a RunnableSequence
chain = RunnableSequence(first=prompt, last=llm)
# Define an async function to run the chain
async def run_chain(text):
return await chain.ainvoke({"text": text})
# Define a function to run multiple chains concurrently
async def run_multiple_chains(texts):
tasks = [run_chain(text) for text in texts]
results = await asyncio.gather(*tasks)
return results
# List of texts to process
texts = [
"Hello, how are you?",
"What is your name?",
"Where are you from?",
"What do you do?"
]
# Run the chains concurrently
results = asyncio.run(run_multiple_chains(texts))
# Print the results
for result in results:
print(result) In this updated code, |
Beta Was this translation helpful? Give feedback.
-
@dosu I run the below code but I got this error how can I fix it? RuntimeError Traceback (most recent call last) /usr/lib/python3.10/asyncio/runners.py in run(main, debug) RuntimeError: asyncio.run() cannot be called from a running event loop import asyncio llm = ChatOpenAI(model_name='openai/gpt-4o-mini') Combine them into a RunnableSequencechain = RunnableSequence(first=chat_prompt_template, middle=llm, last=parser) Define an async function to run the chainasync def run_chain(text, image_path): Define a function to run multiple chains concurrentlyasync def run_multiple_chains(images, texts): Run the chains concurrentlyresults = asyncio.run(run_multiple_chains(input_texts, images_path)) Print the resultsfor result in results: |
Beta Was this translation helpful? Give feedback.
-
@dosu I got this error now. fix it and tell me how can I create a chain with 3 or more parts with RunnableSequence 2624 if not steps: TypeError: can only concatenate list (not "ChatOpenAI") to list |
Beta Was this translation helpful? Give feedback.
To send multiple requests concurrently using the LangChain library, you should use the
ainvoke
method instead ofrun_async
. Here's how you can modify your code to achieve this: