使用Azure OpenAI进行函数调用报错:Unrecognized request argument supplied: functions
解决Azure OpenAI函数调用的InvalidRequestError错误
出现InvalidRequestError: Unrecognized request argument supplied: functions错误的核心原因是:Azure OpenAI API的函数调用参数命名与原生OpenAI不同,原生OpenAI用functions参数,但Azure OpenAI需要使用tools和tool_choice(可选)来实现相同功能。
修改后的代码示例
response = openai.ChatCompletion.create( engine="XXX", # 部署ChatGPT或GPT-4时选择的部署名称 messages=[ {"role": "system", "content": "Assistant is a large language model trained by OpenAI."}, {"role": "user", "content": "Calculate the circumference of circle with radius 5?"} ], # 将原functions替换为tools,并增加type字段 tools=[ { "type": "function", "function": { "name": "circumference_calc", "description": "Calculate the circumference of circle given the radius", "parameters": { "type": "object", "properties": { "radius": { "description": "The radius of the circle", "type": "number" } }, "required": ["radius"] } } } ], # 可选:强制模型调用指定函数,若无需强制可省略 tool_choice={"type": "function", "function": {"name": "circumference_calc"}} )
关键修改点
- 把原
functions参数替换为tools,每个工具对象必须包含type: "function"字段,函数定义放在function子对象中 - 如果需要强制模型调用函数而非直接回答,添加
tool_choice参数指定目标函数;若让模型自行判断是否调用函数,可省略该参数或设置为"auto"
内容的提问来源于stack exchange,提问作者Deejay217
相关产品推荐
相关产品推荐

