欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java調(diào)用Deepseek-R1.1.5b大模型的超詳細教程(附代碼)

 更新時間:2025年03月05日 08:40:18   作者:小星袁  
這篇文章主要為大家介紹了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)文章

  • Java對象的內(nèi)存布局全流程

    Java對象的內(nèi)存布局全流程

    這篇文章主要介紹了Java對象的內(nèi)存布局全流程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • JDK動態(tài)代理過程原理及手寫實現(xiàn)詳解

    JDK動態(tài)代理過程原理及手寫實現(xiàn)詳解

    這篇文章主要為大家介紹了JDK動態(tài)代理過程原理及手寫實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • SpringBoot之controller參數(shù)校驗詳解

    SpringBoot之controller參數(shù)校驗詳解

    介紹了Java中使用@Validated和@Valid進行參數(shù)校驗的方法,包括不同標簽的使用場景、基本屬性和一些常用的注解類型,同時,還討論了如何在控制器中使用這些校驗標簽,以及如何處理校驗結(jié)果和自定義錯誤消息,最后,還介紹了如何實現(xiàn)分組校驗和嵌套校驗,并提供了一些示例代碼
    2024-11-11
  • spring-boot-starter-parent的作用詳解

    spring-boot-starter-parent的作用詳解

    這篇文章主要介紹了spring-boot-starter-parent的作用詳解,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08
  • 設(shè)計模式之責(zé)任鏈模式_動力節(jié)點Java學(xué)院整理

    設(shè)計模式之責(zé)任鏈模式_動力節(jié)點Java學(xué)院整理

    這篇文章主要為大家詳細介紹了設(shè)計模式之責(zé)任鏈模式的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • 史上最簡單的MyBatis動態(tài)SQL入門示例代碼

    史上最簡單的MyBatis動態(tài)SQL入門示例代碼

    動態(tài)sql,可以根據(jù)用戶對字段選擇和輸入,動態(tài)生成一條sql執(zhí)行。接下來通過本文給大家分享MyBatis動態(tài)SQL入門示例代碼,一起看看吧
    2017-03-03
  • mybatis日志打印的兩款I(lǐng)DEA插件推薦

    mybatis日志打印的兩款I(lǐng)DEA插件推薦

    這篇文章主要給大家推薦介紹了關(guān)于mybatis日志打印的兩款I(lǐng)DEA插件,文中通過圖文以及實例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用mybatis具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-04-04
  • SpringBoot打印系統(tǒng)執(zhí)行的sql語句及日志配置指南

    SpringBoot打印系統(tǒng)執(zhí)行的sql語句及日志配置指南

    這篇文章主要給大家介紹了關(guān)于SpringBoot打印系統(tǒng)執(zhí)行的sql語句及日志配置的相關(guān)資料,在Java SpringBoot項目中如果使用了Mybatis框架,默認情況下執(zhí)行的所有SQL操作都不會打印日志,需要的朋友可以參考下
    2023-10-10
  • Idea2023創(chuàng)建springboot不能選擇java8的解決方法(最新推薦)

    Idea2023創(chuàng)建springboot不能選擇java8的解決方法(最新推薦)

    在idea2023版本創(chuàng)建springboot的過程中,選擇java版本時發(fā)現(xiàn)沒有java8版本,只有java17和java20,遇到這樣的問題如何解決呢,下面小編給大家分享Idea2023創(chuàng)建springboot不能選擇java8的解決方法,感興趣的朋友一起看看吧
    2024-01-01
  • Java SHA-256加密的兩種實現(xiàn)方法詳解

    Java SHA-256加密的兩種實現(xiàn)方法詳解

    這篇文章主要介紹了Java SHA-256加密的兩種實現(xiàn)方法,結(jié)合實例形式分析了java實現(xiàn)SHA-256加密的實現(xiàn)代碼與相關(guān)注意事項,需要的朋友可以參考下
    2017-08-08

最新評論