Skip to main content
Azure OpenAI 是一项云服务,可帮助您快速开发使用来自 OpenAI、Meta 以及其他机构的多样化预构建和精选模型的生成式 AI 体验。 LangChain.js 支持通过 Azure OpenAI 使用 OpenAI SDK 中的新 Azure 集成进行集成。 您可以在本页面上了解更多关于Azure OpenAI及其与OpenAI API的区别。如果您没有Azure账户,您可以创建一个免费账户开始使用。 这将帮助您开始使用LangChain和AzureOpenAIEmbeddings 嵌入模型。有关AzureOpenAIEmbeddings功能和配置选项的详细文档,请参阅API参考
之前,LangChain.js 支持使用专用 Azure OpenAI SDK 与 Azure OpenAI 集成。该 SDK 现已弃用,转而采用 OpenAI SDK 中的新 Azure 集成,允许在模型发布当天访问最新的 OpenAI 模型和功能,并允许在 OpenAI API 和 Azure OpenAI 之间实现无缝过渡。如果您正在使用已弃用的SDK版本的Azure OpenAI,请参阅迁移指南以更新到新API。

概述

集成详情

安装

要访问 Azure OpenAI 嵌入式模型,您需要创建一个 Azure 账户,获取一个 API 密钥,并安装 @langchain/openai 集成包。

凭证

您需要部署一个Azure OpenAI实例。您可以在Azure门户中按照此指南部署一个版本。 一旦您的实例运行起来,请确保您有实例名称和密钥。您可以在Azure门户中找到密钥,它位于您实例的“密钥和端点”部分。 如果您正在使用Node.js,可以定义以下环境变量以使用该服务:
AZURE_OPENAI_API_INSTANCE_NAME=<YOUR_INSTANCE_NAME>
AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME=<YOUR_EMBEDDINGS_DEPLOYMENT_NAME>
AZURE_OPENAI_API_KEY=<YOUR_KEY>
AZURE_OPENAI_API_VERSION="2024-02-01"
如果您想获取模型调用的自动化跟踪,您也可以通过取消以下注释来设置您的 LangSmith API 密钥:
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"

安装

LangChain AzureOpenAIEmbeddings 集成位于 @langchain/openai 包中:
npm install @langchain/openai @langchain/core
您可以在Azure OpenAI文档中找到支持的API版本列表。
如果 AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME 未定义,则将回退到部署名称的 AZURE_OPENAI_API_DEPLOYMENT_NAME 的值。同样,对于 AzureOpenAIEmbeddings 构造函数中的 azureOpenAIApiEmbeddingsDeploymentName 参数,如果未定义,则将回退到 azureOpenAIApiDeploymentName 的值。

## Instantiation

Now we can instantiate our model object and embed text:

```typescript
import { AzureOpenAIEmbeddings } from "@langchain/openai";

const embeddings = new AzureOpenAIEmbeddings({
  azureOpenAIApiKey: "<your_key>", // 在 Node.js 中默认为 process.env.AZURE_OPENAI_API_KEY
  azureOpenAIApiInstanceName: "<your_instance_name>", // 在 Node.js 中默认为 process.env.AZURE_OPENAI_API_INSTANCE_NAME
  azureOpenAIApiEmbeddingsDeploymentName: "<your_embeddings_deployment_name>", // 在 Node.js 中默认为 process.env.AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME
  azureOpenAIApiVersion: "<api_version>", // 在 Node.js 中默认为 process.env.AZURE_OPENAI_API_VERSION
  maxRetries: 1,
});

Indexing and Retrieval

Embedding models are often used in retrieval-augmented generation (RAG) flows, both as part of indexing data as well as later retrieving it. For more detailed instructions, please see our RAG tutorials under the Learn tab. Below, see how to index and retrieve data using the embeddings object we initialized above. In this example, we will index and retrieve a sample document using the demo MemoryVectorStore.
// 使用示例文本创建一个向量存储
import { MemoryVectorStore } from "@langchain/classic/vectorstores/memory";

const text = "LangChain 是构建具有上下文感知推理应用的框架";

const vectorstore = await MemoryVectorStore.fromDocuments(
  [{ pageContent: text, metadata: {} }],
  embeddings,
);

// 使用向量存储作为返回单个文档的检索器
const retriever = vectorstore.asRetriever(1);

// 获取最相似文本
const retrievedDocuments = await retriever.invoke("什么是LangChain?");

retrievedDocuments[0].pageContent;
LangChain 是构建上下文感知推理应用的框架

Direct Usage

Under the hood, the vectorstore and retriever implementations are calling embeddings.embedDocument(...) and embeddings.embedQuery(...) to create embeddings for the text(s) used in fromDocuments and the retriever’s invoke operations, respectively. You can directly call these methods to get embeddings for your own use cases.

Embed single texts

You can embed queries for search with embedQuery. This generates a vector representation specific to the query:
const singleVector = await embeddings.embedQuery(text);

console.log(singleVector.slice(0, 100));
[
   -0.024253517, -0.0054218727,   0.048715446,   0.020580322,    0.03180832,
   0.0028770117,  -0.012367731,   0.037383243,  -0.054915592,   0.032225136,
     0.00825818,  -0.023888804,   -0.01184671,   0.012257014,   0.016294925,
    0.009254632,  0.0051353113,  -0.008889917,   0.016855022,    0.04207243,
  0.00082589936,  -0.011664353,    0.00818654,   0.029020859,  -0.012335167,
   -0.019603407,  0.0013945447,    0.05538451,  -0.011625277,  -0.008153976,
    0.038607642,   -0.03811267, -0.0074440846,   0.047647353,   -0.00927417,
    0.024201415, -0.0069230637,  -0.008538228,   0.003910912,   0.052805457,
   -0.023159374,  0.0014352495,  -0.038659744,   0.017141584,   0.005587948,
    0.007971618,  -0.016920151,    0.06658646, -0.0016916894,   0.045667473,
   -0.042202685,   -0.03983204,   -0.04160351,  -0.011729481,  -0.055905532,
    0.012543576,  0.0038848612,   0.007919516,   0.010915386,  0.0033117384,
   -0.007548289,  -0.030427614,  -0.041890074,   0.036002535,  -0.023771575,
   -0.008792226,  -0.049444873,   0.016490309, -0.0060568666,   0.040196754,
    0.014106638,  -0.014575557, -0.0017356506,  -0.011234511,  -0.012517525,
    0.008362384,    0.01253055,   0.036158845,   0.008297256, -0.0010908874,
   -0.014888169,  -0.020489143,   0.018965157,  -0.057937514, -0.0037122732,
    0.004402626,   -0.00840146,   0.042984217,   -0.04936672,   -0.03714878,
    0.004969236,    0.03707063,   0.015396165,   -0.02055427,    0.01988997,
    0.030219207,  -0.021257648,    0.01340326,   0.003692735,   0.012595678
]

Embed multiple texts

You can embed multiple texts for indexing with embedDocuments. The internals used for this method may (but do not have to) differ from embedding queries:
const text2 = "LangGraph 是一个用于构建具有 LLMs 的状态化、多智能体应用的库";

const vectors = await embeddings.embedDocuments([text, text2]); → const vectors = await embeddings.embedDocuments([text, text2]);

console.log(vectors[0].slice(0, 100));
console.log(vectors[1].slice(0, 100));
[
   -0.024253517, -0.0054218727,   0.048715446,   0.020580322,    0.03180832,
   0.0028770117,  -0.012367731,   0.037383243,  -0.054915592,   0.032225136,
     0.00825818,  -0.023888804,   -0.01184671,   0.012257014,   0.016294925,
    0.009254632,  0.0051353113,  -0.008889917,   0.016855022,    0.04207243,
  0.00082589936,  -0.011664353,    0.00818654,   0.029020859,  -0.012335167,
   -0.019603407,  0.0013945447,    0.05538451,  -0.011625277,  -0.008153976,
    0.038607642,   -0.03811267, -0.0074440846,   0.047647353,   -0.00927417,
    0.024201415, -0.0069230637,  -0.008538228,   0.003910912,   0.052805457,
   -0.023159374,  0.0014352495,  -0.038659744,   0.017141584,   0.005587948,
    0.007971618,  -0.016920151,    0.06658646, -0.0016916894,   0.045667473,
   -0.042202685,   -0.03983204,   -0.04160351,  -0.011729481,  -0.055905532,
    0.012543576,  0.0038848612,   0.007919516,   0.010915386,  0.0033117384,
   -0.007548289,  -0.030427614,  -0.041890074,   0.036002535,  -0.023771575,
   -0.008792226,  -0.049444873,   0.016490309, -0.0060568666,   0.040196754,
    0.014106638,  -0.014575557, -0.0017356506,  -0.011234511,  -0.012517525,
    0.008362384,    0.01253055,   0.036158845,   0.008297256, -0.0010908874,
   -0.014888169,  -0.020489143,   0.018965157,  -0.057937514, -0.0037122732,
    0.004402626,   -0.00840146,   0.042984217,   -0.04936672,   -0.03714878,
    0.004969236,    0.03707063,   0.015396165,   -0.02055427,    0.01988997,
    0.030219207,  -0.021257648,    0.01340326,   0.003692735,   0.012595678
]
[
   -0.033366997,   0.010419146,  0.0118083665,  -0.040441725, 0.0020355924,
   -0.015808804,  -0.023629595, -0.0066180876,  -0.040004376,  0.020053642,
  -0.0010797002,   -0.03900105,  -0.009956073,  0.0027896944,  0.003305828,
   -0.034010153,   0.009833873,  0.0061164247,   0.022536227,  0.029147884,
    0.017789727,    0.03182342,   0.010869357,   0.031849146, -0.028093107,
    0.008283865, -0.0145610785,    0.01645196,  -0.029430874,  -0.02508313,
    0.046178687,   -0.01722375,  -0.010046115,   0.013101112, 0.0044538635,
     0.02197025,    0.03985002,   0.007955855,  0.0008819293,  0.012657333,
    0.014368132,  -0.014007963,   -0.03722594,   0.031617608, -0.011570398,
    0.039052505,  0.0020018267,   0.023706773, -0.0046950476,  0.056083307,
    -0.08412496,  -0.043425974,  -0.015512952,   0.015950298,  -0.03624834,
  -0.0053317733,  -0.037251666,  0.0046339477,    0.04193385,  0.023475237,
   -0.021378545,   0.013699248,  -0.026009277,   0.050757967,   -0.0494202,
   0.0007874656,   -0.07208506,   0.015885983,  -0.003259199,  0.015127057,
   0.0068946453,  -0.035373647,  -0.005875241, -0.0032238255,  -0.04185667,
   -0.022047428,  0.0014326327, -0.0070940237, -0.0027864785, -0.016271876,
    0.005097021,   0.034473225,   0.012361481,  -0.026498076, 0.0067274245,
   -0.026330855,  -0.006132504,   0.008180959,  -0.049368747, -0.032337945,
    0.011049441,    0.00186194,  -0.012097787,    0.01930758,   0.07059293,
    0.029713862,    0.04337452, -0.0048461896,  -0.019976463,  0.011473924
]

Using Azure Managed Identity

If you’re using Azure Managed Identity, you can configure the credentials like this:
import {
  DefaultAzureCredential,
  getBearerTokenProvider,
} from "@azure/identity";
import { AzureOpenAIEmbeddings } from "@langchain/openai";

const credentials = new DefaultAzureCredential();
const azureADTokenProvider = getBearerTokenProvider(
  credentials,
  "https://cognitiveservices.azure.com/.default"
);

const modelWithManagedIdentity = new AzureOpenAIEmbeddings({
  azureADTokenProvider,
  azureOpenAIApiInstanceName: "<your_instance_name>",
  azureOpenAIApiEmbeddingsDeploymentName: "<your_embeddings_deployment_name>",
  azureOpenAIApiVersion: "<api_version>",
});

Using a different domain

If your instance is hosted under a domain other than the default openai.azure.com, you’ll need to use the alternate AZURE_OPENAI_BASE_PATH environment variable. For example, here’s how you would connect to the domain https://westeurope.api.microsoft.com/openai/deployments/{DEPLOYMENT_NAME}:
import { AzureOpenAIEmbeddings } from "@langchain/openai";

const embeddingsDifferentDomain = new AzureOpenAIEmbeddings({
  azureOpenAIApiKey: "<your_key>", // 在 Node.js 中默认为 process.env.AZURE_OPENAI_API_KEY
  azureOpenAIApiEmbeddingsDeploymentName: "<your_embedding_deployment_name>", // 在 Node.js 中默认为 process.env.AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME
  azureOpenAIApiVersion: "<api_version>", // 在 Node.js 中默认为 process.env.AZURE_OPENAI_API_VERSION
  azureOpenAIBasePath:
    "https://westeurope.api.microsoft.com/openai/deployments", // 在 Node.js 中默认为 process.env.AZURE_OPENAI_BASE_PATH
});

Custom headers

You can specify custom headers by passing in a configuration field:
import { AzureOpenAIEmbeddings } from "@langchain/openai";

const embeddingsWithCustomHeaders = new AzureOpenAIEmbeddings({
  azureOpenAIApiKey: "<your_key>",
  azureOpenAIApiInstanceName: "<your_instance_name>",
  azureOpenAIApiEmbeddingsDeploymentName: "<your_embeddings_deployment_name>",
  azureOpenAIApiVersion: "<api_version>",
  configuration: {
    defaultHeaders: {
      "x-custom-header": `SOME_VALUE`,
    },
  },
});
The configuration field also accepts other ClientOptions parameters accepted by the official SDK. Note: The specific header api-key currently cannot be overridden in this manner and will pass through the value from azureOpenAIApiKey.

Migration from Azure OpenAI SDK

If you are using the deprecated Azure OpenAI SDK with the @langchain/azure-openai package, you can update your code to use the new Azure integration following these steps:
  1. Install the new @langchain/openai package and remove the previous @langchain/azure-openai package:
    npm
    npm install @langchain/openai
    npm uninstall @langchain/azure-openai
    
  2. Update your imports to use the new AzureOpenAIEmbeddings classe from the @langchain/openai package:
    import { AzureOpenAIEmbeddings } from "@langchain/openai";
    
  3. Update your code to use the new AzureOpenAIEmbeddings class and pass the required parameters:
    const model = new AzureOpenAIEmbeddings({
      azureOpenAIApiKey: "<your_key>",
      azureOpenAIApiInstanceName: "<your_instance_name>",
      azureOpenAIApiEmbeddingsDeploymentName:
        "<your_embeddings_deployment_name>",
      azureOpenAIApiVersion: "<api_version>",
    });
    
    Notice that the constructor now requires the azureOpenAIApiInstanceName parameter instead of the azureOpenAIEndpoint parameter, and adds the azureOpenAIApiVersion parameter to specify the API version.
    • If you were using Azure Managed Identity, you now need to use the azureADTokenProvider parameter to the constructor instead of credentials, see the Azure Managed Identity section for more details.
    • If you were using environment variables, you now have to set the AZURE_OPENAI_API_INSTANCE_NAME environment variable instead of AZURE_OPENAI_API_ENDPOINT, and add the AZURE_OPENAI_API_VERSION 环境变量来指定 API 版本。

API 参考文档

有关所有 AzureOpenAIEmbeddings 功能和配置的详细文档,请参阅 API 参考