Spring AI(3) :对话机器人开发快速入门

📅 2026/7/23 2:01:44 👁️ 阅读次数 📝 编程学习
Spring AI(3) :对话机器人开发快速入门

本章代码已分享至Gitee:https://gitee.com/lengcz/ai-study.git

文章目录

  • 快速入门AI开发
  • 准备工作和环境
  • 创建项目
  • 阻塞式chat
  • 流式chat
  • 给System设置名称

快速入门AI开发

本章讲解如何使用spring ai 快速入门对话机器人的开发。

准备工作和环境

由于spring AI 要求的jdk 最低为17,所以这里环境为jdk17
本地使用ollama部署了deepseek-r1:7b,可以根据自己电脑的配置决定。
项目运行时,Idea版本如果过低会报错,如运行时发生错误,请注意检查Idea版本,这里使用的Idea 2024

demo使用ollama,如果自己使用其他模型,请注意使用对应的依赖和配置。

创建项目

  1. 创建maven项目

  2. 引入依赖

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.4.13</version><relativePath/><!-- lookup parent from repository --></parent><groupId>com.lengcz</groupId><artifactId>ai-study</artifactId><version>0.0.1-SNAPSHOT</version><name>ai-study</name><description>ai study</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>17</java.version><spring-ai.version>1.0.0-M6</spring-ai.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-ollama-spring-boot-starter</artifactId></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!--建议不用使用自带的lombok,而是由自己指定版本--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.22</version></dependency></dependencies><dependencyManagement><dependencies><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><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

3 启动类

importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublicclassAiStudyApplication{publicstaticvoidmain(String[]args){SpringApplication.run(AiStudyApplication.class,args);}}

4 配置yaml文件

spring:application:name:ai-studyai:ollama:base-url:http://localhost:11434chat:model:deepseek-r1:7b
  1. 创建配置类
importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.ai.ollama.OllamaChatModel;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;@ConfigurationpublicclassCommonConfiguration{@BeanpublicChatClientchatClient(OllamaChatModelollamaChatModel){returnChatClient.builder(ollamaChatModel).build();}}

阻塞式chat

编写一个 controller,试一下验证聊天

packagecom.lengcz.ai.controller;importlombok.RequiredArgsConstructor;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importreactor.core.publisher.Flux;@RequiredArgsConstructor//有参构造器@RestController@RequestMapping("/ai")publicclassChatController{privatefinalChatClientchatClient;@RequestMapping("/chat")publicStringchat(Stringprompt){returnchatClient.prompt().user(prompt).call().content();//call() 表示阻塞调用}}

启动后,请求一下接口测试,可以看到接口稍等片刻,瞬间返回了所有内容,这就是阻塞式的返回。

流式chat

流式返回,将前面的call改成stream再验证一下接口,可以看到返回数据以流的方式输出。

//produces = "text/html;charset=utf-8;" 指定编码,防止中文乱码@RequestMapping(value="/stream_chat",produces="text/html;charset=utf-8;")publicFlux<String>stream_chat(Stringprompt){returnchatClient.prompt().user(prompt).stream().content();//stream() 表示流式输出}

给System设置名称

importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.ai.ollama.OllamaChatModel;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;@ConfigurationpublicclassCommonConfiguration{@BeanpublicChatClientchatClient(OllamaChatModelollamaChatModel){returnChatClient.builder(ollamaChatModel).defaultSystem("你是一个热心的智能助手,你的名字叫小可爱,请以小可爱的名义回答用户的问题。").build();}}


再次请求接口,可以看到AI已经以重新定义的名称进行回复。