Azure OpenAI Embeddings

Azure 的 OpenAI 扩展了 OpenAI 的功能,为各种任务提供了安全的文本生成和嵌入计算模型:

Azure’s OpenAI extends the OpenAI capabilities, offering safe text generation and Embeddings computation models for various task:

  • Similarity embeddings are good at capturing semantic similarity between two or more pieces of text.

  • Text search embeddings help measure whether long documents are relevant to a short query.

  • Code search embeddings are useful for embedding code snippets and embedding natural language search queries.

Azure OpenAI 嵌入依赖于“余弦相似性”来计算文档和查询之间的相似性。

The Azure OpenAI embeddings rely on cosine similarity to compute similarity between documents and a query.

Prerequisites

Azure Portal 上的 Azure OpenAI 服务部分中获取 Azure OpenAI endpointapi-key

Obtain your Azure OpenAI endpoint and api-key from the Azure OpenAI Service section on the Azure Portal.

Spring AI 定义了一个名为 spring.ai.azure.openai.api-key 的配置属性,你应该将其设置为从 Azure 获得的 API Key 的值。还存在一个名为 spring.ai.azure.openai.endpoint 的配置属性,你应该将其设置为在 Azure 中配置模型时获得的端点 URL。

Spring AI defines a configuration property named spring.ai.azure.openai.api-key that you should set to the value of the API Key obtained from Azure. There is also a configuration property named spring.ai.azure.openai.endpoint that you should set to the endpoint URL obtained when provisioning your model in Azure.

导出环境变量是一种设置这些配置属性的方法:

Exporting environment variables is one way to set these configuration properties:

export SPRING_AI_AZURE_OPENAI_API_KEY=<INSERT KEY HERE>
export SPRING_AI_AZURE_OPENAI_ENDPOINT=<INSERT ENDPOINT URL HERE>

Add Repositories and BOM

Spring AI 工件发布在 Spring Milestone 和 Snapshot 存储库中。有关将这些存储库添加到你的构建系统的说明,请参阅 Repositories 部分。

Spring AI artifacts are published in Spring Milestone and Snapshot repositories. Refer to the Repositories section to add these repositories to your build system.

为了帮助进行依赖项管理,Spring AI 提供了一个 BOM(物料清单)以确保在整个项目中使用一致版本的 Spring AI。有关将 Spring AI BOM 添加到你的构建系统的说明,请参阅 Dependency Management 部分。

To help with dependency management, Spring AI provides a BOM (bill of materials) to ensure that a consistent version of Spring AI is used throughout the entire project. Refer to the Dependency Management section to add the Spring AI BOM to your build system.

Auto-configuration

Spring AI 为 Azure OpenAI 嵌入客户端提供了 Spring Boot 自动配置。要启用它,请将以下依赖项添加到项目的 Maven pom.xml 文件:

Spring AI provides Spring Boot auto-configuration for the Azure OpenAI Embedding Client. To enable it add the following dependency to your project’s Maven pom.xml file:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
</dependency>

或添加到 Gradle build.gradle 构建文件中。

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-azure-openai-spring-boot-starter'
}
  1. 参见 Dependency Management 部分,将 Spring AI BOM 添加到你的构建文件中。

Refer to the Dependency Management section to add the Spring AI BOM to your build file.

Embedding Properties

前缀 spring.ai.azure.openai 是用于配置与 Azure OpenAI 连接的属性前缀。

The prefix spring.ai.azure.openai is the property prefix to configure the connection to Azure OpenAI.

Property Description Default

spring.ai.azure.openai.api-key

The Key from Azure AI OpenAI Keys and Endpoint section under Resource Management

-

spring.ai.azure.openai.endpoint

The endpoint from the Azure AI OpenAI Keys and Endpoint section under Resource Management

-

前缀 spring.ai.azure.openai.embeddings 是为 Azure OpenAI 配置 EmbeddingClient 实现的属性前缀

The prefix spring.ai.azure.openai.embeddings is the property prefix that configures the EmbeddingClient implementation for Azure OpenAI

Property Description Default

spring.ai.azure.openai.embedding.enabled

Enable Azure OpenAI embedding client.

true

spring.ai.azure.openai.embedding.metadata-mode

Document content extraction mode

EMBED

spring.ai.azure.openai.embedding.options.deployment-name

This is the value of the 'Deployment Name' as presented in the Azure AI Portal

text-embedding-ada-002

spring.ai.azure.openai.embedding.options.user

An identifier for the caller or end user of the operation. This may be used for tracking or rate-limiting purposes.

-

spring.ai.azure.openai.embedding.options 为前缀的所有属性都可以在运行时通过向 EmbeddingRequest 调用添加特定于请求的 Embedding Options 来覆盖。

