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
$

...Or Buy With Credits!

You can get credits with a paid monthly or annual Reader Membership, or you can buy them here.
PDF
EPUB
WEB
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

Owen Ou

Owen Ou 是被数百万开发者每天使用的JSON处理器 `jq` 的维护者。在过去十多年里,他在 CrowdStrike、Heroku、Salesforce 和 Amazon 等公司构建开发者工具和平台。

他写这本书是为了让一线工程师能够理解AI代理的本质。他坚信:如果你无法用 `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 密钥
  2. 密钥保管(.env)
  3. 请求的解析
  4. 代码
  5. 运行程序
  6. 故障排除
  7. 清理工作
  8. 总结

第三章:无限循环

  1. 记忆的假象
  2. 测试问题
  3. 响应类型
  4. 模拟大脑模式
  5. 定义成功标准
  6. Claude 类
  7. Agent 类(更新版)
  8. 主循环(已更新)
  9. 验证测试是否通过
  10. 测试内存
  11. 上下文窗口问题
  12. 总结

第4章:通用适配器

  1. 适配器模式
  2. HTTP 弹性处理
  3. 大脑接口
  4. FakeBrain(已更新)
  5. Claude 大脑(重构版)
  6. DeepSeek大脑
  7. BRAINS注册表
  8. Agent 类(更新版)
  9. 多智能模型支持的测试
  10. 主循环(已更新)
  11. 设置 DeepSeek
  12. 试一试
  13. “我们只是移动了代码位置”
  14. 总结

第二部分:实践之手

第5章:工具协议

  1. 工具实际如何工作
  2. 定义工具接口
  3. ReadFile工具
  4. 写入文件工具
  5. 工具辅助函数
  6. 更新Thought类
  7. 更新 Claude 类
  8. 带工具的智能代理类
  9. 主循环
  10. 测试一下
  11. 总结

第6章:记事本(内存)

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

第7章:安全防护机制(计划模式)

  1. 概念
  2. 先写测试
  3. 扩展 ToolContext
  4. 受保护的 WriteFile 工具
  5. 代理类(已更新)
  6. 主循环(更新版)
  7. 测试框架
  8. “计划“的心理学
  9. 总结

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

  1. ListFiles工具
  2. 搜索代码库工具
  3. 更新工具列表
  4. “深入探索“测试
  5. 等等,这是RAG吗?
  6. 总结

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

  1. 反馈循环
  2. 先有测试
  3. 命令执行工具
  4. 交互式陷阱
  5. 自我修复演示
  6. TDD工作流程
  7. 精准修改
  8. 闭环
  9. 上下文压缩
  10. 安全注意事项
  11. 总结

第三部分:探索前沿

第十章:转向本地(本地模型)

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

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

  1. 第1步:元提示
  2. 第2步:手术过程
  3. 步骤3:参考实现
  4. 步骤 4:测试
  5. 自我修改
  6. 总结

第12章:终章(构建一个游戏)

  1. 第1步:准备工作
  2. 第2步:设计师(规划模式)
  3. 第3步:构建器(执行模式)
  4. 第4步:现实检查
  5. 第5步:转折点(功能蔓延)
  6. 可能出现的问题
  7. 总结
  8. 结语

致谢

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 $14 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