Agent Executor astream_events
with asyncGenerator
tools.
#25789
Replies: 1 comment
-
I found some similar discussions and issues that might be relevant to your question:
To address your specific question about restricting # If the tool chosen is the finishing tool, then we end and return.
if isinstance(output, AgentFinish):
yield output
return
actions: List[AgentAction]
if isinstance(output, AgentAction):
actions = [output]
else:
actions = output
for agent_action in actions:
yield agent_action
# Use asyncio.gather to run multiple tool.arun() calls concurrently
result = await asyncio.gather(
*[
self._aperform_agent_action(
name_to_tool_map, color_mapping, agent_action, run_manager
)
for agent_action in actions
],
)
# TODO This could yield each result as it becomes available
for chunk in result:
yield chunk This code ensures that all the asynchronous tasks ( Additionally, ensure that the from langchain_core.runnables import RunnableConfig
@tool
async def special_summarization_tool_with_config(
long_text: str, config: RunnableConfig
) -> str:
"""A tool that summarizes input text using advanced techniques."""
prompt = ChatPromptTemplate.from_template(
"You are an expert writer. Summarize the following text in 10 words or less:\n\n{long_text}"
)
def reverse(x: str):
return x[::-1]
chain = prompt | model | StrOutputParser() | reverse
# Pass the "config" object as an argument to any executed runnables
summary = await chain.ainvoke({"long_text": long_text}, config=config)
return summary By ensuring the proper propagation of the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
I attempted to use the langchain agent executor with
astream_events
. When I gave the following instruction:"Please take a photo, describe it, and tell me if there are any white clouds in the photo?"
I usually receive a response like the following (output in streaming mode):
"There is a big sun in the photo, ... Get Image Successfully..., I cannot determine the contents of the photo..., there are white clouds and an airplane..."
I have the following questions:
astream_events
tends to execute each tool (e.g.,get_image_tool
,describe_image
) concurrently due to its async nature.describe_image
is anasyncGenerator
, the agent begins summarizing before the photo description is complete, leading to an incorrect understanding, such as "I cannot determine the contents of the photo."I would like to know if there is a way to restrict
astream_event
to wait for specific tools (asyncGenerators
) to finish their tasks before proceeding to the next step or summarizing.System Info
System Information
Package Information
Optional packages not installed
Other Dependencies
Beta Was this translation helpful? Give feedback.
All reactions