文章目录
- 1 AI框架
- 1.1 Spring AI 简介
- 1.2 Spring AI 使用
- 1.2.1 pom.xml
- 1.2.2 可实现的功能
- 1.3 Spring Cloud Alibaba AI
- 1.4 Spring Cloud Alibaba AI 实践操作
- 1.4.1 pom.xml
- 1.4.2 配置文件
- 1.4.3 对接文本模型
- 1.4.4 文生图模型
- 1.4.5 语音合成模型
1 AI框架
1.1 Spring AI 简介
在软件开发的世界中,Java一直是企业级应用的主力军。而Spring框架,尤其是Spring Boot,以其生态系统的丰富性,为开发者提供了无与伦比的便利。现在,Spring Boot正迈向一个新的纪元——人工智-能的时代。Spring AI项目的推出,不仅标志着Spring生态的进一步扩展,也为广大Java开发者开启了一个全新的编程领域。
Spring AI是从著名的Python项目LangChain和LlamaIndex中汲取灵感,它不是这些项目的直接移植,它的成立信念是,下一波生成式人工智能应用程序将不仅适用于 Python 开发人员,而且将在许多编程语言中无处不在。
Spring AI功能,可以看Spring推出的官方文档:https://spring.io/projects/spring-ai
我们可以从Spring AI的官网描述中,总结出Spring AI的几个核心的关键词:
- 提供抽象能力
- 简化AI应用的开发
- 模型与向量支持
- AI集成与自动配置
Spring AI简化了我们构建大型复杂的AI应用的过程,当然如果你的项目仅仅是需要调用一个AI接口,那其实直接调用官方SDK反而更方便。
Spring AI提供的功能如下:
- SQL类过滤器API: 提供类似SQL的元数据过滤器API,实现跨供应商的一致性。
- Spring Boot集成: 专为Spring Boot设计的自动配置和启动器,让AI集成变得轻而易举。
- API可移植性,支持所有主要的模型提供商,如OpenAI,Microsoft,Amazon,Google和Huggingface。支持的模型类型包括聊天和文本到图像。
- 跨 AI 提供商的可移植 API,用于聊天和嵌入模型。支持同步和流 API 选项。还支持下拉以访问特定于模型的功能。
- 将 AI 模型输出映射到 POJO。
- 支持所有主要的向量数据库,例如 Azure Vector Search、Chroma、Milvus、Neo4j、PostgreSQL/PGVector、PineCone、Qdrant、Redis 和 Weaviate。
- 跨 Vector Store 提供程序的可移植 API,包括新颖的类似 SQL 的元数据过滤器 API,该 API 也是可移植的。
- AI 模型和矢量存储的 Spring Boot stater。
- 用于数据工程的 ETL 框架
1.2 Spring AI 使用
1.2.1 pom.xml
添加Maven存储库: 在项目的pom.xml中添加Spring Milestone和Snapshot存储库。
<repositories> <!-- Spring Milestone Repository for milestones --> <repository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </repository> <!-- Spring Snapshot Repository for snapshots --> <repository> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url> </repository> </repositories>注意:在集成 Spring AI 或其他较新的 Spring 生态系统组件时,添加Spring Milestone和Snapshot存储库是为了确保能够访问到最新的开发版本、里程碑版本和快照版本。这些版本可能尚未发布到Maven Central这样的稳定仓库,但包含了最新的特性、修复和改进
导入Spring AI BOM: 使用Spring AI BOM定义,可以确保你使用的是测试过的、兼容的库版本。
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-bom</artifactId> <version>0.8.1-SNAPSHOT</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>添加AI功能: 根据你的需求,添加相关的AI模块依赖项到pom.xml。
<dependencies> <!-- 示例:添加OpenAI的支持 --> <dependency> <groupId>org.springframework.experimental.ai</groupId> <artifactId>spring-ai-openai</artifactId> </dependency> </dependencies>1.2.2 可实现的功能
可实现的功能:
生成式AI:利用Spring AI,你可以通过简单的API调用,实现文本的生成、翻译、摘要等功能。矢量数据库:当你需要对文本数据进行语义搜索时,Spring AI提供的矢量数据库支持使得相关操作变得简单高-效。AI绘画:对于需要将文本转换为图像的应用场景,Spring AI的绘画功能可以无缝集成到你的应用中。
1.3 Spring Cloud Alibaba AI
原始的Spring AI并没有国内相关大模型的接入,对国内开发者不太友好。
总的来说,Spring Cloud Alibaba AI目前基于Spring AI 0.8.1版本API完成通义系列大模型的接入。
在当前最新版本中,Spring Cloud Alibaba AI主要完成了几种常见生成式模型的适配,包括对话、文生图、文生语音等,开发者可以使用Spring Cloud Alibaba AI开发基于通义的聊天、图片或语音生成 AI 应用,框架还提供 OutParser、Prompt Template、Stuff 等实用能力。Spring Cloud Alibaba AI官方还提供了包括聊天对话、文生图、文生语音等多种应用的开发示例,具体可以前往官网查看:https://sca.aliyun.com
1.4 Spring Cloud Alibaba AI 实践操作
首先新建一个Maven项目,JDK选的是17版本。
1.4.1 pom.xml
Maven文件需要引入spring-cloud-alibaba-dependencies和spring-cloud-starter-alibaba-ai两个依赖。
<dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2023.0.1.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-ai</artifactId> </dependency> </dependencies>1.4.2 配置文件
配置阿里云通义千问的Api-Key,没有的读者可以从官网上申请。
server: port: 8080 spring: application: name: alibaba-spring-ai-demo cloud: ai: tongyi: api-key: 你的api-key主要配置阿里云申请的api-key:https://dashscope.console.aliyun.com/model
1.4.3 对接文本模型
我们首先测试如何对接文本大模型。
新建一个控制器类:新建/simple接口,用来测试基本QA。
@RestController @RequestMapping("/ai") @CrossOrigin public class TongYiController { @Autowired @Qualifier("tongYiSimpleServiceImpl") private TongYiService tongYiSimpleService; @GetMapping("/simple") public String completion( @RequestParam(value = "message", defaultValue = "AI时代下Java开发者该何去何从?") String message ) { return tongYiSimpleService.completion(message); } }新建一个TongyiService服务类:
public interface TongYiService { /** * 基本问答 */ String completion(String message); /** * 文生图 */ ImageResponse genImg(String imgPrompt); /** * 语音合成 */ String genAudio(String text); }具体的实现类如下:由Spring AI自动注入ChatClient、StreamingChatClient,ChatClient屏蔽底层通义大模型交互细节,后者用于流式调用。
对于QA而言,仅仅通过client.call(prompt)一行代码就可以完成对模型的调用。
@Service @Slf4j public class TongYiSimpleServiceImpl extends AbstractTongYiServiceImpl { /** * 自动注入ChatClient、StreamingChatClient,屏蔽模型调用细节 */ private final ChatClient chatClient; private final StreamingChatClient streamingChatClient; @Autowired public TongYiSimpleServiceImpl(ChatClient chatClient, StreamingChatClient streamingChatClient) { this.chatClient = chatClient; this.streamingChatClient = streamingChatClient; } /** * 具体实现: */ @Override public String completion(String message) { Prompt prompt = new Prompt(new UserMessage(message)); return chatClient.call(prompt).getResult().getOutput().getContent(); } }我们发送一个请求,prompt是AI时代下Java开发者该何去何从?测试结果如下:
1.4.4 文生图模型
这里只给出service的代码,其它代码同上面的文本问答。
可以看到,只需要实例化一个imagePrompt,再调用模型即可。
@Slf4j @Service public class TongYiImagesServiceImpl extends AbstractTongYiServiceImpl { private static final Logger logger = LoggerFactory.getLogger(TongYiService.class); private final ImageClient imageClient; @Autowired public TongYiImagesServiceImpl(ImageClient client) { this.imageClient = client; } @Override public ImageResponse genImg(String imgPrompt) { var prompt = new ImagePrompt(imgPrompt); return imageClient.call(prompt); } }测试的prompt是:Painting a boy coding in front of the desk, with his dog.,测试结果如下,效果还是很不错的:
1.4.5 语音合成模型
@Slf4j @Service public class TongYiAudioSimpleServiceImpl extends AbstractTongYiServiceImpl { private static final Logger logger = LoggerFactory.getLogger(TongYiService.class); private final SpeechClient speechClient; @Autowired public TongYiAudioSimpleServiceImpl(SpeechClient client) { this.speechClient = client; } @Override public String genAudio(String text) { logger.info("gen audio prompt is: {}", text); var resWAV = speechClient.call(text); // save的代码省略,就是将音频保存到本地而已 return save(resWAV, SpeechSynthesisAudioFormat.WAV.getValue()); } }测试结果也是成功的: