hermes-agent/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/python-library.md
ethernet d84e11af4d
rip out brew + pip/PyPI wheel support (#68217)
Removes Homebrew and PyPI wheel/sdist as Hermes distribution paths while
preserving the supported source, Docker, and Nix workflows.

Changes:
- Removes the Homebrew formula, PyPI publish workflow, sdist manifest
  (MANIFEST.in), and wheel/sdist release-attachment logic from scripts/release.py.
- Keeps setuptools metadata and entry points required by editable installs
  and Docker/Nix builds, but adds a setup.py guard that rejects wheel/sdist
  builds outside a sealed Nix derivation (HERMES_NIX_BUILD=1).
- Removes pip/Homebrew install detection, PyPI update checks, the pip
  self-update path, the deprecation-banner state, the postinstall subcommand,
  wheel data-directory fallbacks in agent/i18n.py and hermes_constants.py,
  and the ACP Registry manifest/version-lockstep release logic.
- Adds /nix/store/ path detection so `nix run` / `nix profile install`
  installs (which don't set HERMES_MANAGED) are correctly identified as
  "nix" rather than falling through to "git"/"unknown".
- Retired install-method values ("pip", "homebrew") in existing
  .install_method stamps (both code-scoped and home-scoped) are ignored by
  the allowlist reader and fall through to "unknown" instead of resurrecting
  a retired enum value.
- Updates Nix packaging to ship bare runtime data (locales, optional-mcps)
  through store symlinks and wrapper env vars instead of wheel data-files.
- Removes the ACP Registry manifest/icon and their version-lockstep tests.
- Deletes or rewrites packaging, pip-update, Homebrew, and ACP Registry
  tests; adds parametrized coverage for the packaging build guard covering
  BOTH sdist and wheel paths (the guards live in separate cmdclass entries
  — a passing sdist test proves nothing about the wheel path).
- Updates installation/platform documentation and related user-facing copy.
- Adjusts the supply-chain scan so deleted install-hook files do not trigger
  a finding, while additions or modifications still require the existing
  ci-reviewed label gate.

Supported installation paths (unchanged):
- git installer (install.sh)
- Docker
- Nix/NixOS
- editable development installs (uv sync, uv pip install -e ., pip install -e .)
2026-07-22 16:51:01 -04:00

9.8 KiB
Raw Blame History

sidebar_position title description
5 将 Hermes 作为 Python 库使用 将 AIAgent 嵌入你自己的 Python 脚本、Web 应用或自动化流水线——无需 CLI

将 Hermes 作为 Python 库使用

Hermes 不仅仅是一个 CLI 工具。你可以直接导入 AIAgent,在自己的 Python 脚本、Web 应用或自动化流水线中以编程方式使用它。本指南将介绍具体方法。


安装

克隆 Hermes 并创建受支持的可编辑开发环境:

git clone https://github.com/NousResearch/hermes-agent.git
cd hermes-agent
uv sync

在该检出目录中使用 uv run python your_app.py 运行应用。Hermes 不发布用于 requirements.txt 安装的受支持 wheel 或源代码发行版。

:::tip 将 Hermes 作为库使用时CLI 所需的环境变量同样必须设置。至少需要设置 OPENROUTER_API_KEY(若直接访问提供商,则设置 OPENAI_API_KEYANTHROPIC_API_KEY)。 :::


基本用法

使用 Hermes 最简单的方式是 chat() 方法——传入一条消息,返回一个字符串:

from run_agent import AIAgent

agent = AIAgent(
    model="anthropic/claude-sonnet-4",
    quiet_mode=True,
)
response = agent.chat("What is the capital of France?")
print(response)

chat() 在内部处理完整的对话循环——工具调用、重试等一切事务——并仅返回最终的文本响应。

:::warning 将 Hermes 嵌入自己的代码时,务必设置 quiet_mode=True。否则agent 会打印 CLI 的加载动画、进度指示器及其他终端输出,从而干扰你的应用输出。 :::


完整对话控制

如需对对话进行更精细的控制,可直接使用 run_conversation()。它返回一个包含完整响应、消息历史和元数据的字典:

agent = AIAgent(
    model="anthropic/claude-sonnet-4",
    quiet_mode=True,
)

result = agent.run_conversation(
    user_message="Search for recent Python 3.13 features",
    task_id="my-task-1",
)

print(result["final_response"])
print(f"Messages exchanged: {len(result['messages'])}")

返回的字典包含:

  • final_response — agent 的最终文本回复
  • messages — 完整的消息历史(系统消息、用户消息、助手消息、工具调用)

(传入的 task_id 存储在 agent 实例上用于 VM 隔离,不会在返回字典中回显。)

你也可以传入自定义系统消息,覆盖该次调用的临时系统 prompt提示词

result = agent.run_conversation(
    user_message="Explain quicksort",
    system_message="You are a computer science tutor. Use simple analogies.",
)

配置工具集

使用 enabled_toolsetsdisabled_toolsets 控制 agent 可访问的工具集:

# 仅启用 Web 工具(浏览、搜索)
agent = AIAgent(
    model="anthropic/claude-sonnet-4",
    enabled_toolsets=["web"],
    quiet_mode=True,
)

# 启用除终端访问外的所有功能
agent = AIAgent(
    model="anthropic/claude-sonnet-4",
    disabled_toolsets=["terminal"],
    quiet_mode=True,
)

:::tip 当你需要一个功能最小化、受限的 agent 时(例如,仅用于研究机器人的 Web 搜索),使用 enabled_toolsets。当你需要大部分功能但需限制特定能力时(例如,在共享环境中禁用终端访问),使用 disabled_toolsets。 :::


多轮对话

通过将消息历史传回来维护多轮对话的状态:

agent = AIAgent(
    model="anthropic/claude-sonnet-4",
    quiet_mode=True,
)

# 第一轮
result1 = agent.run_conversation("My name is Alice")
history = result1["messages"]

# 第二轮——agent 记住了上下文
result2 = agent.run_conversation(
    "What's my name?",
    conversation_history=history,
)
print(result2["final_response"])  # "Your name is Alice."

conversation_history 参数接受上一次结果的 messages 列表。agent 会在内部复制该列表,因此你的原始列表不会被修改。


保存轨迹数据

启用轨迹保存,以 ShareGPT 格式捕获对话——适用于生成训练数据或调试:

agent = AIAgent(
    model="anthropic/claude-sonnet-4",
    save_trajectories=True,
    quiet_mode=True,
)

agent.chat("Write a Python function to sort a list")
# 以 ShareGPT 格式保存到 trajectory_samples.jsonl

每次对话以单行 JSONL 的形式追加写入,便于从自动化运行中收集数据集。


自定义系统 Prompt

使用 ephemeral_system_prompt 设置自定义系统 prompt用于引导 agent 的行为,但不会保存到轨迹文件中(保持训练数据的整洁):

agent = AIAgent(
    model="anthropic/claude-sonnet-4",
    ephemeral_system_prompt="You are a SQL expert. Only answer database questions.",
    quiet_mode=True,
)

response = agent.chat("How do I write a JOIN query?")
print(response)

这非常适合构建专用 agent——代码审查员、文档撰写员、SQL 助手——全部使用相同的底层工具。


批量处理

如需并行运行大量 promptHermes 提供了 batch_runner.py,它可管理并发的 AIAgent 实例并进行适当的资源隔离:

python batch_runner.py --input prompts.jsonl --output results.jsonl

每个 prompt 都有自己的 task_id 和隔离环境。如果需要自定义批处理逻辑,可以直接使用 AIAgent 构建:

import concurrent.futures
from run_agent import AIAgent

prompts = [
    "Explain recursion",
    "What is a hash table?",
    "How does garbage collection work?",
]

def process_prompt(prompt):
    # 每个任务创建一个新的 agent 实例以保证线程安全
    agent = AIAgent(
        model="anthropic/claude-sonnet-4",
        quiet_mode=True,
        skip_memory=True,
    )
    return agent.chat(prompt)

with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
    results = list(executor.map(process_prompt, prompts))

for prompt, result in zip(prompts, results):
    print(f"Q: {prompt}\nA: {result}\n")

:::warning 务必为每个线程或任务创建一个新的 AIAgent 实例。agent 维护着内部状态(对话历史、工具会话、迭代计数器),这些状态不是线程安全的,不能共享。 :::


集成示例

FastAPI 端点

from fastapi import FastAPI
from pydantic import BaseModel
from run_agent import AIAgent

app = FastAPI()

class ChatRequest(BaseModel):
    message: str
    model: str = "anthropic/claude-sonnet-4"

@app.post("/chat")
async def chat(request: ChatRequest):
    agent = AIAgent(
        model=request.model,
        quiet_mode=True,
        skip_context_files=True,
        skip_memory=True,
    )
    response = agent.chat(request.message)
    return {"response": response}

Discord 机器人

import discord
from run_agent import AIAgent

client = discord.Client(intents=discord.Intents.default())

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith("!hermes "):
        query = message.content[8:]
        agent = AIAgent(
            model="anthropic/claude-sonnet-4",
            quiet_mode=True,
            skip_context_files=True,
            skip_memory=True,
            platform="discord",
        )
        response = agent.chat(query)
        await message.channel.send(response[:2000])

client.run("YOUR_DISCORD_TOKEN")

CI/CD 流水线步骤

#!/usr/bin/env python3
"""CI step: auto-review a PR diff."""
import subprocess
from run_agent import AIAgent

diff = subprocess.check_output(["git", "diff", "main...HEAD"]).decode()

agent = AIAgent(
    model="anthropic/claude-sonnet-4",
    quiet_mode=True,
    skip_context_files=True,
    skip_memory=True,
    disabled_toolsets=["terminal", "browser"],
)

review = agent.chat(
    f"Review this PR diff for bugs, security issues, and style problems:\n\n{diff}"
)
print(review)

关键构造函数参数

参数 类型 默认值 描述
model str "anthropic/claude-opus-4.6" OpenRouter 格式的模型名称
quiet_mode bool False 抑制 CLI 输出
enabled_toolsets List[str] None 白名单指定工具集
disabled_toolsets List[str] None 黑名单指定工具集
save_trajectories bool False 将对话保存为 JSONL
ephemeral_system_prompt str None 自定义系统 prompt不保存到轨迹文件
max_iterations int 90 每次对话的最大工具调用迭代次数
skip_context_files bool False 跳过加载 AGENTS.md 文件
skip_memory bool False 禁用持久化内存的读写
api_key str None API 密钥(回退到环境变量)
base_url str None 自定义 API 端点 URL
platform str None 平台提示("discord""telegram" 等)

重要说明

:::tip

  • 如果不希望将工作目录中的 AGENTS.md 文件加载到系统 prompt 中,请设置 skip_context_files=True
  • 设置 skip_memory=True 可阻止 agent 读写持久化内存——推荐用于无状态 API 端点。
  • platform 参数(如 "discord""telegram")会注入平台特定的格式化提示,使 agent 适配其输出风格。 :::

:::warning

  • 线程安全:每个线程或任务创建一个 AIAgent 实例。切勿在并发调用中共享同一实例。
  • 资源清理agent 在对话结束时会自动清理资源(终端会话、浏览器实例)。若在长期运行的进程中使用,请确保每次对话正常结束。
  • 迭代限制:默认的 max_iterations=90 较为宽松。对于简单的问答场景,建议适当降低该值(如 max_iterations=10),以防止工具调用循环失控并控制成本。 :::