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

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
}
相关推荐
biuyyyxxx8 小时前
Python自动化办公学习笔记(一) 工具安装&教程
笔记·python·学习·自动化
chatexcel11 小时前
元空AI+Clawdbot:7×24 AI办公智能体新形态详解(长期上下文/自动化任务/工具粘合)
运维·人工智能·自动化
阿杰学AI13 小时前
AI核心知识78——大语言模型之CLM(简洁且通俗易懂版)
人工智能·算法·ai·语言模型·rag·clm·语境化语言模型
玄同76515 小时前
Llama.cpp 全实战指南:跨平台部署本地大模型的零门槛方案
人工智能·语言模型·自然语言处理·langchain·交互·llama·ollama
玄同76515 小时前
LangChain v1.0+ Prompt 模板完全指南:构建精准可控的大模型交互
人工智能·语言模型·自然语言处理·langchain·nlp·交互·知识图谱
我送炭你添花17 小时前
工业触摸屏:PCAP(投影电容式)触摸屏控制器选型推荐(工业级,2025-2026主流)
嵌入式硬件·自动化
菜青虫嘟嘟17 小时前
Expert Iteration:一种无需人工标注即可显著提升大语言模型推理能力的简单方法核心
人工智能·语言模型·自然语言处理
MaoziShan18 小时前
[ICLR 2026] 一文读懂 AutoGEO:生成式搜索引擎优化(GEO)的自动化解决方案
人工智能·python·搜索引擎·语言模型·自然语言处理·内容运营·生成式搜索引擎
整列机厂家-唯思特18 小时前
细小晶片精密自动化排列的技术路径与工程实践
科技·自动化·制造
软件资深者18 小时前
USB存储设备管理工具 启用或者禁用+usb修复
windows·microsoft·计算机外设·系统修复