learn-hermes-agent

longyunfeiguMIT更新放缓

27 章手把手教程,用 Python 从零搭建自主 AI Agent,覆盖 Agent 循环、工具系统、记忆、技能、MCP 与自进化。

A 27-chapter hands-on tutorial for building an autonomous AI agent from zero in Python. Agent loop, tool system, memory, skills, MCP, multi-platform gateway, and self-evolution — inspired by Hermes Agent.

暂无版本238star34fork同步于 2026-11-07 07:29 UTC

怎么上手

由 README 提炼,执行前请确认
git clone <repo-url>
cd learn-hermes-agent
pip install -r requirements.txt
cp .env.example .env
python agents/s01_agent_loop.py
硬件要求按 README 说明

README 未说明

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

27 章 Python 实战教程,从零手写可跨平台、可持久化的自主 AI Agent

27 章手把手教程,用 Python 从零搭建自主 AI Agent,覆盖 Agent 循环、工具系统、记忆、技能、MCP 与自进化。

核心能力

  • 按 s01-s27 分五阶段递进,每章配可运行参考实现
  • 用 SQLite 加 FTS5 实现跨重启的会话持久化
  • 自注册工具注册表与分发层,作为 Agent 能力中心
  • 上下文超长时自动触发 LLM 摘要压缩窗口
  • 单一 Agent 循环接入 Telegram、Discord、Slack、微信等平台

容易踩的坑

运行前必须在 .env 中配置 API key,否则示例跑不起来;s24 插件架构只有文档没有 agents/s24_*.py 参考实现;中文文档是主线且更新最快,英文版可能滞后

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

0 个版本

