本示例演示使用 XML 风格 tool_prompt 时,模型如何输出 <function_calls> 并被框架解析为实际工具调用,完成天气查询与预报。
- 自定义工具调用文本格式(见模型输出中的
<invoke>片段) - 与标准
get_weather_report/get_weather_forecast工具配合 - 多会话轮次覆盖缺省城市澄清、单日与多日预报
root_agent (LlmAgent, tool_prompt=XML style)
└── tools: get_weather_report, get_weather_forecast
关键文件:
- examples/llmagent_with_tool_prompt/agent/agent.py
- examples/llmagent_with_tool_prompt/run_agent.py
- examples/llmagent_with_tool_prompt/.env
- Agent 配置中指定工具提示模板,引导模型用标签包裹工具名与参数
- Runner 将解析后的调用映射到已注册工具并回灌结果
- Python 3.12
git clone https://github.com/trpc-group/trpc-agent-python.git
cd trpc-agent-python
python3 -m venv .venv
source .venv/bin/activate
pip3 install -e .在 examples/llmagent_with_tool_prompt/.env 中配置(或通过 export 设置):
TRPC_AGENT_API_KEYTRPC_AGENT_BASE_URLTRPC_AGENT_MODEL_NAME
cd examples/llmagent_with_tool_prompt
python3 run_agent.py📝 User: What's the weather like today?
🤖 Assistant: ... which city ...
📝 User: What's the current weather in Guangzhou?
🤖 Assistant: <function_calls>...<tool_name>get_weather_report</tool_name>...
🔧 [Invoke Tool:: get_weather_report({'city': 'Guangzhou'})]
📊 [Tool Result: {'temperature': '32°C', 'condition': 'Thunderstorm', ...}]
...
结论:符合本示例测试要求。
- 首轮澄清城市;后续轮次出现 XML 风格工具块并成功执行工具
- 需与仅支持特定工具语法的模型或遗留提示词对齐的集成
- 对比 JSON tool calls 与 XML 风格可解析性的评测