news 2026/2/7 12:02:02

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/2/5 8:05:06

JNPF 钉钉双向同步攻略:组织 / 用户一键打通,触发事件自动联动

企业用钉钉办公&#xff0c;却要在 JNPF 和钉钉间重复录入组织、用户数据&#xff1f; 修改信息后两边不同步&#xff0c;协作效率大打折扣&#xff1f; JNPF 钉钉服务直接破解这一痛点 —— 支持 JNPF 与钉钉双向数据同步&#xff0c;组织架构、用户信息一键互通&#xff0c…

作者头像 李华
网站建设 2026/2/5 18:10:11

Solidity入门(7)- 合约继承

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录1. 为什么需要继承1.1 代码复用的问题1.2 继承的解决方案1.3 继承的实际应用场景2. 单继承2.1 单继承基础语法2.2 访问权限3. 多重继承3.1 多重继承基础3.2 C3线性化…

作者头像 李华
网站建设 2026/2/1 6:24:57

CTF比赛含金量高吗?(非常详细),零基础入门CTF,看这一篇就够了

文章目录 前言 关于我一、基础环境二、常用工具三、Web 安全四、加密解密五、密码爆破六、文件工具七、隐写图片八、隐写音频九、隐写取证十、逆向工具十一、Java 反编译十二、Python反编译十三、PWN二进制 前言 CTF&#xff08;Capture The Flag&#xff09;比赛在网络安全…

作者头像 李华
网站建设 2026/2/7 1:37:46

7.2 深度研究:利用大模型高级检索与分析能力

7.2 深度研究:利用大模型高级检索与分析能力 在上一节课中,我们学习了如何整合NotebookLM、大语言模型和Cursor等工具,构建完整的AI辅助开发工作流。本节课我们将深入探讨如何利用大模型的高级检索与分析能力,提升技术研究和方案设计的效率与质量。 大模型检索能力概述 …

作者头像 李华
网站建设 2026/2/3 8:54:07

restTemplate发送POST

HttpHeaders headers new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);// 2. 构建请求参数&#xff08;与文档示例完全一致&#xff09;Map<String, Object> requestBody new HashMap<>();requestBody.put("grant_type", "…

作者头像 李华