【AutoGen: 工具调用完整底层流程图】

流程图升级为更完整、更详细的版本,涵盖:

  • 每一方角色(用户 / Agent / Model / AutoGen)
  • 每一步执行(函数调用 / JSON处理 / 工具执行)
  • 数据结构流转(messages / schema / JSON / dict)

🔁 AutoGen 工具调用完整底层流程图(Function Calling)

[用户]
   |
   | 输入自然语言:
   | “请查询一下北京的天气”
   ▼
[Agent]
   |
   | 封装成 message:
   | {"role": "user", "content": "请查询一下北京的天气"}
   ▼
[ModelClient.create()]
   |
   | 将注册的工具(FunctionTool)转成 JSON Schema,例如:
   |
   | {
   |   "name": "search_weather",
   |   "parameters": {
   |     "type": "object",
   |     "properties": {
   |       "city": { "type": "string" }
   |     },
   |     "required": ["city"]
   |   }
   | }
   |
   | + 加上用户 message 一起发给 GPT-4
   ▼
[模型(如 GPT-4)]
   |
   | 接收 schema + 用户意图 → 推理决定:
   | “我想调用 search_weather,参数是 city = '北京'”
   |
   | 生成工具调用响应:
   | {
   |   "tool_calls": [
   |     {
   |       "name": "search_weather",
   |       "arguments": "{\"city\": \"北京\"}"
   |     }
   |   ]
   | }
   ▼
[AutoGen 的 model_client]
   |
   | 解析 JSON 字符串:
   | tool_call.arguments → 用 json.loads → 得到 Python dict
   |     '{"city": "北京"}' → {"city": "北京"}
   |
   | 生成 FunctionCall 对象
   ▼
[FunctionTool 实例]
   |
   | 执行 run_json(dict)
   |    → 调用你原来定义的函数:
   |    → search_weather(city="北京")
   ▼
[函数执行结果]
   |
   | 返回值如:
   | {"result": "今天北京多云,气温18度"}
   ▼
[AutoGen 系统]
   |
   | 把函数结果包装成模型输入 message:
   | {"role": "tool", "content": "..."}
   |
   | 再次发送给模型,用于生成最终回复
   ▼
[模型 GPT-4 再次生成]
   |
   | “好的,北京今天多云,气温18度。”
   ▼
[Agent 把最终回复返回给用户]

✅ 每个阶段的关键对象:

阶段 数据类型 举例
用户输入 UserMessage "北京天气?"
工具 schema JSON {"type": "object", properties: ...}
模型响应 JSON 字符串 {"tool_calls": [...]}
函数参数 Python dict {"city": "北京"}
执行器调用 run_json(dict) search_weather(city=...)
工具输出 字典/文本 {"result": ...}
最终 reply AssistantMessage “今天北京...”

可运行 demo(含工具注册、模型调用、结果流转),

posted @ 2025-05-26 00:06  十三山入秋  阅读(131)  评论(0)    收藏  举报