All properties prefixed with spring.ai.azure.openai.embedding.options can be overridden at runtime by adding a request specific Embedding Options to the EmbeddingRequest call.

Embedding Options

AzureOpenAiEmbeddingOptions 提供嵌入请求的配置信息。AzureOpenAiEmbeddingOptions 提供一个生成该选项的构建器。

The AzureOpenAiEmbeddingOptions provides the configuration information for the embedding requests. The AzureOpenAiEmbeddingOptions offers a builder to create the options.

在启动时,使用 AzureOpenAiEmbeddingClient 构造函数设置用于所有嵌入请求的默认选项。在运行时,可以通过将 AzureOpenAiEmbeddingOptions 实例与你的 EmbeddingRequest 请求一起传递来覆盖默认选项。

At start time use the AzureOpenAiEmbeddingClient constructor to set the default options used for all embedding requests. At run-time you can override the default options, by passing a AzureOpenAiEmbeddingOptions instance with your to the EmbeddingRequest request.

例如,要覆盖特定请求的默认模型名称:

For example to override the default model name for a specific request:

EmbeddingResponse embeddingResponse = embeddingClient.call(
    new EmbeddingRequest(List.of("Hello World", "World is big and salvation is near"),
        AzureOpenAiEmbeddingOptions.builder()
        .withModel("Different-Embedding-Model-Deployment-Name")
        .build()));

Sample Code

这将创建一个 EmbeddingClient 实现,你可以将其注入到你的类中。这里有一个简单的 @Controller 类的示例,它使用 EmbeddingClient 实现。

This will create a EmbeddingClient implementation that you can inject into your class. Here is an example of a simple @Controller class that uses the EmbeddingClient implementation.

spring.ai.azure.openai.api-key=YOUR_API_KEY
spring.ai.azure.openai.endpoint=YOUR_ENDPOINT
spring.ai.azure.openai.embedding.options.model=text-embedding-ada-002
@RestController
public class EmbeddingController {

    private final EmbeddingClient embeddingClient;

    @Autowired
    public EmbeddingController(EmbeddingClient embeddingClient) {
        this.embeddingClient = embeddingClient;
    }

    @GetMapping("/ai/embedding")
    public Map embed(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        EmbeddingResponse embeddingResponse = this.embeddingClient.embedForResponse(List.of(message));
        return Map.of("embedding", embeddingResponse);
    }
}

Manual Configuration

如果你不想使用 Spring Boot 自动配置,则可以手动在应用程序中配置 AzureOpenAiEmbeddingClient。为此,将 spring-ai-azure-openai 依赖项添加到项目的 Maven pom.xml 文件中:

If you prefer not to use the Spring Boot auto-configuration, you can manually configure the AzureOpenAiEmbeddingClient in your application. For this add the spring-ai-azure-openai dependency to your project’s Maven pom.xml file:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-azure-openai</artifactId>
</dependency>

或添加到 Gradle build.gradle 构建文件中。

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-azure-openai'
}
  1. 参见 Dependency Management 部分,将 Spring AI BOM 添加到你的构建文件中。

Refer to the Dependency Management section to add the Spring AI BOM to your build file.

spring-ai-azure-openai 依赖项还提供对 AzureOpenAiEmbeddingClient 的访问。有关 AzureOpenAiChatClient 的更多信息,请参阅 Azure OpenAI Embeddings 部分。

The spring-ai-azure-openai dependency also provide the access to the AzureOpenAiEmbeddingClient. For more information about the AzureOpenAiChatClient refer to the Azure OpenAI Embeddings section.

接下来,创建一个 AzureOpenAiEmbeddingClient 实例,并使用它来计算两个输入文本之间的相似度:

Next, create an AzureOpenAiEmbeddingClient instance and use it to compute the similarity between two input texts:

var openAIClient = OpenAIClientBuilder()
        .credential(new AzureKeyCredential(System.getenv("AZURE_OPENAI_API_KEY")))
		.endpoint(System.getenv("AZURE_OPENAI_ENDPOINT"))
		.buildClient();

var embeddingClient = new AzureOpenAiEmbeddingClient(openAIClient)
    .withDefaultOptions(AzureOpenAiEmbeddingOptions.builder()
        .withModel("text-embedding-ada-002")
        .withUser("user-6")
        .build());

EmbeddingResponse embeddingResponse = embeddingClient
	.embedForResponse(List.of("Hello World", "World is big and salvation is near"));

text-embedding-ada-002 实际上是 Deployment Name,如 Azure AI Portal 中所示。

the text-embedding-ada-002 is actually the Deployment Name as presented in the Azure AI Portal.