工具与自动调用
Agently 内置工具系统用于“让模型在需要时调用工具,再把结果回填到回答中”。核心能力包括:
- 工具注册:用函数签名与类型注解描述参数
- 自动判断:模型先判断是否需要工具,再生成调用指令
- 结果回填:工具结果自动写入
action_results供回复使用
工具注册
使用 @agent.tool_func 让函数变成工具,参数说明会被自动提取:
python
from typing import Annotated
from agently import Agently
agent = Agently.create_agent()
@agent.tool_func
async def add(
a: Annotated[int, "first number"],
b: Annotated[int, "second number"],
) -> int:
"""Return a + b"""
return a + b也可以用 register_tool 手动注册:
python
agent.register_tool(
name="add",
desc="Return a + b",
kwargs={"a": (int, "first number"), "b": (int, "second number")},
func=add,
)自动调用(use_tools / use_tool)
use_tools() 会把工具列表挂到当前 Agent 上,模型会先判断是否需要工具,再决定是否调用。
python
response = (
agent
.input("34643523+52131231=? Use tool to calculate!")
.use_tool(add)
.get_response()
)
result = response.get_data()
print(result)工具结果回填与日志
当工具被调用后,Agently 会把结果写入 action_results,并在响应 extra.tool_logs 中记录调用信息。
python
full = response.get_data(type="all")
print(full["extra"]["tool_logs"])如果想在控制台看到工具调用日志,可开启:
python
from agently import Agently
Agently.set_settings("runtime.show_tool_logs", True)