Appearance
Spring AI 模型集成
Spring AI 提供了与多种 AI 模型和服务提供商的集成能力。本文将介绍如何在 Spring 应用中集成和使用不同的 AI 模型。
支持的模型提供商
Spring AI 目前支持多种流行的 AI 服务提供商,包括:
- OpenAI (GPT-3.5/4 等)
- Azure OpenAI
- Amazon Bedrock (Claude, Llama 2 等)
- Google (Gemini, PaLM)
- Anthropic (Claude)
- Ollama (本地运行的开源模型)
- HuggingFace
- Vertex AI
集成 OpenAI
OpenAI 是最常用的 AI 服务提供商之一,集成 OpenAI 的步骤如下:
依赖配置
xml
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>0.8.0</version>
</dependency>
属性配置
在 application.properties
或 application.yml
中配置 API 密钥:
yaml
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
model: gpt-4
temperature: 0.7
max-tokens: 500
基本使用
java
@Service
public class OpenAIService {
private final OpenAiChatClient chatClient;
public OpenAIService(OpenAiChatClient chatClient) {
this.chatClient = chatClient;
}
public String generateText(String prompt) {
ChatResponse response = chatClient.call(new Prompt(prompt));
return response.getResult().getOutput().getContent();
}
}
集成 Azure OpenAI
依赖配置
xml
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
<version>0.8.0</version>
</dependency>
属性配置
yaml
spring:
ai:
azure:
openai:
api-key: ${AZURE_OPENAI_API_KEY}
endpoint: ${AZURE_OPENAI_ENDPOINT}
deployment-name: gpt-4
集成 Anthropic (Claude)
依赖配置
xml
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-anthropic-spring-boot-starter</artifactId>
<version>0.8.0</version>
</dependency>
属性配置
yaml
spring:
ai:
anthropic:
api-key: ${ANTHROPIC_API_KEY}
model: claude-3-opus-20240229
集成 Amazon Bedrock
依赖配置
xml
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bedrock-spring-boot-starter</artifactId>
<version>0.8.0</version>
</dependency>
属性配置
yaml
spring:
ai:
bedrock:
region: us-east-1
credentials:
access-key: ${AWS_ACCESS_KEY}
secret-key: ${AWS_SECRET_KEY}
chat:
model: anthropic.claude-v2
切换和组合多个模型
Spring AI 允许在同一应用中集成和使用多个模型提供商:
java
@Configuration
public class AiConfig {
@Bean
public ChatModel fallbackChatModel(
OpenAiChatClient openAiClient,
AnthropicChatClient anthropicClient) {
return new FallbackChatModel(List.of(
openAiClient,
anthropicClient
));
}
}
使用多个模型进行集成:
java
@Service
public class MultiModelService {
private final OpenAiChatClient openAiClient;
private final AnthropicChatClient anthropicClient;
// 构造函数注入
public String generateWithBestModel(String prompt) {
try {
// 首先尝试 OpenAI
return openAiClient.call(new Prompt(prompt))
.getResult().getOutput().getContent();
} catch (Exception e) {
// 出错时回退到 Anthropic
return anthropicClient.call(new Prompt(prompt))
.getResult().getOutput().getContent();
}
}
}
自定义模型配置
Spring AI 支持在运行时动态配置模型参数:
java
@Service
public class DynamicModelService {
private final OpenAiChatClient openAiClient;
public String generateCreativeText(String prompt) {
// 为创意任务使用更高的温度值
ChatOptions options = ChatOptions.builder()
.temperature(0.9f)
.maxTokens(1000)
.build();
return openAiClient.call(new Prompt(prompt), options)
.getResult().getOutput().getContent();
}
public String generateFactualText(String prompt) {
// 为事实类任务使用较低的温度值
ChatOptions options = ChatOptions.builder()
.temperature(0.1f)
.maxTokens(500)
.build();
return openAiClient.call(new Prompt(prompt), options)
.getResult().getOutput().getContent();
}
}
本地模型集成
对于需要数据隐私或离线使用的场景,Spring AI 支持集成本地运行的模型,如 Ollama:
依赖配置
xml
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
<version>0.8.0</version>
</dependency>
属性配置
yaml
spring:
ai:
ollama:
base-url: http://localhost:11434
model: llama2
总结
Spring AI 提供了丰富而灵活的模型集成能力,使开发人员能够:
- 轻松集成各种 AI 服务提供商
- 在同一应用中使用多个模型
- 针对不同任务动态配置模型参数
- 实现模型回退和组合策略
- 集成本地运行的开源模型
通过统一的抽象层,Spring AI 使得在应用中切换或组合不同的 AI 模型变得简单,同时保持了代码的一致性和可维护性。