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() 这几个东西弄明白!

相关推荐
Gary移动存储固件7 分钟前
eMMC5.1协议中文学习讲解(8~11)
学习·ufs·emmc·nand·存储固件
benchmark_cc1 小时前
一次获取多只股票实时行情:QuantDash `quotes.get(symbols=[...])` 批量快照
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
geovindu1 小时前
python: PCA
开发语言·后端·python·算法
AI模型调用笔记1 小时前
流式 API 的 usage 为 null?用 cURL 看尾帧,别漏空 choices
python
具身AGI2 小时前
物理AI 人类学习路线:三大判断怎么兑现
大数据·人工智能·学习
天诰3 小时前
SA-T-06 轨迹数据工程 脏数据检测
人工智能·python·算法·语言模型·ai编程
忆想不到的晖3 小时前
删掉 1800 行编排代码后,我用 Agent-Native + 薄 AOP 重写了内容工作台
后端·node.js·agent
大模型真好玩4 小时前
DSH+LLM Wiki 个人知识库应用开发实战(二)——DeepSeek Harness 接入 LLM Wiki(实操篇)
人工智能·agent·deepseek
老周聊架构4 小时前
Agent 的成本账:算力经济学正在重新调整
agent
清风小道君4 小时前
DeepArche:把 AI Agent 做成一个能长期维护的运行时
agent