Skip to content

Instantly share code, notes, and snippets.

@muhark
Last active April 10, 2024 14:44
Show Gist options
  • Save muhark/509cd32db86de219f1d4208ea3532b1a to your computer and use it in GitHub Desktop.
Save muhark/509cd32db86de219f1d4208ea3532b1a to your computer and use it in GitHub Desktop.
Migrating to Azure Openai

This gist assumes that:

  • you are already using OpenAI services via the OpenAI API
  • you want to migrate to an Azure-based workflow
  • somebody else has already set up the Azure endpoint for you

Old Workflow (no Azure)

from openai import OpenAI
client = OpenAI(
    # Defaults to os.environ.get("OPENAI_API_KEY")
)

chat_completion = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "Hello world"}]
)

New Workflow (Azure Endpoint)

You now just need to change the instantiation of the client to the following:

from openai import AzureOpenAI

client = AzureOpenAI(
    azure_endpoint=<AZURE_ENDPOINT>,
    api_key=<AZURE_KEY>,
    api_version=<API_VERSION>,
)

The AzureOpenAI client class can then be used in the same way as the regular OpenAI client class.

Managing Credentials

Tip

It's a good idea to still use environmental variables and config files when dealing with these keys.

I recommend using config files and environmental variables as to avoid accidentally leaking these credentials (which would allow others to abuse the resource).

My recommended workflow:

  1. Create a folder .cfg in your home directory. (mkdir ~/.cfg)
  2. Create a file openai.cfg in ~/.cfg/openai.cfg (touch ~/.cfg/openai.cfg)
  3. Copy the credentials I send to you into openai.cfg in the following format:
[AZURE]
key=...
endpoint=...

Where key will be a string of letters and numbers endpoint will be a URL.

You can now use the configparser module in Python to read these values into your script (see attached script below).

from __future__ import annotations
import os
import openai
from configparser import ConfigParser
from pathlib import Path
def seed_azure_key(cfg: str = "~/.cfg/openai.cfg") -> None:
config = ConfigParser()
try:
config.read(Path(cfg).expanduser())
except:
raise ValueError(f"Could not using read file at: {cfg}.")
os.environ["AZURE_ENDPOINT"] = config["AZURE"]["endpoint"]
os.environ["AZURE_SECRET"] = config["AZURE"]["key"]
def initialize_client() -> openai.AsyncClient:
client = openai.AzureOpenAI(
azure_endpoint=os.environ["AZURE_ENDPOINT"],
api_key=os.environ["AZURE_SECRET"],
api_version="2023-05-15",
)
return client
@xehu
Copy link

xehu commented Apr 4, 2024

Note: the Azure endpoints each serve a limited set of OpenAI's services. Depending on which service you want, you may want to choose an endpoint in a different region. For example, gpt-4-vision-preview is not available on the endpoint eastus.

Please refer to the table here: https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/models

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment