> For the complete documentation index, see [llms.txt](https://docs.clore.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clore.ai/guides/guides_v2-zh/ai-bian-cheng-gong-ju/aider.md).

# Aider AI 编程

在 Clore.ai 上基于终端的 AI 编程，使用 Aider——支持 Git 感知、多文件、本地模型，通过 Ollama 实现

Aider 是一款基于终端的 AI 编码助手，在 GitHub 上拥有 3.9 万以上星标。它可以直接在你的代码仓库中编辑文件，自动创建 Git 提交，并且同时支持云端 API（OpenAI、Anthropic）以及通过 Ollama 运行的完全本地模型。在 Clore.ai GPU 上，你可以完全在自己的硬件上运行 DeepSeek-R1 32B 或 Qwen2.5-Coder-32B 这类大型编码模型——私密、快速且成本效益高。

{% hint style="success" %}
所有示例都运行在通过以下方式租用的 GPU 服务器上： [CLORE.AI 市场](https://clore.ai/marketplace).
{% endhint %}

## 主要特性

* **原生终端** — 可通过 SSH 运行，非常适合无头的 Clore.ai 服务器
* **理解 Git** — 每次更改都会自动提交，并附带描述性信息，便于审查和回退
* **多文件编辑** — 将多个文件加入上下文并同时编辑它们
* **支持本地模型** — 连接到 Ollama，实现完全私密、零 API 成本的编码
* **架构模式** — 先用强推理模型进行规划，再用快速模型来实现
* **仓库映射** — 自动为你的代码库建立索引，以便进行上下文感知编辑
* **代码检查和测试** — 每次编辑后运行检查器/测试，自动修复失败项
* **语音输入** — 通过麦克风口述编码指令

## 需求

| 组件     | 最低             | 推荐             |
| ------ | -------------- | -------------- |
| GPU    | RTX 3060 12 GB | RTX 4090 24 GB |
| 显存     | 12 GB          | 24 GB          |
| 内存     | 16 GB          | 32 GB          |
| 磁盘     | 30 GB          | 60 GB          |
| Python | 3.9            | 3.11           |

**Clore.ai 价格：** RTX 4090 约 $0.14–0.42/小时 · RTX 3090 约 $0.07–0.21/小时 · RTX 3060 约 $0.03–0.07/小时

对于仅云端模型（无本地推理），不需要 GPU——但 Clore.ai GPU 允许你在本地运行 Ollama 模型，实现完全隐私。

## 快速开始

### 1. 安装 Aider

```bash
pip install aider-chat
```

### 2. 为本地模型设置 Ollama

```bash
# 安装 Ollama
curl -fsSL https://ollama.com/install.sh | sh

# 拉取一个编码模型
ollama pull deepseek-r1:32b

# 或使用更小的模型以降低显存占用
ollama pull qwen2.5-coder:7b
```

### 3. 使用本地模型启动 Aider

```bash
cd /workspace/your-project

# 通过 Ollama 使用 DeepSeek-R1 32B（需要约 20 GB 显存）
aider --model ollama/deepseek-r1:32b

# 或在 RTX 3060 上使用更小的模型
aider --model ollama/qwen2.5-coder:7b
```

### 4. 开始编码

在 Aider REPL 中：

```
> /add src/main.py src/utils.py
> 为 parse_config 函数添加错误处理并为其编写单元测试
```

Aider 将会：

1. 读取文件并理解代码库
2. 以 diff 形式提出更改
3. 将更改应用到磁盘
4. 创建一个带有描述性信息的 Git 提交

## 使用示例

### 架构模式（双模型设置）

使用一个强大的模型负责推理，另一个快速模型负责代码生成：

```bash
# 云端架构师 + 本地编辑器
aider --architect --model ollama/deepseek-r1:32b --editor-model ollama/qwen2.5-coder:7b
```

架构师模型负责规划更改，而编辑器模型编写实际代码——将高质量推理与快速实现结合起来。

### 添加文件并编辑

```bash
# 将特定文件添加到聊天上下文中
> /add src/api/routes.py src/models/user.py

# 请求更改
> 将用户注册端点重构为使用 async/await，并使用 Pydantic 添加输入验证

# 添加整个目录
> /add src/tests/

# 编辑后运行测试
> /test pytest src/tests/ -v
```

### 与云端 API 一起使用

```bash
# OpenAI
export OPENAI_API_KEY=sk-...
aider --model gpt-4o

# Anthropic
export ANTHROPIC_API_KEY=sk-ant-...
aider --model claude-sonnet-4-20250514
```

### Git 集成

```bash
# 每次更改都会自动创建一个提交
git log --oneline -5
# a1b2c3d aider: 为 parse_config 添加错误处理
# d4e5f6g aider: 为 parse_config 编写单元测试
# h7i8j9k aider: 重构用户注册端点

# 撤销上一次 aider 更改
aider --undo
```

### 代码检查并自动修复

```bash
# 配置一个代码检查器
aider --lint-cmd "ruff check --fix" --auto-lint

# Aider 在每次编辑后运行代码检查器并自动修复问题
```

### 非交互式（脚本）模式

```bash
# 运行单条指令并退出
aider --model ollama/deepseek-r1:32b \
  --message "为 src/utils.py 中的所有函数添加类型注解" \
  --yes  # 自动接受更改
```

## 模型推荐

| 模型                    | 显存      | 速度 | 质量 | 最适合           |
| --------------------- | ------- | -- | -- | ------------- |
| deepseek-r1:32b       | 约 20 GB | 中等 | 高  | 复杂重构          |
| qwen2.5-coder:32b     | 约 20 GB | 中等 | 高  | 代码生成          |
| qwen2.5-coder:7b      | \~5 GB  | 快  | 好  | 快速编辑，RTX 3060 |
| codellama:34b         | 约 20 GB | 中等 | 好  | 旧代码，C/C++     |
| deepseek-coder-v2:16b | 约 10 GB | 快  | 好  | 性能均衡          |

## 提示

* **使用 `/add` 有选择地** — 只添加 Aider 需要查看的文件。文件过多会浪费上下文 token
* **架构模式** 对于复杂更改非常强大——推理模型能捕捉到编辑器模型可能遗漏的边缘情况
* **`/undo`** 通过 Git 干净地还原上一次更改——可放心大胆地尝试
* **`/diff`** 在应用前显示拟议更改——用于审查
* **设置 `--auto-commits`** （默认）为每一次 AI 更改保留完整 Git 历史
* **使用 `.aiderignore`** 用于将文件排除在仓库映射之外（node\_modules、.venv 等）
* **对于大型仓库**，Aider 的仓库映射可帮助模型理解代码结构——让它在首次加载时运行
* **编辑后运行测试** — `/test pytest` 可立即捕获回归问题

## 故障排查

| 问题                    | 解决方案                                                      |
| --------------------- | --------------------------------------------------------- |
| Ollama 模型太慢           | 使用更小的量化版本（q4\_0）或更小的模型                                    |
| `CUDA 内存不足` 配合 Ollama | 拉取一个更小的模型变体，或者使用 `OLLAMA_NUM_GPU=0` 用于 CPU                |
| Git 提交错误              | 确保 `git config user.email` 以及 `user.name` 已设置             |
| Aider 忽略了我的文件         | 使用 `/add filename.py` 显式添加——Aider 只会编辑已添加的文件              |
| 模型生成的编辑效果很差           | 尝试更强的模型，或使用架构模式                                           |
| 连接被拒绝（Ollama）         | 确保 Ollama 正在运行： `ollama serve` 或 `systemctl start ollama` |
| 上下文窗口已超出              | 使用以下命令移除文件 `/drop`，只保留相关文件                                |

## 资源

* [Aider GitHub](https://github.com/Aider-AI/aider)
* [Aider 文档](https://aider.chat)
* [Ollama 模型库](https://ollama.com/library)
* [CLORE.AI 市场](https://clore.ai/marketplace)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/ai-bian-cheng-gong-ju/aider.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
