Create agent_debug.py
Browse files- agent_debug.py +43 -0
agent_debug.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from llama_index.core.agent.workflow import (
|
| 2 |
+
AgentInput,
|
| 3 |
+
AgentOutput,
|
| 4 |
+
ToolCall,
|
| 5 |
+
ToolCallResult,
|
| 6 |
+
AgentStream,
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
async def run_workflow_agent(workflow_agent, user_msg: str):
|
| 10 |
+
handler = workflow_agent.run(user_msg=user_msg)
|
| 11 |
+
|
| 12 |
+
current_agent = None
|
| 13 |
+
|
| 14 |
+
async for event in handler.stream_events():
|
| 15 |
+
# Detect agent switch
|
| 16 |
+
if hasattr(event, "current_agent_name") and event.current_agent_name != current_agent:
|
| 17 |
+
current_agent = event.current_agent_name
|
| 18 |
+
print(f"\n{'='*50}")
|
| 19 |
+
print(f"Agent Name: {current_agent}")
|
| 20 |
+
print(f"{'='*50}\n")
|
| 21 |
+
|
| 22 |
+
# Handle Agent Output
|
| 23 |
+
if isinstance(event, AgentOutput):
|
| 24 |
+
if event.response.content:
|
| 25 |
+
print("Output:", event.response.content)
|
| 26 |
+
if event.tool_calls:
|
| 27 |
+
print("\nPlanning to use tools:", [call.tool_name for call in event.tool_calls])
|
| 28 |
+
|
| 29 |
+
# Handle Tool Call
|
| 30 |
+
elif isinstance(event, ToolCall):
|
| 31 |
+
print("\n**************************Calling***********************")
|
| 32 |
+
print(f"Calling Tool: {event.tool_name}")
|
| 33 |
+
print(f"Calling With Arguments: {event.tool_kwargs}")
|
| 34 |
+
|
| 35 |
+
# Handle Tool Call Result
|
| 36 |
+
elif isinstance(event, ToolCallResult):
|
| 37 |
+
print("\n**************************Results***********************")
|
| 38 |
+
print(f"Tool Result ({event.tool_name}):")
|
| 39 |
+
print(f"Arguments: {event.tool_kwargs}")
|
| 40 |
+
print(f"Output: {event.tool_output}")
|
| 41 |
+
print(f"{'='*100}")
|
| 42 |
+
|
| 43 |
+
return handler
|