SpringAI 技术解析

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 进行自然语言查询的处理:

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); 
} 
}

6 如何结合到当前项目

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

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

7. 与数据库交互

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 "";
    }
}

8. 典型应用

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

在项目 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>

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

spring:
  cloud:
    ai:
      tongyi:
        chat:
          options:
            # Replace the following key with a valid API-KEY.
            api-key: sk-a3d73b1709bf4a178c28ed7c8b3b5axx

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

@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());
  }

}

编写 Spring 入口类并启动应用

@SpringBootApplication
public class TongYiApplication {
  public static void main(String[] args) {
    SpringApplication.run(TongYiApplication.class);
  }
}
  • 23
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值