Kick off your book project in 3 hours! Live workshop on Zoom. You’ll leave with a real book project, progress on your first chapter, and a clear plan to keep going. Saturday, May 16, 2026. Learn more…

Leanpub Header

Skip to main content

构建你自己的编程智能助手

零魔法:纯Python实现人工智能代理指南

跳过黑箱框架。用纯 Python 从零构建生产级 AI 编程智能体——云端或本地,用 pytest 测试,全部在一个文件中完成。

This book is a translation into Chinese (Simplified) of Build Your Own Coding Agent which was originally written in English

Minimum price

$19.99

$34.99

You pay

$34.99

Authors earn

$27.99
$
You can also buy this book with 1 book credit. Get book credits with a Reader Membership or an Organization Membership for your team.
About

About

About the Book

你是一名对 AI 炒作持怀疑态度的软件工程师。

你看过演示,试过框架,亲眼看着你的 LangChain 应用一路幻觉到崩溃。然后你想:"一定有更好的方法。"

确实有。

这本书是对"魔法"的反叛。它教你用纯 Python 从零开始构建一个生产级的 AI 编程智能体。没有 LangChain,没有 Pydantic,没有向量数据库。只有你能用 print() 调试的代码。

读完本书,你将构建出 Nanocode,一个基于终端的编程智能体,它能:

  • 读取、写入和精确编辑代码库中的文件
  • 执行 shell 命令并从错误中自我修正
  • 用纯 Python 映射和搜索代码
  • 通过持久化记事本在会话间记忆上下文
  • 在执行危险操作前请求许可
  • 在网络上搜索最新信息

你将了解到,一个智能体只由四样东西组成:

  1. 大脑 —— 一个无状态的 API 调用(Claude、DeepSeek 或 Ollama)
  2. 工具 —— Python 函数(Read、Write、Edit、Run、Search)
  3. 记忆 —— 一个作为系统提示注入的自修改 Markdown 文件
  4. 循环 —— 一个把一切串联起来的 while True

在这个过程中,你将测试一切。你将构建一个 FakeBrain 测试替身,让你无需消耗 API 额度就能即时运行完整的 pytest 测试套件,验证复杂的智能体逻辑。你可以用一条命令在云端模型和本地模型之间切换,通过 Ollama 在自己的笔记本电脑上免费运行大脑。

在最后一章,你将使用你的智能体在 Pygame 中构建一个完整的贪吃蛇游戏,而你自己不写一行代码。你说出计划,智能体编写、运行、调试和修复代码,直到游戏可以运行。

如果你能读懂 Python,并且想了解 AI 智能体内部到底在发生什么,这本书就是为你写的。

This book is a translation into Chinese (Simplified) of Build Your Own Coding Agent which was originally written in English

Author

About the Authors

J. Owen

J. Owen 拥有十多年构建开发者工具、CLI 实用程序和后端平台的经验,服务于数百万工程师。他写这本书是为了帮助在职专业人士揭开 AI 智能体的神秘面纱,将务实的系统工程思维带入 LLM 的世界。如果你不能用 print() 调试它,它就不该上生产环境。

TranslateAI

Leanpub now has a TranslateAI service which uses AI to translate their book from English into up to 31 languages, or from one of those 31 languages into English. We also have a GlobalAuthor bundle which uses TranslateAI to translate English-language books into either 8 or 31 languages.

Leanpub exists to serve our authors. We want to help you reach as many readers as possible, in their preferred language. So, just as Leanpub automates the process of publishing a PDF and EPUB ebook, we've now automated the process of translating those books!

Translations

Translations

Contents

Table of Contents

前言

  1. 本书适合谁
  2. 你将构建什么
  3. 测试方法
  4. 代码示例
  5. 本书使用的约定

第一部分:大脑

