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 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
Class | Package | Local | Serializable | JS support | Downloads | Version |
---|---|---|---|---|---|---|
AzureChatOpenAI | langchain-openai | ❌ | beta | ✅ |
Model features
Tool calling | Structured output | JSON mode | Image input | Audio input | Video input | Token-level streaming | Native async | Token usage | Logprobs |
---|---|---|---|---|---|---|---|---|---|
✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ |
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 thelangchain-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:Installation
The LangChain AzureOpenAI integration lives in thelangchain-openai
package:
Instantiation
Now we can instantiate our model object and generate chat completions.- Replace
azure_deployment
with the name of your deployment, - You can find the latest supported
api_version
here: learn.microsoft.com/en-us/azure/ai-services/openai/reference.
Invocation
Chaining
We can chain our model with a prompt template like so:Specifying model version
Azure OpenAI responses containmodel_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.