Azure OpenAI 嵌入
Azure 的 OpenAI 扩展了 OpenAI 的功能,为各种任务提供了安全的文本生成和嵌入计算模型:
-
相似性嵌入擅长捕获两个或多个文本片段之间的语义相似性。
-
文本搜索嵌入有助于衡量长文档与短查询的相关性。
-
代码搜索嵌入可用于嵌入代码片段和嵌入自然语言搜索查询。
Azure OpenAI 嵌入依赖于 cosine similarity
来计算文档和查询之间的相似性。
先决条件
Azure OpenAI 客户端提供三种连接选项:使用 Azure API 密钥、使用 OpenAI API 密钥或使用 Microsoft Entra ID。
Azure API 密钥和终结点
从 Azure 门户 的 Azure OpenAI 服务部分获取您的 Azure OpenAI endpoint
和 api-key
。
Spring AI 定义了两个配置属性:
-
spring.ai.azure.openai.api-key
:将其设置为从 Azure 获取的API 密钥
的值。 -
spring.ai.azure.openai.endpoint
:将其设置为在 Azure 中预配模型时获取的终结点 URL。
您可以在 application.properties
或 application.yml
文件中设置这些配置属性:
spring.ai.azure.openai.api-key=<your-azure-api-key>
spring.ai.azure.openai.endpoint=<your-azure-endpoint-url>
如果您更喜欢将环境变量用于 API 密钥等敏感信息,可以在配置中使用 Spring Expression Language (SpEL):
# In application.yml
spring:
ai:
azure:
openai:
api-key: ${AZURE_OPENAI_API_KEY}
endpoint: ${AZURE_OPENAI_ENDPOINT}
# In your environment or .env file
export AZURE_OPENAI_API_KEY=<your-azure-openai-api-key>
export AZURE_OPENAI_ENDPOINT=<your-azure-endpoint-url>
OpenAI 密钥
要使用 OpenAI 服务(而非 Azure)进行身份验证,请提供 OpenAI API 密钥。 这将自动将终结点设置为 [role="bare"][role="bare"][role="bare"]https://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 的环境变量:
# In application.yml
spring:
ai:
azure:
openai:
openai-api-key: ${AZURE_OPENAI_API_KEY}
chat:
options:
deployment-name: ${OPENAI_MODEL_NAME}
# In your environment or .env file
export AZURE_OPENAI_API_KEY=<your-openai-key>
export OPENAI_MODEL_NAME=<openai-model-name>
自动配置
Spring AI 自动配置、启动器模块的工件名称发生了重大变化。 有关更多信息,请参阅 升级说明。 |
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 |
- |
spring.ai.azure.openai.endpoint |
来自 Azure AI OpenAI |
- |
spring.ai.azure.openai.openai-api-key |
(非 Azure)OpenAI API 密钥。用于与 OpenAI 服务(而非 Azure OpenAI)进行身份验证。
这会自动将终结点设置为 [role="bare"][role="bare"][role="bare"]https://api.openai.com/v1。使用 |
- |
嵌入自动配置的启用和禁用现在通过前缀为 |
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 |
操作调用者或最终用户的标识符。这可能用于跟踪或速率限制目的。 |
- |
所有前缀为 |
运行时选项
AzureOpenAiEmbeddingOptions
提供了嵌入请求的配置信息。
AzureOpenAiEmbeddingOptions
提供了一个构建器来创建选项。
在启动时,使用 AzureOpenAiEmbeddingModel
构造函数设置用于所有嵌入请求的默认选项。
在运行时,您可以通过将 AzureOpenAiEmbeddingOptions
实例传递给 EmbeddingRequest
请求来覆盖默认选项。
例如,要覆盖特定请求的默认模型名称:
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 添加到您的构建文件中。 |
|
接下来,创建一个 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"));
|