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

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 2024demo使用ollama如果自己使用其他模型请注意使用对应的依赖和配置。创建项目创建maven项目引入依赖?xml version1.0 encodingUTF-8?projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.4.13/versionrelativePath/!-- lookup parent from repository --/parentgroupIdcom.lengcz/groupIdartifactIdai-study/artifactIdversion0.0.1-SNAPSHOT/versionnameai-study/namedescriptionai study/descriptionurl/licenseslicense//licensesdevelopersdeveloper//developersscmconnection/developerConnection/tag/url//scmpropertiesjava.version17/java.versionspring-ai.version1.0.0-M6/spring-ai.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-ollama-spring-boot-starter/artifactId/dependencydependencygroupIdcom.mysql/groupIdartifactIdmysql-connector-j/artifactIdscoperuntime/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactId/dependency!--建议不用使用自带的lombok而是由自己指定版本--dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.22/version/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-bom/artifactIdversion${spring-ai.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project3 启动类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创建配置类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//有参构造器RestControllerRequestMapping(/ai)publicclassChatController{privatefinalChatClientchatClient;RequestMapping(/chat)publicStringchat(Stringprompt){returnchatClient.prompt().user(prompt).call().content();//call() 表示阻塞调用}}启动后请求一下接口测试可以看到接口稍等片刻瞬间返回了所有内容这就是阻塞式的返回。流式chat流式返回将前面的call改成stream再验证一下接口可以看到返回数据以流的方式输出。//produces text/html;charsetutf-8; 指定编码防止中文乱码RequestMapping(value/stream_chat,producestext/html;charsetutf-8;)publicFluxStringstream_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已经以重新定义的名称进行回复。