引言
DeepSeek-TUI 是深度求索(DeepSeek)推出的开源终端 AI 编程助手。与 Claude Code 类似,它让你在终端中通过自然语言和 AI 对话,完成代码生成、文件编辑、命令执行等开发任务。更重要的是,DeepSeek 提供了免费 API 额度,使得开发者可以零成本体验终端 AI 编程的便捷。
本文将手把手教你从零开始安装和配置 DeepSeek-TUI。
安装前的准备
系统要求
| 项目 | 要求 |
|---|---|
| 操作系统 | Linux / macOS / Windows (WSL2) |
| Python | 3.9 及以上版本 |
| 终端 | 支持 ANSI 彩色输出(大部分现代终端都支持) |
| 网络 | 能够访问 DeepSeek API 服务 |
| 磁盘空间 | 约 100MB(含依赖) |
验证 Python 环境
首先确认你的 Python 版本符合要求:
# 检查 Python 版本
python3 --version
# 预期输出: Python 3.9.x 或更高
# 检查 pip 是否安装
python3 -m pip --version
# 预期输出: pip xx.x from ...
# 检查 pip 版本,建议升级到最新
python3 -m pip install --upgrade pip
Windows 用户请注意:DeepSeek-TUI 需要在类 Unix 环境中运行。如果你在 Windows 上,请安装 WSL2(Windows Subsystem for Linux),然后在 WSL2 终端中操作。
配置 WSL2(Windows 用户)
# 在 Windows PowerShell(管理员)中执行
# 安装 WSL2
wsl --install
# 设置默认版本
wsl --set-default-version 2
# 安装 Ubuntu 发行版
wsl --install -d Ubuntu
# 重启后进入 WSL2 终端
wsl
安装 DeepSeek-TUI
方式一:使用 pip 安装(推荐)
# 直接在终端中安装
pip install deepseek-tui
# 如果遇到权限问题,使用 --user
pip install --user deepseek-tui
# 或者使用虚拟环境
python3 -m venv deepseek-env
source deepseek-env/bin/activate # macOS/Linux
# 或 deepseek-env\Scripts\activate # Windows
pip install deepseek-tui
方式二:从源码安装
如果你想要体验最新开发版功能,可以从 GitHub 克隆源码安装:
# 克隆仓库
git clone https://github.com/deepseek-ai/deepseek-tui.git
cd deepseek-tui
# 安装依赖
pip install -r requirements.txt
# 以开发模式安装
pip install -e .
# 或直接运行
python -m deepseek_tui
验证安装
安装完成后,验证是否可以正常启动:
# 查看版本信息
deepseek-tui --version
# 查看帮助
deepseek-tui --help
如果看到版本号输出,说明安装成功。
配置 DeepSeek API
1. 注册 DeepSeek 账号
前往 DeepSeek 开放平台 注册账号:
1. 打开 https://platform.deepseek.com/
2. 点击"注册",使用手机号或邮箱注册
3. 完成实名认证(中国用户需要)
2. 获取 API Key
# 登录后进入 API Keys 页面
# 点击"创建 API Key"
# 复制并保存生成的 Key(例如: sk-xxxxxxxxxxxxxxxxxxxxx)
⚠️ 安全提醒:API Key 是你的身份凭证,不要提交到公共代码仓库中。建议使用环境变量或配置文件管理。
3. 配置 API Key
DeepSeek-TUI 支持多种方式配置 API Key:
方案一:环境变量(推荐)
# 临时设置(当前终端会话有效)
export DEEPSEEK_API_KEY="sk-your-api-key-here"
# 永久设置(写入 Shell 配置文件)
echo 'export DEEPSEEK_API_KEY="sk-your-api-key-here"' >> ~/.bashrc
source ~/.bashrc
# ZSH 用户
echo 'export DEEPSEEK_API_KEY="sk-your-api-key-here"' >> ~/.zshrc
source ~/.zshrc
方案二:配置文件
# 创建 DeepSeek 配置目录
mkdir -p ~/.config/deepseek
# 创建配置文件
cat > ~/.config/deepseek/config.json << 'EOF'
{
"api_key": "sk-your-api-key-here",
"model": "deepseek-chat",
"temperature": 0.7,
"max_tokens": 4096,
"theme": "auto",
"stream": true
}
EOF
# 设置配置文件权限(只有你能读取)
chmod 600 ~/.config/deepseek/config.json
方案三:启动时指定
# 直接在启动命令中传入
DEEPSEEK_API_KEY="sk-your-api-key-here" deepseek-tui
4. 使用免费 API 额度
DeepSeek 为新用户提供免费 API 额度:
| 项目 | 说明 |
|---|---|
| 免费额度 | 新注册用户赠送 ¥10 体验金 |
| 模型定价 | deepseek-chat: ¥1.0/百万 tokens(输入+输出) |
| 有效期 | 体验金有效期为 180 天 |
| 可调用次数 | 约 250 万 tokens(取决于实际使用情况) |
# 检查账户余额(需要 curl)
curl -X GET "https://api.deepseek.com/v1/account/balance" \
-H "Authorization: Bearer $DEEPSEEK_API_KEY"
# 返回值示例
# {
# "balance": 8.50,
# "currency": "CNY",
# "total_usage": 1.50
# }
启动 DeepSeek-TUI
基本启动
# 在项目目录中启动
cd /path/to/your/project
deepseek-tui
# 如果使用虚拟环境
source deepseek-env/bin/activate
deepseek-tui
启动后的界面结构:
┌─────────────────────────────────────────────────────────┐
│ DeepSeek-TUI v1.0.0 模型: deepseek-chat │
├─────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ ? 你好,欢迎使用 DeepSeek-TUI! │ │
│ │ │ │
│ │ 我可以帮你完成以下操作: │ │
│ │ · 编写和修改代码 │ │
│ │ · 执行 Shell 命令 │ │
│ │ · 搜索文件内容 │ │
│ │ · 解答编程问题 │ │
│ │ · 代码审查和重构 │ │
│ │ │ │
│ │ 有什么需要帮助的吗? │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ > _ │ │
│ │ Ctrl+C 退出 | Ctrl+L 清屏 | / 命令模式 │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────┘
常用操作快捷键
| 快捷键 | 功能 |
|---|---|
Enter | 发送消息 |
Shift + Enter | 换行 |
Ctrl + C | 退出程序 |
Ctrl + L | 清屏 |
Ctrl + D | 结束当前输入 |
↑/↓ | 历史消息导航 |
Tab | 自动补全 |
/ 开头 | 进入命令模式 |
命令模式
DeepSeek-TUI 支持命令模式,以 / 开头:
# 查看可用命令
/help
# 清空对话历史
/clear
# 查看当前会话信息
/info
# 切换模型
/model deepseek-coder
# 设置温度参数
/temperature 0.3
# 保存当前会话
/save my-session
# 加载历史会话
/load my-session
# 查看 Token 使用统计
/stats
# 退出
/exit
常用配置项详解
配置文件路径
DeepSeek-TUI 的配置文件默认位于:
| 系统 | 配置文件路径 |
|---|---|
| Linux | ~/.config/deepseek/config.json |
| macOS | ~/.config/deepseek/config.json |
| Windows (WSL) | ~/.config/deepseek/config.json |
| 自定义路径 | 通过 --config 参数指定 |
完整配置选项
{
// API 配置
"api_key": "sk-your-api-key-here",
"base_url": "https://api.deepseek.com/v1",
"model": "deepseek-chat",
// 模型参数
"temperature": 0.7,
"max_tokens": 4096,
"top_p": 0.95,
"frequency_penalty": 0.0,
"presence_penalty": 0.0,
// 会话配置
"max_history": 50,
"max_context_tokens": 32000,
"auto_save": true,
"save_path": "~/.config/deepseek/sessions",
// 工具配置
"tools": {
"read_file": { "enabled": true },
"write_file": { "enabled": true },
"execute_command": {
"enabled": true,
"allowed_commands": [
"git", "python", "pip", "npm", "node",
"ls", "cat", "grep", "find", "echo",
"mkdir", "touch", "cp", "mv"
],
"blocked_patterns": [
"rm -rf /", "sudo", "chmod 777",
"dd if=", "mkfs", "fdisk"
],
"confirm_before_execute": true,
"timeout": 30
},
"search_files": { "enabled": true }
},
// 界面配置
"theme": "auto",
"code_theme": "monokai",
"font_size": 14,
"show_line_numbers": true,
"stream": true,
"animation": true,
// 代理配置(如需要)
"proxy": {
"http": "http://127.0.0.1:7890",
"https": "http://127.0.0.1:7890"
}
}
配置项说明
核心配置
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
api_key | string | — | DeepSeek API Key(必填) |
model | string | "deepseek-chat" | 使用的模型名称 |
temperature | float | 0.7 | 生成随机性 (0.0~2.0) |
max_tokens | int | 4096 | 单次最大输出 Token 数 |
stream | bool | true | 是否启用流式输出 |
安全配置
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
tools.execute_command.enabled | bool | true | 是否允许执行命令 |
tools.execute_command.confirm_before_execute | bool | true | 高危命令是否确认 |
tools.execute_command.timeout | int | 30 | 命令执行超时(秒) |
模型选择建议
可用模型
| 模型名称 | 类型 | 特性 | 定价 (¥/M tokens) | 推荐场景 |
|---|---|---|---|---|
deepseek-chat | 通用对话 | 综合能力强,适合大多数场景 | 1.0 | 日常首选 |
deepseek-coder | 代码专用 | 代码生成和理解能力最强 | 1.0 | 复杂编程任务 |
deepseek-reasoner | 推理模型 | 长思维链,适合复杂推理 | 2.0 | 架构设计、调试 |
deepseek-chat-202505 | 最新版 | 最新优化版本 | 1.0 | 追求最新能力 |
场景化推荐
# 日常开发:通用对话模型
deepseek-tui --model deepseek-chat
# Code Review 和复杂代码生成:代码专用模型
deepseek-tui --model deepseek-coder
# 架构设计和疑难 Bug 排查:推理模型
deepseek-tui --model deepseek-reasoner
# 切换模型(运行中)
# 输入 /model deepseek-coder
进阶配置
多 API Key 轮询
如果你有多个 API Key,可以配置轮询策略以提高可用性:
{
"api_keys": [
"sk-key-1",
"sk-key-2",
"sk-key-3"
],
"api_key_rotation": "round_robin",
"quota_check": true,
"auto_fallback": true
}
自定义工具
你可以在配置中添加自定义工具:
{
"custom_tools": [
{
"name": "format_code",
"description": "使用 black 格式化 Python 代码",
"command": "black {path}",
"parameters": {
"path": {
"type": "string",
"description": "文件路径"
}
}
},
{
"name": "run_tests",
"description": "运行项目的测试用例",
"command": "pytest {path} -v --tb=short",
"parameters": {
"path": {
"type": "string",
"description": "测试文件路径,可选"
}
}
}
]
}
环境配置多场景
针对不同项目,可以创建多份配置文件:
# 创建不同场景的配置
mkdir -p ~/.config/deepseek/profiles
# Python 项目配置
cat > ~/.config/deepseek/profiles/python.json << 'EOF'
{
"model": "deepseek-coder",
"temperature": 0.3,
"custom_tools": [
{"name": "run_pytest", "command": "pytest"}
]
}
EOF
# 前端项目配置
cat > ~/.config/deepseek/profiles/frontend.json << 'EOF'
{
"model": "deepseek-chat",
"temperature": 0.5,
"custom_tools": [
{"name": "run_eslint", "command": "npx eslint {path}"},
{"name": "run_prettier", "command": "npx prettier --write {path}"}
]
}
EOF
# 按项目加载配置
cd /path/to/python-project
deepseek-tui --profile python
cd /path/to/frontend-project
deepseek-tui --profile frontend
常见问题排查
Q1: 安装失败
问题: pip install deepseek-tui 报错
解决方案:
# 1. 确保 pip 是最新版
python3 -m pip install --upgrade pip setuptools wheel
# 2. 尝试使用国内镜像(中国大陆用户)
pip install deepseek-tui -i https://pypi.tuna.tsinghua.edu.cn/simple
# 3. 如果仍然失败,查看详细错误
pip install deepseek-tui -v
# 4. 某些依赖需要编译环境
# Ubuntu/Debian
sudo apt-get install python3-dev build-essential
# macOS
xcode-select --install
Q2: API 连接失败
错误信息: Failed to connect to API server
排查步骤:
# 1. 检查网络连接
curl -I https://api.deepseek.com/v1
# 2. 检查 API Key 是否正确
echo $DEEPSEEK_API_KEY
# 3. 测试 API 请求
curl -X POST "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"}]}'
# 4. 检查账户余额
curl -X GET "https://api.deepseek.com/v1/account/balance" \
-H "Authorization: Bearer $DEEPSEEK_API_KEY"
Q3: 命令执行被拒绝
错误信息: Command blocked by security policy
解决方案:
# 方案1:在配置中添加白名单
# 编辑 ~/.config/deepseek/config.json
# 在 tools.execute_command.allowed_commands 中添加所需命令
# 方案2:临时放宽限制(不安全)
deepseek-tui --unsafe-mode
# 方案3:确认执行(如果配置了 confirm)
# 当提示确认时,输入 y 确认执行
Q4: 中文显示乱码
问题: 终端中中文显示为乱码或方块
解决方案:
# 1. 检查系统 locale
locale
# 2. 设置 UTF-8 编码
export LANG=zh_CN.UTF-8
export LC_ALL=zh_CN.UTF-8
# 3. 生成中文 locale(Ubuntu/Debian)
sudo locale-gen zh_CN.UTF-8
# 4. 安装中文字体(如需要)
sudo apt-get install fonts-noto-cjk
Q5: 429 Too Many Requests
错误信息: Rate limit exceeded (429)
解决方案:
{
// 在配置中增加请求间隔
"rate_limit": {
"requests_per_minute": 20,
"retry_on_429": true,
"retry_delay": 5
}
}
Q6: 上下文窗口溢出
错误信息: Context length exceeds maximum
解决方案:
# 1. 清空对话历史
/clear
# 2. 减少 max_context_tokens
# 在配置中设置
"max_context_tokens": 16000
# 3. 减少 max_history
"max_history": 20
最佳实践
日常使用工作流
# 1. 进入项目目录
cd ~/projects/my-app
# 2. 启动 DeepSeek-TUI
deepseek-tui
# 3. 开始对话
# "帮我分析这个项目的架构"
# "在 src/utils 下创建一个 helpers.py"
# "解释这段代码的作用"
# "运行测试并修复失败的用例"
# 4. 保存会话(后续可恢复)
/save my-session-name
# 5. 查看本次使用的 Token
/stats
效率提升技巧
# 在 VS Code 终端中嵌入
# 打开 VS Code 的集成终端(Ctrl+`)
deepseek-tui
# 在 tmux/screen 中使用
# 创建一个独立的会话
tmux new -s deepseek-session
deepseek-tui
# 配合文件监视
# 先让 DeepSeek-TUI 修改文件,再用其他工具实时查看效果
总结
本文详细介绍了 DeepSeek-TUI 的安装和配置全过程。关键要点:
- 安装简单:一行
pip install deepseek-tui即可完成安装 - 零成本入门:DeepSeek 提供免费 API 额度,无需付费即可体验
- 配置灵活:支持环境变量、配置文件、命令行参数三种配置方式
- 安全可控:内置命令白名单和安全确认机制
- 场景适配:支持多配置文件,适配不同项目场景
DeepSeek-TUI 是一个功能强大且易于上手的终端 AI 编程助手。结合 DeepSeek 模型出色的中文能力和极具竞争力的定价,它已经成为中国开发者社区中备受欢迎的编程工具。
相关资源
本文最后更新于 2025-05-09,如有问题欢迎在社区讨论。