Java調(diào)用Deepseek-R1.1.5b大模型的超詳細教程(附代碼)
一、部署本地 DeepSeek 模型(核心步驟)
步驟可參考:deepseek本地部署及java、python調(diào)用步驟詳解
二、Java本地測試代碼
public class OllamaDemo { private static final String API_URL = "http://localhost:11435/api/generate"; public static final String askDeepSeek(String prompt) { JSONObject param = new JSONObject(); param.set("model", "deepseek-r1:1.5b") .set("prompt", prompt) .set("stream", false) .set("temperature", 0.7); return HttpRequest.post(API_URL) .body(param.toString()) .timeout(30000) .execute() .body(); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("請輸入你的問題(輸入退出停止運行):"); String question = scanner.nextLine(); System.out.println(askDeepSeek(question)); } }
三、Java實現(xiàn)跨域代碼
3.1 Controller類
public JsonResult getQuestion(String question) { return JsonResult.ok(questionService.getAnswer(question)); }
3.2 Service接口
String getAnswer(String question);
3.3 Service類
private static final String API_URL = "http://localhost:11435/api/generate"; public String getAnswer(String question) { JSONObject param = new JSONObject(); param.set("model", "deepseek-r1:1.5b") .set("prompt", prompt) .set("stream", false) .set("temperature", 0.7); String responseBody = HttpRequest.post(API_URL) .body(param.toString()) .timeout(30000) .execute() .body(); JSONObject responseJson = new JSONObject(responseBody); return responseJson.getStr("response"); }
3.4 html代碼
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>DeepSeek 智能對話</title> <style> :root { --user-bg: #4F46E5; --bot-bg: #F3F4F6; --text-primary: #1F2937; --border-color: #E5E7EB; } body { margin: 0; min-height: 100vh; font-family: 'Segoe UI', system-ui, -apple-system; background: #F9FAFB; display: flex; flex-direction: column; } .chat-container { flex: 1; max-width: 800px; margin: 0 auto; width: 100%; padding: 1rem; overflow-y: auto; } .message { display: flex; gap: 1rem; margin-bottom: 1.5rem; animation: fadeIn 0.3s ease; } .user-message { flex-direction: row-reverse; } .avatar { width: 32px; height: 32px; border-radius: 6px; background: var(--user-bg); display: flex; align-items: center; justify-content: center; color: white; font-weight: 500; } .bot .avatar { background: #6B7280; } .content { max-width: 85%; padding: 1rem; border-radius: 0.75rem; background: white; box-shadow: 0 2px 4px rgba(0,0,0,0.05); } .user .content { background: var(--user-bg); color: white; } .typing-indicator { display: inline-flex; gap: 0.25rem; padding: 0.5rem; } .typing-dot { width: 6px; height: 6px; background: #9CA3AF; border-radius: 50%; animation: bounce 1.4s infinite; } @keyframes bounce { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-4px); } } .input-area { border-top: 1px solid var(--border-color); padding: 1.5rem; background: white; } .input-wrapper { max-width: 800px; margin: 0 auto; display: flex; gap: 1rem; align-items: center; } #question { flex: 1; padding: 0.75rem 1rem; border: 1px solid var(--border-color); border-radius: 0.75rem; resize: none; min-height: 44px; max-height: 200px; } </style> </head> <body> <div class="chat-container" id="chatContainer"> <!-- 示例對話 --> <div class="message bot"> <div class="avatar">AI</div> <div class="content">您好!我是DeepSeek智能助手,有什么可以幫您?</div> </div> </div> <div class="input-area"> <div class="input-wrapper"> <textarea id="question" placeholder="輸入消息..." rows="1" onkeydown="handleKeyDown(event)" ></textarea> <button onclick="sendMessage()" class="send-btn"> 發(fā)送 </button> </div> </div> <script> function sendMessage() { const input = document.getElementById('question'); const message = input.value.trim(); if (!message) return; // 添加用戶消息 addMessage(message, 'user'); input.value = ''; // 顯示加載狀態(tài) const loader = addLoader(); fetch(`http://localhost:8081/ai/get?question=${encodeURIComponent(message)}`) .then(response => response.json()) .then(data => { removeLoader(loader); if(data.code === 2001) { typewriterEffect(data.data); } else { addMessage(`錯誤:${data.msg}`, 'bot'); } }) .catch(() => { removeLoader(loader); addMessage("服務(wù)暫時不可用,請稍后再試", 'bot'); }); } function addMessage(text, type) { const container = document.getElementById('chatContainer'); const messageDiv = document.createElement('div'); messageDiv.className = `message ${type}`; messageDiv.innerHTML = ` <div class="avatar">${type === 'user' ? '你' : 'AI'}</div> <div class="content">${text}</div> `; container.appendChild(messageDiv); container.scrollTop = container.scrollHeight; } function addLoader() { const container = document.getElementById('chatContainer'); const loaderDiv = document.createElement('div'); loaderDiv.className = 'message bot'; loaderDiv.innerHTML = ` <div class="avatar">AI</div> <div class="content"> <div class="typing-indicator"> <div class="typing-dot"></div> <div class="typing-dot"></div> <div class="typing-dot"></div> </div> </div> `; container.appendChild(loaderDiv); container.scrollTop = container.scrollHeight; return loaderDiv; } function removeLoader(element) { element.remove(); } function typewriterEffect(text) { const container = document.getElementById('chatContainer'); const messageDiv = document.createElement('div'); messageDiv.className = 'message bot'; messageDiv.innerHTML = ` <div class="avatar">AI</div> <div class="content"></div> `; container.appendChild(messageDiv); const contentDiv = messageDiv.querySelector('.content'); let index = 0; function type() { if (index < text.length) { contentDiv.innerHTML += text.charAt(index); index++; setTimeout(type, 20); container.scrollTop = container.scrollHeight; } } type(); } function handleKeyDown(event) { if (event.key === 'Enter' && !event.shiftKey) { event.preventDefault(); sendMessage(); } } </script> </body> </html>
3.5 結(jié)果展示
到此這篇關(guān)于Java調(diào)用Deepseek-R1.1.5b大模型的超詳細教程的文章就介紹到這了,更多相關(guān)Java調(diào)用Deepseek-R1.1.5b模型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot之controller參數(shù)校驗詳解
介紹了Java中使用@Validated和@Valid進行參數(shù)校驗的方法,包括不同標簽的使用場景、基本屬性和一些常用的注解類型,同時,還討論了如何在控制器中使用這些校驗標簽,以及如何處理校驗結(jié)果和自定義錯誤消息,最后,還介紹了如何實現(xiàn)分組校驗和嵌套校驗,并提供了一些示例代碼2024-11-11spring-boot-starter-parent的作用詳解
這篇文章主要介紹了spring-boot-starter-parent的作用詳解,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08設(shè)計模式之責(zé)任鏈模式_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細介紹了設(shè)計模式之責(zé)任鏈模式的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08SpringBoot打印系統(tǒng)執(zhí)行的sql語句及日志配置指南
這篇文章主要給大家介紹了關(guān)于SpringBoot打印系統(tǒng)執(zhí)行的sql語句及日志配置的相關(guān)資料,在Java SpringBoot項目中如果使用了Mybatis框架,默認情況下執(zhí)行的所有SQL操作都不會打印日志,需要的朋友可以參考下2023-10-10Idea2023創(chuàng)建springboot不能選擇java8的解決方法(最新推薦)
在idea2023版本創(chuàng)建springboot的過程中,選擇java版本時發(fā)現(xiàn)沒有java8版本,只有java17和java20,遇到這樣的問題如何解決呢,下面小編給大家分享Idea2023創(chuàng)建springboot不能選擇java8的解決方法,感興趣的朋友一起看看吧2024-01-01