大语言模型提示词工程教程:实现自动化工具推理

Function/Tool Calling

@openai: 函数调用为 OpenAI 模型提供了一种强大而灵活的方式,可以与您的代码或外部服务进行交互。通过函数调用,您可以向 Assistant 描述函数,并让其智能地返回需要调用的函数及其参数。@openai

@langchain: 我们将"工具调用"与"函数调用"互换使用。虽然"函数调用"有时指的是单个函数的调用,但我们将所有模型视为可以在每条消息中返回多个工具或函数调用。

工具调用与 ReACT 的区别

项目 ReAct 提示词工程 Function Calling
核心思想 用提示词引导模型「推理 + 行动 + 观察反馈」循环 明确声明函数签名,模型调用结构化函数
技术机制 Prompt 模板控制流程,如:Think -> Act -> Obs 通过函数描述注册函数接口,模型自动生成调用参数
环境交互灵活性 高:适用于复杂多步任务、agent 类流程 中:适合明确定义的单步工具调用
开发复杂度 中:需要精心设计提示词模板与中间状态管理 低~中:注册函数较直接,但需要参数与返回值处理
典型应用场景 多步推理、多工具交替使用、信息抽取等复杂场景 明确 API 调用、数据库查询、插件系统等
与 Agent 框架集成度 高:常用于 LangChain、AutoGPT 中的推理行为生成 高:OpenAI、LangChain、Claude 支持直接注册使用
优缺点总结 ✅ 灵活可控 ✅ 自动结构化调用
❌ 难以规模化自动解析 ❌ 不擅长多步控制与反馈处理

function calling 核心概念

  • tools: 函数列表

  • function: 单个函数定义

  • function.name: 函数名字

  • function.description: 函数作用描述

  • parameters: 函数形参说明

    "tools": [
    {
    "type": "function",
    "function": {
    "name": "current_time",
    "description": "A tool for getting the current time.",
    "parameters": {
    "properties": {},
    "required": [],
    "type": "object"
    }
    }
    },
    {
    "type": "function",
    "function": {
    "name": "simple_code",
    "description": "A tool for running code and getting the result back. Only native packages are allowed, network/IO operations are disabled. and you must use print() or console.log() to output the result or result will be empty.",
    "parameters": {
    "properties": {
    "code": {
    "description": "code to be executed, only native packages are allowed, network/IO operations are disabled.",
    "type": "string"
    },
    "language": {
    "description": "language of the code, only "python3" and "javascript" are supported",
    "type": "string"
    }
    },
    "required": ["language", "code"],
    "type": "object"
    }
    }
    }
    ]

工具调用的返回格式

  • tool_calls: 工具调用序列,单个或者多个

  • function 函数调用

  • function.name 函数名字

  • function.arguments 函数实参

    "message": {
    "role": "assistant",
    "content": "",
    "tool_calls": [
    {
    "function": {
    "name": "simple_code",
    "arguments": {
    "code": "print(0.9111 ** 3)",
    "language": "python3"
    }
    }
    }
    ]
    }

常用工具

  • 代码解析器 python node

  • 命令解析器 bash

  • 文件管理 file directory

  • 浏览器控制 browser use

  • 外部接口 openapi swagger

  • 大模型上下文协议 MCP playwright-mcp

python 工具调用示例

没有工具调用的对话模型

使用工具可以获得更加准确的结果

带有 function call 的请求

复制代码
{
  "model": "qwen3",
"stream": false,
"options": {
    "temperature": 0
  },
"messages": [
    {
      "role": "system",
      "content": "/no_think\n"
    },
    {
      "role": "user",
      "content": "使用python计算 0.9111**3="
    }
  ],
"tools": [
    {
      "type": "function",
      "function": {
        "name": "current_time",
        "description": "A tool for getting the current time.",
        "parameters": {
          "properties": {},
          "required": [],
          "type": "object"
        }
      }
    },
    {
      "type": "function",
      "function": {
        "name": "simple_code",
        "description": "A tool for running code and getting the result back. Only native packages are allowed, network/IO operations are disabled. and you must use print() or console.log() to output the result or result will be empty.",
        "parameters": {
          "properties": {
            "code": {
              "description": "code to be executed, only native packages are allowed, network/IO operations are disabled.",
              "type": "string"
            },
            "language": {
              "description": "language of the code, only \"python3\" and \"javascript\" are supported",
              "type": "string"
            }
          },
          "required": ["language", "code"],
          "type": "object"
        }
      }
    }
  ]
}

