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
237 lines (176 loc) · 8.04 KB
/
Copy pathrun_agent.py
File metadata and controls
237 lines (176 loc) · 8.04 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/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.
import asyncio
import json
import uuid
from agent.agent import UserProfileOutput
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_dotenv()
async def run_agent_with_schema():
"""Run agent with schema,showcase the schema functionality"""
app_name = "profile_analysis_demo"
user_id = "demo_user"
from agent.agent import create_agent
agent = create_agent()
session_service = InMemorySessionService()
runner = Runner(app_name=app_name, agent=agent, session_service=session_service)
# User profile
profile = {
"name": "Zhang San",
"age": 28,
"email": "zhangsan@example.com",
"interests": ["programming", "fitness"],
"location": "Beijing",
}
current_session_id = str(uuid.uuid4())
await session_service.create_session(
app_name=app_name,
user_id=user_id,
session_id=current_session_id,
state={"user_name": profile["name"]},
)
print(f"🆔 Session ID: {current_session_id[:8]}...")
print(f"📝 User profile:\n {json.dumps(profile, indent=4, ensure_ascii=False)}")
# Convert the input data into a JSON string (input_schema requirements)
json_input = json.dumps(profile, ensure_ascii=False)
user_content = Content(parts=[Part.from_text(text=json_input)])
print("🤖 Analysis result: ", end="", flush=True)
try:
async for event in runner.run_async(user_id=user_id, session_id=current_session_id, new_message=user_content):
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
elif part.function_call:
print(f"\n🔧 [Call tool: {part.function_call.name}({part.function_call.args})]")
elif part.function_response:
print(f"📊 [Tool result: {part.function_response.response}]")
# Check whether the result is saved to the session state
session = await session_service.get_session(app_name=app_name, user_id=user_id, session_id=current_session_id)
if session and session.state and agent.output_key in session.state:
saved_result = session.state[agent.output_key]
user_profile = UserProfileOutput.model_validate_json(saved_result)
print(f"💾 Get UserProfileOutput: {user_profile}")
except Exception as e:
print(f"\n❌ Analysis error: {e}")
await runner.close()
print("\n" + "-" * 60)
# ============================================================================
# Agent Without Tools Demo
# ============================================================================
async def run_agent_without_tools():
"""Run tool-free agent demo: direct JSON output"""
print("\n🚀 Agent Without Tools - Direct JSON Output Demo")
print("=" * 60)
app_name = "direct_profile_analysis_demo"
user_id = "demo_user"
from agent.agent import create_agent_without_tools
agent = create_agent_without_tools()
session_service = InMemorySessionService()
runner = Runner(app_name=app_name, agent=agent, session_service=session_service)
# User profile
profile = {
"name": "Wang Wu",
"age": 35,
"email": "wangwu@example.com",
"interests": ["reading", "traveling", "photography", "cooking"],
"location": "Shenzhen",
}
current_session_id = str(uuid.uuid4())
await session_service.create_session(
app_name=app_name,
user_id=user_id,
session_id=current_session_id,
state={"user_name": profile["name"]},
)
print(f"🆔 Session ID: {current_session_id[:8]}...")
print(f"📝 User profile:\n {json.dumps(profile, indent=4, ensure_ascii=False)}")
print("🤖 Direct JSON analysis result: ", end="", flush=True)
# Convert the input data into a JSON string (input_schema requirements)
json_input = json.dumps(profile, ensure_ascii=False)
user_content = Content(parts=[Part.from_text(text=json_input)])
try:
async for event in runner.run_async(user_id=user_id, session_id=current_session_id, new_message=user_content):
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
elif part.function_call:
print(f"\n🔧 [Call tool: {part.function_call.name}({part.function_call.args})]")
elif part.function_response:
print(f"📊 [Tool result: {part.function_response.response}]")
session = await session_service.get_session(app_name=app_name, user_id=user_id, session_id=current_session_id)
if session and session.state and agent.output_key in session.state:
saved_result = session.state[agent.output_key]
user_profile = UserProfileOutput.model_validate_json(saved_result)
print(f"\n💾 Get UserProfileOutput: {user_profile}")
except Exception as e:
print(f"\n❌ Analysis error: {e}")
await runner.close()
print("\n" + "-" * 60)
# ============================================================================
# AgentTool Usage Example
# ============================================================================
async def run_agent_tool_with_schema():
"""Demonstrate wrapping schema-enabled agent via AgentTool"""
print("\n🔧 AgentTool with Schema example")
print("=" * 60)
# Create a main agent to use this tool
from agent.agent import create_agent_tool_with_schema
main_agent = create_agent_tool_with_schema()
session_service = InMemorySessionService()
runner = Runner(app_name="agent_tool_demo", agent=main_agent, session_service=session_service)
user_id = "demo_user"
session_id = str(uuid.uuid4())
# Test Data(Natural language input) for the main agent to construct UserProfileInput
description = "My name is Li Si, I'm 32 years old, my email is lisi@example.com, I like reading, traveling and photography, and I live in Shanghai."
user_content = Content(parts=[Part.from_text(text=description)])
print(f"\n📝 Extract user profile information: {description}")
try:
async for event in runner.run_async(user_id=user_id, session_id=session_id, new_message=user_content):
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
elif part.function_call:
print(f"\n🔧 [Call tool: {part.function_call.name}({part.function_call.args})]")
elif part.function_response:
print(f"📊 [Tool result: {part.function_response.response}]")
except Exception as e:
print(f"\n❌ Runtime error: {e}")
print("\n" + "-" * 30)
await runner.close()
print("\n" + "-" * 60)
async def main():
print("\n🚀 Start running Agent Schema example...")
await run_agent_with_schema()
await run_agent_without_tools()
await run_agent_tool_with_schema()
print("🎉 Successfully running all examples!")
if __name__ == "__main__":
try:
asyncio.run(main())
except Exception as e:
print(f"\n❌ Runtime error: {e}")
import traceback
traceback.print_exc()