GitHub - magiclyde/code-editing-agent: How to Build an Agent

这是一个基于大语言模型(LLM)构建的自主代码编辑代理。
本项目深受 Amp Code 启发,并在此基础上进行了重构,通过抽象后端接口实现了对云端与本地模型的双重支持。

"It's an LLM, a loop, and enough tokens." — Thorsten Ball

🚀 核心改进:多后端支持 (Multi-Backend)

为了避免供应商绑定(Vendor Lock-in)并提升灵活性,本项目引入了 ModelBackend 接口。 这允许 Agent 在不同的推理引擎之间无缝切换:

  • Anthropic Claude: 利用 Claude 3.5 Sonnet 强大的推理与工具调用(Tool Use)能力处理复杂任务。
  • Ollama (Local): 支持在本地运行开源模型(如 Llama 3, Qwen 等),确保代码隐私并降低 API 开销。

🏗 系统架构

Architecture

技术实现细节

  • backend.go: 定义了统一的 ModelBackend 接口,规定了如何发送消息以及如何处理工具调用。
  • backend_anthropic.go: 针对 Anthropic Messages API 的具体封装。
  • backend_ollama.go: 针对本地 Ollama API 的适配实现。

✨ 功能特性

  • 自主工具调用:Agent 可以自主决定何时使用 read_file, list_files, write_file 等工具。
  • 命令执行:具备在终端执行 go test 或其他 shell 命令的能力,实现“编写-运行-修复”的闭环。
  • 上下文感知:在整个会话中保持对项目结构的理解。
  • 命令行交互:支持通过参数动态指定后端模型和 API 参数。

🛠 快速开始

1. 克隆仓库

git clone https://github.com/magiclyde/code-editing-agent.git
cd code-editing-agent

2. 运行 Agent

你可以通过 -backend 参数动态选择你偏好的推理引擎:

方案 A:使用 Anthropic (云端)

export ANTHROPIC_API_KEY='your-api-key'
go run . -backend anthropic -model claude-3-5-sonnet-20240620

方案 B:使用 Ollama (本地)

# 请确保本地 Ollama 服务已启动
go run . -backend ollama -model llama3

📖 References