使用ZipAgent创建计算型AI助手只需三个标准步骤:
第一步:定义计算工具
使用@function_tool装饰器将Python函数转化为Agent可调用的工具,其中文档字符串(docstring)至关重要,它决定了Agent对工具功能的理解:
@function_tool def calculate(expression: str) -> str: """数学表达式计算工具""" return str(eval(expression))
第二步:初始化Agent实例
配置Agent的基础属性,包括名称、指令说明和可用工具集:
agent = Agent( name="MathExpert", instructions="你是一个数学计算专家,请使用工具解决用户问题", tools=[calculate] )
第三步:运行测试对话
通过Runner执行具体的用户查询:
result = Runner.run(agent, "计算(25+37)*2的值") print(result.content) # 输出: (25+37)*2的计算结果是124
整个过程无需处理复杂的对话状态维护,框架会自动管理完整的交互流程。
This answer comes from the articleZipAgent: a lightweight Python framework for building exclusive AI assistants in 5 minutesThe