forked from trpc-group/trpc-agent-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
97 lines (82 loc) · 3.5 KB
/
Copy pathagent.py
File metadata and controls
97 lines (82 loc) · 3.5 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
# 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.
""" Agent module"""
from trpc_agent_sdk.agents import InvocationContext
from trpc_agent_sdk.agents import LlmAgent
from trpc_agent_sdk.models import LLMModel
from trpc_agent_sdk.models import LlmRequest
from trpc_agent_sdk.models import OpenAIModel
from trpc_agent_sdk.tools import FunctionTool
from .config import get_model_config
from .prompts import COORDINATOR_INSTRUCTION
from .prompts import TRANSLATION_INSTRUCTION
from .prompts import WEATHER_INSTRUCTION
from .tools import get_weather_report
from .tools import translate_text
def _print_system_instruction(ctx: InvocationContext, req: LlmRequest):
"""before_model_callback: print the actual system instruction sent to LLM,
for comparing whether the framework has injected the name and transfer instruction.
"""
agent_name = ctx.agent.name
instruction = req.config.system_instruction if req.config else "(empty)"
print(f"\n{'·' * 60}")
print(f"📋 [{agent_name}] System Instruction sent to LLM:")
print(f"{instruction}")
print(f"{'·' * 60}\n")
def _create_model() -> LLMModel:
""" Create a model"""
api_key, url, model_name = get_model_config()
model = OpenAIModel(model_name=model_name, api_key=api_key, base_url=url)
return model
def create_agent(
add_name: bool = True,
transfer_message: str | None = None,
) -> LlmAgent:
"""Create a coordinator agent with configurable prompt injection.
Args:
add_name: Whether the framework auto-injects agent name into instruction.
True (default): framework adds "You are an agent who's name is [name]." to the instruction.
False: no auto-injection, the instruction is used as-is.
transfer_message: Controls transfer instruction injection for sub_agents.
None (default): framework auto-injects transfer instructions.
"": disables auto-injection entirely.
Custom string: replaces framework default with this string.
"""
model = _create_model()
weather_assistant = LlmAgent(
name="WeatherAssistant",
model=model,
description="Weather assistant who answers weather questions",
instruction=WEATHER_INSTRUCTION,
tools=[FunctionTool(get_weather_report)],
add_name_to_instruction=add_name,
before_model_callback=_print_system_instruction,
disallow_transfer_to_parent=True,
disallow_transfer_to_peers=True,
)
translation_assistant = LlmAgent(
name="TranslationAssistant",
model=model,
description="Translation assistant who translates text",
instruction=TRANSLATION_INSTRUCTION,
tools=[FunctionTool(translate_text)],
add_name_to_instruction=add_name,
before_model_callback=_print_system_instruction,
disallow_transfer_to_parent=True,
disallow_transfer_to_peers=True,
)
coordinator = LlmAgent(
name="Coordinator",
model=model,
description="Customer service coordinator",
instruction=COORDINATOR_INSTRUCTION,
sub_agents=[weather_assistant, translation_assistant],
add_name_to_instruction=add_name,
default_transfer_message=transfer_message,
before_model_callback=_print_system_instruction,
)
return coordinator
root_agent = create_agent()