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已经以重新定义的名称进行回复。

相关新闻

最新新闻

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现 【免费下载链接】serenity The Serenity Operating System 🐞 项目地址: https://gitcode.com/GitHub_Trending/se/serenity 导读 本文以 getopt(3) 手册 为核心&a…

2026/9/25 12:45:43
轻量服务器还是ECS?大促云服务器选购与避坑实战指南

轻量服务器还是ECS?大促云服务器选购与避坑实战指南

每年大促节点,群里永远有人在问同一个问题:“38元的轻量服务器到底怎么抢?为什么我每次点进去都是已售罄?68元直购和99元的ECS我到底选哪个?”作为一个常年帮团队和自己采购云服务器的老用户,我太清楚这种纠…

2026/9/24 14:25:52
为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南 【免费下载链接】agents Multi-harness agentic plugin marketplace for Claude Code, Codex, Cursor, OpenCode, GitHub Copilot, and Google Antigravity 项目地址:…

2026/9/26 3:42:08
PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署 【免费下载链接】PaddleOCR Turn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between i…

2026/9/26 11:37:29
Spring源码解析:构造器注入的类型转换与候选匹配机制

Spring源码解析:构造器注入的类型转换与候选匹配机制

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/9/26 4:08:27
openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由 【免费下载链接】openai-agents-python A lightweight, powerful framework for multi-agent workflows 项目地址: https://gitcode.com/GitHub_Trending/op/openai-agents-pyth…

2026/9/25 15:49:36

日新闻

周新闻