Skip to main content
This guide will help you get started with AzureOpenAI chat models.
AzureChatOpenAI shares the same underlying base implementation as ChatOpenAI, which interfaces with OpenAI services directly.This page serves as a quickstart for authenticating and connecting your Azure OpenAI service to a LangChain chat model.Visit the ChatOpenAI docs for details on available features, or head to the API reference.
Azure OpenAI has several chat models. You can find information about their latest models and their costs, context windows, and supported input types in the Azure docs.
Azure OpenAI vs OpenAIAzure OpenAI refers to OpenAI models hosted on the Microsoft Azure platform. OpenAI also provides its own model APIs. To access OpenAI services directly, use the ChatOpenAI integration.

Overview

Integration details

Model features

Setup

To access AzureOpenAI models you’ll need to create an Azure account, create a deployment of an Azure OpenAI model, get the name and endpoint for your deployment, get an Azure OpenAI API key, and install the langchain-openai integration package.

Credentials

Head to the Azure docs to create your deployment and generate an API key. Once you’ve done this set the AZURE_OPENAI_API_KEY and AZURE_OPENAI_ENDPOINT environment variables:
To enable automated tracing of your model calls, set your LangSmith API key:

Installation

The LangChain AzureOpenAI integration lives in the langchain-openai package:

Instantiation

Now we can instantiate our model object and generate chat completions.

Invocation

Chaining

We can chain our model with a prompt template like so:

Specifying model version

Azure OpenAI responses contain model_name response metadata property, which is name of the model used to generate the response. However unlike native OpenAI responses, it does not contain the specific version of the model, which is set on the deployment in Azure. E.g. it does not distinguish between gpt-35-turbo-0125 and gpt-35-turbo-0301. This makes it tricky to know which version of the model was used to generate the response, which as result can lead to e.g. wrong total cost calculation with OpenAICallbackHandler. To solve this problem, you can pass model_version parameter to AzureChatOpenAI class, which will be added to the model name in the llm output. This way you can easily distinguish between different versions of the model.

API reference

For detailed documentation of all AzureChatOpenAI features and configurations head to the API reference: python.langchain.com/api_reference/openai/chat_models/langchain_openai.chat_models.azure.AzureChatOpenAI.html