如何自己电脑上部署DeepSeek,并且接口访问?
话不多说,直接上步骤
(下面的是windows版本安装部署DeepSeek 以及通过代码接口方式请求)
一、安装Ollama:
- 访问官网,下载Windows版安装包OllamaSetup.exe
二、管理员运行cmd窗口
1.检查是否安装成功
ollama -v
2.拉取DeepSeek模型(注意模型名)
ollama pull deepseek-r1:8b
模型参考
3.运行
ollama run deepseek-r1:8b
4.访问设置
如果本机可以访问,其他机器通过ip不能访问,尝试修改为其他机器可以访问(跟mysql redis差不多的设置)
setx OLLAMA_HOST "0.0.0.0:11434" /M
然后重启
ollama serve
三、验证
游览器输入
http://localhost:11434/
如上表示正常启动成功。
四、接口访问,接入项目
接口请求工具测试了下 正常
Java代码测试接口
import cn.hutool.http.HtmlUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;import java.util.HashMap;
import java.util.Map;public class DeepSeekUtils {public static final String ai_host = "http://192.168.1.18:11434/";public static void main(String[] args) {String str = "翻译:where are you from?";String res = getContent(str);System.out.println("内容: " + res);}public static String getContent(String q){// 定义请求的 URLString api = ai_host + "v1/chat/completions";// 创建 JSON 对象JSONObject jsonObject = new JSONObject();jsonObject.put("model", "deepseek-r1:8b");JSONObject messageObject = new JSONObject();messageObject.put("role", "user");messageObject.put("content", q);jsonObject.put("messages", new JSONObject[]{messageObject});jsonObject.put("stream", false);// 创建请求参数 MapMap<String, String> paramMap = new HashMap<>();paramMap.put("data", jsonObject.toJSONString());// 发送 POST 请求HttpRequest request = HttpRequest.post(api).headerMap(paramMap, false).body(jsonObject.toString()).contentType("application/json;charset=UTF-8");HttpResponse response = request.execute();String content = "";// 检查响应状态码if (response.isOk()) {try {// 解析响应的 JSON 数据JSONObject responseJson = (JSONObject) JSONObject.parse(response.body());// 获取 choices 数组JSONArray choices = responseJson.getJSONArray("choices");if (choices != null && choices.size() > 0) {// 获取第一个 choice 对象JSONObject firstChoice = choices.getJSONObject(0);// 获取 message 对象JSONObject message = firstChoice.getJSONObject("message");// 提取 content 字段的值content = message.getString("content");content = HtmlUtil.filter(content);content = content.trim();}} catch (Exception e) {System.err.println("JSON 解析出错: " + e.getMessage());}} else {System.err.println("请求失败,状态码: " + response.getStatus());}return content;}
}
控制台打印:
一切顺利!