简介:本文详细介绍在Windows 11系统中通过Ollama框架部署DeepSeek大语言模型的全流程,涵盖环境配置、依赖安装、模型下载及API调用等关键步骤,提供可复用的技术方案和故障排查指南。
DeepSeek-R1系列模型对硬件资源有明确要求:7B参数版本建议配置16GB以上内存,NVIDIA RTX 3060及以上显卡;13B版本需32GB内存和RTX 4090显卡。内存不足时可通过设置OLLAMA_HOST=0.0.0.0:11434启用交换空间。
在”设置>系统>关于”中确认系统版本为Windows 11 22H2或更高版本。通过”控制面板>程序>启用或关闭Windows功能”启用:
Get-Service -Name OllamaService | Select Status
在系统环境变量中添加:
OLLAMA_MODELS: 指定模型存储路径(如D:\OllamaModels)OLLAMA_ORIGINAL_HOST: 设置为127.0.0.1
# 检查服务版本ollama --version# 列出可用模型ollama list# 运行测试命令ollama run hello-world
# 拉取DeepSeek-R1-7B模型ollama pull deepseek-r1:7b# 自定义运行参数(示例)$config = @{"template" = "{{.Input}}""system" = "You are a helpful AI assistant""parameters" = @{"temperature" = 0.7"top_p" = 0.9"max_tokens" = 2048}}# 创建自定义模型配置文件$config | ConvertTo-Json | Out-File -FilePath "$env:OLLAMA_MODELS\deepseek-custom.json"
启动模型时添加性能参数:
ollama run deepseek-r1:7b --num-gpu 1 --num-cpu 8 --memory 32G
关键优化参数说明:
| 参数 | 作用范围 | 推荐值 |
|———-|—————|————|
| --num-gpu | GPU计算单元 | 1(单卡) |
| --kv-cache | 上下文缓存 | 启用可提升连续对话性能 |
| --rope-scaling | 长文本处理 | 设置为linear |
修改C:\Program Files\Ollama\config.json:
{"api": {"enabled": true,"port": 11434,"cors": ["*"]}}
重启服务:
Restart-Service OllamaService
import requestsurl = "http://localhost:11434/api/generate"headers = {"Content-Type": "application/json"}data = {"model": "deepseek-r1:7b","prompt": "解释量子计算的基本原理","stream": False,"temperature": 0.7}response = requests.post(url, json=data, headers=headers)print(response.json()["response"])
using System;using System.Net.Http;using System.Text;using System.Threading.Tasks;class Program {static async Task Main() {var client = new HttpClient();var request = new {model = "deepseek-r1:7b",prompt = "用C#实现快速排序算法",temperature = 0.5};var content = new StringContent(System.Text.Json.JsonSerializer.Serialize(request),Encoding.UTF8,"application/json");var response = await client.PostAsync("http://localhost:11434/api/generate",content);var result = await response.Content.ReadAsStringAsync();Console.WriteLine(result);}}
CUDA内存不足错误:
--batch参数值--share参数共享内存nvidia-smi设置持久化模式:
nvidia-smi -pm 1
API连接失败:
New-NetFirewallRule -DisplayName "OllamaAPI" -Direction Inbound -LocalPort 11434 -Protocol TCP -Action Allow
netstat -ano | findstr 11434
内存优化:
--memory参数限制模型内存占用GPU加速:
# 安装TensorRTwinget install nvidia.tensorrt# 启用优化Set-ItemProperty -Path "HKLM:\SOFTWARE\Ollama" -Name "EnableTensorRT" -Value 1
通过配置ollama serve命令实现多模型并行:
ollama serve --models deepseek-r1:7b,deepseek-r1:13b --port 11434,11435
启用API认证:
// 在config.json中添加"auth": {"enabled": true,"api_key": "your-secret-key"}
限制访问IP:
New-NetFirewallRule -DisplayName "OllamaAPI_IPRestrict" -Direction Inbound -RemoteAddress 192.168.1.0/24 -LocalPort 11434 -Protocol TCP -Action Allow
本指南通过系统化的步骤分解和实操示例,完整呈现了从环境搭建到模型部署的全流程。实际部署时建议先在测试环境验证,再逐步迁移到生产环境。对于企业级应用,可考虑结合Kubernetes实现容器化部署,通过ollama-operator实现自动化管理。