纪元117:罗马和平中文试玩版
42.7G · 2025-09-10
@[toc]
在经历了八个里程碑式的版本之后(M1~M8),Spring AI 1.0 正式版本,终于在 2025 年 5 月 20 日正式发布了,这是另一个新高度的里程碑式的版本,标志着 Spring 生态系统正式全面拥抱人工智能技术,并且意味着 Spring AI 将会给企业带来稳定 API 支持。
Spring AI 是 Spring 官方推出的一个人工智能集成框架,旨在简化 AI 功能在 Spring 应用中的整合。它提供了一套标准化的 API 和工具,让开发者能够轻松接入多种主流 AI 服务(如 OpenAI、Azure AI、Hugging Face 等),同时保持 Spring 生态的简洁性和灵活性。
通过抽象层屏蔽不同 AI 提供商(如 OpenAI、Gemini、Ollama 等)的接口差异,开发者只需使用 Spring AI 的通用接口(如 ChatModel),即可切换底层 AI 服务。
PromptTemplate
等工具,方便动态生成提示词(Prompts),支持上下文管理。spring-ai-openai
spring-ai-vertexai
在SpringAI中,模型类型和AI提供商是两个不同维度的概念,但它们又相互关联。让我用更清晰的方式帮你区分和理解:
指的是AI模型的功能类别,即它能完成什么任务。
特点:与具体厂商无关,是通用的能力分类。
常见模型类型:
模型类型 | 功能说明 | 典型应用场景 |
---|---|---|
Chat(对话型) | 对话交互(如ChatGPT) | 客服机器人、聊天助手 |
Embedding(嵌入型) | 将文本转换为向量(数值数组) | 语义搜索、RAG + 传统搜索基于关键词匹配(如Google早期的搜索),而语义搜索通过理解查询的语义(含义)来返回更相关的结果。 + 它利用深度学习模型(如BERT、Embedding模型)将文本转换为向量(vector),通过向量相似度匹配内容,即使查询词和文档没有直接的关键词重叠。 |
Image(文生图型) | 生成/处理图像(如Stable Diffusion) | 设计辅助、内容生成 |
Text-to-Speech(文转语音型) | 将文本转为语音 | 语音助手、有声内容 |
Function Calling(函数回调型) | 让AI调用外部函数/API | 实时数据查询、工作流自动化 |
提供具体AI模型服务的公司或平台。
特点: 同一提供商可能支持多种模型类型
常见提供商:
提供商 | 支持的模型类型 | 具体模型示例 |
---|---|---|
OpenAI | Chat, Embedding, Image | GPT-4o、text-embedding-3、DALL-E |
Google Vertex AI | Chat, Embedding, Image | PaLM 2、Imagen |
Azure OpenAI | Chat, Embedding | 微软托管的OpenAI服务 |
Hugging Face | Chat, Embedding, Image | 开源模型(如BLOOM、Stable Diffusion) |
Stability AI | Image | Stable Diffusion系列 |
DeepSeek | Chat,代码专用模型、Embedding、数学专用模型 | DeepSeek-V3、DeepSeek-Coder、DeepSeek-Embedding、DeepSeek-Math |
Spring AI 框架支持主流的 AI 提供商的主流模型,并且会随着 Spring AI 版本的升级而变化。
以下是截至 2024年6月,Spring AI 框架官方及社区支持的 AI 提供商及其对应的 模型类型的详细列表,包含国内和国外主流厂商。
AI 提供商 | 支持的模型类型 | 具体模型示例 | 是否国内厂商 | Spring AI 模块名 |
---|---|---|---|---|
OpenAI | Chat, Embedding, Image Generation, Function Calling | GPT-4, GPT-3.5, text-embedding-3, DALL-E 3 | ❌ | spring-ai-openai |
Azure OpenAI | Chat, Embedding, Image Generation | GPT-4, GPT-3.5, text-embedding-ada-002 | ❌ | spring-ai-azure-openai |
Google Vertex AI | Chat, Embedding, Code Generation | Gemini 1.5, PaLM 2, textembedding-gecko | ❌ | spring-ai-vertexai |
Hugging Face | Chat, Embedding, Text Generation, Image Generation | BLOOM, Llama 2, Stable Diffusion, BERT | ❌ | spring-ai-huggingface |
Stability AI | Image Generation | Stable Diffusion XL, Stable Diffusion 3 | ❌ | spring-ai-stabilityai |
Anthropic | Chat | Claude 3, Claude 2 | ❌ |
spring-ai-anthropic (社区支持) |
Ollama | Chat, Embedding (本地运行开源模型) | Llama 3, Mistral, Gemma | ❌ | spring-ai-ollama |
DeepSeek | ❌ (尚未官方支持,但未来可能集成) | DeepSeek-V3, DeepSeek-Coder | ✅ | 暂无,虽然没有给 deepseek 提供专门的 starter,但是由于 deepseek API 接口规范与 OpenAI 保持一致,因此也可以使用 openai 的 starter。 |
百度文心大模型 | ❌ (尚未官方支持) | ERNIE-Bot 4.0, ERNIE-Embedding | ✅ | 暂无 |
阿里云通义千问 | ❌ (尚未官方支持) | Qwen-72B, Qwen-Embedding | ✅ | 暂无 |
智谱AI (GLM) | ❌ (尚未官方支持) | ChatGLM3, GLM-Embedding | ✅ | 暂无 |
讯飞星火 | ❌ (尚未官方支持) | SparkDesk 3.0 | ✅ | 暂无 |
MiniMax | ❌ (尚未官方支持) | ABAB 5.5 | ✅ | 暂无 |
DeepSeek 的 API 设计兼容 OpenAI:DeepSeek 的 API 接口规范(如请求/响应格式、鉴权方式)与 OpenAI 保持一致,因此可以直接使用 OpenAI 的客户端库调用 DeepSeek。
<dependencies>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--
DeepSeek 的 API 设计兼容 OpenAI:
DeepSeek 的 API 接口规范(如请求/响应格式、鉴权方式)与 OpenAI 保持一致,
因此可以直接使用 OpenAI 的客户端库调用 DeepSeek。
-->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>1.0.0-M6</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<repositories>
<!--指向 Spring 官方提供的快照仓库,为了尝试 Spring 的最新功能-->
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.sp*r*i*ng.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
# Spring AI OpenAI/DeepSeek 配置
spring:
ai:
openai:
api-key: sk-b768607df6cd4xxx0f5aa38 # 设置 DeepSeek API 的访问密钥,永远不要将密钥提交到代码仓库,建议通过环境变量注入。搞不好要花钱的。
base-url: https://api.d*eep**seek.com # 指定 DeepSeek API 的基础地址,格式与OpenAI相同。
chat:
options:
model: deepseek-chat # 选择要调用的 DeepSeek 模型名称,必须与 DeepSeek 支持的模型列表匹配(如 deepseek-chat、deepseek-coder 等),不同模型可能有不同的计费标准和能力。
temperature: 1.3 # temperature 值越高,AI 回答越随机和创意;值越低,回答越确定和保守。1.3 属于高值,适合需要发散性输出的场景,但可能牺牲准确性。
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
@RequiredArgsConstructor
@Service
public class AiService {
private final ChatModel chatModel;
// 简单的直接调用
public String generate(String message) {
return chatModel.call(message);
}
// 使用系统提示模板
public String generateWithSystemPrompt(String userMessage) {
SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate("""
你是一个资深{domain}专家,回答需满足以下要求:
1. 语言风格:{tone}
2. 回答长度:{length}
3. 用户问题:{userMessage}
""");
Prompt prompt = new Prompt(
systemPromptTemplate.createMessage(Map.of(
"domain", "科技",
"tone", "幽默",
"length", "不超过100字",
"userMessage", userMessage
))
);
return chatModel.call(prompt).getResult().getOutput().getText();
}
}
import com.example.demo.service.AiService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/ai")
@RequiredArgsConstructor
public class AiController {
private final AiService aiService;
@GetMapping("/chat")
public String chat(@RequestParam String message) {
return aiService.generate(message);
}
@GetMapping("/chat-with-prompt")
public String chatWithPrompt(@RequestParam String message) {
return aiService.generateWithSystemPrompt(message);
}
}
启动服务,在浏览器地址栏上访问:
到此,Spring AI 接入 DeepSeek 就完成了。