当前位置:   article > 正文

SpringAI 技术解析_abstracttongyiserviceimpl

abstracttongyiserviceimpl

1. 发展历史

        SpringAI 的发展历史可以追溯到对 Spring 框架的扩展和改进,以支持人工智能相关的功能。随着人工智能技术的快速发展,SpringAI 逐渐成为 Spring 生态系统中的一个重要组成部分,为开发者提供了便捷、灵活的解决方案。

        项目的灵感来自著名的 Python 项目,如 LangChain 和 LlamaIndex,但 Spring AI 并不是这些项目的直接复制。Spring AI 相信下一波 Generative AI 生成式应用程序将不仅面向 Python 开发人员,而且将在许多编程语言中无处不在。

2. 技术特点

SpringAI 的技术特点包括但不限于以下几点:

  • 基于 Spring 框架:SpringAI 基于 Spring 框架,使得开发者可以轻松地将其集成到现有的 Spring 项目中。
  • 语义分析和自然语言处理:SpringAI 结合了先进的语义分析和自然语言处理技术,能够理解和处理用户的自然语言输入。
  • 灵活的集成方式:SpringAI 提供了简洁的 API 和注解,开发者可以根据自己的需求灵活地集成到项目中,并与其他人工智能技术进行结合。
  • 支持数据库交互:SpringAI 可以与数据库进行交互,帮助开发者将用户的自然语言查询转换为 SQL 查询,并执行相应的数据库操作。

3. 功能

Spring AI 的核心是提供抽象,作为开发 Java AI 应用程序的基础,提供以下功能:

  • 大模型对接:提供多种大模型服务对接能力,包括业界大多数主流大模型服务等;
  • 模型解析:支持灵活的 Prompt Template 和模型输出解析 Output Parsing 能力;
  • AIGC支持:支持多模态的生成式 AI 能力,如对话,文生图、文生语音等;
  • 调用能力:提供通用的可移植的 API 以访问各类模型服务和 Embedding 服务,支持同步和流式调用,同时也支持传递特定模型的定制参数;
  • RGA插件:支持 RAG 能力的基础组件,包括 DocumentLoader、TextSpillter、EmobeddingClient、VectorStore 等;
  • Spring框架:支持 AI Spring Boot Starter 实现配置自动装配;
  • 自然语言查询处理:SpringAI 可以接收用户的自然语言输入,并进行语义分析和处理,以理解用户的意图并给出相应的响应。
  • 智能化的用户交互:SpringAI 可以用于构建智能化的用户界面,例如智能客服系统、智能搜索引擎等,提供更加智能化的用户交互体验。
  • 与数据库交互:SpringAI 支持与数据库进行交互,帮助开发者将用户的自然语言查询翻译成 SQL 查询,并执行相应的数据库操作,实现更高级的功能。
  • ai.springai.core核心功能包,包括语义分析、自然语言处理等。
  • ai.springai.openapi与外部人工智能服务集成的包,如大型语言模型的客户端。

4. 使用场景

SpringAI 可以用于以下场景:

  • 构建智能化的用户界面,例如智能客服系统、智能搜索引擎等。
  • 实现自然语言查询功能,如数据库查询、智能推荐等。
  • 辅助开发者快速构建基于自然语言的应用程序。

5. 开发代码举例

下面是一个简单的示例,演示了如何使用 SpringAI 进行自然语言查询的处理:

  1. import org.springframework.web.bind.annotation.PostMapping;
  2. import org.springframework.web.bind.annotation.RequestBody;
  3. import org.springframework.web.bind.annotation.RestController;
  4. import ai.springai.semantic.PGQueryProcessor;
  5. @RestController
  6. public class PGController {
  7. @PostMapping("/pg/query")
  8. public String processPGQuery(@RequestBody String userInput) {
  9. // 使用SpringAI进行语义分析和解析
  10. PGQueryProcessor processor = new PGQueryProcessor();
  11. return processor.process(userInput);
  12. }
  13. }

6 如何结合到当前项目

要将 SpringAI 结合到当前项目中,您可以按照以下步骤进行:

  1. 添加 SpringAI 的依赖到项目的 Maven 配置文件中。
  2. 创建一个 Controller 类,用于处理用户的自然语言输入。
  3. 在 Controller 类中调用 SpringAI 提供的 API 进行语义分析和处理。
  4. 根据业务需求,可以结合其他人工智能技术(如大型语言模型)来实现更高级的功能。

