DeepSeek 使用教程¶
什么是DeepSeek¶
DeepSeek是一家专注于AI研究的中国公司,开发了多个开源大语言模型,以高性价比和开源特性著称。2026年发布的DeepSeek V4 Pro性能接近闭源旗舰模型。
DeepSeek模型系列(2026年最新)¶
模型对比¶
| 模型 | 参数量 | 特点 | 适用场景 |
|---|---|---|---|
| DeepSeek V4 Pro | 671B(MoE) | 最强旗舰 | 复杂推理、编程 |
| DeepSeek V4 | 671B(MoE) | 开源最强 | 私有部署 |
| DeepSeek Coder V3 | 236B | 编程专用 | 代码生成、调试 |
| DeepSeek VL2 | - | 多模态 | 图像理解 |
模型特点¶
# DeepSeek的优势
1. 开源免费:模型权重开源,可本地部署
2. 性价比高:API价格仅为GPT-5.5的1/5
3. 中文优化:中文能力接近GPT-5.5
4. 编程能力:SWE-Bench得分接近Claude
5. 本地部署:支持Ollama等工具本地运行
使用DeepSeek的方式¶
1. 在线对话¶
访问 DeepSeek 官网使用在线版本,适合日常对话和简单任务。
2. API调用¶
import requests
import json
# DeepSeek API调用示例
def chat_with_deepseek(prompt, api_key):
url = "https://api.deepseek.com/v1/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"model": "deepseek-chat",
"messages": [
{"role": "user", "content": prompt}
],
"temperature": 0.7,
"max_tokens": 2000
}
response = requests.post(url, headers=headers, json=data)
return response.json()
# 使用示例
api_key = "your_api_key"
result = chat_with_deepseek("请解释什么是机器学习", api_key)
print(result["choices"][0]["message"]["content"])
3. 本地部署¶
DeepSeek在编程中的应用¶
代码生成¶
# 提示词示例
请用Python实现一个功能:
功能描述:实现一个简单的REST API服务器
技术要求:
- 使用Flask框架
- 支持CRUD操作
- 数据存储使用SQLite
- 包含错误处理
- 添加日志记录
请提供完整的代码实现,包括:
1. 项目结构
2. 主程序代码
3. 数据模型
4. API路由
5. 测试代码
代码解释¶
# 提示词示例
请解释以下Python代码的功能:
```python
def fibonacci(n):
if n <= 1:
return n
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
请说明: 1. 这段代码的功能是什么 2. 算法的时间复杂度 3. 空间复杂度 4. 如何优化
TypeError: 'NoneType' object is not iterable请分析错误原因并提供修复方案。
2. 代码补全¶
# DeepSeek-Coder支持代码补全
# 输入
def calculate_average(numbers):
"""计算数字列表的平均值"""
# DeepSeek会自动补全后续代码
total = sum(numbers)
return total / len(numbers)
3. 多轮对话¶
使用DeepSeek的最佳实践¶
提示词优化¶
清晰明确:
❌ 不好的提示:
"帮我写个程序"
✅ 好的提示:
"请用Python编写一个函数,功能是:
1. 输入:一个整数列表
2. 处理:过滤出所有偶数
3. 输出:返回过滤后的列表,按升序排列
4. 要求:添加类型注解和文档字符串"
分步骤:
角色扮演¶
DeepSeek与其他AI的对比(2026年最新)¶
| 特性 | DeepSeek V4 | GPT-5.5 | Claude Opus 4.7 |
|---|---|---|---|
| 开源 | ✅ | ❌ | ❌ |
| 中文支持 | 优秀 | 良好 | 良好 |
| 编程能力 | 强 | 强 | 最强 |
| 价格 | 最低 | 中 | 高 |
| 长文本 | 128K | 105万 | 200K |
| API价格 | ~\(1/\)3 | \(5/\)30 | \(15/\)75 |
常见问题¶
Q1: DeepSeek的API如何获取?¶
Q2: 本地部署需要什么配置?¶
Q3: 如何选择合适的模型?¶
实战示例¶
示例1:构建聊天机器人¶
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
DEEPSEEK_API_KEY = "your_api_key"
@app.route('/chat', methods=['POST'])
def chat():
user_message = request.json.get('message')
response = requests.post(
"https://api.deepseek.com/v1/chat/completions",
headers={
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
"Content-Type": "application/json"
},
json={
"model": "deepseek-chat",
"messages": [{"role": "user", "content": user_message}]
}
)
return jsonify(response.json()["choices"][0]["message"]["content"])
if __name__ == '__main__':
app.run(debug=True)
示例2:代码审查工具¶
def code_review(code, language="python"):
prompt = f"""
请审查以下{language}代码,提供:
1. 代码质量评估
2. 潜在问题
3. 优化建议
4. 安全风险
代码:
```{language}
{code}
```
"""
# 调用DeepSeek API
result = chat_with_deepseek(prompt, API_KEY)
return result
下一步学习¶
- Vibe Coding教程 - 学习AI辅助编程
- 探索DeepSeek的开源模型
- 尝试本地部署和微调