1. 环境准备
确保你的系统满足以下基本要求:
- 操作系统:Linux / Windows / macOS(推荐 Linux)
- Python 版本:≥ 3.9
- 显卡(可选):支持 CUDA 的 NVIDIA GPU(用于加速推理)
- 磁盘空间:至少 20GB 可用空间(视模型大小而定)
2. 安装依赖
打开终端,执行以下命令安装必要依赖:
pip install torch transformers accelerate sentencepiece
3. 下载 DeepSeek 模型
你可以通过 Hugging Face 或官方渠道获取 DeepSeek 模型权重。例如:
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/deepseek-coder-1.3b-instruct", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("deepseek-ai/deepseek-coder-1.3b-instruct", trust_remote_code=True)
4. 运行 DeepSeek
加载模型后,即可进行推理:
inputs = tokenizer("写一个 Python 快速排序函数", return_tensors="pt")
outputs = model.generate(**inputs, max_new_tokens=200)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
5. 常见问题
- 模型加载慢? 首次加载会自动下载,建议使用稳定网络。
- 显存不足? 可尝试使用量化版本或 CPU 模式运行。
- 权限错误? 在 Linux/macOS 中可使用
chmod调整文件权限。