openai / openai-python

The official Python library for the OpenAI API
https://pypi.org/project/openai/
Apache License 2.0
23.3k stars 3.28k forks source link

Azure API does not support functions in chat? #517

Closed Billijk closed 1 year ago

Billijk commented 1 year ago

Describe the bug

New functions parameter in ChatCompletion is not recognized when calling MS Azure endpoints. I got this error when I pass in my functions:

  File "call_chatgpt.py", line 52, in chatgpt
    chat_completion = openai.ChatCompletion.create(
  File "/root/anaconda3/lib/python3.8/site-packages/openai/api_resources/chat_completion.py", line 25, in create
    return super().create(*args, **kwargs)
  File "/root/anaconda3/lib/python3.8/site-packages/openai/api_resources/abstract/engine_api_resource.py", line 153, in create
    response, _, api_key = requestor.request(
  File "/root/anaconda3/lib/python3.8/site-packages/openai/api_requestor.py", line 298, in request
    resp, got_stream = self._interpret_response(result, stream)
  File "/root/anaconda3/lib/python3.8/site-packages/openai/api_requestor.py", line 700, in _interpret_response
    self._interpret_response_line(
  File "/root/anaconda3/lib/python3.8/site-packages/openai/api_requestor.py", line 763, in _interpret_response_line
    raise self.handle_error_response(
openai.error.InvalidRequestError: Unrecognized request argument supplied: functions

To Reproduce

  1. run the code snippet
  2. get the error

Code snippets

import openai

AZURE_KEY = "<key here>"
AZURE_ENDPOINT = "<endpoint here>"

messages = [{"role": "user", "content": "What's the weather like in Boston?"}]
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"],
        },
    }
]
openai.api_type = "azure"
openai.api_key = AZURE_KEY
openai.api_base = AZURE_ENDPOINT
openai.api_version = "2023-05-15"

chat_completion = openai.ChatCompletion.create(
    engine="gpt-35-turbo",
    messages=messages,
    functions=functions
)

OS

linux

Python version

python 3.8

Library version

v0.27.8

Billijk commented 1 year ago

Found same issue here: https://stackoverflow.com/questions/76543136/how-to-do-function-calling-using-azure-openai

luc-vocab commented 1 year ago

is there a known workaround ? or we need to wait for azure to support functions ?

yippp commented 1 year ago

https://learn.microsoft.com/en-us/answers/questions/1323694/error-calling-function-when-using-model-gpt-35-tur Seems Azure has known this problem and is tring to fix it.

kristapratico commented 1 year ago

Function support with Azure API will be supported soon, but there is not an ETA quite yet.

See this comment for more details: https://github.com/Azure/azure-rest-api-specs/pull/24534#issuecomment-1632852649

kristapratico commented 1 year ago

Functions are now supported with the Azure API. You can see more details here: https://learn.microsoft.com/azure/ai-services/openai/how-to/function-calling

amanoooo commented 1 year ago

remember update this config openai.api_version = "2023-07-01-preview"

aiakubovich commented 1 year ago

here what is working for me:

openai.api_version = "2023-07-01-preview"
response = openai.ChatCompletion.create(
    engine="gpt-35-turbo-0613",
    messages=[{"role": "user", "content": "Efficient operations on the ground and friendly, attentive employees everywhere."}],
    functions=functions,
    function_call="auto",
)
qkxie commented 1 year ago

remember update this config openai.api_version = "2023-07-01-preview"

You save me.

Billijk commented 1 year ago

Thanks a lot for the efforts to make this work!

linchpinlin commented 1 year ago

The question came up again.

kristapratico commented 1 year ago

The question came up again.

@linchpinlin can you share the error and an example of the code that reproduces it?

linchpinlin commented 1 year ago

The question came up again.

@linchpinlin can you share the error and an example of the code that reproduces it? https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/function-calling Running the official demo also gives this error

File "text2sql.py", line 36, in gpt3_main response = openai.ChatCompletion.create( File "/home/lixaoyun/.local/lib/python3.8/site-packages/openai/api_resources/chat_completion.py", line 25, in create return super().create(*args, **kwargs) File "/home/lixaoyun/.local/lib/python3.8/site-packages/openai/api_resources/abstract/engine_apiresource.py", line 153, in create response, , api_key = requestor.request( File "/home/lixaoyun/.local/lib/python3.8/site-packages/openai/api_requestor.py", line 298, in request resp, got_stream = self._interpret_response(result, stream) File "/home/lixaoyun/.local/lib/python3.8/site-packages/openai/api_requestor.py", line 700, in _interpret_response self._interpret_response_line( File "/home/lixaoyun/.local/lib/python3.8/site-packages/openai/api_requestor.py", line 765, in _interpret_response_line raise self.handle_error_response( openai.error.InvalidRequestError: Unrecognized request argument supplied: functions

kristapratico commented 1 year ago

@linchpinlin I copy/pasted the example from the demo code and wasn't able to reproduce the error from it. Can you confirm that you've set the API version and model to those supported by functions?

Function calling is available in the 2023-07-01-preview API version and works with version 0613 of gpt-35-turbo, gpt-35-turbo-16k, gpt-4, and gpt-4-32k.

linchpinlin commented 1 year ago

Thanks for your help, I redeployed this model and the problem was solved!

Sky640q commented 1 year ago

Issue exists with azureopenai endpoint with client config: model="gpt-35-turbo-0613"

client = AzureOpenAI( azure_endpoint=endpoint, api_key=api_key, api_version="2023-07-01-preview" )

NotFoundError: Error code: 404 - {'error': {'message': 'Unrecognized request argument supplied: functions', 'type': 'invalid_request_error', 'param': None, 'code': None}}