大模型的响应

复制代码
{
  "model": "qwen3",
"created_at": "2025-05-05T16:57:36.016688Z",
"message": {
    "role": "assistant",
    "content": "",
    "tool_calls": [
      {
        "function": {
          "name": "simple_code",
          "arguments": {
            "code": "print(0.9111 ** 3)",
            "language": "python3"
          }
        }
      }
    ]
  },
"done_reason": "stop",
"done": true,
"total_duration": 2037533417,
"load_duration": 34440000,
"prompt_eval_count": 275,
"prompt_eval_duration": 776227166,
"eval_count": 42,
"eval_duration": 1225024292
}

最终请求

复制代码
{
  "model": "qwen3",
"stream": false,
"options": {
    "temperature": 0
  },
"messages": [
    {
      "role": "system",
      "content": "/no_think\n"
    },
    {
      "role": "user",
      "content": "使用python计算 0.9111**3="
    },
    {
      "role": "assistant",
      "content": ""
    },
    {
      "role": "tool",
      "content": "0.756307034631\n"
    }
  ],
"tools": [
    {
      "type": "function",
      "function": {
        "name": "current_time",
        "description": "A tool for getting the current time.",
        "parameters": {
          "properties": {},
          "required": [],
          "type": "object"
        }
      }
    },
    {
      "type": "function",
      "function": {
        "name": "simple_code",
        "description": "A tool for running code and getting the result back. Only native packages are allowed, network/IO operations are disabled. and you must use print() or console.log() to output the result or result will be empty.",
        "parameters": {
          "properties": {
            "code": {
              "description": "code to be executed, only native packages are allowed, network/IO operations are disabled.",
              "type": "string"
            },
            "language": {
              "description": "language of the code, only \"python3\" and \"javascript\" are supported",
              "type": "string"
            }
          },
          "required": ["language", "code"],
          "type": "object"
        }
      }
    }
  ]
}

最终响应

复制代码
{
  "model": "qwen3",
"created_at": "2025-05-05T16:57:37.121945Z",
"message": {
    "role": "assistant",
    "content": "<think>\n\n</think>\n\n0.9111 raised to the power of 3 is approximately **0.7563**."
  },
"done_reason": "stop",
"done": true,
"total_duration": 1030672292,
"load_duration": 11999667,
"prompt_eval_count": 303,
"prompt_eval_duration": 122764292,
"eval_count": 29,
"eval_duration": 892274041
}
相关推荐
AlanBruce33 分钟前
从串口仪表到西门子PLC:摩尔信使MThings的多协议设备接入能力
自动化·plc·modbus·mthings·摩尔信使·西门子s7
大模型任我行34 分钟前
阿里:端到端文档解析模型OvisOCR2
人工智能·语言模型·自然语言处理·论文笔记
微硬创新12 小时前
耐达讯自动化 CC-Link IE 转 PROFINET网关在污水厂智能化升级实战
人工智能·物联网·网络协议·自动化·信息与通信
KAU的云实验台15 小时前
【研究分享】大语言模型 × 进化计算新范式? —— 以ReEvo为例拆解
人工智能·语言模型·自然语言处理
壮哥_icon18 小时前
【Android 系统开发】使用 BAT 脚本高效自动化管理 /system/priv-app/ 系统应用(安装与卸载)
android·运维·自动化
xexpertS18 小时前
分布式系统过载治理:如何通过较小服务控制请求节奏
微服务·自动化
硅谷秋水19 小时前
PhyGround:生成式世界模型中的物理推理基准测试
人工智能·深度学习·机器学习·计算机视觉·语言模型
AOwhisky19 小时前
Python 学习笔记(第十三期)——运维自动化(下·前篇):远程命令执行——paramiko基础篇
运维·python·学习·云原生·自动化·运维开发·paramiko
AOwhisky19 小时前
Python 学习笔记(第十四期)——运维自动化(下·中篇):远程文件传输——paramiko进阶篇
运维·python·学习·云原生·自动化·文件传输·paramiko
Land032921 小时前
AI网页元素变化无法自动修复?自带元素自愈的自动化解决方案
运维·人工智能·ai·自动化·rpa