第一章:零魔法宣言

  1. 智能体到底是什么?
  2. 我们在构建什么
  3. 项目搭建
  4. AgentStop 异常
  5. Agent 类
  6. 通过测试定义成功标准
  7. 主循环
  8. 运行程序
  9. 小结

第二章:原始请求

  1. 获取 API Key
  2. 保险库(.env)
  3. 请求的结构
  4. 代码
  5. 运行程序
  6. 故障排查
  7. 清理工作
  8. 小结

第三章:无限循环

  1. 记忆的幻觉
  2. 测试的难题
  3. 响应类型
  4. FakeBrain 模式
  5. 定义成功
  6. Claude 类
  7. Agent 类(更新版)
  8. 主循环(已更新)
  9. 验证测试通过
  10. 测试记忆
  11. 上下文窗口问题
  12. 本章小结

第四章:通用适配器

  1. 适配器模式
  2. HTTP 弹性
  3. Brain 接口
  4. FakeBrain(已更新)
  5. Claude 大脑(重构版)
  6. DeepSeek 大脑
  7. BRAINS 注册表
  8. Agent 类(更新版)
  9. 多大脑支持的测试
  10. 主循环(已更新)
  11. 配置 DeepSeek
  12. 试一试
  13. “我们不过是把代码挪了挪”
  14. 小结

第二部分:双手

第五章:工具协议

  1. 工具究竟是如何运作的
  2. 定义工具接口
  3. ReadFile 工具
  4. WriteFile 工具
  5. 工具辅助函数
  6. 更新 Thought 类
  7. 更新 Claude 类
  8. 带工具的 Agent 类
  9. 主循环
  10. 测试一下
  11. 总结

第六章:便签本(记忆)

  1. “零魔法“记忆
  2. Memory 类
  3. ToolContext 类
  4. SaveMemory 工具
  5. 更新 Claude 类
  6. 编写系统提示
  7. 更新 Agent 类
  8. 主循环(已更新)
  9. 测试持久性
  10. 总结

第七章:安全装置(计划模式)

  1. 核心概念
  2. 测试先行
  3. WritePlan 工具
  4. 一份列表,两种视图
  5. 告诉大脑当前处于哪种模式
  6. Agent 类(已更新)
  7. 主循环(更新版)
  8. 测试框架
  9. “计划“的心理学
  10. 小结

第8章:上下文管道(映射与搜索)

  1. ListFiles 工具
  2. SearchCodebase 工具
  3. 更新工具列表
  4. “放大查看“测试
  5. 等等,这是RAG吗?
  6. 小结

第九章:现实检验(运行代码)

  1. 反馈循环
  2. 测试先行
  3. RunCommand 工具
  4. 交互式陷阱
  5. 自我修复演示
  6. TDD 工作流程
  7. 外科手术式编辑
  8. 闭环
  9. 加固循环
  10. 上下文压缩
  11. 安全注意事项
  12. 总结

第三部分:前沿

第十章:离线运行(本地模型)

  1. 权衡取舍
  2. 安装 Ollama
  3. Ollama 大脑类
  4. 使用 Ollama 运行
  5. “无限循环“实验
  6. 实际差异
  7. 混合工作流
  8. 模型选择
  9. Ollama 故障排查
  10. 本章小结

第 11 章:扩展功能(网络搜索)

  1. 步骤 1:元提示词
  2. 第二步:手术
  3. 第三步:参考实现
  4. 第 4 步:测试
  5. 自我修改
  6. 小结

第 12 章:压轴章节(构建一个游戏)

  1. 第一步:准备工作
  2. 第二步:架构师(计划模式)
  3. 第 3 步:构建者(执行模式)
  4. 第四步:现实检验
  5. 第五步:转向(需求蔓延)
  6. 会出什么问题
  7. 结语
  8. 后记

附录 A:流式响应

  1. 流式传输的工作原理
  2. 具体实现
  3. 与第 11 章相比的变化
  4. 权衡取舍
  5. 运行代码

致谢

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub