Why do I get the error "Unrecognized request argument supplied: functions" when using `functions` when calling Azure OpenAI GPT?
Asked Answered
H

2

9

I'm trying to use functions when calling Azure OpenAI GPT, as documented in https://platform.openai.com/docs/api-reference/chat/create#chat/create-functions

I use:

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",
        )

but I get the error:

openai.error.InvalidRequestError:
Unrecognized request argument supplied: functions

Why?


Data to run the example code above (messages and functions need to be defined):

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"]
        }
    }
]
Highhat answered 24/7, 2023 at 18:35 Comment(0)
F
15

Function support with the Azure API was added in 2023-07-01-preview. The API version needs to be updated in your example:

openai.api_version = "2023-07-01-preview"
Flaviaflavian answered 24/7, 2023 at 18:50 Comment(3)
Do you have a source for this information? I'm pretty sure I had functions working with 2023-03-15-preview. Just needed the 0613 model versions.Ronel
This doesn't solve the problem for me.Ropy
Update: 2023-12-01-previewsupports "tools" instead of deprecated "functions". Official docHedi
H
1

As a complement to Krista's answer, it seems that one must use latest 0613 versions of gpt-35-turbo and gpt-4.

Example code:

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)

Output:

{
  "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
  }
}
Highhat answered 24/7, 2023 at 20:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.