-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathrun_agent.py
More file actions
90 lines (71 loc) · 3.4 KB
/
Copy pathrun_agent.py
File metadata and controls
90 lines (71 loc) · 3.4 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
# 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.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 environment variables from the .env file
load_dotenv()
async def run_user_history_demo():
""" run user history injection demo.
Demonstrate how to inject user history into the Agent's context through HistoryRecord,
so that the Agent can answer questions based on historical information without calling tools.
"""
app_name = "user_history_demo"
from agent.agent import root_agent
from agent.tools import make_user_history_record
session_service = InMemorySessionService()
runner = Runner(app_name=app_name, agent=root_agent, session_service=session_service)
user_id = "demo_user"
# multiple turns of conversation share the same session_id, so that the Agent can see the historical messages of this session
session_id = str(uuid.uuid4())
demo_queries = [
"What's your name?",
"what is the weather like in paris?",
"Do you remember my name?",
]
for query in demo_queries:
print(f"📝 User: {query}")
# construct user history record, and build context content based on the current query
# history_content will contain historical question-answer pairs related to the query, for Agent reference
history_record = make_user_history_record()
history_content = history_record.build_content(query)
user_content = Content(parts=[Part.from_text(text=query)])
print("🤖 Assistant: ", end="", flush=True)
# enable session history saving, so that subsequent turns can accumulate context
run_config = RunConfig(save_history_enabled=True)
# new_message is passed in as a list [history_content, user_content],
# so that the historical record and the user's current question are injected into the Agent's input
async for event in runner.run_async(
user_id=user_id,
session_id=session_id,
new_message=[history_content, user_content],
run_config=run_config,
):
# skip empty content event
if not event.content or not event.content.parts:
continue
# partial=True means the intermediate fragment of streaming output, print text in real time
if event.partial:
for part in event.content.parts:
if part.text:
print(part.text, end="", flush=True)
continue
# complete event: print tool call and tool return result, skip thinking process
for part in event.content.parts:
if part.thought:
continue
if part.function_call:
print(f"\n🔧 [Invoke Tool: {part.function_call.name}({part.function_call.args})")
elif part.function_response:
print(f"📊 [Tool Result: {part.function_response.response}]")
print("\n" + "-" * 40)
if __name__ == "__main__":
asyncio.run(run_user_history_demo())