-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathagent.py
More file actions
70 lines (60 loc) · 3.12 KB
/
Copy pathagent.py
File metadata and controls
70 lines (60 loc) · 3.12 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
# 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.
from trpc_agent_sdk.agents import LlmAgent
from trpc_agent_sdk.models import LLMModel
from trpc_agent_sdk.models import OpenAIModel
from trpc_agent_sdk.planners import BuiltInPlanner
from trpc_agent_sdk.tools import FunctionTool
from trpc_agent_sdk.types import GenerateContentConfig
from trpc_agent_sdk.types import ThinkingConfig
from trpc_agent_sdk.types import HttpOptions
from .config import get_model_config
from .prompts import INSTRUCTION
from .tools import get_weather_forecast
from .tools import get_weather_report
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,
# There are two scenarios where enabling add_tools_to_prompt can improve the generation performance of the Agent:
# 1. When the thinking model does not support tool calling,
# you can enable the ToolPrompt framework to parse the tool calling capability from the LLM-generated text.
# 2. When the thinking model calls tools during the reasoning process,
# if the LLM model service fails to return the JSON format of tool calls, you can also enable ToolPrompt.
# This will prompt the LLM model to output the special text for tool calling in the main content,
# thereby increasing the probability of successful tool invocation.
# Thinking models may emit tool calls as text. ToolPrompt lets the
# framework parse those text calls back into executable FunctionCalls.
add_tools_to_prompt=model_name.lower() == "hy3-preview", # Enable ToolPrompt for Hy3-preview model
)
return model
def create_agent():
"""Create a weather query agent to demonstrate the various capabilities of an LLM agent."""
# Create tools
weather_tool = FunctionTool(get_weather_report)
forecast_tool = FunctionTool(get_weather_forecast)
# Set reasoning effort to high for Hy3-preview model
http_options=HttpOptions(extra_body={"chat_template_kwargs": {"reasoning_effort": "high"}})
return LlmAgent(
name="weather_agent",
description=
"A professional weather query assistant that can provide real-time weather and forecast information.",
model=_create_model(),
# Use state variables for template replacement - Demonstration of the {var} syntax
instruction=INSTRUCTION,
tools=[weather_tool, forecast_tool],
# Note: thinking_budget must be less than max_output_tokens
generate_content_config=GenerateContentConfig(max_output_tokens=10240, http_options=http_options),
# The model must be a thinking model to use this Planner; this configuration will not take effect for non-thinking models.
planner=BuiltInPlanner(thinking_config=ThinkingConfig(
include_thoughts=True,
thinking_budget=2048,
), ),
)
root_agent = create_agent()