智慧乡村管理系统接入deepseek大模型实现AI智能问答最近刚刚完成的项目Springboot Vue 技术栈搭建采用 MySQL 数据库运行于 jdk1.8 和 mysql8.0 环境。亮点创新接入deepseek大模型系统涵盖农产品、乡村项目、线下活动等多个核心业务的全流程管理。从农产品的展示、预约到乡村项目的招标、竞标申请再到线下活动的发布与预约形成完整闭环功能模块前台面向用户有登录注册入口首页展示轮播图等内容还包括农产品展、乡村项目、线下活动等模块支持留言反馈、来访登记个人中心可编辑信息及查看预约竞标等。后台服务管理员含主页、数据报表、系统管理涵盖用户等管理、信息管理涉及活动等各类信息以及产品字典管理产品分类管理。11DeepSeek 大模型接入传统的 Spring Boot Vue 业务系统实现了从“信息化管理”到“智能化服务”的跨越。以下是该系统的核心架构代码实现重点展示DeepSeek AI 问答集成、业务流程闭环以及前后端交互。️ 1. 系统技术架构概览层级技术栈说明前端Vue 2/3 Element UI Axios响应式界面包含大屏数据可视化、用户中心、后台管理后端Spring Boot 2.7 (JDK 1.8)核心业务逻辑RESTful APIAI 代理层数据库MySQL 8.0存储用户、产品、项目、活动、日志等结构化数据AI 引擎DeepSeek API (HTTP Client)智能问答、政策咨询、农产品知识科普、竞标建议部署环境JDK 1.8 Tomcat/Nginx兼容老旧服务器环境稳定运行 2. 核心代码实现 后端部分 (Spring Boot)1.pom.xml依赖配置引入 DeepSeek 调用所需的 HTTP 客户端和 JSON 处理库。dependencies!-- Spring Boot Web --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- MyBatis Plus (简化数据库操作) --dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.5.2/version/dependency!-- MySQL Driver --dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependency!-- Lombok --dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependency!-- Hutool (HTTP 请求工具调用 DeepSeek) --dependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.8.16/version/dependency!-- Jackson --dependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-databind/artifactId/dependency/dependencies2. 配置文件application.ymlserver:port:8080spring:datasource:url:jdbc:mysql://localhost:3306/smart_village?useSSLfalseserverTimezoneUTCcharacterEncodingutf-8username:rootpassword:your_passworddriver-class-name:com.mysql.cj.jdbc.Driver# DeepSeek 配置deepseek:api-key:sk-your-deepseek-api-key# 替换为真实 Keybase-url:https://api.deepseek.com/v1model:deepseek-chatsystem-prompt:你是一个智慧乡村助手熟悉农产品知识、乡村项目招标流程、线下活动安排。请根据以下上下文回答用户问题。如果涉及具体数据如库存、报名情况请提示用户查看系统实时数据。mybatis-plus:configuration:map-underscore-to-camel-case:true3. DeepSeek AI 服务层 (service/AiConsultService.java)核心亮点封装 DeepSeek 接口支持上下文记忆可选和系统提示词注入。packagecom.village.service;importcn.hutool.http.HttpRequest;importcn.hutool.json.JSONArray;importcn.hutool.json.JSONObject;importcn.hutool.json.JSONUtil;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.stereotype.Service;importjava.util.ArrayList;importjava.util.List;ServicepublicclassAiConsultService{Value(${deepseek.api-key})privateStringapiKey;Value(${deepseek.base-url})privateStringbaseUrl;Value(${deepseek.model})privateStringmodel;Value(${deepseek.system-prompt})privateStringsystemPrompt;/** * 调用 DeepSeek 进行智能问答 * param userQuestion 用户问题 * param context 可选的业务上下文如当前浏览的农产品信息 * return AI 回答 */publicStringchat(StringuserQuestion,Stringcontext){ListJSONObjectmessagesnewArrayList();// 1. 系统人设JSONObjectsystemMsgnewJSONObject();systemMsg.put(role,system);systemMsg.put(content,systemPrompt);messages.add(systemMsg);// 2. 注入业务上下文 (RAG 简单实现)if(context!null!context.isEmpty()){JSONObjectcontextMsgnewJSONObject();contextMsg.put(role,user);contextMsg.put(content,参考背景信息context\n用户问题userQuestion);messages.add(contextMsg);}else{// 无上下文直接问JSONObjectuserMsgnewJSONObject();userMsg.put(role,user);userMsg.put(content,userQuestion);messages.add(userMsg);}// 3. 构建请求体JSONObjectrequestBodynewJSONObject();requestBody.put(model,model);requestBody.put(messages,messages);requestBody.put(temperature,0.7);try{StringresponseStrHttpRequest.post(baseUrl/chat/completions).header(Authorization,Bearer apiKey).header(Content-Type,application/json).body(requestBody.toString()).timeout(10000)// 10秒超时.execute().body();JSONObjectresponseJsonJSONUtil.parseObj(responseStr);JSONArraychoicesresponseJson.getJSONArray(choices);if(choices!nullchoices.size()0){returnchoices.getJSONObject(0).getJSONObject(message).getStr(content);}returnAI 服务暂时繁忙请稍后再试。;}catch(Exceptione){e.printStackTrace();return连接 AI 服务失败e.getMessage();}}}4. 业务控制器 (controller/ConsultController.java)提供统一的 AI 问答接口并可结合数据库查询增强回答。packagecom.village.controller;importcom.village.service.AiConsultService;importcom.village.service.ProductService;// 假设的产品服务importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.*;importjava.util.HashMap;importjava.util.Map;RestControllerRequestMapping(/api/ai)CrossOrigin(origins*)// 允许前端跨域publicclassConsultController{AutowiredprivateAiConsultServiceaiConsultService;AutowiredprivateProductServiceproductService;// 用于获取实时数据作为上下文/** * 智能问答接口 * param question 用户问题 * param categoryId 可选当前所在的分类ID用于获取上下文 */GetMapping(/chat)publicMapString,Objectchat(RequestParamStringquestion,RequestParam(requiredfalse)StringcategoryId){MapString,ObjectresultnewHashMap();Stringcontext;// 如果用户在农产品详情页提问自动注入该产品信息作为上下文if(categoryId!null){// 伪代码从数据库获取产品详情// String productInfo productService.getProductDetails(categoryId);// context 当前用户正在浏览产品 productInfo;context用户正在浏览分类ID为 categoryId 的相关内容。;}StringansweraiConsultService.chat(question,context);result.put(success,true);result.put(answer,answer);result.put(timestamp,System.currentTimeMillis());returnresult;}}5. 实体类示例 (entity/Product.java)packagecom.village.entity;importcom.baomidou.mybatisplus.annotation.TableName;importlombok.Data;DataTableName(village_product)publicclassProduct{privateLongid;privateStringname;privateStringcategory;// 产品分类privateDoubleprice;privateIntegerstock;privateStringdescription;privateStringimageUrl;privateStringstatus;// 上架/下架} 前端部分 (Vue Element UI)1. AI 智能问答悬浮窗组件 (components/AiChatBox.vue)亮点全局悬浮支持流式输出模拟简化版为一次性返回界面友好。template div classai-chat-container !-- 悬浮按钮 -- div classchat-toggle-btn clicktoggleChat v-if!isOpen i classel-icon-cpu/i spanAI 助手/span /div !-- 聊天窗口 -- div classchat-window v-showisOpen div classchat-header span 智慧乡村 AI 助手 (DeepSeek)/span i classel-icon-close clicktoggleChat/i /div div classchat-body refchatBody div v-for(msg, index) in messages :keyindex :class[message, msg.role user ? user-msg : ai-msg] div classavatar{{ msg.role user ? : }}/div div classcontent{{ msg.content }}/div /div div v-ifloading classmessage ai-msg div classavatar/div div classcontent typingAI 思考中.../div /div /div div classchat-input-area input typetext v-modelinputText placeholder问问我关于农产品、项目招标或活动... keyup.entersendMessage :disabledloading / button clicksendMessage :disabledloading发送/button /div /div /div /template script import axios from axios; export default { data() { return { isOpen: false, inputText: , loading: false, messages: [ { role: ai, content: 您好我是智慧乡村 AI 助手。我可以为您解答农产品知识、指导项目竞标流程或查询线下活动信息。请问有什么可以帮您 } ] }; }, methods: { toggleChat() { this.isOpen !this.isOpen; if (this.isOpen) { this.$nextTick(() this.scrollToBottom()); } }, async sendMessage() { if (!this.inputText.trim()) return; const userMsg this.inputText; this.messages.push({ role: user, content: userMsg }); this.inputText ; this.loading true; this.scrollToBottom(); try { // 调用后端接口 const res await axios.get(/api/ai/chat, { params: { question: userMsg } }); if (res.data.success) { this.messages.push({ role: ai, content: res.data.answer }); } else { this.messages.push({ role: ai, content: 抱歉我遇到了一些问题请稍后再试。 }); } } catch (error) { this.messages.push({ role: ai, content: 网络错误无法连接 AI 服务。 }); } finally { this.loading false; this.$nextTick(() this.scrollToBottom()); } }, scrollToBottom() { this.$nextTick(() { const container this.$refs.chatBody; if (container) container.scrollTop container.scrollHeight; }); } } }; /script style scoped .ai-chat-container { position: fixed; bottom: 20px; right: 20px; z-index: 9999; } .chat-toggle-btn { background: #409EFF; color: white; padding: 12px 20px; border-radius: 30px; cursor: pointer; box-shadow: 0 4px 12px rgba(0,0,0,0.15); display: flex; align-items: center; gap: 8px; } .chat-window { width: 350px; height: 500px; background: white; border-radius: 12px; box-shadow: 0 5px 20px rgba(0,0,0,0.2); display: flex; flex-direction: column; overflow: hidden; margin-bottom: 10px; } .chat-header { background: #f5f7fa; padding: 15px; font-weight: bold; border-bottom: 1px solid #eee; display: flex; justify-content: space-between; align-items: center; } .chat-body { flex: 1; padding: 15px; overflow-y: auto; background: #fff; } .message { display: flex; margin-bottom: 15px; align-items: flex-start; } .user-msg { flex-direction: row-reverse; } .avatar { font-size: 20px; margin: 0 8px; } .content { background: #f0f2f5; padding: 10px 14px; border-radius: 8px; max-width: 80%; font-size: 14px; line-height: 1.5; word-wrap: break-word; } .user-msg .content { background: #409EFF; color: white; } .chat-input-area { padding: 10px; border-top: 1px solid #eee; display: flex; gap: 10px; } .chat-input-area input { flex: 1; padding: 8px; border: 1px solid #ddd; border-radius: 4px; outline: none; } .chat-input-area button { background: #409EFF; color: white; border: none; padding: 8px 15px; border-radius: 4px; cursor: pointer; } .chat-input-area button:disabled { background: #ccc; } .typing { font-style: italic; color: #999; } /style2. 主布局引入 (App.vue或Layout.vue)template div idapp router-view / !-- 全局挂载 AI 助手 -- AiChatBox / /div /template script import AiChatBox from ./components/AiChatBox.vue; export default { components: { AiChatBox } } /script️ 3. 数据库设计简述 (MySQL 8.0)为了支撑上述功能核心表结构如下-- 1. 农产品表CREATETABLEvillage_product(idBIGINTPRIMARYKEYAUTO_INCREMENT,nameVARCHAR(100)NOTNULL,category_idINT,priceDECIMAL(10,2),stockINT,descriptionTEXT,image_urlVARCHAR(255),statusTINYINTDEFAULT1,-- 1:上架 0:下架create_timeDATETIMEDEFAULTCURRENT_TIMESTAMP);-- 2. 乡村项目表 (招标/竞标)CREATETABLEvillage_project(idBIGINTPRIMARYKEYAUTO_INCREMENT,titleVARCHAR(200)NOTNULL,typeVARCHAR(50),-- 招标类型budgetDECIMAL(12,2),deadlineDATETIME,statusVARCHAR(20),-- 进行中/已结束requirementsTEXT);-- 3. 线下活动表CREATETABLEvillage_activity(idBIGINTPRIMARYKEYAUTO_INCREMENT,activity_nameVARCHAR(100),locationVARCHAR(100),start_timeDATETIME,max_participantsINT,current_countINTDEFAULT0,detailsTEXT);-- 4. 用户预约/竞标记录表CREATETABLEuser_booking(idBIGINTPRIMARYKEYAUTO_INCREMENT,user_idBIGINT,target_typeVARCHAR(20),-- PRODUCT, PROJECT, ACTIVITYtarget_idBIGINT,statusVARCHAR(20),-- PENDING, CONFIRMEDcreate_timeDATETIMEDEFAULTCURRENT_TIMESTAMP); 4. 项目运行与亮点总结运行步骤数据库在 MySQL 8.0 中执行建表语句导入基础字典数据。后端修改application.yml中的数据库账号密码和DeepSeek API Key。使用 JDK 1.8 运行mvn spring-boot:run。前端npm install安装依赖。修改vue.config.js或.env中的后端代理地址。npm run serve启动。 核心创新点总结AI 赋能业务闭环不仅仅是简单的问答而是结合业务场景如用户在“项目招标”页面提问AI 自动加载招标规则作为上下文实现了场景化智能辅助。全流程数字化打通了从“看农产品展示”到“动预约/竞标”再到“管后台数据报表”的全链路。技术栈稳健基于成熟的 Spring Boot Vue 架构兼容 JDK 1.8适合乡村基层现有的服务器环境易于部署和维护。可扩展性预留了context接口未来可轻松接入 RAG检索增强生成连接向量数据库让 AI 掌握更详细的本地乡村档案。以上文字及代码仅供参考。