How to use the Python openai client with both Azure and OpenAI at the same time? – Python

by
Ali Hasan
azure-active-directory llama-cpp-python openai-api openai-whisper python-module

Quick Fix: To use the Python OpenAI client with both the OpenAI and Microsoft API services simultaneously, you can use per-method overrides in each method to specify API-specific configuration options. To use the Microsoft API for chat completions, pass in your Microsoft-specific API base and key, while for the OpenAI API you can specify the OpenAI API key.

The Problem:

I want to use the Python OpenAI client to call both the ChatCompletion endpoint from Azure and the Transcribe endpoint from OpenAI, but I’m not sure how to do so since the client only supports using one provider at a time.

The Solutions:

Solution 1: Use the library’s per-method configuration overrides

You can pass in specific configurations for each method call, overriding the global settings. This allows you to use Azure’s API for chat completions and OpenAI’s API for transcription in the same script.

Solution 2: Use per-method overrides

The official OpenAI Python client supports both Azure and OpenAI, and you can use them simultaneously by using per-method overrides. Here’s how:

  1. For Azure: When calling methods that are supported by Azure (e.g., `ChatCompletion`), set the following per-method overrides:
    • `api_type = “azure”`
    • `api_base = (Azure OpenAI endpoint URL)`
    • `api_key = (Azure OpenAI API key)`
    • `api_version = (Azure OpenAI API version)`
  2. For OpenAI: When calling methods that are only available on OpenAI (e.g., `Audio.transcribe`), set the following per-method overrides:
    • `api_type = “openai”`
    • `api_key = (OpenAI API key)`

This way, you can use both Azure and OpenAI services seamlessly within the same codebase.

Note: Per-method overrides are supported from OpenAI Python client version 0.27.0 onwards.

Q&A

Can i use Python openai client with Azure and OpenAI at same time?

Yes, you can use the Python OpenAI client with both Azure and OpenAI at the same time. There are three ways to do this:

What are the 3 ways to use Python openai client?

  1. Change the globals before every call.

  2. Duplicate/Forking the library to have two versions run concurrently, one for each provider.

  3. Use an alternative client for OpenAI’s Whisper, if any.

Video Explanation:

The following video, titled "Learn Live - Azure OpenAI: Introduction to Language Models and ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Presenters will demonstrate how users can access the service through REST APIs, Python SDK, or our web-based interface in the Azure OpenAI ...