OpenAI Chat Completions API请求报错:functions参数格式问题求助
解决OpenAI Chat Completions API的functions参数报错问题
问题根源
OpenAI Chat Completions API对functions和function_call参数有明确规则:
- 不需要使用函数调用功能时,直接省略这两个参数,而非设置为
null或空数组[]。 - 若传入
functions参数,必须是包含至少一个有效函数定义的数组;搭配function_call时,需与合法的functions配置匹配。
正确请求示例
无需函数调用时,移除functions和function_call字段即可:
{ "model": "gpt-3.5-turbo", "messages": [ { "role": "system", "content": "etc" }, { "role": "user", "content": "etc" } ], "temperature": null, "top_p": null, "n": null, "stream": null, "stop": null, "max_tokens": null, "user": null }
过往请求报错原因
- 第一个请求
functions: null:API要求functions必须是数组类型,不接受null值。 - 第二个请求
functions: []且function_call: "none":传入空数组会被判定为启用函数功能但未提供有效定义,因此触发“数组过短”报错。
函数调用场景的正确示例
若确实需要使用函数调用,functions数组需包含至少一个符合格式的函数定义:
{ "model": "gpt-3.5-turbo", "messages": [ { "role": "system", "content": "etc" }, { "role": "user", "content": "etc" } ], "functions": [ { "name": "get_current_weather", "description": "Get the current weather in a given location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] } }, "required": ["location"] } } ], "function_call": "auto" }
内容的提问来源于stack exchange,提问作者Luke Puplett
相关产品推荐
相关产品推荐

