查看“︁30行代码构建你的第一个AI Agent”︁的源代码
←
30行代码构建你的第一个AI Agent
跳转到导航
跳转到搜索
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
<blockquote>"One loop & Bash is all you need" —— 一个工具 + 一个循环 = 一个智能体。</blockquote>在前一篇文章中,我们理解了 Agent 的本质:'''Agent 是模型,Harness 是框架'''。今天,让我们动手构建最简单的 Agent Harness。 只需要 30 行 Python 代码,你就能拥有一个真正能与现实世界交互的 AI Agent。 == 一、问题:模型被困在象牙塔里 == 大语言模型能推理代码,但碰不到真实世界——不能读文件、跑测试、看报错。 没有循环,每次工具调用你都得手动把结果粘回去。'''你自己就是那个循环。''' 这既不优雅,也不可扩展。我们需要让模型能够自主地与工具交互,形成一个闭环。 == 二、解决方案:Agent 循环 == 核心思想很简单: <code>+--------+ +-------+ +---------+ | User | ---> | LLM | ---> | Tool | | prompt | | | | execute | +--------+ +---+---+ +----+----+ ^ | | tool_result | +----------------+ (loop until stop_reason != "tool_use")</code> 一个退出条件控制整个流程。循环持续运行,直到模型不再调用工具。 == 三、工作原理 == === 1. 用户 prompt 作为第一条消息 === <code>messages.append({"role": "user", "content": query})</code> === 2. 将消息和工具定义一起发给 LLM === <code>response = client.messages.create( model=MODEL, system=SYSTEM, messages=messages, tools=TOOLS, max_tokens=8000, )</code> === 3. 追加助手响应,检查 stop_reason === <code>messages.append({"role": "assistant", "content": response.content}) if response.stop_reason != "tool_use": return</code> 如果模型没有调用工具,任务完成。 === 4. 执行每个工具调用,收集结果,作为 user 消息追加,回到第 2 步 === <code>results = [] for block in response.content: if block.type == "tool_use": output = run_bash(block.input["command"]) results.append({ "type": "tool_result", "tool_use_id": block.id, "content": output, }) messages.append({"role": "user", "content": results})</code> == 四、完整代码 == 组装为一个完整函数: <code>def agent_loop(query): messages = [{"role": "user", "content": query}] while True: response = client.messages.create( model=MODEL, system=SYSTEM, messages=messages, tools=TOOLS, max_tokens=8000, ) messages.append({"role": "assistant", "content": response.content}) if response.stop_reason != "tool_use": return results = [] for block in response.content: if block.type == "tool_use": output = run_bash(block.input["command"]) results.append({ "type": "tool_result", "tool_use_id": block.id, "content": output, }) messages.append({"role": "user", "content": results})</code> '''不到 30 行,这就是整个智能体。''' 后面所有复杂的机制(规划、子 Agent、任务系统、团队协作)都是在这个循环上叠加的——'''循环本身始终不变'''。 == 五、核心组件 == {| class="wikitable" !组件 !作用 |- |Agent loop |<code>while True</code> + <code>stop_reason</code> 检查 |- |Tools |从 <code>bash</code> 开始,单一工具足矣 |- |Messages |累积式消息列表,记录完整对话历史 |- |Control flow |<code>stop_reason != "tool_use"</code> 决定退出 |} == 六、试一试 == <code>git clone <nowiki>https://github.com/shareAI-lab/learn-claude-code</nowiki> cd learn-claude-code python agents/s01_agent_loop.py</code> 试试这些 prompt: # <code>Create a file called hello.py that prints "Hello, World!"</code> # <code>List all Python files in this directory</code> # <code>What is the current git branch?</code> # <code>Create a directory called test_output and write 3 files in it</code> 你会看到 Agent 自主地调用 Bash 工具,执行命令,读取结果,然后决定下一步做什么,直到任务完成。 == 七、关键洞察 == '''模型决定什么时候调用工具,什么时候停止。''' 代码只是执行模型要求做的事。这个循环属于 Agent,机制属于 Harness。 这个看似简单的循环是所有现代 AI Agent(包括 Claude Code、Devin、OpenCode)的基础。无论 Agent 多复杂,核心都是这个循环: <code>messages[] --> LLM --> response | stop_reason == "tool_use"? / \ yes no | | 执行工具 返回文本 追加结果 循环回去 ----------> messages[]</code> == 八、下一步 == 这只是一个开始。在这个基础上,我们将逐步添加: * '''工具扩展''':不只 Bash,还有文件读写、代码编辑、搜索 * '''规划能力''':让 Agent 列步骤再执行 * '''子 Agent''':大任务拆小,上下文隔离 * '''知识加载''':按需注入领域知识 * '''上下文压缩''':处理长对话 * '''任务系统''':持久化的目标管理 * '''团队协作''':多 Agent 并行工作 但在那之前,'''先掌握这个循环'''。理解它,你就理解了 Agent 的本质。 ----''Bash 就够了。真正的 Agent 是宇宙所需要的全部。'' '''延伸阅读''':learn-claude-code 开源项目,12 个渐进式教程,从简单循环到隔离式自主执行。
返回
30行代码构建你的第一个AI Agent
。
导航菜单
个人工具
登录
命名空间
页面
讨论
大陆简体
查看
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
特殊页面
工具
链入页面
相关更改
页面信息