openai / openai-python

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

NotFoundError when specifying azure_deployment in AzureOpenAI client #1163

Open IANTHEREAL opened 8 months ago

IANTHEREAL commented 8 months ago

Confirm this is an issue with the Python library and not an underlying OpenAI API

Describe the bug

When initializing the AzureOpenAI client with the azure_deployment parameter specified, a NotFoundError with error code 404 is raised upon calling client.beta.assistants.list(). However, omitting the azure_deployment parameter results in the expected behavior with no errors.

(autogen) ➜  autogen git:(main) ✗ python test.py
Traceback (most recent call last):
  File "/Users/ianz/Work/autogen/test.py", line 11, in <module>
    print(client.beta.assistants.list())
  File "/Users/ianz/Work/miniconda3/envs/autogen/lib/python3.10/site-packages/openai/resources/beta/assistants/assistants.py", line 270, in list
    return self._get_api_list(
  File "/Users/ianz/Work/miniconda3/envs/autogen/lib/python3.10/site-packages/openai/_base_client.py", line 1145, in get_api_list
    return self._request_api_list(model, page, opts)
  File "/Users/ianz/Work/miniconda3/envs/autogen/lib/python3.10/site-packages/openai/_base_client.py", line 990, in _request_api_list
    return self.request(page, options, stream=False)
  File "/Users/ianz/Work/miniconda3/envs/autogen/lib/python3.10/site-packages/openai/_base_client.py", line 856, in request
    return self._request(
  File "/Users/ianz/Work/miniconda3/envs/autogen/lib/python3.10/site-packages/openai/_base_client.py", line 908, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

To Reproduce

import os
from openai import AzureOpenAI

client = AzureOpenAI(
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),  
    api_version="2024-02-15-preview",
    azure_endpoint = os.getenv("AZURE_OPENAI_API_BASE"),
    azure_deployment = "gpt-4-turbo"
)

print(client.beta.assistants.list())

Code snippets

No response

OS

macOS

Python version

Python 3.10.13

Library version

1.3.7

kristapratico commented 8 months ago

@IANTHEREAL This is a good call out. For Azure assistants APIs, azure_deployment is not supported. To explain, Assistants is a bit different than other deployment-based features, in that it does not require the deployment name in the URL like: {endpoint}/openai/deployments/{azure_deployment}/chat/completions. While the client-level azure_deployment keyword argument is meant as a convenience to add the deployment name to the URL for you, we see in this case that it's not needed and results in 404. For Assistants APIs where you do need to pass the name of your deployment, i.e. assistants.create, you should pass it at the method-level:

my_assistant = client.beta.assistants.create(
    instructions="You are a personal math tutor. When asked a question, write and run Python code to answer the question.",
    name="Math Tutor",
    tools=[{"type": "code_interpreter"}],
    model="deployment-name",
)

@rattrayalex - I think the AzureOpenAI docstring should be updated to mention that azure_deployment is not supported for the assistants APIs. I can open a PR.

rattrayalex commented 8 months ago

Please do, thank you Krista!

cc @RobertCraigie