How to distinguish AIMessageChunk
used for AgentAction
from AIMessageChunk
used for AgentFinish
when using .astream_events?
#24576
Replies: 3 comments 2 replies
-
Hey @mckinseyalexanderfalaleev! 👋 I'm here to help you with bug fixes, questions, and more. Let's tackle this together! Yes, it is possible to distinguish message chunks corresponding to For example, in the {
"output": "curses foiled again",
"messages": [
AIMessage(content="Oh well\nFinal Answer: curses foiled again")
],
} In contrast, {
"actions": [
AgentAction(
tool="Search",
tool_input="misalignment",
log="FooBarBaz\nAction: Search\nAction Input: misalignment",
)
],
"messages": [
AIMessage(
content="FooBarBaz\nAction: Search\nAction Input: misalignment"
)
],
} This distinction allows you to differentiate between the final output of the agent and the intermediate actions it takes. Additionally, the In the context of using the async for event in agent_executor.astream_events(
{"messages": [HumanMessage(content="whats the weather in sf?")]}, version="v1"
):
kind = event["event"]
if kind == "on_chain_start":
if event["name"] == "Agent":
print(f"Starting agent: {event['name']} with input: {event['data'].get('input')}")
elif kind == "on_chain_end":
if event["name"] == "Agent":
print()
print("--")
print(f"Done agent: {event['name']} with output: {event['data'].get('output')['output']}")
if kind == "on_chat_model_stream":
content = event["data"]["chunk"].content
if content:
print(content, end="|")
elif kind == "on_tool_start":
print("--")
print(f"Starting tool: {event['name']} with inputs: {event['data'].get('input')}")
elif kind == "on_tool_end":
print(f"Done tool: {event['name']}")
print(f"Tool output was: {event['data'].get('output')}")
print("--") This code snippet demonstrates how to handle different types of events, including those corresponding to |
Beta Was this translation helpful? Give feedback.
-
any updates @mckinseyalexanderfalaleev ? |
Beta Was this translation helpful? Give feedback.
-
I wasn't able to solve this :( |
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
Context:
I have the
AgentExecutor
with aPythonREPL
-like tool connected topandas.DataFrame
nameddf
andOpenAIToolsAgentOutputParser
.Question:
When using
AgentExecutor
inastream_events
mode, I capture all tokens produced by LLMs, including those that will be used forAgentAction
's log through filtering usingon_chat_model_stream
:Input:
Output:
I only need the capture tokens that will get into
AgentFinish
later. For example, the "output" field of the agent's response is precisely what I need when invoking agents in non-streaming mode.Input:
Output:
Is it possible to distinguish message chunks corresponding to
AgentFinish
from message chunks that will later becomeAgentAction
's log while usingastream_events
method? These events look the same, and filtering rules can not be implemented.System Info
Beta Was this translation helpful? Give feedback.
All reactions