-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathrun_agent.py
More file actions
113 lines (90 loc) · 3.78 KB
/
Copy pathrun_agent.py
File metadata and controls
113 lines (90 loc) · 3.78 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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.
import asyncio
import uuid
from dotenv import load_dotenv
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 environment variables from the .env file
load_dotenv()
async def run_custom_prompt_demo():
"""Run custom prompt injection demonstration.
Run three configuration scenarios to demonstrate the effects of add_name_to_instruction and default_transfer_message:
- Default behavior: framework automatically injects Agent name and forwarding instruction
- Disable name injection: add_name_to_instruction=False
- Custom forwarding instruction: default_transfer_message uses custom content
"""
app_name = "custom_prompt_demo"
user_id = "demo_user"
from agent.agent import create_agent
from agent.prompts import CUSTOM_TRANSFER_MESSAGE
# Three configuration scenarios comparison
scenarios = [
{
"title": "Scenario 1: Default (framework auto-injection enabled)",
"add_name": True,
"transfer_message": None,
},
{
"title": "Scenario 2: add_name_to_instruction=False",
"add_name": False,
"transfer_message": None,
},
{
"title": "Scenario 3: Custom default_transfer_message",
"add_name": True,
"transfer_message": CUSTOM_TRANSFER_MESSAGE,
},
]
# Test query coverage of two sub-Agents: weather and translation
demo_queries = [
"What's the weather in Beijing?",
"Translate 'hello' to Chinese",
]
for scenario in scenarios:
print("\n" + "=" * 80)
print(scenario["title"])
print(f" add_name_to_instruction = {scenario['add_name']}")
print(
f" default_transfer_message = {repr(scenario['transfer_message'][:30] + '...') if scenario['transfer_message'] else repr(scenario['transfer_message'])}"
)
print("=" * 80)
agent = create_agent(
add_name=scenario["add_name"],
transfer_message=scenario["transfer_message"],
)
session_service = InMemorySessionService()
runner = Runner(app_name=app_name, agent=agent, session_service=session_service)
current_session_id = str(uuid.uuid4())
for query in demo_queries:
print(f"\n📝 User query: {query}")
print("🤖 Assistant: ", end="", flush=True)
user_content = Content(parts=[Part.from_text(text=query)])
async for event in runner.run_async(
user_id=user_id,
session_id=current_session_id,
new_message=user_content,
):
if not event.content or not event.content.parts:
continue
if event.partial:
for part in event.content.parts:
if part.text:
print(part.text, end="", flush=True)
continue
for part in event.content.parts:
if part.thought:
continue
if part.function_call:
print(f"\n🔧 [{event.author}] Invoke Tool: {part.function_call.name}({part.function_call.args})")
elif part.function_response:
print(f"📊 [{event.author}] Tool Result: {part.function_response.response}")
print("\n" + "-" * 40)
print()
if __name__ == "__main__":
asyncio.run(run_custom_prompt_demo())