Java调用Deepseek-R1.1.5b大模型的超详细教程(附代码)

 更新时间:2025年03月05日 08:40:18   作者:小星袁  
这篇文章主要为大家介绍了Java调用Deepseek-R1.1.5b大模型的超详细教程并附上了代码,文中的教程讲解详细,有需要的小伙伴可以参考一下

一、部署本地 DeepSeek 模型(核心步骤)

步骤可参考:deepseek本地部署及java、python调用步骤详解

二、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实现跨域代码

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">
      发送
    </button>
  </div>
</div>
 
<script>
  function sendMessage() {
    const input = document.getElementById('question');
    const message = input.value.trim();
    if (!message) return;
 
    // 添加用户消息
    addMessage(message, 'user');
    input.value = '';
 
    // 显示加载状态
    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("服务暂时不可用,请稍后再试", '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 结果展示

到此这篇关于Java调用Deepseek-R1.1.5b大模型的超详细教程的文章就介绍到这了,更多相关Java调用Deepseek-R1.1.5b模型内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java对象的内存布局全流程

    Java对象的内存布局全流程

    这篇文章主要介绍了Java对象的内存布局全流程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • JDK动态代理过程原理及手写实现详解

    JDK动态代理过程原理及手写实现详解

    这篇文章主要为大家介绍了JDK动态代理过程原理及手写实现详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • SpringBoot之controller参数校验详解

    SpringBoot之controller参数校验详解

    介绍了Java中使用@Validated和@Valid进行参数校验的方法,包括不同标签的使用场景、基本属性和一些常用的注解类型,同时,还讨论了如何在控制器中使用这些校验标签,以及如何处理校验结果和自定义错误消息,最后,还介绍了如何实现分组校验和嵌套校验,并提供了一些示例代码
    2024-11-11
  • spring-boot-starter-parent的作用详解

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

    这篇文章主要介绍了spring-boot-starter-parent的作用详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • 设计模式之责任链模式_动力节点Java学院整理

    设计模式之责任链模式_动力节点Java学院整理

    这篇文章主要为大家详细介绍了设计模式之责任链模式的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • 史上最简单的MyBatis动态SQL入门示例代码

    史上最简单的MyBatis动态SQL入门示例代码

    动态sql,可以根据用户对字段选择和输入,动态生成一条sql执行。接下来通过本文给大家分享MyBatis动态SQL入门示例代码,一起看看吧
    2017-03-03
  • mybatis日志打印的两款IDEA插件推荐

    mybatis日志打印的两款IDEA插件推荐

    这篇文章主要给大家推荐介绍了关于mybatis日志打印的两款IDEA插件,文中通过图文以及实例代码介绍的非常详细,对大家学习或者使用mybatis具有一定的参考学习价值,需要的朋友可以参考下
    2023-04-04
  • SpringBoot打印系统执行的sql语句及日志配置指南

    SpringBoot打印系统执行的sql语句及日志配置指南

    这篇文章主要给大家介绍了关于SpringBoot打印系统执行的sql语句及日志配置的相关资料,在Java SpringBoot项目中如果使用了Mybatis框架,默认情况下执行的所有SQL操作都不会打印日志,需要的朋友可以参考下
    2023-10-10
  • Idea2023创建springboot不能选择java8的解决方法(最新推荐)

    Idea2023创建springboot不能选择java8的解决方法(最新推荐)

    在idea2023版本创建springboot的过程中,选择java版本时发现没有java8版本,只有java17和java20,遇到这样的问题如何解决呢,下面小编给大家分享Idea2023创建springboot不能选择java8的解决方法,感兴趣的朋友一起看看吧
    2024-01-01
  • Java SHA-256加密的两种实现方法详解

    Java SHA-256加密的两种实现方法详解

    这篇文章主要介绍了Java SHA-256加密的两种实现方法,结合实例形式分析了java实现SHA-256加密的实现代码与相关注意事项,需要的朋友可以参考下
    2017-08-08

最新评论