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

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
}
相关推荐
阿杰学AI2 小时前
AI核心知识91——大语言模型之 Transformer 架构(简洁且通俗易懂版)
人工智能·深度学习·ai·语言模型·自然语言处理·aigc·transformer
SmartBrain3 小时前
战略洞察:以AI为代表的第四次工业革命
人工智能·语言模型·aigc
Yeats_Liao5 小时前
评估体系构建:基于自动化指标与人工打分的双重验证
运维·人工智能·深度学习·算法·机器学习·自动化
好好学习天天向上~~6 小时前
6_Linux学习总结_自动化构建
linux·学习·自动化
陈天伟教授6 小时前
人工智能应用- 语言处理:02.机器翻译:规则方法
人工智能·深度学习·神经网络·语言模型·自然语言处理·机器翻译
量子-Alex8 小时前
【大模型RLHF】Training language models to follow instructions with human feedback
人工智能·语言模型·自然语言处理
阿杰学AI8 小时前
AI核心知识92——大语言模型之 Self-Attention Mechanism(简洁且通俗易懂版)
人工智能·ai·语言模型·自然语言处理·aigc·transformer·自注意力机制
骆驼爱记录8 小时前
Word样式检查器使用指南
自动化·word·excel·wps·新人首发
XiaoMu_0019 小时前
自动化漏洞扫描与预警平台
运维·网络·自动化
AI资源库9 小时前
microsoftVibeVoice-ASR模型深入解析
人工智能·语言模型