使用 Ollama 本地部署 DeepSeek 模型及 Chatbox 配置
文章目录
- Github
- 官网
- 简介
- 安装
- 基础命令
- 模型下载
- 运行模型
- 测试模型
- API文档
- 常用API
- Generate 响应
- Python Generate 示例
- Chat 响应
- Python Chat 示例
 
- Chatbox
- Mac 开放局域网访问
Github
- https://github.com/ollama/ollama
官网
- https://ollama.com/
简介
Ollama 是一个本地化大语言模型运行框架,主要特点包括:
- 支持在本地运行 Llama 3、Mistral、DeepSeek 等主流大模型
- 提供REST API接口(如您代码中的 base_url 配置)
- 支持多平台(MacOS/Linux/Windows)
- 内置模型库管理功能
安装

- 验证是否安装成功
ollama -v
# 监听 Ollama 服务端口
lsof -i :11434 | grep LISTEN
基础命令
# 下载模型到本地
ollama pull deepseek-r1:1.5b
# 列出已有的模型
ollama list
# 运行指定模型
ollama run deepseek-r1:1.5b
ollama run deepseek-r1:7b
# 删除本地模型
ollama rm deepseek-r1:7b
模型下载
- https://ollama.com/library/deepseek-r1
  
  
注:本地资源有限就只下载了 1.5b 版本。
运行模型
ollama list
ollama run deepseek-r1:1.5b

注:首次执行会先下载模型;按 Ctr + d 停止运行
测试模型
API文档
- https://github.com/ollama/ollama/blob/main/docs/api.md
常用API
- http://127.0.0.1:11434
  
- http://127.0.0.1:11434/api/tags
  
- http://127.0.0.1:11434/api/ps
  
Generate 响应
- http://127.0.0.1:11434/api/generate
curl http://127.0.0.1:11434/api/generate \
 -H "Content-Type: application/json" \
 -d '{
      "model": "deepseek-r1:1.5b",
      "prompt": "你好",
      "stream": false
    }' \
 --max-time 30 \
 --no-buffer

Python Generate 示例
import requests
import json
API_URL = "http://localhost:11434/api/generate"
def Demo1():
    data = {
        "model": "deepseek-r1:1.5b",
        "system": "You are a helpful assistant, and you only speak Chinese.",
        "prompt": "你好",
        "stream": False
    }
    response = requests.post(API_URL, json=data)
    print(response)
    json_response = response.json()
    print(json.dumps(json_response, indent=2, ensure_ascii=False))
    print(json_response.get("response", "").strip())
if __name__ == "__main__":
    Demo1()
Chat 响应
- http://127.0.0.1:11434/api/chat
curl http://127.0.0.1:11434/api/chat \
 -H "Content-Type: application/json" \
 -d '{
      "model": "deepseek-r1:1.5b",
      "messages": [
        {"role": "system", "content": "You are a helpful assistant, and you only speak Chinese."},
        {"role": "user", "content": "你好"}
      ],
      "stream": false
    }' \
 --max-time 30 \
 --no-buffer

Python Chat 示例
import requests
import json
API_URL = "http://localhost:11434/api/chat"
def Demo1():
    data = {
        "model": "deepseek-r1:1.5b",
        "messages": [
            {"role": "system", "content": "You are a helpful assistant, and you only speak Chinese."},
            {"role": "user", "content": "你好"}
        ],
        "stream": False
    }
    response = requests.post(API_URL, json=data)
    print(response)
    json_response = response.json()
    print(json.dumps(json_response, indent=2, ensure_ascii=False))
    print(json_response['message']['content'])
if __name__ == "__main__":
    Demo1()
- 响应内容
{
  "model": "deepseek-r1:1.5b",
  "created_at": "2025-03-20T15:10:32.605318Z",
  "message": {
    "role": "assistant",
    "content": "<think>\n\n</think>\n\n你好!有什么我可以帮助你的吗?"
  },
  "done_reason": "stop",
  "done": true,
  "total_duration": 1046775688,
  "load_duration": 25081968,
  "prompt_eval_count": 16,
  "prompt_eval_duration": 69998462,
  "eval_count": 13,
  "eval_duration": 950691930
}
Chatbox
- https://chatboxai.app/zh
Chatbox AI 是一款 AI 客户端应用和智能助手,支持众多先进的 AI 模型和 API,可在 Windows、MacOS、Android、iOS、Linux 和网页版上使用。

 
 
 
 
 
Mac 开放局域网访问
# 强制关闭 Ollama
sudo pkill -9 ollama 
- 使用正确的Ollama配置创建plist文件
echo '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>ollama</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/bash</string>
        <string>-c</string>
        <string>export OLLAMA_HOST="0.0.0.0:11434" && /usr/local/bin/ollama serve</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
</dict>
</plist>' > ~/Library/LaunchAgents/ollama.plist
- 设置权限并重新加载服务
chmod 644 ~/Library/LaunchAgents/ollama.plist
launchctl unload ~/Library/LaunchAgents/ollama.plist 2>/dev/null
launchctl load ~/Library/LaunchAgents/ollama.plist
- 启启 Ollama 服务后
lsof -i :11434 | grep LISTEN

- 局域网内访问测试
curl http://192.168.0.100:11434/api/generate \
 -H "Content-Type: application/json" \
 -d '{
      "model": "deepseek-r1:1.5b",
      "prompt": "你好",
      "stream": false
    }' \
 --max-time 30 \
 --no-buffer
