大语言模型提示词工程教程 | 使用工具的自动推理
关注 霍格沃兹测试学院公众号,回复「资料」, 领取人工智能测试开发技术合集
Function/Tool Calling
@openai: 函数调用为 OpenAI 模型提供了一种强大而灵活的方式,可以与您的代码或外部服务进行交互。通过函数调用,您可以向 Assistant 描述函数,并让其智能地返回需要调用的函数及其参数。@openai
@langchain: 我们将“工具调用”与“函数调用”互换使用。虽然“函数调用”有时指的是单个函数的调用,但我们将所有模型视为可以在每条消息中返回多个工具或函数调用。

工具调用与 ReACT 的区别

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 工具调用示例
没有工具调用的对话模型

Playwright mcp技术学习交流群
伙伴们,对AI测试、大模型评测、质量保障感兴趣吗?我们建了一个 「Playwright mcp技术学习交流群」,专门用来探讨相关技术、分享资料、互通有无。无论你是正在实践还是好奇探索,都欢迎扫码加入,一起抱团成长!期待与你交流!👇

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



带有 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": "
},
"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
}
推荐学习
欢迎加入霍格沃兹测试开发学社 · 人工智能测试开发训练营(VIP)支持 线上线下
扫码进群了解课程详情
领取 VIP 专属优惠与学习资料


浙公网安备 33010602011771号