Azure / azure-rest-api-specs

The source for REST API specifications for Microsoft Azure.
MIT License
2.54k stars 4.99k forks source link

[BUG] `tool_choice="required"` is still not supported in the latest OpenAI API version #29844

Open minhduc0711 opened 1 month ago

minhduc0711 commented 1 month ago

API Spec link

https://github.com/Azure/azure-rest-api-specs/blob/main/specification/cognitiveservices/data-plane/AzureOpenAI/inference/stable/2024-06-01/inference.yaml#L1678-L1686

API Spec version

2024-06-01

Describe the bug

In the specification file above, I see that the chat completion's parameter tool_choice should support required as a valid value, but I got an error when setting it as such.

Expected behavior

I should be able to set tool_choice="required", and the endpoint will always return a tool call.

Actual behavior

I got the following error:

Traceback (most recent call last):
  File "/data/mdm/iva_webapi/test_openai.py", line 16, in <module>
    chat_completion = client.chat.completions.create(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/mdm/miniforge3/envs/iva/lib/python3.11/site-packages/openai/_utils/_utils.py", line 277, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/data/mdm/miniforge3/envs/iva/lib/python3.11/site-packages/openai/resources/chat/completions.py", line 643, in create
    return self._post(
           ^^^^^^^^^^^
  File "/data/mdm/miniforge3/envs/iva/lib/python3.11/site-packages/openai/_base_client.py", line 1250, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/mdm/miniforge3/envs/iva/lib/python3.11/site-packages/openai/_base_client.py", line 931, in request
    return self._request(
           ^^^^^^^^^^^^^^
  File "/data/mdm/miniforge3/envs/iva/lib/python3.11/site-packages/openai/_base_client.py", line 1030, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - {'error': {'message': "'$.tool_choice' is invalid. Please check the API reference: https://platform.openai.com/docs/api-reference.", 'type': 'invalid_request_error', 'param': None, 'code': None}}

Reproduction Steps

import os

from openai import AzureOpenAI

gpt_35_16k_endpoint = os.getenv("OPENAI_GPT_35_16K_ENDPOINT")
gpt_35_16k_key = os.getenv("OPENAI_GPT_35_16K_KEY")
gpt_35_16k_deployment = os.getenv("OPENAI_GPT_35_16K_DEPLOYMENT")

if gpt_35_16k_deployment and gpt_35_16k_key and gpt_35_16k_endpoint:
    client = AzureOpenAI(
        azure_endpoint=gpt_35_16k_endpoint,
        api_key=gpt_35_16k_key,
        api_version="2024-06-01",
    )

    chat_completion = client.chat.completions.create(
        model=gpt_35_16k_deployment,
        messages=[
            {
                "role": "user",
                "content": "Weather in hanoi today",
            }
        ],
        tools=[
            {
                "type": "function",
                "function": {
                    "name": "get_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"}
                        },
                    },
                },
            }
        ],
        tool_choice="required",
    )
    print(chat_completion.to_json(indent=2))

Environment

openai python package version: 1.35.14

hobofan commented 1 month ago

And here I thought I was just holding it wrong...

I've been encountering the exact same issue. Would be very nice to see this fixed, as for our current use-case this is quite crucial, and with tool_choice=auto, I'm getting a high rate of text responses that identify the tool to call correctly but don't actually trigger a tool call response.

janaka commented 1 month ago

I think the issue still is that the latest Azure OpenAI API with these options has not been released yet. It's confusing because the Azure client has the enum to set the latest version so we expect the service exist. Even some places of the docs and specs have the new version but the following page is still showing 2024-05-01-preview. I think 2024-06-01 is what has this feature.

https://learn.microsoft.com/en-us/azure/ai-services/openai/api-version-deprecation

hobofan commented 1 month ago

@janaka From what is shown on other pages, the 2024-06-01 version of the inference API has been released as GA. This page shows it as the current GA release: https://learn.microsoft.com/en-us/azure/ai-services/openai/reference . It was also mentioned on the "What's new page": https://learn.microsoft.com/en-us/azure/ai-services/openai/whats-new#new-ga-api-release. If it wasn't released yet, I would assume that you would get the same 404 error that you get if you submit a non-existent API version with a request.

