Agent学习记录三:完成 Agent Loop

一、最基础的 Agent Loop。

修改代码

python 复制代码
from openai import OpenAI
import json

client = OpenAI()
def calculator(a, b):
    return a * b

tools = [
    {
        "type": "function",
        "name": "calculator",
        "description": "计算两个数字的乘积",
        "parameters": {
            "type": "object",
            "properties": {
                "a": {
                    "type": "number",
                    "description": "第一个数字"
                },
                "b": {
                    "type": "number",
                    "description": "第二个数字"
                }
            },
            "required": ["a", "b"],
            "additionalProperties": False
        }
    }
]

response = client.responses.create(
    model="gpt-5.6-sol",
    input="请调用 calculator 工具,计算 123 × 456",
    tools=tools,
    tool_choice="required"
)

# 找到 LLM 发出的工具调用
for item in response.output:

    if item.type == "function_call":

        print("========== Tool Call ==========")
        print("工具名称:", item.name)
        print("参数:", item.arguments)
        print("Call ID:", item.call_id)

        # JSON字符串 → Python字典
        arguments = json.loads(item.arguments)

        # 真正执行Python函数
        result = calculator(
            arguments["a"],
            arguments["b"]
        )

        print("========== Tool Result ==========")
        print(result)

         # 第二次请求:把工具结果交给 LLM
        response2 = client.responses.create(
            model="gpt-5.6-sol",
            input=[
                *response.output,
                {
                    "type": "function_call_output",
                    "call_id": item.call_id,
                    "output": str(result)
                }
            ],
            tools=tools
        )

        print("========== Final Answer ==========")
        print(response2.output_text)

告诉 LLM 这是工具执行完之后返回给你的结果。:

"type": "function_call_output"

这个结果对应你刚才发出的那一次 calculator 调用

"call_id": item.call_id

【*response.output】把第一次 LLM 的输出上下文带过去。让LLM知道刚才我让你调用了 calculator,现在这是那个调用的结果。

运行输出:

已经亲手完成了一个最小 Agent Loop!!

二、完整过程

┌──────────────┐

│ LLM │

└──────┬───────┘

│

Function Call

│

↓

┌──────────────┐

│ Python │

└──────┬───────┘

│

calculator(123,456)

│

↓

56088

│

Tool Output

│

↓

┌──────────────┐

│ LLM │

└──────┬───────┘

│

↓

"123 × 456 = 56,088"

【注意】把这个过程彻底搞懂,而不是继续加代码。尤其是 tools、function_call、function_call_output、call_id、两次 responses.create() 这几个东西弄明白!

相关推荐
外收内放4 小时前
面向对象高级(实战案例)
开发语言·python·学习
FITA阿泽要努力4 小时前
用 Hooks 给 Claude Code 加规矩
人工智能·agent
专业程序开发源4 小时前
springboot欧缇蔓月子中心管理系统82697-计算机课程设计、毕业设计
java·spring boot·后端·python·spring·django·课程设计
FPGA小迷弟4 小时前
用 Claude Code 重构遗留系统:从评估到落地的完整实践指南
c++·python·ai·plotly·django·ai编程·claude
卷无止境5 小时前
面向arm64桌面客户端开发全景:语言、框架与生态
后端·python
薛定e的猫咪5 小时前
(ICLR 2020)MAML 靠的不是快速学习,而是特征复用
人工智能·深度学习·学习·算法·机器学习
fanruitian5 小时前
python else用法
开发语言·python
薛定e的猫咪5 小时前
(ICLR 2026)DF-META:面向 FJSP 的无导数元学习实例自适应调度
人工智能·深度学习·学习·算法·机器学习
Thinker QAQ5 小时前
并发编程(十一):Shared Memory 与 Message Passing——关系与组合使用
java·python·go·并发编程·shared memory·message passing
用户8356290780515 小时前
如何使用 Python 对 Excel 数据进行分组和分级显示
后端·python