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

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
}
相关推荐
天空属于哈夫克32 小时前
非官方接口下,基于RPA实现企业微信外部群主动调用的技术实践
microsoft·企业微信·rpa
m0_603888712 小时前
Over-Searching in Search-Augmented Large Language Models
人工智能·ai·语言模型·自然语言处理·论文速览
梦想的旅途22 小时前
利用 RPA 自动化技术实现企业微信外部群的功能扩展与链路闭环
自动化·企业微信·rpa
狮子座明仔2 小时前
GDPO:英伟达提出多奖励强化学习的“解耦归一化“策略,解决GRPO的优势崩溃问题
人工智能·gpt·语言模型·自然语言处理
rgb2gray2 小时前
论文深度解析:基于大语言模型的城市公园多维度感知解码与公平性提升
大数据·人工智能·机器学习·语言模型·自然语言处理·数据分析·可解释
天空属于哈夫克32 小时前
非官方接口实现企业微信外部群主动调用:基于RPA的自动化实践
自动化·企业微信·rpa
huazi-J4 小时前
Datawhale 大模型基础与量化微调 task0:Tokenizer
语言模型·大模型·tokenizer·datawhale
实战项目14 小时前
大语言模型幻觉抑制方法的研究与实现
人工智能·语言模型·自然语言处理
汽车仪器仪表相关领域15 小时前
全自动化精准检测,赋能高效年检——NHD-6108全自动远、近光检测仪项目实战分享
大数据·人工智能·功能测试·算法·安全·自动化·压力测试