But yeah, for some reason the current preview release is older than the current GA release.

janaka commented 1 month ago

@hobofan oh interesting. I'd not seen those pages before. If the GA bit was update when that page says it was last updated it's after I last tried. iirc I was getting a 404 or similar. Thanks for sharing those links.

Update: just tried it now. Still getting a 400 error. Invalid value: 'required'. Supported values are: 'none' and 'auto'.'

I wonder if there is a CLI command to check the API version for our deployment.

janaka commented 1 month ago

Looks like my assumption is invalid, that the Azure OpenAI API version numbers tracked the OpenAI OpenAI API version. They seem completely independent.

So Azure just have not released these features on the inference API.

hobofan commented 1 month ago

So Azure just have not released these features on the inference API.

See the link to the specification in top-level comment. According the the spec, the required option should now be supported (it didn't exist in the enum in previous versions). So either the spec is wrong, or this is a bug in the implementation.

janaka commented 1 month ago

ah I see, on main. Totally not clear if main is release or what is the commit that's released as version X

janaka commented 1 month ago

@jpalvarezl @YunsongB @trrwilson any of you able to shed some light? Please help us understand what the actual released version of the inference API is? is the readme up to date? release_2023_05_15 is the released version? seems off to me.

https://github.com/Azure/azure-rest-api-specs/blob/main/specification/cognitiveservices/data-plane/AzureOpenAI/inference/readme.md

hobofan commented 1 month ago

Tried to do the same API calls, and looks like tool_choice="required" is working now!

janaka commented 1 month ago

just tried now, still getting Invalid value: 'required'. Supported values are: 'none' and 'auto'.' Maybe the update is still being rolled out.

PatrickLef commented 4 weeks ago

Also getting Invalid value: 'required'. Supported values are: 'none' and 'auto'.' with gpt-4o in east-us and sweden

vivia11 commented 4 weeks ago

Also getting Invalid value: 'required'. Supported values are: 'none' and 'auto'. with gpt-4o in Canada.

This worked with gpt-3.5 turbo though.

janaka commented 4 weeks ago

Our deployment is east-us

@hobofan which region is your deployment?

hobofan commented 3 weeks ago

From what I can tell in France Central (I only consume the deployment and don't have access to see the deployment details though).

minhduc0711 commented 2 weeks ago

Our deployments are also in France Central. Similar to what @vivia11 observed, gpt-3.5-turbo accepts required, but 4o does not.

janaka commented 2 weeks ago

there's a new preview spec with tool_choice=required that's something though prob means nothing given the stable spec has it but doesn't work in all regions.

https://github.com/Azure/azure-rest-api-specs/blob/4b847aabd57c3477f6018bcce7d5156006dd214d/specification/cognitiveservices/data-plane/AzureOpenAI/inference/preview/2024-07-01-preview/inference.json#L5580

minhduc0711 commented 2 weeks ago

gpt-3.5-turbo accepts required, but 4o does not.

Yeah, this still happens in 2024-07-01-preview for me.

janaka commented 2 weeks ago

gtk - how are you testing? curl or something? at least yesterday the dotnet lib didn't have that version in the enum.

minhduc0711 commented 2 weeks ago

I used the Python script in https://github.com/Azure/azure-rest-api-specs/issues/29844#issue-2410566860 and simply updated the api_version parameter.

janaka commented 2 weeks ago

I used the Python script in #29844 (comment) and simply updated the api_version parameter.

gotcha - with gtp-4o right?

minhduc0711 commented 2 weeks ago

Yes with gpt-4o

janaka commented 2 weeks ago

it will be interesting to see what happens when they release gpt-4o 2024-08-06 on the inferencing API as that's a bit useless without an updated REST API.

janaka commented 1 week ago

So 2024-07-01-preview announced on the what's new page but the tool_choice=required isn't deployed yet :( https://learn.microsoft.com/en-us/azure/ai-services/openai/whats-new