GPT-4-0613调用函数时编造参数问题及解决咨询
解决GPT-4-0613对话后期编造函数参数的问题
问题说明
使用gpt-4-0613模型,配置了单个fill_form函数及自定义系统提示词。对话前两次触发函数时,API能正常向用户索要缺失的必填参数;但对话后期(如第5次请求)触发函数时,会编造对话上下文中不存在的虚构参数并返回函数调用结果。添加或移除function_call='auto'参数均无法解决该问题,需要找到阻止AI编造参数、始终向用户索要缺失参数的方法。
代码示例
completion = openai.ChatCompletion.create( model='gpt-4-0613', messages=prompts, functions=[ { "name": "fill_form", "description": "Helps the user create an XYZ Report", "parameters": { "type": "object", "properties": { "name": { "type": "string", "description": "the full name of the person issuing this report" }, "zip": { "type": "string", "description": "the 5 digit zip code of the address" }, "address": { "type": "string", "description": "the street address, only the street and not the city, state or zip" }, "year_end": { "type": "string", "description": "the full four digit year of the fiscal year" }, }, "required": ["name", "address", "year_end", "zip"] } }], )
已尝试参数:
function_call='auto'
解决方案
1. 强化系统提示词约束
在系统提示词中明确强硬地要求AI必须校验参数完整性,禁止编造参数:
你需要协助用户填写XYZ报告,仅能调用`fill_form`函数完成。调用前必须严格检查对话历史,只有当所有必填参数(name、address、year_end、zip)都已从用户处获取时,才能触发函数调用;若有任何一个参数缺失,必须向用户询问对应的信息,绝对不能编造参数值。
2. 手动校验参数后再触发函数调用
在调用OpenAI API之前,自行编写逻辑检查对话历史中是否已收集到所有必填参数:
- 如果存在缺失参数,直接生成询问用户的消息,添加到对话列表后调用API并设置
function_call='none',让AI返回询问内容; - 如果所有参数齐全,再调用API并指定
function_call={"name": "fill_form"}强制触发函数调用。
示例代码逻辑:
# 定义必填参数列表 required_params = ["name", "address", "year_end", "zip"] params_collected = {} # 遍历对话历史提取用户提供的参数(需根据实际对话内容做针对性解析) for msg in prompts: if msg["role"] == "user": # 示例:从用户输入中提取参数,实际需根据对话内容调整解析逻辑 # 比如用户说"我叫李四,住在Oak Avenue 456",则提取name="李四",address="Oak Avenue 456" # 这里仅为逻辑示例,实际需实现具体的参数提取逻辑 pass # 找出缺失的参数 missing_params = [param for param in required_params if param not in params_collected] if missing_params: # 生成询问用户的消息 prompt_msg = f"请补充以下必填信息:{', '.join(missing_params)}" prompts.append({"role": "assistant", "content": prompt_msg}) # 调用API返回询问内容,不触发函数调用 completion = openai.ChatCompletion.create( model='gpt-4-0613', messages=prompts, functions=[ # 原函数定义 { "name": "fill_form", "description": "Helps the user create an XYZ Report", "parameters": { "type": "object", "properties": { "name": {"type": "string", "description": "the full name of the person issuing this report"}, "zip": {"type": "string", "description": "the 5 digit zip code of the address"}, "address": {"type": "string", "description": "the street address, only the street and not the city, state or zip"}, "year_end": {"type": "string", "description": "the full four digit year of the fiscal year"}, }, "required": ["name", "address", "year_end", "zip"] } } ], function_call='none' ) else: # 参数齐全,强制调用函数 completion = openai.ChatCompletion.create( model='gpt-4-0613', messages=prompts, functions=[ # 原函数定义 { "name": "fill_form", "description": "Helps the user create an XYZ Report", "parameters": { "type": "object", "properties": { "name": {"type": "string", "description": "the full name of the person issuing this report"}, "zip": {"type": "string", "description": "the 5 digit zip code of the address"}, "address": {"type": "string", "description": "the street address, only the street and not the city, state or zip"}, "year_end": {"type": "string", "description": "the full four digit year of the fiscal year"}, }, "required": ["name", "address", "year_end", "zip"] } } ], function_call={"name": "fill_form"} )
3. 启用函数调用的强制校验模式
通过设置function_call为指定函数,并在系统提示词中反复强调参数必须来自用户输入,结合API的函数调用校验机制,减少AI编造参数的概率。不过该方法效果可能有限,建议配合前两种方法使用。
内容的提问来源于stack exchange,提问作者LJF 2525
相关产品推荐
相关产品推荐