暂无 release 数据(可能没有发版,或尚未同步)。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    English | 中文

    Learn Hermes Agent

    Build a production-grade autonomous AI agent from scratch in Python. A 27-chapter, code-first tutorial covering the agent loop, tool system, session persistence, memory, skills, context compression, MCP, multi-platform gateway (Telegram / Discord / Slack / WeChat), and RL-based self-evolution — inspired by Hermes Agent.

    Every chapter ships a runnable reference implementation under agents/sNN_*.py, paired with a prose explanation under docs/en/ (and docs/zh/ for the Chinese mainline). Read, run, tweak, repeat.

    This repo does not try to mirror every product detail from the Hermes Agent codebase. It focuses on the mechanisms that actually decide whether an agent can work autonomously across platforms:

    • the conversation loop
    • tool registry and dispatch
    • session persistence
    • prompt assembly
    • context compression
    • memory and skill management
    • skill system
    • permission and safety
    • multi-platform gateway
    • terminal backends
    • scheduling
    • external capability routing

    The goal is simple:

    understand the real design backbone well enough that you can rebuild it yourself.

    What This Repo Is Really Teaching

    One sentence first:

    The model does the reasoning. The harness gives the model a working environment that spans platforms, persists across sessions, and manages its own skills.

    That working environment is made of a few cooperating parts:

    • Agent Loop: send messages to the model, execute tool calls, append results, continue
    • Tool System: a self-registering dispatch layer — the agent's hands
    • Session Store: SQLite with FTS5 — conversation memory that survives restarts
    • Prompt Builder: assemble system prompts from personality, memory, config, and context
    • Context Compression: keep the active window small when conversations grow long
    • Memory & Skills: durable knowledge and agent-managed skill files
    • Permission System: detect dangerous commands before execution
    • Gateway: a single agent loop that listens on Telegram, Discord, Slack, WeChat, and more
    • Terminal Backends: run commands locally, in Docker, over SSH, or on serverless platforms
    • Cron / MCP / Voice: grow the single-agent core into a full working platform

    This is the teaching promise of the repo:

    • teach the mainline in a clean order
    • explain unfamiliar concepts before relying on them
    • stay close to real system structure
    • avoid drowning the learner in irrelevant product details

    What This Repo Deliberately Does Not Teach

    This repo is not trying to preserve every detail that exists in the production system.

    If a detail is not central to the agent's core operating model, it should not dominate the teaching line. That includes things like:

    • packaging, Nix flakes, and release mechanics
    • landing pages and marketing assets
    • enterprise subscription and billing wiring
    • telemetry and analytics
    • RL training pipeline and batch runner internals
    • platform-specific API quirks (WeChat XML parsing, Telegram inline keyboards)
    • skin/theme engine cosmetics
    • historical migration logic

    Those details may matter in production. They do not belong at the center of a 0-to-1 teaching path.

    Who This Is For

    The assumed reader:

    • knows basic Python
    • understands functions, classes, async/await basics
    • may be completely new to agent systems or multi-platform bots

    So the repo tries to keep a few strong teaching rules:

    • explain a concept before using it
    • keep one concept fully explained in one main place
    • start from "what it is", then "why it exists", then "how to implement it"
    • avoid forcing beginners to assemble the system from scattered fragments

    Recommended Reading Order

    If This Is Your First Visit, Start Here

    Do not open random chapters first.

    The safest path is:

    1. Read docs/en/s00-architecture-overview.md for the full system map.
    2. Read docs/en/s00f-code-reading-order.md so you know which source files to open first.
    3. Follow the five stages in order: s01-s06 -> s07-s11 -> s12-s15 -> s16-s20 -> s21-s27.
    4. After each stage, stop and rebuild the smallest version yourself before continuing.

    If the middle and late chapters start to blur together, reset in this order:

    1. docs/en/data-structures.md
    2. docs/en/entity-map.md
    3. then return to the chapter body

    Five Stages

    1. s01-s06: build a working single-agent core with persistence
    2. s07-s11: add intelligence — memory, skills, safety, delegation, and configuration
    3. s12-s15: go multi-platform — gateway, adapters, terminal backends, and scheduling
    4. s16-s20: add advanced capabilities — MCP, browser, voice, vision, and background review
    5. s21-s27: self-improvement — skill creation, hooks, trajectory/RL, plugins, evaluation, and optimization

    Main Chapters

    ChapterTopicWhat you get
    s00Architecture Overviewthe global map, key terms, and learning order
    s01Agent Loopthe synchronous conversation loop — ask, tool-call, append, continue
    s02Tool Systema self-registering tool registry with dispatch orchestration
    s03Session StoreSQLite + FTS5 persistence — conversations that survive restarts
    s04Prompt Buildersection-based system prompt assembly from personality, memory, and config
    s05Context Compressionauto-triggered LLM summarization when context grows too long
    s06Error RecoveryAPI error classification, retry with backoff, and provider failover
    s07Memory Systemcross-session persistent knowledge with MEMORY.md and USER.md
    s08Skill Systemagent-managed skills — create, edit, and execute
    s09Permission Systemdangerous command detection and approval gates
    s10Subagent Delegationspawn fresh context for isolated subtasks
    s11Configuration SystemYAML config, env vars, profiles, and runtime migration
    s12Gateway Architecturethe multi-platform message dispatch loop
    s13Platform Adaptersbuilding integrations for Telegram, Discord, Slack, WeChat, and more
    s14Terminal Backendsrun commands in Docker, over SSH, on Modal, or Daytona
    s15Cron Schedulertime-based automation with duration strings and cron expressions
    s16MCP Integrationexternal capability routing via Model Context Protocol
    s17Browser AutomationPlaywright + Browserbase for web interaction
    s18Voice & VisionTTS/STT pipelines and image analysis
    s19CLI Interfaceprompt_toolkit + Rich for an interactive terminal experience
    s20Background Reviewevery N turns, a background pass updates memory and extracts skills
    s21Skill Creation Loopbackground review extracts patterns into reusable skills
    s22Hook Systemlifecycle hooks for extensibility without modifying core code
    s23Trajectory & RLconversation trajectories become training data for model improvement
    s24Plugin Architecturepluggable memory, compression, and capability providers
    s25Self-Evolution Overviewthe core insight, four evolution targets, and full pipeline overview
    s26Evaluation Systemeval datasets, LLM-as-judge fitness scoring, and constraint gates
    s27Optimization & Deploythe feedback→mutate→select loop, full pipeline, and Phase 2-4 concepts

    Chapter Index: What to Focus on in Each Chapter

    If this is your first time learning this material systematically, do not spread your attention evenly across all details. For each chapter, focus on 3 things:

    1. What new capability this chapter adds.
    2. Where the key state lives.
    3. After finishing, can you hand-write this minimal mechanism yourself?
    ChapterKey Data Structures / EntitiesWhat you should have after this chapter
    s01messages list / AIAgent class / run_conversation()a minimal working synchronous conversation loop
    s02ToolRegistry / ToolEntry / tool_resulta self-registering, self-discovering tool system
    s03SessionDB / state.db / FTS5 indexa SQLite persistence layer — conversations survive restarts
    s04build_context_files_prompt() / build_skills_system_prompt()a pipeline assembling prompts from personality, memory, and config
    s05ContextCompressor / compression trigger thresholdan auto-summarization layer when context grows too long
    s06ClassifiedError / FailoverReason / classify_api_error()error classification + backoff retry + provider failover
    s07MemoryStore / MemoryManager / MEMORY.md / USER.mda layer that separates "temporary context" from "cross-session memory"
    s08SkillMeta / SkillBundle / skill SKILL.md filesa skill system that can create, edit, and execute
    s09DANGEROUS_PATTERNS / detect_dangerous_command() / _ApprovalEntrya "dangerous operations must pass the gate" approval pipeline
    s10delegate_tool / child messages / isolated AIAgenta subagent mechanism with isolated context for one-off delegation
    s11config dict / Profile management / migration functionsYAML config + profiles + runtime migration
    s12GatewayRunner / MessageEvent / platform routinga unified multi-platform message dispatch loop
    s13BasePlatformAdapter / MessageType / SendResulta reusable platform adapter pattern
    s14BaseEnvironment / local / docker / ssh / modal / daytonaabstract execution environments: local, Docker, SSH, cloud
    s15parse_schedule() / create_job() / get_due_jobs() / job dictsa "when the time comes, work starts" scheduling layer
    s16mcp_tool / MCP config / tool schema bridginga bus for plugging external tools and capabilities into the system
    s17browser_tool / Playwright / Browserbase providera browser automation layer for web interaction
    s18tts_tool / voice_mode / vision_toolsmultimodal pipelines: voice I/O + image analysis
    s19HermesCLI / CommandDef / KawaiiSpinner / Rich renderinga fully-featured interactive terminal interface
    s20BackgroundReviewer / _MEMORY_REVIEW_PROMPT / dual trigger countersan "every N turns, auto-reflect → update memory/skills" background review mechanism
    s21skill creation loop / pattern extraction prompt / skill persistence pipelinethe "discover patterns → create reusable skills" prerequisite for self-evolution
    s22HookRegistry / PluginHookRegistry / BOOT.md handlerlifecycle hooks — inject custom logic without modifying core code
    s23convert_to_trajectory() / compress_trajectory() / reward functionsconversation data → training pipeline for model improvement
    s24plugin interfaces / memory providers / compression providerspluggable memory and compression without touching core code
    s25EvalExample / EvalDatasetthe foundational data structures for self-evolution
    s26SyntheticDatasetBuilder / FitnessScore / ConstraintValidatormeasurement infrastructure — generate data, score outputs, gate changes
    s27SkillOptimizer / EvolutionResult / evolve_skill()the optimization loop and full 7-step pipeline

    Reading Approaches for Beginners

    Approach 1: Steady Mainline

    Best for readers encountering agent systems for the first time.

    Read in this order:

    s00 -> s01 -> ... -> s20 -> s21 -> ... -> s27 (follow the numbers; s24 is docs-only).

    Approach 2: Build First, Complete Later

    Best for "get it running, then fill in the gaps" readers.

    Read in this order:

    1. s01-s06: build a core agent with persistence and context compression
    2. s07-s11: add memory, skills, safety, delegation, and config
    3. s12-s15: go multi-platform, learn cross-environment execution
    4. s16-s20: advanced capabilities plus the background self-review
    5. s21-s27: step into self-evolution — skill creation, hooks, trajectories, evaluation, and optimization

    Approach 3: When You Get Stuck

    If you hit a wall in the middle or late chapters, do not push forward blindly.

    Reset in this order:

    1. docs/en/s00-architecture-overview.md
    2. docs/en/data-structures.md
    3. docs/en/entity-map.md
    4. the chapter you are stuck on

    When readers truly get stuck, it is usually not "I can't read the code" but rather:

    • which layer does this mechanism plug into?
    • which data structure holds this state?
    • what is the difference between this term and another that looks similar?

    Quick Start

    git clone <repo-url>
    cd learn-hermes-agent
    pip install -r requirements.txt
    cp .env.example .env
    

    Then configure your API key in .env, and run:

    python agents/s01_agent_loop.py
    

    Suggested order:

    1. Run s01 and make sure the minimal loop really works.
    2. Read s00, then move through s01 -> s06 in order.
    3. Only after the single-agent core plus its persistence feel stable, continue into s07 -> s11.
    4. Move into gateway and platform chapters s12 -> s15 only after the core agent makes sense.
    5. Continue through s16 -> s20, then the self-evolution chapters s21 -> s27.

    How To Read Each Chapter

    Each chapter is easier to absorb if you keep the same reading rhythm:

    1. what problem appears without this mechanism
    2. what the new concept means
    3. what the smallest correct implementation looks like
    4. where the state actually lives
    5. how it plugs back into the loop
    6. where to stop first, and what can wait until later

    If you keep asking:

    • "Is this core mainline or just a side detail?"
    • "Where does this state actually live?"

    go back to:

    Repository Structure

    learn-hermes-agent/
    ├── agents/              # runnable Python reference implementations per chapter (s24 is an exception, see below)
    ├── docs/zh/             # Chinese mainline docs
    ├── docs/en/             # English docs
    ├── illustrations/       # chalkboard-style diagrams for each chapter
    ├── tests/               # smoke tests
    ├── web/                 # web teaching platform (optional)
    ├── .env.example         # environment variable template
    └── requirements.txt     # Python dependencies
    

    Note: s24 Plugin Architecture currently ships with documentation only (docs/en/s24-plugin-architecture.md and the Chinese counterpart). There is no agents/s24_*.py reference implementation. The doc is self-contained and does not block the rest of the reading order.

    Teaching Tradeoffs

    To ensure "buildable from 0 to 1", this repo makes deliberate tradeoffs:

    • Teach the minimal correct version first, then explain extension boundaries.
    • If a real mechanism is complex but the core idea is not, teach the core idea first.
    • If an advanced term appears, explain it — do not assume the reader already knows.
    • If an edge case in the real system has low teaching value, remove it entirely.

    This means the repo aims for:

    High fidelity on core mechanisms, deliberate tradeoffs on peripheral details.

    Language Status

    Chinese is the canonical teaching line and the fastest-moving version.

    • zh: most reviewed and most complete
    • en: all chapters s00-s27 available; Chinese is updated first

    If you want the fullest and most frequently refined explanation path, use the Chinese docs first.

    End Goal

    By the end of the repo, you should be able to answer these questions clearly:

    • what is the minimum state an autonomous agent needs to persist across sessions?
    • why is the tool registry the center of the agent's capability?
    • how does a single conversation loop scale to 15+ messaging platforms?
    • what problem do memory, skills, permissions, context compression, and error recovery each solve?
    • how do terminal backends abstract away the execution environment?
    • when should a single-agent system grow into gateway, scheduling, MCP, and voice?

    If you can answer those questions clearly and build a similar system yourself, this repo has done its job.


    This is not "copy the source code line by line." This is "grasp the designs that truly matter, then build it yourself."