Skip to main content

Azure OpenAI

Azure OpenAI is a cloud service to help you quickly develop generative AI experiences with a diverse set of prebuilt and curated models from OpenAI, Meta and beyond.

LangChain.js supports integration with Azure OpenAI using either the dedicated Azure OpenAI SDK or the OpenAI SDK.

You can learn more about Azure OpenAI and its difference with the OpenAI API on this page. If you don't have an Azure account, you can create a free account to get started.

Using Azure OpenAI SDK

You'll first need to install the @langchain/azure-openai package:

npm install -S @langchain/azure-openai

We're unifying model params across all packages. We now suggest using model instead of modelName, and apiKey for API keys.

You'll also need to have an Azure OpenAI instance deployed. You can deploy a version on Azure Portal following this guide.

Once you have your instance running, make sure you have the endpoint and key. You can find them in the Azure Portal, under the "Keys and Endpoint" section of your instance.

You can then define the following environment variables to use the service:


Alternatively, you can pass the values directly to the AzureOpenAI constructor:

import { AzureOpenAI } from "@langchain/azure-openai";

const model = new AzureOpenAI({
azureOpenAIEndpoint: "<your_endpoint>",
apiKey: "<your_key>",
azureOpenAIApiDeploymentName: "<your_embedding_deployment_name",

If you're using Azure Managed Identity, you can also pass the credentials directly to the constructor:

import { DefaultAzureCredential } from "@azure/identity";
import { AzureOpenAI } from "@langchain/azure-openai";

const credentials = new DefaultAzureCredential();

const model = new AzureOpenAI({
azureOpenAIEndpoint: "<your_endpoint>",
azureOpenAIApiDeploymentName: "<your_embedding_deployment_name",

Usage example

import { AzureOpenAIEmbeddings } from "@langchain/azure-openai";

const model = new AzureOpenAIEmbeddings();
const res = await model.embedQuery(
"What would be a good company name for a company that makes colorful socks?"
console.log({ res });

API Reference:

Using OpenAI SDK

The OpenAIEmbeddings class can also use the OpenAI API on Azure to generate embeddings for a given text. By default it strips new line characters from the text, as recommended by OpenAI, but you can disable this by passing stripNewLines: false to the constructor.

For example, if your Azure instance is hosted under https://{MY_INSTANCE_NAME}{DEPLOYMENT_NAME}, you could initialize your instance like this:

npm install @langchain/openai
import { OpenAIEmbeddings } from "@langchain/openai";

const embeddings = new OpenAIEmbeddings({
azureOpenAIApiKey: "YOUR-API-KEY", // In Node.js defaults to process.env.AZURE_OPENAI_API_KEY
azureOpenAIApiVersion: "YOUR-API-VERSION", // In Node.js defaults to process.env.AZURE_OPENAI_API_VERSION
azureOpenAIApiInstanceName: "{MY_INSTANCE_NAME}", // In Node.js defaults to process.env.AZURE_OPENAI_API_INSTANCE_NAME
azureOpenAIApiDeploymentName: "{DEPLOYMENT_NAME}", // In Node.js defaults to process.env.AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME

If you'd like to initialize using environment variable defaults, the process.env.AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME will be used first, then process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME. This can be useful if you're using these embeddings with another Azure OpenAI model.

If your instance is hosted under a domain other than the default, you'll need to use the alternate AZURE_OPENAI_BASE_PATH environment variable. For example, here's how you would connect to the domain{DEPLOYMENT_NAME}:

import { OpenAIEmbeddings } from "@langchain/openai";

const embeddings = new OpenAIEmbeddings({
azureOpenAIApiKey: "YOUR-API-KEY",
azureOpenAIApiVersion: "YOUR-API-VERSION",
azureOpenAIApiDeploymentName: "{DEPLOYMENT_NAME}",
"", // In Node.js defaults to process.env.AZURE_OPENAI_BASE_PATH

Help us out by providing feedback on this documentation page: