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,可以定义以下环境变量以使用该服务:安装
LangChain AzureOpenAIEmbeddings 集成位于@langchain/openai 包中:
您可以在Azure OpenAI文档中找到支持的API版本列表。
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 theembeddings object we initialized above. In this example, we will index and retrieve a sample document using the demo MemoryVectorStore.
Direct Usage
Under the hood, the vectorstore and retriever implementations are callingembeddings.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 withembedQuery. This generates a vector representation specific to the query:
Embed multiple texts
You can embed multiple texts for indexing withembedDocuments. The internals used for this method may (but do not have to) differ from embedding queries:
Using Azure Managed Identity
If you’re using Azure Managed Identity, you can configure the credentials like this:Using a different domain
If your instance is hosted under a domain other than the defaultopenai.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}:
Custom headers
You can specify custom headers by passing in aconfiguration field:
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:
-
Install the new
@langchain/openaipackage and remove the previous@langchain/azure-openaipackage:npm -
Update your imports to use the new
AzureOpenAIEmbeddingsclasse from the@langchain/openaipackage: -
Update your code to use the new
AzureOpenAIEmbeddingsclass and pass the required parameters:Notice that the constructor now requires theazureOpenAIApiInstanceNameparameter instead of theazureOpenAIEndpointparameter, and adds theazureOpenAIApiVersionparameter to specify the API version.-
If you were using Azure Managed Identity, you now need to use the
azureADTokenProviderparameter to the constructor instead ofcredentials, 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_NAMEenvironment variable instead ofAZURE_OPENAI_API_ENDPOINT, and add theAZURE_OPENAI_API_VERSION环境变量来指定 API 版本。
-
If you were using Azure Managed Identity, you now need to use the