调用Azure OpenAI GPT时遇'Unrecognized request argument supplied: functions'错误原因
Azure OpenAI调用工具功能时提示"Unrecognized request argument supplied: functions"的原因
我尝试在调用Azure OpenAI GPT时使用工具调用功能,参考了OpenAI官方文档的写法,代码如下:
import openai import os openai.api_type = "azure" openai.api_base = "https://XXXXXXXX.openai.azure.com/" openai.api_version = "2023-06-01-preview" openai.api_key = os.getenv("OPENAI_API_KEY") response = openai.ChatCompletion.create( engine="gpt-35-turbo-XXX", model="gpt-35-turbo-0613-XXXX" messages=messages, functions=functions, function_call="auto", )
运行后出现如下错误:
openai.error.InvalidRequestError: Unrecognized request argument supplied: functions
对应的messages和functions定义如下:
messages = [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello!"}] functions = [ { "name": "fetch_pages", "description": "Fetch the content of specified pages from the document.", "parameters": { "type": "object", "properties": { "pages": { "type": "array", "items": { "type": "number" }, "description": "The list of pages to fetch." } }, "required": ["pages"] } }, { "name": "fetch_section", "description": "Fetch the content of a specified section.", "parameters": { "type": "object", "properties": { "section_title": { "type": "string", "description": "The title of the section to fetch." } }, "required": ["section_title"] } }, { "name": "search", "description": "Search the document for a string query.", "parameters": { "type": "object", "properties": { "query": { "type": "string", "description": "The search term." } }, "required": ["query"] } } ]
错误原因及修正方案
这个错误的核心是Azure OpenAI与原生OpenAI的API参数规范不一致,具体问题和解决方法如下:
参数名称不匹配
Azure OpenAI不使用functions和function_call参数,对应的正确参数名为tools和tool_choice。同时每个工具的结构需要调整——原functions数组中的每个对象必须包裹在{"type": "function", "function": {...}}格式中。冗余参数导致解析错误
代码中同时传入了engine和model参数,在Azure OpenAI的调用逻辑里,只需要engine参数(对应你在Azure平台部署的模型名称),model参数属于原生OpenAI的用法,会干扰请求解析。模型版本与功能兼容性
需确认你部署的模型是gpt-35-turbo-0613或gpt-4-0613及以上版本,且Azure资源已启用工具调用功能,旧版本模型不支持该参数。
修正后的代码示例
import openai import os openai.api_type = "azure" openai.api_base = "https://XXXXXXXX.openai.azure.com/" openai.api_version = "2023-06-01-preview" openai.api_key = os.getenv("OPENAI_API_KEY") # 调整为Azure支持的tools格式 tools = [ { "type": "function", "function": { "name": "fetch_pages", "description": "Fetch the content of specified pages from the document.", "parameters": { "type": "object", "properties": { "pages": { "type": "array", "items": { "type": "number" }, "description": "The list of pages to fetch." } }, "required": ["pages"] } } }, { "type": "function", "function": { "name": "fetch_section", "description": "Fetch the content of a specified section.", "parameters": { "type": "object", "properties": { "section_title": { "type": "string", "description": "The title of the section to fetch." } }, "required": ["section_title"] } } }, { "type": "function", "function": { "name": "search", "description": "Search the document for a string query.", "parameters": { "type": "object", "properties": { "query": { "type": "string", "description": "The search term." } }, "required": ["query"] } } } ] response = openai.ChatCompletion.create( engine="gpt-35-turbo-XXX", messages=messages, tools=tools, tool_choice="auto" )
内容的提问来源于stack exchange,提问作者Franck Dernoncourt
相关产品推荐
相关产品推荐

