第一学: SpringAi最新版本1.0.0 实现最简单对话(详细小白都会操作java实现ai)

发布时间:2026/7/28 22:13:54
第一学: SpringAi最新版本1.0.0 实现最简单对话(详细小白都会操作java实现ai) SpringAi版本1.0.0结合Redis实现上下文记忆对话(详细)版本在文章开始之前我们需要弄清楚几个依赖目前springchat对话的依赖包括目前需要引入必须保证jdk17 和springboot在3.xx以上目前提供了springboot 和springai相关版本最新版本可以直接去中央参考查看springboot最新支持版本1.0.0-M6spring-ai最新支持版本 1.0.0目前如果仅使用chat功能 使用spring-boot-ai对应的依赖帮助我们快速开发引入依赖此处直接省略创建idea-springboot项目过程?xml version1.0encodingUTF-8?project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersion!--继承SpringBoot父工程--parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.3.5/version/parentgroupIdcn.itcast/groupIdartifactIdmy-spring-ai/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncodingspring-ai.version1.0.0-M6/spring-ai.versionorg.projectlombok.version1.18.36/org.projectlombok.version/propertiesdependencyManagementdependencies!--SpringAIBOM--dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-bom/artifactIdversion${spring-ai.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementdependencies!--lombok 管理--dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion${org.projectlombok.version}/version/dependency!--web--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--单元测试--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.8.34/version/dependency!--SpringAIOpenAI依赖--dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-openai-spring-boot-starter/artifactId/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactId/dependency/dependenciesbuildpluginManagementpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdversion3.13.0/versionconfigurationsource17/source!--depending on your project--target17/target!--depending on your project--/configuration/plugin/plugins/pluginManagement/build/project官方文档对应的示列-提示我们需要通过ChatClient 的实列对应进行访问操作也就是说底层以及集成访问大模型对应连接地址配置类server:port:8099#端口 tomcat:uri-encoding:UTF-8#服务编码 spring:application:name:spring-ai ai:openai:#openai配置 base-url:#api地址 api-key:#OPENAI_API_KEYkey chat:options:model:gpt-3.5-turbo #模型名称如果有翻墙直接申请openai的api直接使用OpenAI官网地址没有对应的key申请地址使用国内的通义千问也行需要修改具体直接创建对应实列直接上官网申请即可通义千问packagecn.itcast.config;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;ConfigurationpublicclassSpringAIConfig{/** * 创建并返回一个ChatClient的Spring Bean实例。 * * param builder 用于构建ChatClient实例的构建者对象 * return 构建好的ChatClient实例 */BeanpublicChatClientchatClient(ChatClient.Builderbuilder){returnbuilder.build();}}服务接口packagecn.itcast.service;publicinterfaceChatService{/** * 普通聊天 * * param question 用户提问 * return 大模型的回答 */Stringchat(Stringquestion);}服务层packagecn.itcast.service.impl;importcn.itcast.service.ChatService;importlombok.RequiredArgsConstructor;importlombok.extern.slf4j.Slf4j;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.stereotype.Service;Slf4jServiceRequiredArgsConstructorpublicclassChatServiceImplimplementsChatService{privatefinalChatClientchatClient;/** * 与聊天客户端进行交互发送用户问题并获取响应内容。 * * param question 用户输入的问题内容 * return 聊天客户端返回的响应内容 */OverridepublicStringchat(Stringquestion){// 调用聊天客户端处理用户问题并获取响应内容StringcontentchatClient.prompt().user(question).call().content();log.info(question: {}, content: {},question,content);returncontent;}}测试packagecn.itcast.service;importjakarta.annotation.Resource;importorg.junit.jupiter.api.Test;importorg.springframework.boot.test.context.SpringBootTest;SpringBootTestclassChatServiceTest{ResourceChatServicechatService;Testpublicvoidchat(){chatService.chat(讲一篇文章);}}目前当前只是简单对话记忆不存在连贯性。下篇文章我将整合redisspring-ai 1.0.0实现我们上下文记忆对话的过程