在 Cursor 中使用 DeepSeek 模型:完整设置指南

DeepSeek 模型以具有竞争力的价格提供强大的推理能力。Cursor 通过多种方法支持 DeepSeek 集成,从原生 API 支持到通过 Ollama 进行本地部署。本指南涵盖在您的 Cursor 工作流程中使用 DeepSeek 的所有方法。
可用的 DeepSeek 模型
| 模型 | 上下文 | 最适合 | Cursor 支持 |
|---|---|---|---|
| DeepSeek V3 | 64K | 通用编码、聊天 | 原生 |
| DeepSeek R1 | 64K | 推理、数学、逻辑 | 原生 |
| DeepSeek V4 | 128K | 复杂分析 | 通过 Ollama/代理 |
| DeepSeek Coder | 16K | 代码特定任务 | 通过 OpenRouter |
方法 1:原生 DeepSeek 支持(推荐)
Cursor 内置支持 DeepSeek V3 和 R1。
步骤 1:获取 API 密钥
- 访问 DeepSeek 平台
- 创建账户或登录
- 导航到 "API 密钥"
- 生成新密钥并复制
步骤 2:在 Cursor 中配置
- 打开 Cursor 设置 (
Cmd/Ctrl + ,) - 转到 "模型" 或 "AI 功能"
- 找到 "自定义 API 密钥" 或 "添加提供商"
- 选择 "DeepSeek" 作为提供商
- 粘贴您的 API 密钥
- 保存设置
步骤 3:选择 DeepSeek 模型
在任何聊天或 Composer 会话中:
- 点击模型选择器(聊天顶部)
- 选择 "DeepSeek V3" 或 "DeepSeek R1"
- 开始编码
方法 2:通过 OpenRouter 使用 DeepSeek
对于原生不支持的模型,使用 OpenRouter 作为桥梁。
设置
- 获取 OpenRouter API 密钥
- 在 Cursor 设置中,添加 OpenRouter 作为提供商:
{
"customModels": [
{
"name": "deepseek/deepseek-chat",
"provider": "openrouter",
"apiKey": "your-openrouter-key"
}
]
}
可用的 OpenRouter DeepSeek 模型
deepseek/deepseek-chat # DeepSeek V3
deepseek/deepseek-reasoner # DeepSeek R1
deepseek/deepseek-coder # DeepSeek Coder
方法 3:使用 Ollama 的本地 DeepSeek
运行本地 DeepSeek 以实现隐私和离线使用。
安装 Ollama
# macOS
brew install ollama
# Linux
curl -fsSL https://ollama.com/install.sh | sh
# Windows
# 从 https://ollama.com/download 下载
拉取 DeepSeek 模型
# DeepSeek Coder(推荐用于编码)
ollama pull deepseek-coder:6.7b
# 用于更大的上下文(需要更多 RAM)
ollama pull deepseek-coder:33b
为 Ollama 配置 Cursor
- 确保 Ollama 正在运行:
ollama serve
- 在 Cursor 设置中,添加本地模型:
{
"customModels": [
{
"name": "deepseek-coder",
"provider": "ollama",
"baseUrl": "http://localhost:11434"
}
]
}
Ollama 连接故障排除
如果 Cursor 无法连接到 Ollama:
# 检查 Ollama 是否正在运行
curl http://localhost:11434/api/tags
# 设置 Ollama 主机的环境变量
export OLLAMA_HOST=0.0.0.0
# 在 macOS 上,允许网络访问
launchctl setenv OLLAMA_HOST "0.0.0.0"
方法 4:通过 LiteLLM 代理使用 DeepSeek V4
对于 DeepSeek V4 Pro(云端),使用 LiteLLM 作为代理。
设置 LiteLLM
pip install litellm
创建 litellm_config.yaml:
model_list:
- model_name: deepseek-v4
litellm_params:
model: deepseek/deepseek-chat-v4
api_key: os.environ/DEEPSEEK_API_KEY
运行代理:
litellm --config litellm_config.yaml --port 8000
配置 Cursor
{
"customModels": [
{
"name": "deepseek-v4",
"provider": "openai-compatible",
"apiKey": "sk-any",
"baseUrl": "http://localhost:8000"
}
]
}
处理推理内容
DeepSeek R1 输出需要特殊处理的推理内容。
问题
R1 返回推理和最终答案:
{
"choices": [{
"message": {
"content": "最终答案在这里...",
"reasoning_content": "让我想想... 步骤 1... 步骤 2..."
}
}]
}
解决方案:使用代理过滤
使用简单代理去除推理内容:
# deepseek_proxy.py
from flask import Flask, request, Response
import requests
app = Flask(__name__)
@app.route('/v1/chat/completions', methods=['POST'])
def proxy():
resp = requests.post(
'https://api.deepseek.com/v1/chat/completions',
headers={'Authorization': f'Bearer {API_KEY}'},
json=request.get_json()
)
data = resp.json()
# 去除 reasoning_content
for choice in data.get('choices', []):
msg = choice.get('message', {})
msg.pop('reasoning_content', None)
return Response(
json.dumps(data),
mimetype='application/json'
)
为编码优化 DeepSeek
系统提示
将此添加到您的 Cursor 规则以获得更好的 DeepSeek 性能:
使用 DeepSeek 模型时:
- 明确说明文件路径和行号
- 为复杂任务请求逐步推理
- 使用结构化输出格式(JSON、markdown 表格)
- 指定预期的响应长度
温度设置
| 任务 | 推荐温度 |
|---|---|
| 代码生成 | 0.1 - 0.3 |
| 调试 | 0.2 - 0.4 |
| 创意探索 | 0.5 - 0.7 |
| 代码审查 | 0.1 - 0.2 |
成本比较
| 模型 | 输入/1M 令牌 | 输出/1M 令牌 |
|---|---|---|
| DeepSeek V3 | $0.14 | $0.28 |
| DeepSeek R1 | $0.55 | $2.19 |
| GPT-4o | $5.00 | $15.00 |
| Claude 3.5 Sonnet | $3.00 | $15.00 |
截至 2026 年的价格。DeepSeek 明显更具成本效益。
故障排除
| 问题 | 解决方案 |
|---|---|
| "模型不可用" | 检查 API 密钥是否有效且有信用额度 |
| 响应缓慢 | 使用更小的模型或启用流式传输 |
| 推理内容可见 | 使用代理或过滤器(见上文) |
| Ollama 连接被拒绝 | 确保 Ollama 正在运行且端口可访问 |
| 中文输出 | 在提示中添加 "Respond in English" |
快速参考
# 测试 DeepSeek API
curl https://api.deepseek.com/v1/chat/completions \
-H "Authorization: Bearer $DEEPSEEK_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-chat",
"messages": [{"role": "user", "content": "Hello"}]
}'
# 列出 Ollama 模型
ollama list
# 检查 Ollama 状态
curl http://localhost:11434/api/tags