How to utilize Azure OpenAI with version 0.28.1

I’m looking to integrate Azure OpenAI but need to stick with an older version of the OpenAI library (0.28.1), rather than any version above 1.0.

Currently, I have the following setup using the newer OpenAI library:

import os

os.environ["OPENAI_API_TYPE"] = "azure"
os.environ["OPENAI_API_VERSION"] = "2023-07-01-preview"
os.environ["OPENAI_API_BASE"] = "your-endpoint-here"
os.environ["OPENAI_API_KEY"] = "your-key-here"

from langchain_openai import AzureOpenAI

ai_model = AzureOpenAI(
    deployment_name="my-deployment",
    model_name="gpt-3.5-turbo-instruct",
)

response = ai_model("Give me a funny story")

Unfortunately, this setup doesn’t function with openai==0.28.1. I’m restricted to this older version due to my project’s specifications. How can I adjust my code to work with the legacy OpenAI library while maintaining the same straightforward usage, like using ai_model("my prompt here") to receive a response?

I hit this exact issue a few months ago on a legacy project. Version 0.28.1 uses a completely different API structure. Drop the langchain_openai import and use the older openai module directly.

Here’s what worked for me:

import openai

openai.api_type = "azure"
openai.api_base = "your-endpoint-here"
openai.api_version = "2023-07-01-preview"
openai.api_key = "your-key-here"

response = openai.Completion.create(
    engine="my-deployment",
    prompt="Give me a funny story",
    max_tokens=150
)

print(response.choices[0].text)

Use engine instead of deployment_name and the response structure is different. The older version returns a response object where you access .choices[0].text to get the actual text. Also, set max_tokens explicitly since it’s required in the legacy version.