openai / openai-python

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

When changing the version specification, the Azure deployment name cannot be retrieved. #460

Closed uchimanajet7 closed 4 months ago

uchimanajet7 commented 1 year ago

Describe the bug

Until now, I had been referring to the following post to retrieve the Azure deployment name.

import openai

openai.api_type = "azure"
openai.api_base = "https://<your-resource>.openai.azure.com/"
openai.api_key = "<your-key>"
openai.api_version = "2023-03-15-preview"

deployments = openai.Deployment.list()
print(deployments)

When I set the openai.api_version to the latest 2023-05-15, it stopped functioning properly. Since there are no other changes, I believe it's due to the version. Is there any way to address this issue?

To Reproduce

Here is the code that works without any issues:

import openai

openai.api_type = "azure"
openai.api_base = "https://<your-resource>.openai.azure.com/"
openai.api_key = "<your-key>"
openai.api_version = "2023-03-15-preview"

deployments = openai.Deployment.list()
print(deployments)

This returns a response where id is your deployment name and model is the model name.

When executing the code by simply switching to openai.api_version = "2023-05-15", the following stack trace is returned:

---------------------------------------------------------------------------
InvalidRequestError                       Traceback (most recent call last)
[<ipython-input-20-6ce049a89318>](https://localhost:8080/#) in <cell line: 8>()
      6 openai.api_version = "2023-05-15"
      7 
----> 8 deployments = openai.Deployment.list()
      9 print(deployments)

4 frames
[/usr/local/lib/python3.10/dist-packages/openai/api_requestor.py](https://localhost:8080/#) in _interpret_response_line(self, rbody, rcode, rheaders, stream)
    685         stream_error = stream and "error" in resp.data
    686         if stream_error or not 200 <= rcode < 300:
--> 687             raise self.handle_error_response(
    688                 rbody, rcode, resp.data, rheaders, stream_error=stream_error
    689             )

InvalidRequestError: Resource not found

Code snippets

No response

OS

Google Colab

Python version

3.10.11

Library version

openai-python v0.27.7

bdleavitt commented 1 year ago

Also seeing this behavior.

Confirming that it appears to be tied to the API version: This works:

import openai
import os
from dotenv import load_dotenv

load_dotenv()

openai.api_version = '2023-03-15-preview'
openai.api_base = os.environ.get('AZURE_OPENAI_ENDPOINT') # Please add your endpoint here
openai.api_key = os.environ.get('AZURE_OPENAI_API_KEY') # Please add your api key here
openai.api_type = 'azure'

engine = "gpt-35-turbo"

openai.Deployment.list()

But this does not

import os
from dotenv import load_dotenv

load_dotenv()

openai.api_version = '2023-05-15'
openai.api_base = os.environ.get('AZURE_OPENAI_ENDPOINT') # Please add your endpoint here
openai.api_key = os.environ.get('AZURE_OPENAI_API_KEY') # Please add your api key here
openai.api_type = 'azure'

engine = "gpt-35-turbo"

openai.Deployment.list()
kristapratico commented 1 year ago

Moving forward (Azure API versions 2023-05-15 and newer), the deployments APIs are deprecated in the openai library. They will remain accessible in earlier versions (like 2023-03-15-preview). For all code managing deployments, it is recommended to use the azure-mgmt-cognitiveservices client library. This library has additional options for working with deployments, like setting a deployment name or specific model version, and is the place where new features for deployments will be added.

Here are some samples to assist in migrating the code:

Create a deployment Retrieve a deployment List deployments Delete a deployment