news 2026/7/24 9:31:18

SpringAI入门代码--从0到1搭建DeepSeek对话案例

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
SpringAI入门代码--从0到1搭建DeepSeek对话案例

说明:这里使用SpringBoot 3.5.8版本、JDK17版本、Maven3.9.11版本。
创建一个如下的SpringBoot项目,下面说明如何配置及编写代码。

  1. 配置pom.xml文件,增加如下依赖
<!-- 导入 Spring AI BOM,用于统一管理 Spring AI 依赖的版本, --><dependencyManagement><dependencies><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-bom</artifactId><version>1.0.0-SNAPSHOT</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-model-deepseek</artifactId></dependency></dependencies><!-- 声明仓库, 用于获取 Spring AI 以及相关预发布版本--><repositories><repository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/snapshot</url><releases><enabled>false</enabled></releases></repository><repository><name>Central Portal Snapshots</name><id>central-portal-snapshots</id><url>https://central.sonatype.com/repository/maven-snapshots/</url><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories>
  1. 在resources下创建一个application.properties文件,文件内容如下:
#配置项目名称spring.application.name=SpringAIQuickStart#配置端口server.port=8080#配置 Deepseek的基础URL、密钥和使用模型#配置 URLspring.ai.deepseek.base-url=https://api.deepseek.com#配置密钥,自己去创建spring.ai.deepseek.api-key=sk-xxxxxxxxxxxxxxxxx#配置使用模型名称spring.ai.deepseek.chat.options.model=deepseek-chat# 介于0和2之间,0表示随机性最小,2表示随机性最大。spring.ai.deepseek.chat.options.temperature=0.9
  1. 创建一个com.test.controller包,并创建一个ChatController.java文件
    具体内容下入:
packagecom.test.controller;importorg.springframework.ai.deepseek.DeepSeekChatModel;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/firstAi")publicclassChatController{@AutowiredprivateDeepSeekChatModelchatModel;@GetMapping("/generate")publicStringgenerate(@RequestParam(value="message",defaultValue="你好")Stringmessage){Stringresult=chatModel.call(message);//模型返回的内容System.out.println(result);returnresult;}}
  1. 编写启动类,创建一个SpringBootAIApplication.java文件,内容如下:
packagecom.test.controller;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublicclassSpringBootAIApplication{publicstaticvoidmain(String[]args){SpringApplication.run(SpringBootAIApplication.class,args);}}
  1. 启动项目并测试,启动项目后,浏览器输入“http://localhost:8080/firstAi/generate?message=你是谁”,看到返回结果如下:
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/24 7:38:23

Flutter动态UI终极指南:JSON驱动的UI构建革命

Flutter动态UI终极指南&#xff1a;JSON驱动的UI构建革命 【免费下载链接】dynamic_widget A Backend-Driven UI toolkit, build your dynamic UI with json, and the json format is very similar with flutter widget code. 项目地址: https://gitcode.com/gh_mirrors/dy/d…

作者头像 李华
网站建设 2026/7/24 6:52:13

速藏!程序员/小白必看:传统技术顶不住?大模型才是新饭碗

3年前&#xff0c;你或许能用Java搞定分布式系统、用Python玩转数据分析&#xff0c;就在团队里站稳脚跟&#xff1b;2年前&#xff0c;Spring Cloud微服务架构熟稔于心&#xff0c;薪资谈判都多三分底气。但2024年的今天&#xff0c;只抱着这些“老本行”的程序员会发现&#…

作者头像 李华
网站建设 2026/7/24 6:53:40

oracle的model子句让sql像excel一样灵活2

Excel中当前sheet进行计算时&#xff0c;可以使用其他sheet中的表格数据。嗨&#xff0c;你别说&#xff0c;model也可以。还以之前的测试数据为例&#xff0c;假如这些测试数据分别是电表度数、水表吨数&#xff0c;要计算降本金额&#xff0c;还需要电单价、水单价。单价测试…

作者头像 李华
网站建设 2026/7/24 3:17:24

SAP开发被调用的:webservice接口教程

摘要&#xff1a;本文详细介绍了在SAP系统中创建RFC函数和Web Service的完整流程。首先在SE80中创建远程启用的函数模块&#xff0c;定义输入/输出参数表结构并编写处理逻辑。然后通过企业服务向导创建Web Service&#xff0c;选择Function Module作为服务源并设置SOAP配置。最…

作者头像 李华
网站建设 2026/7/24 7:38:22

2026年护网就在眼前了,蓝队护网签合同一定要擦亮眼睛

2024年护网的前期准备已经陆陆续续在进行了&#xff0c;很多厂商、安全团队开始向市面上招聘蓝队护网成员了&#xff0c;价格从800-4000不等。关于护网的面试我前面已经说过了。 乙方单方违约扣50%违约金 今天聊聊护网合同的事情&#xff0c;护网的合同可谓处处是坑&#xff0c…

作者头像 李华
网站建设 2026/7/24 9:10:36

【期末复习01-参考答案】-算法题ProgramDesign

文章目录一、程序分析题(20分)项目结构分析题01分析题02分析题03分析题04二、程序改错题(20分)项目结构改错题01改错题02改错题03改错题04改错题05改错题06一、程序分析题(20分) 项目结构 分析题01 代码运行结果 分析题02 代码运行结果 分析题03 代码运行结果 分析题04 代码…

作者头像 李华