博客網站(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.pswp.cn/pingmian/72403.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/72403.shtml
英文地址,請注明出處:http://en.pswp.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) 哪吒在數據修仙界中繼續他的修煉之旅。這一次,他來到了一片神秘的混元谷,谷中有一座巨大的混元金斗,斗身閃爍著神秘的光芒。谷口有一塊巨大的石碑,上面刻著一行文字:“欲…