当前位置: 首页 > news >正文

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=你是谁”,看到返回结果如下:
http://www.cnnetsun.cn/news/84004.html

相关文章:

  • 终极Flutter滚动布局指南:打造流畅动态Header效果
  • 程序员必看:大模型基础原理与GPU并行训练指南(建议收藏)
  • 30分钟快速部署企业级智能管理平台:SmartAdmin完整安装指南
  • 含中间直流的三相电力电子变压器PET仿真模型(Simulink仿真实现)
  • 【面试精选】26年最全网络安全面试,华为大佬带你快速通关面试!!吃透面试成功率96%
  • 转录组研究攻略|常见可视化结果解读
  • 新增AI引擎!快快网络联合集美大学共建工业智能与网络安全创新实验室
  • 5.3 从零构建MCP Server:实现文件处理与数据库访问
  • PapersGPT for Zotero 完整安装与使用指南:让文献管理更智能
  • 7.3 任务分解与管理:利用Cursor Memory Bank和Claude Code自定义命令
  • 中美文化对 AI 意识觉醒的根本差异:文明基因与 AGI 时代的未来路径
  • 豆包AI手机动了谁的“生态命门”?
  • 万字长文,保姆级教程!从零教你优雅开发复杂AI Agent,从入门到精通,看这篇就够了!
  • 标注好的胃病识别数据集,可识别食管炎,胃炎,胃出血,健康,息肉,胃溃疡等常见疾病,支持yolo, coco json,pascal voc xml格式的标注
  • 轻松上手:零基础使用AI智能图表工具制作专业可视化图表
  • 201React-Query:useQuery基本使用
  • 开源可定制的订水小程序系统详解 带完整的搭建部署教程以及搭建指南
  • Qwen-Image-Edit-2509实现任意尺寸图像智能编辑
  • 收藏!企业AI转型的真相:88%已应用,但93%未规模化,问题出在哪?
  • 4步构建高性能SVG动画:Glide与Lottie的深度集成方案
  • WordPress编辑器优化终极指南:让Gutenberg加速运行
  • Qwen3-VL-30B部署实战:GPU配置与推理优化
  • (N_121)基于微信小程序网上书城系统
  • OpenXR Toolkit:3大核心功能让你的VR应用性能翻倍
  • Web Service 接口测试,So easy~
  • Qwen3-VL-30B部署全指南:GPU配置与推理优化
  • Qwen3-8B大模型快速上手与部署实践
  • 开源大模型新选择:Anything-LLM镜像在GPU算力环境下的性能优化
  • PaddleDetection + Dify智能体平台:打造自动化目标检测SaaS服务
  • AIGC 商用实战派:集之互动用 “高可控” 接住品牌真需求