Mac系统本地部署Deepseek极简教程

环境准备

  1. 操作系统:macOS 10.15+
  2. 终端工具:iTerm2或系统自带终端
  3. 内存要求:建议16GB以上
  4. 存储空间:至少10GB可用空间

Ollama安装

方法一:Homebrew安装(推荐)

bash 复制代码
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install ollama

方法二:手动安装

  1. 访问Ollama官网下载dmg安装包
  2. 双击打开Ollama.dmg文件
  3. 将Ollama图标拖拽到Applications文件夹

模型部署

bash 复制代码
ollama pull deepseek-coder:6.7b

Python客户端实现

python 复制代码
# test_ollama.py
import requests
import json

def generate(prompt: str, model: str = 'deepseek-coder:6.7b') -> str:
    """
    Deepseek模型生成接口
    
    参数:
        prompt: 输入提示文本
        model: 使用的模型名称(默认deepseek-coder:6.7b)
    
    返回:
        模型生成的文本结果
    """
    try:
        response = requests.post(
            'http://localhost:11434/api/generate',
            json={
                'model': model,
                'prompt': prompt,
                'stream': True
            },
            headers={'Content-Type': 'application/json'},
            stream=True
        )
        response.raise_for_status()
        
        full_response = ''
        for line in response.iter_lines():
            if line:
                chunk = json.loads(line)
                print(chunk.get('response', ''), end='', flush=True)
                full_response += chunk.get('response', '')
        return full_response

    except requests.exceptions.RequestException as e:
        print(f"\nAPI请求错误: {e}")
        return ''
    except json.JSONDecodeError:
        print("\n响应解析失败")
        return ''

if __name__ == "__main__":
    test_prompt = """
    请用Python编写一个快速排序算法函数,要求:
    1. 输入参数为一个整数列表
    2. 返回排序后的升序列表
    3. 添加详细的代码注释
    """
    
    print("测试prompt:\n", test_prompt)
    print("\n模型响应:")
    result = generate(test_prompt)
    
    # 验证代码可执行性
    try:
        exec(result)
        print("\n\n代码验证通过")
    except Exception as e:
        print(f"\n\n代码执行错误: {e}")

执行验证

bash 复制代码
python3 test_ollama.py

预期终端输出片段:

ini 复制代码
模型响应:

def quick_sort(arr):
    # 基线条件:空或单元素数组直接返回
    if len(arr) <= 1:
        return arr
    # 选择基准元素
    pivot = arr[len(arr)//2]
    # 分割为三个子数组
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    # 递归排序并合并
    return quick_sort(left) + middle + quick_sort(right)

代码验证通过

常见问题

Q1: 服务启动失败提示端口占用

bash 复制代码
lsof -i :11434
kill -9 <PID>

Q2: 模型下载中断

bash 复制代码
ollama pull deepseek-coder:6.7b

Q3: Python依赖缺失

bash 复制代码
pip3 install requests

最佳实践

  1. 生产环境建议使用Docker容器化部署
  2. 复杂需求可配合LangChain框架使用
  3. 实时监控API调用:
bash 复制代码
ollama serve
相关推荐
AC赳赳老秦2 小时前
Unity游戏开发实战指南:核心逻辑与场景构建详解
开发语言·spring boot·爬虫·搜索引擎·全文检索·lucene·deepseek
且去填词3 小时前
DeepSeek-R1 实战:数据分析
人工智能·python·mysql·语言模型·deepseek·structured data
且去填词16 小时前
DeepSeek API 深度解析:从流式输出、Function Calling 到构建拥有“手脚”的 AI 应用
人工智能·python·语言模型·llm·agent·deepseek
AC赳赳老秦20 小时前
Shell 脚本批量生成:DeepSeek 辅助编写服务器运维自动化指令
运维·服务器·前端·vue.js·数据分析·自动化·deepseek
AC赳赳老秦1 天前
量化交易脚本开发:DeepSeek生成技术指标计算与信号触发代码
数据库·elasticsearch·信息可视化·流程图·数据库架构·memcached·deepseek
AC赳赳老秦2 天前
Python 爬虫进阶:DeepSeek 优化反爬策略与动态数据解析逻辑
开发语言·hadoop·spring boot·爬虫·python·postgresql·deepseek
Java后端的Ai之路2 天前
【大模型技术栈】-Qwen与DeepSeek如何构建智能大脑?
大模型·qwen·deepseek
AC赳赳老秦2 天前
Go语言微服务文档自动化生成:基于DeepSeek的智能解析实践
大数据·开发语言·人工智能·微服务·golang·自动化·deepseek
AC赳赳老秦3 天前
前端可视化组件开发:DeepSeek辅助Vue/React图表组件编写实战
前端·vue.js·人工智能·react.js·信息可视化·数据分析·deepseek
TGITCIC5 天前
mHC架构:用数学约束驯服超宽残差,大模型训练的新范式
ai大模型·开源大模型·deepseek·大模型ai·国产大模型·国产模型