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

Azure OpenAI GPT-4未调用指定函数,改用文本提示的问题咨询

问题分析与解决方案

背景信息

我正在使用Azure OpenAI的GPT-4实现对话式AI助手,用于处理用户查询,助手需遵循预设指令并通过指定函数完成特定任务。

原精简提示词

You are an AI assistant for xyz company. Follow the instructions to resolve user queries.

1. Ask for name and address.
2. Ask for date of birth.
3. Ask the user if they want OptionA or OptionB by calling the provided function prompt_user_to_choose_an_option.
4. Do a search with the chosen option.

定义的函数

{
    "name": "prompt_user_to_choose_an_option",
    "description": "Display the options for the user to choose from (OptionA or OptionB)",
    "parameters": {
        "properties": {
            "options": {
                "type": "array",
                "items": {
                    "type": "string"
                },
                "description": "The list of available options for the user to choose from."
            }
        },
        "required": [
            "options"
        ],
        "type": "object"
    }
}

问题现象

在执行步骤3时,GPT-4并未返回函数调用,而是直接用纯文本提示Do you want OptionA or OptionB?询问用户;当用户回复如OptionB后,它才返回本该在步骤3触发的函数调用。我需要通过函数调用来触发特定UI元素展示,但添加禁止文本提示、强制使用函数的指令后效果不稳定。

原因分析

  • 模型默认交互倾向:GPT-4的训练数据以自然语言对话为主,默认优先选择纯文本交互方式,当任务看起来可通过自然语言直接完成时,会忽略“调用函数”的指令。
  • 提示词约束不足:原提示词仅提及“通过调用函数询问”,但未明确禁止纯文本替代,也未强调函数调用是完成该步骤的唯一合法方式,模型会将其视为可选方案。
  • 函数描述歧义:函数描述中的“Display the options”表述模糊,模型会认为纯文本展示选项也符合要求,未意识到该函数是触发UI组件的唯一途径。

稳定实现函数调用的方案

1. 强化提示词的刚性约束

修改提示词,明确禁止纯文本询问,强调函数调用是唯一合规方式:

You are an AI assistant for xyz company. Follow the instructions STRICTLY to resolve user queries:

1. Ask for name and address using natural language.
2. Ask for date of birth using natural language.
3. **MUST call the `prompt_user_to_choose_an_option` function to present OptionA and OptionB to the user. UNDER NO CIRCUMSTANCES use plain text to ask this question.**
4. Do a search with the chosen option only after receiving the user's selection.

2. 优化函数定义的精准性

调整函数描述,明确其唯一用途是触发UI组件,消除歧义:

{
    "name": "prompt_user_to_choose_an_option",
    "description": "ONLY use this function to trigger a UI selection component for the user to choose between OptionA and OptionB. Do NOT use plain text to ask the user this question.",
    "parameters": {
        "properties": {
            "options": {
                "type": "array",
                "items": {
                    "type": "string"
                },
                "description": "Fixed list of options to show in the UI component: ['OptionA', 'OptionB']"
            }
        },
        "required": ["options"],
        "type": "object"
    }
}

3. 启用Azure OpenAI的强制工具调用模式

在API请求中,当进入步骤3的触发条件时,设置tool_choice参数强制模型调用指定函数:

# 示例Python代码中的参数设置
response = client.chat.completions.create(
    model="gpt-4",
    messages=messages,
    tools=[your_function_definition],
    tool_choice={"type": "function", "function": {"name": "prompt_user_to_choose_an_option"}}
)

该配置会强制模型返回函数调用结果,而非自然语言响应。

4. 添加Few-Shot示例明确预期行为

在提示词中加入正确的对话示例,让模型直观理解预期流程:

### Correct Workflow Example:
User: I need help with my request
Assistant: Could you please provide your name and address?
User: John Doe, 123 Main Street
Assistant: Could you share your date of birth?
User: January 1, 1990
Assistant: <|FunctionCallBegin|>{"name":"prompt_user_to_choose_an_option","parameters":{"options":["OptionA","OptionB"]}}<|FunctionCallEnd|>

内容的提问来源于stack exchange,提问作者Nimish David Mathew

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 23:47:45