How to Migrate Your Custom Code for Chat Completions to a New Version of the OpenAI Python API library on Azure OpenAI.
OpenAI released a new version of the OpenAI Python API library.
Starting on November 6, 2023 pip install openai
and pip install openai --upgrade
will install version 1.x
of the OpenAI Python library.
You can now instantiate a client, instead of using a global default.
Switch to explicit client instantiation.
Upgrade OpenAi, or install it anew if you don’t already have it installed.
pip install openai --upgrade
Import openai libraries:
import os
from openai import AzureOpenAI
Create a client:
client = AzureOpenAI(
azure_endpoint=openai.api_base,
api_key=openai.api_key,
api_version="2023-05-15"
)
Create a query:
query = "What is the median value for acidity in this file?"
Create a response. Use exact name of the deployment you created to set the model
variable:
response = client.chat.completions.create(
model="gpt-4-32k-custom", # model = "deployment_name".
messages=[
{"role": "user", "content": query},
]
)
Display query and response:
response = response.choices[0].message.content
print('\n\n-------Your question to LLM -----\n\n', query)
print("\n\n -------OpenAI response: ------\n\n", response)
You will need to use a different date for Completions. Update syntax for response.
client = AzureOpenAI(
azure_endpoint=openai.api_base,
api_key=openai.api_key,
api_version="2023-10-01-preview",
)
deployment_name = 'REPLACE_WITH_YOUR_DEPLOYMENT_NAME' # This will correspond to the custom name you chose for your deployment when you deployed a model.
# Send a completion call to generate an answer
print('Sending a test completion job')
start_phrase = 'Write a tagline for an ice cream shop. '
response = client.completions.create(model=deployment_name, prompt=start_phrase, max_tokens=10)
print(response.choices[0].text)
As of June 2024, GPT-4 Omni is available in many regions. Although Azure documentation provides the release date: “2024–05–13”, this is not the date you should use for the api_version. Using it will result in a ‘Resource not found’ error.
Instead, use api_version = “2024–02–15-preview”
To use embeddings, make sure you have a deployment for embeddings:
import os
from openai import AzureOpenAI
client = AzureOpenAI(
azure_endpoint=openai.api_base,
api_key=openai.api_key,
api_version="2023-05-15"
)
response = client.embeddings.create(
input = "Your text string goes here",
model= "text-embedding-ada-002"
)
print(response.model_dump_json(indent=2))
References: https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/migration?tabs=python-new%2Cdalle-fix