为什么会出现错误“Unrecognized request argument supplied:在调用Azure OpenAI GPT时使用`functions`时使用“functions”?

mspsb9vt  于 2023-08-07  发布在  其他
关注(0)|答案(2)|浏览(381)

我尝试在调用Azure OpenAI GPT时使用functions,如https://platform.openai.com/docs/api-reference/chat/create#chat/create-functions中所述
我用途:

import openai
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


为什么?为什么?
运行上面示例代码的数据(需要定义messagesfunctions):

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"]
        }
    }
]

dl5txlt9

dl5txlt91#

Azure API的函数支持已在2023-07-01-preview中添加。示例中的API版本需要更新:

openai.api_version = "2023-07-01-preview"

字符串

lokaqttq

lokaqttq2#

作为对Krista答案的补充,它seems,一个人必须使用最新的0613版本的gpt-35-turbogpt-4
示例代码:

import openai
openai.api_type = "azure"
openai.api_base = "https://XXXX.openai.azure.com/"
openai.api_version = "2023-07-01-preview"
openai.api_key = "XXXX" 
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"]
        }
    }
]

response = openai.ChatCompletion.create(
            engine="gpt-35-turbo-XXXX",
            model="gpt-35-turbo-0613-XXXX"
            messages=messages,
            functions=functions,
            function_call="auto",
        )

print(response)

字符串
输出量:

{
  "choices": [
    {
      "content_filter_results": {
        "hate": {
          "filtered": false,
          "severity": "safe"
        },
        "self_harm": {
          "filtered": false,
          "severity": "safe"
        },
        "sexual": {
          "filtered": false,
          "severity": "safe"
        },
        "violence": {
          "filtered": false,
          "severity": "safe"
        }
      },
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "Hi there! How can I assist you today?",
        "role": "assistant"
      }
    }
  ],
  "created": 1690229943,
  "id": "chatcmpl-sdkopsdvomsdvpomi156sdv",
  "model": "gpt-35-turbo",
  "object": "chat.completion",
  "prompt_annotations": [
    {
      "content_filter_results": {
        "hate": {
          "filtered": false,
          "severity": "safe"
        },
        "self_harm": {
          "filtered": false,
          "severity": "safe"
        },
        "sexual": {
          "filtered": false,
          "severity": "safe"
        },
        "violence": {
          "filtered": false,
          "severity": "safe"
        }
      },
      "prompt_index": 0
    }
  ],
  "usage": {
    "completion_tokens": 11,
    "prompt_tokens": 127,
    "total_tokens": 138
  }
}

相关问题