You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenAI Functions工具调用失效:为何未触发指定计算函数?

为什么Horn Bet派彩计算工具未被触发调用?

核心问题分析

  • 工具参数定义逻辑错误:你在工具的required数组里包含了payout和still_up_to_win,但这两个是函数的输出结果,并非需要传入的输入参数。模型无法获取这两个值,会判断无法满足工具调用的参数要求,转而自行回答。
  • 缺少必要输入条件:Horn Bet的派彩结果依赖于开出的数字(2、3、11、12),但用户提问未指定winning_number,而工具定义中将其设为必填项。模型无法凭空生成该参数,因此不会触发工具调用。
  • API方法使用错误:OpenAI Node.js SDK中不存在openai.responses.create方法,正确的工具调用应使用openai.chat.completions.create,错误的方法调用会导致工具逻辑完全不生效。
  • 模型名称不合法:gpt-4.1不是官方支持的模型ID,支持工具调用的模型为gpt-4o、gpt-4-turbo、gpt-3.5-turbo-1106等,错误的模型名称可能导致工具调用功能无法启用。

修正后的代码示例

require('dotenv').config({ path: __dirname + '/.env' });

const { OpenAI } = require("openai");
const { inspect } = require('util');

const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY,
});

// 修正工具定义:仅保留输入参数,移除输出参数
const tools = [
    {
        "type": "function",
        "name": "calculate_horn_bet_payout",
        "description": "Calculate the payout of a horn bet which is divided into 4 units for numbers 2, 3, 11, and 12. Requires the total bet amount and the winning number rolled.",
        "strict": true,
        "parameters": {
            "type": "object",
            "required": ["bet_amount", "winning_number"],
            "properties": {
                "bet_amount": {
                    "type": "number",
                    "description": "The total amount of the bet placed on the horn bet"
                },
                "winning_number": {
                    "type": "number",
                    "description": "The number that rolled, which can be either 2, 3, 11, or 12"
                }
            },
            "additionalProperties": false
        }
    }
];

async function calculateHornBetPayout(amount, roll) {
    console.log("CALLING calculateHornBetPayout");
    const perBetAmount = amount / 4;
    let multiple = 16;

    if (roll === 2 || roll === 12) {
        multiple = 31;
    }

    const totalPayout = perBetAmount * multiple;
    const stillUpToWin = totalPayout - amount;

    return { totalPayout, stillUpToWin };
}

(async () => {
    // 使用正确的API方法与官方模型ID
    const response = await openai.chat.completions.create({
        model: "gpt-4o",
        messages: [
            {
                role: "user",
                content: "What does a $4 Horn Bet pay if the winning number is 3?" // 补充必要的winning_number参数
            }
        ],
        tools: tools,
        tool_choice: "auto" // 让模型自动判断是否调用工具
    });

    console.log(inspect(response, false, null));

    // 处理工具调用结果
    if (response.choices[0].message.tool_calls) {
        const toolCall = response.choices[0].message.tool_calls[0];
        if (toolCall.function.name === "calculate_horn_bet_payout") {
            const args = JSON.parse(toolCall.function.arguments);
            const result = await calculateHornBetPayout(args.bet_amount, args.winning_number);
            console.log("工具执行结果:", result);
        }
    }
})();

额外说明

  • 若用户提问未指定winning_number,可以在prompt中引导用户补充信息,或让模型在调用工具前主动追问用户。
  • 必须使用官方文档列出的合法模型ID,避免因模型名称错误导致功能失效。

内容的提问来源于stack exchange,提问作者lando2319

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 04:13:16