Azure OpenAI 嵌入

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

  • 相似性嵌入擅长捕捉两个或多个文本片段之间的语义相似性。

  • 文本搜索嵌入帮助衡量长文档是否与短查询相关。

  • 代码搜索嵌入对于嵌入代码片段和嵌入自然语言搜索查询很有用。

Azure OpenAI 嵌入使用 余弦相似度 来计算文档和查询之间的相似性。

前提条件

Azure OpenAI 客户端提供三种连接选项:使用 Azure API 密钥、使用 OpenAI API 密钥或使用 Microsoft Entra ID。

Azure API 密钥和端点

从 Azure 门户的 Azure OpenAI 服务部分获取您的 Azure OpenAI endpointapi-key

Spring AI 定义了两个配置属性:

  1. spring.ai.azure.openai.api-key:设置为从 Azure 获取的 API Key 值。

  2. spring.ai.azure.openai.endpoint:设置为在 Azure 中配置模型时获取的端点 URL。

您可以在 application.propertiesapplication.yml 文件中设置这些配置属性:

spring.ai.azure.openai.api-key=<your-azure-api-key>
spring.ai.azure.openai.endpoint=<your-azure-endpoint-url>

如果您更喜欢使用环境变量来处理敏感信息(如 API 密钥),可以在配置中使用 Spring 表达式语言(SpEL):

# 在 application.yml 中
spring:
  ai:
    azure:
      openai:
        api-key: ${AZURE_OPENAI_API_KEY}
        endpoint: ${AZURE_OPENAI_ENDPOINT}
# 在您的环境或 .env 文件中
export AZURE_OPENAI_API_KEY=<your-azure-openai-api-key>
export AZURE_OPENAI_ENDPOINT=<your-azure-endpoint-url>

OpenAI 密钥

要使用 OpenAI 服务(非 Azure)进行身份验证,请提供 OpenAI API 密钥。 这将自动将端点设置为 api.openai.com/v1。

使用此方法时,将 spring.ai.azure.openai.chat.options.deployment-name 属性设置为您希望使用的 OpenAI 模型 的名称。

在您的应用程序配置中:

spring.ai.azure.openai.openai-api-key=<your-azure-openai-key>
spring.ai.azure.openai.chat.options.deployment-name=<openai-model-name>

使用 SpEL 的环境变量:

# 在 application.yml 中
spring:
  ai:
    azure:
      openai:
        openai-api-key: ${AZURE_OPENAI_API_KEY}
        chat:
          options:
            deployment-name: ${OPENAI_MODEL_NAME}
# 在您的环境或 .env 文件中
export AZURE_OPENAI_API_KEY=<your-openai-key>
export OPENAI_MODEL_NAME=<openai-model-name>

Microsoft Entra ID

对于使用 Microsoft Entra ID(原 Azure Active Directory)的无密钥身份验证,仅设置 spring.ai.azure.openai.endpoint 配置属性,而不设置上述 api-key 属性。

仅找到端点属性时,您的应用程序将评估几种不同的选项来检索凭据,并使用令牌凭据创建 OpenAIClient 实例。

注意:不再需要创建 TokenCredential bean;它会自动为您配置。

添加仓库和 BOM

Spring AI 构件发布在 Maven Central 和 Spring Snapshot 仓库中。 请参阅 构件仓库 部分,将这些仓库添加到您的构建系统中。

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

自动配置

Spring AI 自动配置、starter 模块的构件名称发生了重大变化。 请参阅 升级说明 了解更多信息。

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

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

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

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-azure-openai'
}

提示:请参阅 依赖管理 部分,将 Spring AI BOM 添加到您的构建文件中。

嵌入属性

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

属性 描述 默认值

spring.ai.azure.openai.api-key

来自 Azure AI OpenAI Keys and Endpoint 部分的密钥,位于 Resource Management

-

spring.ai.azure.openai.endpoint

来自 Azure AI OpenAI Keys and Endpoint 部分的端点,位于 Resource Management

-

spring.ai.azure.openai.openai-api-key

(非 Azure)OpenAI API 密钥。用于向 OpenAI 服务进行身份验证,而不是 Azure OpenAI。 这会自动将端点设置为 api.openai.com/v1。使用 api-keyopenai-api-key 属性之一。 使用此配置时,spring.ai.azure.openai.embedding.options.deployment-name 被视为 OpenAi 模型 名称。

-

嵌入自动配置的启用和禁用现在通过前缀为 spring.ai.model.embedding 的顶级属性进行配置。

要启用,spring.ai.model.embedding=azure-openai(默认启用)

要禁用,spring.ai.model.embedding=none(或任何不匹配 azure-openai 的值)

此更改是为了允许多个模型的配置。

前缀 spring.ai.azure.openai.embedding 是配置 Azure OpenAI 的 EmbeddingModel 实现的属性前缀

属性 描述 默认值

spring.ai.azure.openai.embedding.enabled(已移除且不再有效)

启用 Azure OpenAI 嵌入模型。

true

spring.ai.model.embedding

启用 Azure OpenAI 嵌入模型。

azure-openai

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

文档内容提取模式

EMBED

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

这是在 Azure AI 门户中显示的"部署名称"的值

text-embedding-ada-002

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

操作的调用者或最终用户的标识符。这可用于跟踪或速率限制目的。

-

提示:所有以 spring.ai.azure.openai.embedding.options 为前缀的属性都可以通过在 EmbeddingRequest 调用中添加请求特定的 运行时选项 在运行时被覆盖。

运行时选项

AzureOpenAiEmbeddingOptions 提供嵌入请求的配置信息。 AzureOpenAiEmbeddingOptions 提供了一个构建器来创建选项。

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

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

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

示例代码

这将创建一个 EmbeddingModel 实现,您可以将其注入到您的类中。 以下是一个使用 EmbeddingModel 实现的简单 @Controller 类示例。

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 EmbeddingModel embeddingModel;

    @Autowired
    public EmbeddingController(EmbeddingModel embeddingModel) {
        this.embeddingModel = embeddingModel;
    }

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

手动配置

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

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

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

dependencies {
    implementation 'org.springframework.ai:spring-ai-azure-openai'
}

提示:请参阅 依赖管理 部分,将 Spring AI BOM 添加到您的构建文件中。

注意:spring-ai-azure-openai 依赖项还提供对 AzureOpenAiEmbeddingModel 的访问。有关 AzureOpenAiChatModel 的更多信息,请参阅 Azure OpenAI 嵌入 部分。

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

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

var embeddingModel = new AzureOpenAiEmbeddingModel(this.openAIClient)
    .withDefaultOptions(AzureOpenAiEmbeddingOptions.builder()
        .model("text-embedding-ada-002")
        .user("user-6")
        .build());

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

注意:text-embedding-ada-002 实际上是 Azure AI 门户中显示的 Deployment Name