跳到主要内容

开始第一个对话

首先我们要实现的是在项目中实现可以和 ai 模型进行简单对话的功能,至于后续的高级东西,比如:RAG、Function Calling 等后面会详细的介绍

既然是 SpringBoot 项目,Spring 团队也专门出了组件来更加方便的使用 AI 功能,这就是 SpringAI。

SpringAI

官网地址:https://docs.spring.io/spring-ai/reference/index.html

Spring AI 是 Spring 团队推出的一个用于集成 AI 能力的框架,目标是让开发者更方便地在 Spring 应用中使用大语言模型(LLM),比如 OpenAI、DeepSeek、Azure OpenAI、ChatGLM 等。

既然现在出了 SpringAI 专门解决这个事,所以第一步就是要把 SpringAI 依赖到项目中


SpingAI 依赖

SpringAI 已经出了正式版本,而大麦ai项目也是用的正式版本,也就是说是完全可以在正式生产环境中使用的!

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
<spring.boot.version>3.5.0</spring.boot.version>
<spring-ai.version>1.0.0</spring-ai.version>
<lombok.version>1.18.20</lombok.version>
<mybatis-plus.version>3.5.7</mybatis-plus.version>
<fastjson.version>1.2.83</fastjson.version>
<hutool.version>5.8.25</hutool.version>
<easy-es.version>3.0.0</easy-es.version>
<elasticsearch.version>7.17.28</elasticsearch.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

SpringAI 支持多种 AI,例如:OpenAI、DeepSeek 等。而这里我们就可以使用国内最火的 ai:DeepSeek


DeepSeek 依赖

既然使用 DeepSeek,那肯定要把 DeepSeek 的依赖也要集成到项目中

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-deepseek</artifactId>
</dependency>

ChatClient

接下来进行要使用 DeepSeek 的功能了,SpringAI 将一系列的 api 操作进行了封装让我们使用起来非常的简单,我们只要使用一个叫 chatClient 的东西就可以了

什么是 ChatClient

作用

ChatClient 是一个用于与 LLM 进行聊天(对话)交互的客户端封装。
它屏蔽了很多底层 HTTP 调用、请求构造、响应解析等细节,开发者只需要通过它简单的 API,就可以发起问题并得到 AI 的回答。

核心功能

  • 自动注入底层模型(如 OpenAI、DeepSeek 等)
  • 支持设置默认的 system prompt(系统角色描述)
  • 支持拦截器(advisor)扩展,比如记录日志、预处理消息等
  • 提供同步/异步的聊天接口

ChatClient 的配置

既然是要使用 ChatClient,那么肯定先要把它给配置出来

org.javaup.ai.config.DaMaiAiAutoConfiguration

@Bean
public ChatClient chatClient(DeepSeekChatModel model) {
return ChatClient
.builder(model)
.build();
}
  • public ChatClient chatClient(DeepSeekChatModel model)

    • 定义一个返回 ChatClient 的 Bean 方法,并接收一个 DeepSeekChatModel 对象作为参数。
    • DeepSeekChatModel 是 Spring AI 中对 DeepSeek 模型的封装,负责和 DeepSeek API 通信。
  • ChatClient.builder(model)

    • 创建一个 ChatClient 的构建器,告诉客户端使用哪个模型。
  • .build()

    • 构建最终的 ChatClient 实例。

进行对话

ChatClient 创建出来后,我们就可以来使用了。写一个Controller

@RestController
@RequestMapping("/simple")
public class SimpleChatController {

@Resource
private ChatClient chatClient;


@RequestMapping(value = "/chat", produces = "text/html;charset=utf-8")
public String chat(@RequestParam("prompt") String prompt) {
return chatClient.prompt()
//传入用户的对话
.user(prompt)
//进行调用
.call()
//返回内容
.content();
}
}

用浏览器调用下

恭喜!到这里就已经实现了在 SpringBoot 项目中进行AI对话的功能,是不是超级的简单!