forked from trpc-group/trpc-agent-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_agent.py
More file actions
97 lines (76 loc) · 3.26 KB
/
Copy pathrun_agent.py
File metadata and controls
97 lines (76 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
# Tencent is pleased to support the open source community by making tRPC-Agent-Python available.
#
# Copyright (C) 2026 Tencent. All rights reserved.
#
# tRPC-Agent-Python is licensed under Apache-2.0.
"""
Multi-agent example demonstrating start_from_last_agent feature.
This example shows how start_from_last_agent=True allows follow-up questions
to be handled by the last active sub-agent instead of routing back through
the coordinator.
"""
import asyncio
import uuid
from dotenv import load_dotenv
from trpc_agent_sdk.configs import RunConfig
from trpc_agent_sdk.runners import Runner
from trpc_agent_sdk.sessions import InMemorySessionService
from trpc_agent_sdk.types import Content
from trpc_agent_sdk.types import Part
load_dotenv()
async def run_multi_agent_demo():
"""Run the multi-agent demo with start_from_last_agent enabled."""
app_name = "multi_agent_start_from_last_demo"
from agent.agent import root_agent
session_service = InMemorySessionService()
runner = Runner(app_name=app_name, agent=root_agent, session_service=session_service)
user_id = "demo_user"
session_id = str(uuid.uuid4())
# Create run config with start_from_last_agent enabled
run_config = RunConfig(start_from_last_agent=True)
# Demo conversation showing the feature:
# 1. First message goes to coordinator, which routes to sales
# 2. Second message (follow-up) goes directly to sales, not back to coordinator
demo_queries = [
"I'm interested in your smart speakers. What do you have?",
"What about the display products?", # Follow-up goes directly to sales
"Are there any discounts available?", # Another follow-up to sales
]
print("=" * 60)
print("Multi-Agent Demo: start_from_last_agent=True")
print("=" * 60)
print(f"\nSession ID: {session_id[:8]}...")
print("\nThis demo shows how follow-up questions stay with the")
print("last active agent instead of routing back to the coordinator.\n")
print("-" * 60)
for i, query in enumerate(demo_queries, 1):
print(f"\n[Turn {i}] User: {query}")
print("-" * 40)
user_content = Content(parts=[Part.from_text(text=query)])
async for event in runner.run_async(
user_id=user_id,
session_id=session_id,
new_message=user_content,
run_config=run_config,
):
if not event.content or not event.content.parts:
continue
if event.partial:
continue
for part in event.content.parts:
if part.thought:
continue
if part.function_call:
print(f"[{event.author}] Tool: {part.function_call.name}({part.function_call.args})")
elif part.function_response:
print(f"[{event.author}] Result: {part.function_response.response}...")
elif part.text:
print(f"[{event.author}] {part.text}")
print("\n" + "=" * 60)
print("Demo completed!")
print("=" * 60)
if __name__ == "__main__":
print("Multi-Agent Start From Last Agent Example")
print("Shows how follow-up queries stay with the last active sub-agent\n")
asyncio.run(run_multi_agent_demo())