news 2026/4/17 20:35:20

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/4/18 9:18:50

Pintr图像艺术化工具:从照片到专业线条画的终极转换方案

Pintr图像艺术化工具&#xff1a;从照片到专业线条画的终极转换方案 【免费下载链接】pintr Create single line illustrations from your pictures. Get a drawing, SVG or coordinates for a CNC. 项目地址: https://gitcode.com/gh_mirrors/pi/pintr 你是否曾想过将普…

作者头像 李华
网站建设 2026/4/18 0:02:29

Stable Diffusion 3.5本地部署与使用指南

Stable Diffusion 3.5本地部署与使用指南 2024年10月&#xff0c;Stability AI 推出 Stable-Diffusion-3.5-FP8 —— 一款将性能、效率与画质平衡推向新高度的文生图模型。这不是一次简单的版本更新&#xff0c;而是通过引入 FP8 精度量化技术&#xff0c;在不牺牲图像质量的前…

作者头像 李华
网站建设 2026/4/18 6:28:03

收藏!2025 AI最大风口:大模型应用开发,程序员逆袭正当时

2025年的AI行业&#xff0c;机遇早已不是模糊的概念——应用层就是那片肉眼可见的黄金赛道&#xff01;字节跳动7大团队all in Agent&#xff0c;研发资源全力倾斜&#xff1b; 大模型相关岗位同比暴增69%&#xff0c;头部企业核心岗年薪轻松破百万&#xff1b; 腾讯、京东、百…

作者头像 李华
网站建设 2026/4/18 6:28:26

FF14 XIVLauncher启动器使用全攻略:从新手到大神的进阶指南

FF14 XIVLauncher启动器使用全攻略&#xff1a;从新手到大神的进阶指南 【免费下载链接】FFXIVQuickLauncher Custom launcher for FFXIV 项目地址: https://gitcode.com/GitHub_Trending/ff/FFXIVQuickLauncher 还在为每次登录FF14都要重复输入账号密码而烦恼吗&#x…

作者头像 李华
网站建设 2026/4/18 6:25:06

Flutter Dynamic Widget 终极指南:用 JSON 构建动态 UI

Flutter Dynamic Widget 终极指南&#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_mir…

作者头像 李华