Azure的OpenAI扩展了OpenAI的功能,为各种任务提供安全的文本生成和嵌入计算模型:
Azure OpenAI嵌入模型依赖于余弦相似度来计算文档与查询之间的相似度。
前提条件
Azure OpenAI客户端提供了三种连接选项:使用Azure API密钥、使用OpenAI API密钥或使用Microsoft Entra ID。
Azure API 密钥和端点
从Azure门户的Azure OpenAI服务部分获取您的Azure OpenAI端点和API密钥。
Spring AI 定义了两个配置属性:
您可以在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表达式语言(SpEL):
# In application.ymlspring: ai: azure: openai: api-key: ${AZURE_OPENAI_API_KEY} endpoint: ${AZURE_OPENAI_ENDPOINT}
# In your environment or .env fileexport 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中使用环境变量:
# In application.ymlspring: ai: azure: openai: openai-api-key: ${AZURE_OPENAI_API_KEY} chat: options: deployment-name: ${OPENAI_MODEL_NAME}
# In your environment or .env fileexport 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;它已为您自动配置。
添加仓库和物料清单
Spring AI 工件发布在 Maven Central 和 Spring Snapshot 仓库中。请参阅“工件仓库”部分,以将这些仓库添加到您的构建系统中。
为了辅助依赖管理,Spring AI 提供了一个物料清单 (BOM),以确保整个项目中使用的是一致版本的 Spring AI。请参阅“依赖管理”部分,将 Spring AI BOM 添加到您的构建系统中。
自动配置
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.azure.openai”是用于配置与Azure OpenAI连接的属性前缀。
| 属性 | 描述 | 默认值 |
|---|
spring.ai.azure.openai.api-key | 来自Azure AI OpenAI Keys and Endpoint部分的密钥,位于Resource Management下 | - |
spring.ai.azure.openai.endpoint | 来自Keys and Endpoint下Azure AI OpenAI Resource Management部分的端点 | - |
spring.ai.azure.openai.openai-api-key | (非Azure)OpenAI API密钥。用于使用OpenAI服务进行身份验证,而不是Azure OpenAI。这会自动将端点设置为api.openai.com/v1。使用 api-key或openai-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 Portal中显示的“部署名称”的值 | text-embedding-ada-002 |
spring.ai.azure.openai.embedding.options.user | 操作的调用方或最终用户的标识符。这可以用于跟踪或速率限制目的。 | - |
所有以spring.ai.azure.openai.embedding.options为前缀的属性,都可以在运行时通过向EmbeddingRequest调用添加特定于请求的运行时选项来进行覆盖。
运行时选项
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_KEYspring.ai.azure.openai.endpoint=YOUR_ENDPOINTspring.ai.azure.openai.embedding.options.model=text-embedding-ada-002
@RestControllerpublic 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'}
接下来,创建一个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"));
文本嵌入-ada-002实际上是Azure AI门户中显示的部署名称。