博客网站(springboot)整合deepseek实现在线调用

🎉🎉🎉🎉🎉🎉
欢迎访问的个人博客:https://swzbk.site/,加好友,拉你入福利群
🎉🎉🎉🎉🎉🎉

1、deepseek介绍

DeepSeek(深度求索)是一家成立于2023年7月的中国人工智能公司,由量化资管巨头幻方量化创立,专注于开发高性能大语言模型(LLM)及相关技术。今年年初以其模型高性能能力而备受关注,目前国内企业正在广泛接入使用。

2、vue前端界面

  • 加了一个nav页签:深度探索
    在这里插入图片描述

  • 页面代码如下:

<template><div class="chat-wrapper"><div class="chat-container"><div class="chat-header"><h2>DeepSeek 对话</h2></div><div class="chat-messages" ref="chatMessages"><div v-for="(message, index) in chatMessages" :key="index" :class="['message', message.includes('你:') ? 'user-message' : 'bot-message']"><span>{{ message }}</span></div></div><div class="input-container"><input v-model="userInput" type="text" placeholder="输入你的问题" @keydown.enter="sendMessage"><button @click="sendMessage">发送</button></div></div></div>
</template><script>
export default {data() {return {userInput: '',chatMessages: []};},methods: {async sendMessage() {if (this.userInput.trim() === '') return;// 显示用户消息this.chatMessages.push(`你: ${this.userInput}`);try {//这里后面换成服务器ipconst response = await fetch('http://localhost:8090/deepseek', {method: 'POST',headers: {'Content-Type': 'application/json'},body: JSON.stringify({ prompt: this.userInput })});const data = await response.json();const answer = data.answer;// 显示 DeepSeek 的回答this.chatMessages.push(`DeepSeek: ${answer}`);} catch (error) {console.error('请求出错:', error);this.chatMessages.push('请求出错,请稍后再试');}// 清空输入框this.userInput = '';this.$nextTick(() => {this.$refs.chatMessages.scrollTop = this.$refs.chatMessages.scrollHeight;});}}
};
</script><style scoped>
.chat-wrapper {display: flex;justify-content: center;align-items: center;min-height: 100vh;background-color: #f4f4f9;
}.chat-container {width: 80%;height: 80vh;border-radius: 12px;box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);background-color: #fff;overflow: hidden;display: flex;flex-direction: column;
}.chat-header {background-color: #007bff;color: #fff;padding: 15px 20px;text-align: center;flex-shrink: 0;
}.chat-header h2 {margin: 0;font-size: 1.3rem;
}.chat-messages {flex-grow: 1;overflow-y: auto;padding: 20px;display: flex;flex-direction: column;
}.message {padding: 10px 15px;border-radius: 8px;margin-bottom: 10px;max-width: 80%;word-wrap: break-word;
}.user-message {background-color: #e0f7fa;align-self: flex-end;color: #212121;
}.bot-message {background-color: #f1f8e9;align-self: flex-start;color: #212121;
}.input-container {display: flex;padding: 15px 20px;border-top: 1px solid #e0e0e0;flex-shrink: 0;
}.input-container input {flex: 1;padding: 10px;border: 1px solid #ccc;border-radius: 6px;margin-right: 10px;font-size: 1rem;
}.input-container button {padding: 10px 20px;background-color: #007bff;color: #fff;border: none;border-radius: 6px;cursor: pointer;font-size: 1rem;transition: background-color 0.2s ease;
}.input-container button:hover {background-color: #0056b3;
}
</style>

3、deepseek api申请

deepseek api
申请完之后,需要充值使用,有的新人会有免费使用次数,我这里充值了5米,测试用够了。

4、springboot接口增加

  • DeepSeekController接口
package top.naccl.controller;
import com.fasterxml.jackson.databind.JsonNode;
import okhttp3.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import top.naccl.model.dto.DeepSeekRequest;
import top.naccl.model.dto.DeepSeekResponse;
import com.fasterxml.jackson.databind.ObjectMapper;import java.io.IOException;
import java.util.concurrent.TimeUnit;@RestController
public class DeepseekController {private static final ObjectMapper objectMapper = new ObjectMapper();//换成自己的deepseek apiprivate static final String DEEPSEEK_API_KEY = "sk-xxxx";private static final String DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions";@PostMapping("/deepseek")public DeepSeekResponse handleDeepSeekRequest(@RequestBody DeepSeekRequest request) throws IOException {OkHttpClient client = new OkHttpClient.Builder().readTimeout(100, TimeUnit.SECONDS)  // 增加读取超时时间.build();MediaType JSON = MediaType.get("application/json; charset=utf-8");String json = "{\"model\": \"deepseek-chat\", \"messages\": [{\"role\": \"user\", \"content\": \"" + request.getPrompt() + "\"}]}";// 使用 okhttp3.RequestBody 创建请求体okhttp3.RequestBody body = okhttp3.RequestBody.create(JSON, json.getBytes());Request apiRequest = new Request.Builder().url(DEEPSEEK_API_URL).post(body).addHeader("Authorization", "Bearer " + DEEPSEEK_API_KEY).addHeader("Content-Type", "application/json").build();try (Response response = client.newCall(apiRequest).execute()) {if (!response.isSuccessful()) {return new DeepSeekResponse("请求失败:" + response.code());}return handleNormalResponse(response);} catch (IOException e) {return new DeepSeekResponse("请求异常:" + e.getMessage());}}private DeepSeekResponse handleNormalResponse(Response response) throws IOException {try (ResponseBody body = response.body()) {if (body == null) {return new DeepSeekResponse("空响应");}String responseData = body.string();JsonNode jsonNode = objectMapper.readTree(responseData);if (jsonNode.has("choices") && !jsonNode.get("choices").isEmpty()) {JsonNode messageNode = jsonNode.get("choices").get(0).get("message");if (messageNode != null && messageNode.has("content")) {return new DeepSeekResponse(messageNode.get("content").asText());}}return new DeepSeekResponse("未找到有效回答");}}}
  • DeepSeekRequest请求类
package top.naccl.model.dto;import com.fasterxml.jackson.annotation.JsonProperty;// 请求类,用于接收前端传来的用户问题
public class DeepSeekRequest {@JsonProperty("prompt")private String prompt;public String getPrompt() {return prompt;}public void setPrompt(String prompt) {this.prompt = prompt;}
}
  • DeepSeekResponse响应类
package top.naccl.model.dto;// 响应类,用于返回 DeepSeek 的回答给前端
public class DeepSeekResponse {private String answer;public DeepSeekResponse(String answer) {this.answer = answer;}public String getAnswer() {return answer;}public void setAnswer(String answer) {this.answer = answer;}
}

5、pom.xml引入

    <!-- deepseek --><dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>4.11.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency>

6、问题总结

  • 调用比较慢,上面的读取时间需要设置长点
  • 目前不支持记忆问答,每次刷新之前的问答就会清空
  • 目前使用的是非流式响应,具体后面在研究流式响应,据说比较快,所以推荐
  • 测试使用调用api 30次左右,花费0.05
  • 界面还需待优化

🎉🎉🎉🎉🎉🎉
欢迎访问的个人博客:https://swzbk.site/,加好友,拉你入福利群
🎉🎉🎉🎉🎉🎉

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/72403.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Kubernetes 单节点集群搭建

Kubernetes 单节点集群搭建教程 本人尝试基于Ubuntu搭建一个单节点K8S集群&#xff0c;其中遇到各种问题&#xff0c;最大的问题就是网络&#xff0c;各种镜像源下载不下来&#xff0c;特此记录&#xff01;注意&#xff1a;文中使用了几个镜像&#xff0c;将看来可能失效导致安…

【PTA题目解答】7-3 字符串的全排列(20分)next_permutation

1.题目 给定一个全由小写字母构成的字符串&#xff0c;求它的全排列&#xff0c;按照字典序从小到大输出。 输入格式: 一行&#xff0c;一个字符串&#xff0c;长度不大于8。 输出格式: 输出所有全排列&#xff0c;每行一种排列形式&#xff0c;字典序从小到大。 输入样例…

专题三0~n-1中缺失的数字

1.题目 给一个数组&#xff0c;单调性是递增的&#xff0c;需要找到缺失的数字&#xff0c;加上这个数字就变为等差数组了。 2.算法原理 这里用二分来解决&#xff0c;而二段性是根据下标区分&#xff0c;临界值前的数字于下标相对应&#xff0c;临界值后的于下标相差1&#x…

【图像处理】ISP(Image Signal Processor) 图像处理器的用途和工作原理?

ISP&#xff08;图像信号处理器&#xff09;是数字影像设备的“视觉大脑”&#xff0c;负责将传感器捕获的原始电信号转化为我们看到的高清图像。以下从用途和工作原理两方面通俗解析&#xff1a; 一、ISP的核心用途&#xff1a;让照片“更像眼睛看到的” 提升画质&#xff1a…

python学习笔记-mysql数据库操作

现有一个需求&#xff0c;调用高德api获取全国县级以上行政区数据并保存为json文件&#xff0c;使用python获取&#xff1a; import requests import json# 高德API Key api_key "your_api_key"# 调用行政区域查询API def fetch_districts():url f"https://r…

Redisson 实现分布式锁源码浅析

大家好&#xff0c;我是此林。 今天来分享Redisson分布式锁源码。还是一样&#xff0c;我们用 问题驱动 的方式展开讲述。 1. redis 中如何使用 lua 脚本&#xff1f; Redis内置了lua解释器&#xff0c;lua脚本有两个好处&#xff1a; 1. 减少多次Redis命令的网络传输开销。…

【软件】免费的PDF全文翻译软件,能保留公式图表的样式

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhagn.cn] 如果本文帮助到了你&#xff0c;欢迎[点赞、收藏、关注]哦~ 很多PDF全文翻译软件都是收费的&#xff0c;而划线翻译看着又很累。这个开源的PDF全文翻译软件非常好用&#xff0c;并且能够保留公式、图表、目录和注…

CentOS 7 系统上安装 SQLite

1. 检查系统更新 在安装新软件之前&#xff0c;建议先更新系统的软件包列表&#xff0c;以确保使用的是最新的软件源和补丁。打开终端&#xff0c;执行以下命令&#xff1a; sudo yum update -y -y 选项表示在更新过程中自动回答 “yes”&#xff0c;避免手动确认。 2. 安装 …

Gin(后端)和 Vue3(前端)中实现 Server-Sent Events(SSE)推送

在 Gin&#xff08;后端&#xff09;和 Vue3&#xff08;前端&#xff09;中实现 Server-Sent Events&#xff08;SSE&#xff09;推送&#xff0c;主要分为以下几个步骤&#xff1a; 后端&#xff08;Gin&#xff09;实现 SSE Gin 框架可以使用 c.SSEvent 方法来推送 SSE 事…

大模型微调中显存占用和训练时间的影响因素

BatchSize 显存占用&#xff1a;与batch_size呈线性关系&#xff0c;可理解为 M t o t a l M f i x e d B a t c h S i z e ∗ M p e r − s a m p l e M_{total}M_{fixed}BatchSize*M_{per-sample} Mtotal​Mfixed​BatchSize∗Mper−sample​&#xff0c;其中 M f i x e d…

【排序算法对比】快速排序、归并排序、堆排序

排序算法对比&#xff1a;快速排序、归并排序、堆排序 1. 快速排序&#xff08;Quick Sort&#xff09; 原理 快速排序采用 分治法&#xff08;Divide and Conquer&#xff09;&#xff0c;通过选取基准值&#xff08;pivot&#xff09;&#xff0c;将数组划分为 小于基准值…

PentestGPT 下载

PentestGPT 下载 PentestGPT 介绍 PentestGPT&#xff08;Penetration Testing GPT&#xff09;是一个基于大语言模型&#xff08;LLM&#xff09;的智能渗透测试助手。它结合了 ChatGPT&#xff08;或其他 GPT 模型&#xff09;与渗透测试工具&#xff0c;帮助安全研究人员自…

防火墙虚拟系统实验

一实验拓扑 二实验过程 配置资源 创建虚拟系统 配置管理员 创建安全策略

代码随想录算法训练营第31天 | 56. 合并区间 738.单调递增的数字 968.监控二叉树

56. 合并区间 代码随想录 56. 合并区间 - 力扣&#xff08;LeetCode&#xff09; class Solution {public int[][] merge(int[][] intervals) {Arrays.sort(intervals,(a,b)->{if(a[0] b[0])return a[1] - b[1];return a[0] - b[0];});List<int[]> result new Arra…

Go语言对于MySQL的基本操作

一.下载依赖 终端中输入&#xff1a; go get -u github.com/go-sql-driver/mysql 导入包 import ("database/sql"_ "github.com/go-sql-driver/mysql" ) 二.案例 package main//go get-u github.com/go-sql-driver/mysql 获取驱动 import ("databa…

Linux与深入HTTP序列化和反序列化

深入HTTP序列化和反序列化 本篇介绍 在上一节已经完成了客户端和服务端基本的HTTP通信&#xff0c;但是前面的传递并没有完全体现出HTTP的序列化和反序列化&#xff0c;为了更好得理解其工作流程&#xff0c;在本节会以更加具体的方式分析到HTTP序列化和反序列化 本节会在介绍…

基于Python+SQLite实现(Web)验室设备管理系统

实验室设备管理系统 应用背景 为方便实验室进行设备管理&#xff0c;某大学拟开发实验室设备管理系统 来管理所有实验室里的各种设备。系统可实现管理员登录&#xff0c;查看现有的所有设备&#xff0c; 增加设备等功能。 开发环境 Mac OSPyCharm IDEPython3Flask&#xff…

深拷贝and浅拷贝!

一、什么是拷贝&#xff1f;什么是深拷贝和浅拷贝&#xff1f; &#xff08;1&#xff09;拷贝&#xff1a;拷贝就是为了复用原对象的部分or全部数据&#xff0c;在原对象的基础上通过复制的方式创建一个新的对象。 拷贝对象可以分为三种类型&#xff1a;直接赋值、浅拷贝和深拷…

高频面试题(含笔试高频算法整理)基本总结回顾43

干货分享&#xff0c;感谢您的阅读&#xff01; &#xff08;暂存篇---后续会删除&#xff0c;完整版和持续更新见高频面试题基本总结回顾&#xff08;含笔试高频算法整理&#xff09;&#xff09; 备注&#xff1a;引用请标注出处&#xff0c;同时存在的问题请在相关博客留言…

《灵珠觉醒:从零到算法金仙的C++修炼》卷三·天劫试炼(34)混元金斗装万物 - 0-1背包问题(二维DP)

《灵珠觉醒:从零到算法金仙的C++修炼》卷三天劫试炼(34)混元金斗装万物 - 0-1背包问题(二维DP) 哪吒在数据修仙界中继续他的修炼之旅。这一次,他来到了一片神秘的混元谷,谷中有一座巨大的混元金斗,斗身闪烁着神秘的光芒。谷口有一块巨大的石碑,上面刻着一行文字:“欲…