7. 与数据库交互

SpringAI 可以与数据库进行交互,例如,您可以将用户的自然语言查询翻译成 SQL 查询,并执行相应的数据库操作。下面是一个简单的示例:

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.jdbc.core.JdbcTemplate;
  3. import org.springframework.web.bind.annotation.PostMapping;
  4. import org.springframework.web.bind.annotation.RequestBody;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import ai.springai.semantic.PGQueryProcessor;
  7. @RestController
  8. public class PGController {
  9. @Autowired
  10. private JdbcTemplate jdbcTemplate;
  11. @PostMapping("/pg/query")
  12. public String processPGQuery(@RequestBody String userInput) {
  13. // 使用SpringAI进行语义分析和解析
  14. PGQueryProcessor processor = new PGQueryProcessor();
  15. String parsedQuery = processor.process(userInput);
  16. // 将解析后的查询转换为 SQL 查询并执行
  17. String sqlQuery = convertToSQL(parsedQuery);
  18. return jdbcTemplate.queryForObject(sqlQuery, String.class);
  19. }
  20. private String convertToSQL(String parsedQuery) {
  21. // 实现将解析后的查询转换为 SQL 查询的逻辑
  22. // 省略具体实现
  23. return "";
  24. }
  25. }

8. 典型应用

SpringAI 的典型应用包括:
  • 构建智能化的在线客服系统,实现用户问题的自然语言处理和解答。
  • 开发智能搜索引擎,支持用户通过自然语言查询进行信息检索。
  • 设计智能推荐系统,根据用户的自然语言输入推荐相关内容。
举例开发聊天应用:

在项目 pom.xml 中加入 2023.0.1.0 版本 Spring Cloud Alibaba 依赖:

  1. <dependencyManagement>
  2.   <dependencies>
  3.     <dependency>
  4.       <groupId>com.alibaba.cloud</groupId>
  5.       <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  6.       <version>2023.0.1.0</version>
  7.       <type>pom</type>
  8.       <scope>import</scope>
  9.      </dependency>
  10.    </dependencies>
  11. </dependencyManagement>
  12. <dependencies>
  13.   <dependency>
  14.       <groupId>com.alibaba.cloud</groupId>
  15.       <artifactId>spring-cloud-starter-alibaba-ai</artifactId>
  16.   </dependency>
  17. </dependencies>

在 application.yml 配置文件中加入以下配置:

  1. spring:
  2. cloud:
  3. ai:
  4. tongyi:
  5. chat:
  6. options:
  7. # Replace the following key with a valid API-KEY.
  8. api-key: sk-a3d73b1709bf4a178c28ed7c8b3b5axx

编写聊天服务实现类,由 Spring AI 自动注入 ChatClientStreamingChatClientChatClient 屏蔽底层通义大模型交互细节。

  1. @Service
  2. public class TongYiSimpleServiceImpl extends AbstractTongYiServiceImpl {
  3. private final ChatClient chatClient;
  4. private final StreamingChatClient streamingChatClient;
  5. @Autowired
  6. public TongYiSimpleServiceImpl(ChatClient chatClient, StreamingChatClient streamingChatClient) {
  7. this.chatClient = chatClient;
  8. this.streamingChatClient = streamingChatClient;
  9. }
  10. }

提供具体聊天逻辑实现

  1. @Service
  2. public class TongYiSimpleServiceImpl extends AbstractTongYiServiceImpl {
  3. // ......
  4. @Override
  5. public String completion(String message) {
  6. Prompt prompt = new Prompt(new UserMessage(message));
  7. return chatClient.call(prompt).getResult().getOutput().getContent();
  8. }
  9. @Override
  10. public Map<String, String> streamCompletion(String message) {
  11. StringBuilder fullContent = new StringBuilder();
  12. streamingChatClient.stream(new Prompt(message))
  13. .flatMap(chatResponse -> Flux.fromIterable(chatResponse.getResults()))
  14. .map(content -> content.getOutput().getContent())
  15. .doOnNext(fullContent::append)
  16. .last()
  17. .map(lastContent -> Map.of(message, fullContent.toString()))
  18. .block();
  19. log.info(fullContent.toString());
  20. return Map.of(message, fullContent.toString());
  21. }
  22. }

编写 Spring 入口类并启动应用

  1. @SpringBootApplication
  2. public class TongYiApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(TongYiApplication.class);
  5. }
  6. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/707067
推荐阅读
相关标签
  

闽ICP备14008679号