赞
踩
SpringAI 的发展历史可以追溯到对 Spring 框架的扩展和改进,以支持人工智能相关的功能。随着人工智能技术的快速发展,SpringAI 逐渐成为 Spring 生态系统中的一个重要组成部分,为开发者提供了便捷、灵活的解决方案。
项目的灵感来自著名的 Python 项目,如 LangChain 和 LlamaIndex,但 Spring AI 并不是这些项目的直接复制。Spring AI 相信下一波 Generative AI 生成式应用程序将不仅面向 Python 开发人员,而且将在许多编程语言中无处不在。
SpringAI 的技术特点包括但不限于以下几点:
Spring AI 的核心是提供抽象,作为开发 Java AI 应用程序的基础,提供以下功能:
ai.springai.core
:核心功能包,包括语义分析、自然语言处理等。ai.springai.openapi
:与外部人工智能服务集成的包,如大型语言模型的客户端。SpringAI 可以用于以下场景:
下面是一个简单的示例,演示了如何使用 SpringAI 进行自然语言查询的处理:
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RestController;
- import ai.springai.semantic.PGQueryProcessor;
-
- @RestController
- public class PGController {
- @PostMapping("/pg/query")
- public String processPGQuery(@RequestBody String userInput) {
- // 使用SpringAI进行语义分析和解析
- PGQueryProcessor processor = new PGQueryProcessor();
- return processor.process(userInput);
- }
- }
要将 SpringAI 结合到当前项目中,您可以按照以下步骤进行:
SpringAI 可以与数据库进行交互,例如,您可以将用户的自然语言查询翻译成 SQL 查询,并执行相应的数据库操作。下面是一个简单的示例:
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RestController;
- import ai.springai.semantic.PGQueryProcessor;
-
- @RestController
- public class PGController {
-
- @Autowired
- private JdbcTemplate jdbcTemplate;
-
- @PostMapping("/pg/query")
- public String processPGQuery(@RequestBody String userInput) {
- // 使用SpringAI进行语义分析和解析
- PGQueryProcessor processor = new PGQueryProcessor();
- String parsedQuery = processor.process(userInput);
-
- // 将解析后的查询转换为 SQL 查询并执行
- String sqlQuery = convertToSQL(parsedQuery);
- return jdbcTemplate.queryForObject(sqlQuery, String.class);
- }
-
- private String convertToSQL(String parsedQuery) {
- // 实现将解析后的查询转换为 SQL 查询的逻辑
- // 省略具体实现
- return "";
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
在项目 pom.xml 中加入 2023.0.1.0 版本 Spring Cloud Alibaba 依赖:
- <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>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
在 application.yml
配置文件中加入以下配置:
- spring:
- cloud:
- ai:
- tongyi:
- chat:
- options:
- # Replace the following key with a valid API-KEY.
- api-key: sk-a3d73b1709bf4a178c28ed7c8b3b5axx
编写聊天服务实现类,由 Spring AI 自动注入 ChatClient
、StreamingChatClient
,ChatClient
屏蔽底层通义大模型交互细节。
- @Service
- public class TongYiSimpleServiceImpl extends AbstractTongYiServiceImpl {
-
- private final ChatClient chatClient;
-
- private final StreamingChatClient streamingChatClient;
-
- @Autowired
- public TongYiSimpleServiceImpl(ChatClient chatClient, StreamingChatClient streamingChatClient) {
- this.chatClient = chatClient;
- this.streamingChatClient = streamingChatClient;
- }
- }
提供具体聊天逻辑实现
- @Service
- public class TongYiSimpleServiceImpl extends AbstractTongYiServiceImpl {
-
- // ......
-
- @Override
- public String completion(String message) {
-
- Prompt prompt = new Prompt(new UserMessage(message));
-
- return chatClient.call(prompt).getResult().getOutput().getContent();
- }
-
- @Override
- public Map<String, String> streamCompletion(String message) {
-
- StringBuilder fullContent = new StringBuilder();
-
- streamingChatClient.stream(new Prompt(message))
- .flatMap(chatResponse -> Flux.fromIterable(chatResponse.getResults()))
- .map(content -> content.getOutput().getContent())
- .doOnNext(fullContent::append)
- .last()
- .map(lastContent -> Map.of(message, fullContent.toString()))
- .block();
-
- log.info(fullContent.toString());
-
- return Map.of(message, fullContent.toString());
- }
-
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
编写 Spring 入口类并启动应用
- @SpringBootApplication
- public class TongYiApplication {
- public static void main(String[] args) {
- SpringApplication.run(TongYiApplication.class);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。