Skip to content

Linux 部署 DeepSeek-R1 实现API调用

DeepSeek-R1 对话

DeepSeek 是由深度求索(DeepSeek Inc.)团队开发的一系列通用大语言模型,旨在提供高效、低成本的 AI 解决方案。

安装Ollama

使用docker安装ollama方便管理

sh
docker run --restart always -d --name ollama -p 11434:11434 -v ollama-data:/data ollama/ollama:latest

可能遇到的问题一: docker下载超时

sh
Unable to find image 'ollama/ollama:latest' locally
docker: Error response from daemon: Get "https://registry-1.docker.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).
See 'docker run --help'.

这是因为阿里云Docker镜像仓库策略变更。2024-12-13 参考文章

建议替换为

sh
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": [
        "https://ghcr.nju.edu.cn",
        "https://do.nark.eu.org",
        "https://dc.j8.work",
        "https://docker.m.daocloud.io",
        "https://dockerproxy.com",
        "https://docker.mirrors.ustc.edu.cn",
        "https://docker.nju.edu.cn"
    ]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

安装DeepSeek-R1模型

模型可选参考 DeepSeek-R1模型

各模型占用磁盘情况, 模型越大, 回答质量越好, 但是耗时越长, 建议使用32b

这里只做测试, 使用的是deepseek-r1:8b模型(启动时大概需要6G的内存), 也可以使用deepseek-r1:1.5b小模型

sh
docker exec -it ollama /bin/bash
ollama pull deepseek-r1:8b
ollama run deepseek-r1:8b

将ollama设置为后台启动

sh
docker exec -it ollama /bin/bash
ollama run deepseek-r1:8b & tail -f /dev/null

集成Open-WebUI (纯api调用此步骤可省略)

sh
docker run -d -p 5000:8080 --add-host=host.docker.internal:host-gateway -v open-webui:/app/backend/data --name open-webui --restart always open-webui/open-webui:main

下载太慢可以试试指定 ghcr.nju.edu.cn 南大配置镜像加速

sh
docker pull ghcr.nju.edu.cn/open-webui/open-webui:main

通过浏览器访问http://ip:5000来登录Open-WebUI。

首次登录需要注册账号。 这里如果不能访问外网时加载会特别慢, 可以在进入成功后点击 设置 -> 管理员设置 -> 外部连接 -> 关闭OpenAI API开关 -> 右下角保存, 然后重启open-webui服务

API调用

Ollama vs 原生API

特性Ollama方式原生DeepSeek API (官方的python SDK)
API端点/api/generate/api/chat
必需参数model + prompt根据具体实现不同
流式响应stream参数控制通常需要特殊处理
模型加载自动从仓库下载需要手动管理模型文件

查看可用模型列表

sh
curl http://localhost:11434/api/tags

可知已下载了deepseek-r1:1.5bdeepseek-r1:8b两个模型

java代码调用示例

java
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class OllamaDeepSeekClient {
    public static void main(String[] args) {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost post = new HttpPost("http://ip:11434/api/generate");

            // 注意Ollama的API参数格式
            String json = "{"
                    + "\"model\": \"deepseek-r1:8b\","
                    + "\"prompt\": \"Hello World!\","
//                    + "\"temperature\": 0.7,"  // 新增温度参数
//                    + "\"top_p\": 0.9,"        // 新增top_p参数
                    + "\"stream\": false,"  // 是否启用流式   true时会逐个返回结果,而不是一次性返回全部结果
                    + "\"max_tokens\": 50"
                    + "}";

            post.setEntity(new StringEntity(json));
            post.setHeader("Content-Type", "application/json");

            HttpResponse response = httpClient.execute(post);
            String result = EntityUtils.toString(response.getEntity());
            System.out.println("API Response:\n" + result);

            // 解析 JSON
            JSONObject jsonObject = JSON.parseObject(result);

            // 提取 "response" 字段的值
            String responseStr = jsonObject.getString("response");
            // 去除 <think></think> 标签内的思考内容(如果需要)
            String cleanedResponse = responseStr.replaceAll("\\u003c/?.*?\\u003e", "").trim();
            // 打印结果
            System.out.println(String.format("提取的回答内容:%s", cleanedResponse));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

重要返回数据解析

  • response: 表示模型生成的响应内容。
text
\u003cthink\u003e\n\n\u003c/think\u003e\n\n您好!请问有什么可以帮助您的?如果您有任何问题或需要信息,请随时告诉我。😊,其中 \u003c 是小于号 < 的 Unicode 转义字符,\u003e 是大于号 > 的 Unicode 转义字符,实际内容为:
<think>(表示开始思考)
</think>(表示结束思考)
您好!请问有什么可以帮助您的?如果您有任何问题或需要信息,请随时告诉我。😊(实际的响应内容,包含一个笑脸表情符号)。
  • done: 表示请求是否已完成, true表示请求已完成。
  • done_reason: 表示请求完成的原因, stop表示请求因达到停止条件而完成。
  • context: 表示模型生成响应时使用的上下文信息,通常是一些 token 的索引值。
  • total_duration: 表示整个请求的总耗时,单位为纳秒。2839000000 纳秒,即约 2.839 秒。

粤ICP备20009776号