什么是Spring AI
Spring AI是从著名的Python项目 LangChain 和 LlamaIndex 中汲取灵感,它不是这些项目的直接移植,它的成立信念是,
「下一波生成式人工智能应用程序将不仅适用于 Python 开发人员,而且将在许多编程语言中无处不在」
。
我们可以从Spring AI的官网描述中,总结出Spring AI的几个核心的关键词:
❝
Spring AI简化了我们构建
「大型复杂的AI应用」
的过程,当然如果你的项目仅仅是需要调用一个AI接口,那其实直接调用官方SDK反而更方便。
❞
Spring AI提供的功能如下:
支持所有主要的模型提供商,如OpenAI,Microsoft,Amazon,Google和Huggingface。支持的模型类型包括聊天和文本到图像。
跨 AI 提供商的可移植 API,用于聊天和嵌入模型。支持同步和流 API 选项。还支持下拉以访问特定于模型的功能。
支持所有主要的向量数据库,例如 Azure Vector Search、Chroma、Milvus、Neo4j、PostgreSQL/PGVector、PineCone、Qdrant、Redis 和 Weaviate。
跨 Vector Store 提供程序的可移植 API,包括新颖的类似 SQL 的元数据过滤器 API,该 API 也是可移植的。
AI 模型和矢量存储的
「Spring Boot stater」
。
什么是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]
动手体验Spring Cloud Alibaba AI
首先新建一个Maven项目,JDK选的是17版本。
Maven文件需要引入
spring-cloud-alibaba-dependencies
和
spring-cloud-starter-alibaba-ai
两个依赖。
<dependencyManagement > <dependencies > <dependency > <groupId > com.alibaba.cloudgroupId > <artifactId > spring-cloud-alibaba-dependenciesartifactId > <version > 2023.0.1.0version > <type > pomtype > <scope > importscope > dependency > dependencies >dependencyManagement ><dependencies > <dependency > <groupId > com.alibaba.cloudgroupId > <artifactId > spring-cloud-starter-alibaba-aiartifactId > dependency >dependencies >
配置阿里云通义千问的Api-Key,没有的读者可以从官网上申请。
server: port: 8080 spring: application: name: alibaba-spring-ai-demo cloud: ai: tongyi: api-key: 你的api-key
新建SpringBoot启动类:
@SpringBootApplication public class MyAiApplication { public static void main (String[] args) { SpringApplication.run(MyAiApplication.class ,args ) ; } }
对接文本模型
我们首先测试如何对接文本大模型。
新建一个控制器类:新建
/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 @Slf 4jpublic 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开发者该何去何从?
测试结果如下:
文生图模型
这里只给出service的代码,其它代码同上面的文本问答。
可以看到,只需要实例化一个
imagePrompt
,再调用模型即可。
@Slf 4j@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.
,测试结果如下,效果还是很不错的:
语音合成模型
@Slf 4j@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) {