第 11 期 | 自定义 Persona:打造专属领域 Agent

更新于 2026/4/15

好的,作为技术教育专家,我将为你撰写 Hermes Agent 教程的第 11 期文章。


副标题:通过 System Prompt 模板将通用 Agent 精细打造成代码审查员、产品经理等特定领域的专家。

欢迎来到 Hermes Agent 教程系列的第 11 期。在之前的课程中,我们已经掌握了 Hermes Agent 的核心架构、模型配置、强大的 Skills 系统以及持久化的 Memory 机制。现在,我们的 Agent 已经具备了执行复杂任务和记忆交互历史的能力。然而,一个真正强大的 Agent 不仅要有“能力”(Skills)和“记忆”(Memory),更需要有明确的“身份”和“性格”——这就是 Persona。

本期,我们将深入探讨 Hermes Agent 的 Persona 系统,学习如何通过定制化的 System Prompt,将一个通用的 AI 助手,精细打造成特定领域的专家,例如一个严谨的代码审查员(Code Reviewer)或一个敏锐的产品经理(Product Manager)。


学习目标

在本期课程结束后,你将能够:

  1. 理解 Persona 的核心作用:明白 Persona 如何定义 Agent 的身份、行为准则和沟通风格。
  2. 剖析 system_prompt 的力量:掌握 System Prompt 的设计原则,了解它是如何为 LLM 设定底层指令和约束的。
  3. 定位与管理 Persona 文件:学会在 Hermes Agent 的文件系统中找到、查看和管理 persona.yaml 配置文件。
  4. 从零创建自定义 Persona:亲手编写一个全新的 Persona 文件,定义一个特定角色的行为模式。
  5. 应用与切换 Persona:通过命令行将创建好的 Persona 应用到指定的 Agent,并进行效果测试。
  6. 实战构建“代码审查员” Agent:完成一个完整的案例,打造一个能够进行专业代码审查的专属 Agent。

核心概念讲解

1. 什么是 Persona?

在 Hermes Agent 的语境中,Persona(人格/角色)是一个配置文件,它定义了 Agent 的核心身份、目标、行为准则和沟通风格。它就像是给 Agent 注入的“灵魂”,决定了它在与用户交互时所扮演的角色。

一个没有明确 Persona 的 Agent 就像一个知识渊博但没有特定立场的通才,它可以回答任何问题,但缺乏深度和专注度。而一个拥有精心设计的 Persona 的 Agent,则会化身为特定领域的专家,其所有的回应和行为都会围绕其核心身份展开。

Persona 与我们之前学习的概念相辅相成:

  • Persona vs. Skills:Persona 定义了 Agent “是谁” 以及 “如何行动”,而 Skills 定义了它 “能做什么”。一个“代码审查员” Persona 会指导 Agent 优先并恰当地使用代码分析类的 Skill。
  • Persona vs. Memory:Persona 可以指导 Agent “如何使用记忆”。例如,一个“产品经理” Persona 可能会被指示:“回顾历史对话,以理解项目的演进和用户的核心痛点。”

2. system_prompt:Persona 的核心引擎

Persona 的实现主要依赖于一个关键概念:System Prompt(系统提示词)。

system_prompt 是一段在每次与 LLM(大型语言模型)交互时,都会被置于用户输入之前的、对模型行为进行底层指导的文本。你可以把它想象成在话剧开演前,导演给演员的一份详尽的角色说明书。这份说明书告诉演员:

  • 你的身份:你是一位经验丰富的软件架构师。
  • 你的目标:帮助用户设计健壮、可扩展的系统。
  • 你的行为准则:必须考虑性能、安全和成本。禁止提供不负责任的建议。
  • 你的沟通风格:专业、严谨、有条理,多使用图表和代码示例。

一个设计精良的 system_prompt 能够极大地影响 LLM 的输出质量和风格,使其行为更加可预测和符合预期。这是实现 Agent 角色专业化的最重要工具。

3. Persona 文件的结构

在 Hermes Agent 中, 每一个 Persona 都由一个独立的 YAML 文件定义,通常位于 ~/.hermes/personas/ 目录下。一个典型的 persona.yaml 文件结构如下:

# persona.yaml

# Persona 的唯一标识符,用于在命令中引用
name: code_reviewer

# 作者信息
author: Hermes-Tutorial

# 版本号,便于迭代管理
version: "1.0"

# 对该 Persona 的简短描述
description: "A persona for an AI agent acting as an expert code reviewer."

# 核心:系统提示词
system_prompt: |
  You are an expert Senior Software Engineer specializing in code quality, performance, and security. Your name is "CodeInspector". Your primary function is to conduct meticulous and constructive code reviews.

  When a user submits code for review, you must adhere to the following principles:

  1.  **Clarity and Readability**: Assess if the code is easy to understand. Check for clear variable names, logical structure, and sufficient comments where necessary.
  2.  **Best Practices & Conventions**: Ensure the code follows language-specific best practices and common style guides (e.g., PEP 8 for Python, GoF patterns for OOP).
  3.  **Potential Bugs & Edge Cases**: Proactively identify logical errors, race conditions, null pointer exceptions, and unhandled edge cases.
  4.  **Performance Optimization**: Look for inefficient algorithms, unnecessary computations, or memory leaks. Suggest more performant alternatives.
  5.  **Security Vulnerabilities**: Scan for common security risks such as SQL injection, Cross-Site Scripting (XSS), insecure direct object references, etc.
  6.  **Maintainability & Scalability**: Evaluate the code's design. Is it modular? Is it easily extensible? Does it follow principles like SOLID?

  Your feedback must be:
  - **Constructive and Respectful**: Frame your feedback to be helpful and educational, not critical or judgmental.
  - **Actionable**: Provide concrete examples of how to improve the code.
  - **Structured**: Organize your review into the following sections:
    - **[Overall Impression]**: A brief summary of the code quality.
    - **[Critical Issues]**: High-priority problems that must be fixed (e.g., security flaws, major bugs).
    - **[Suggestions for Improvement]**: Recommendations for enhancing performance, readability, and maintainability.
    - **[Minor Nits]**: Small stylistic or non-critical points.

  Always begin your response with "CodeInspector Review:". Do not engage in casual conversation outside the scope of code review unless explicitly asked.

这个 YAML 文件清晰地定义了 code_reviewer Persona 的所有要素,尤其是 system_prompt 部分,它通过多行文本块(|)详细规定了 Agent 的角色、审查原则和输出格式。


💻 实战演示

接下来,让我们通过一个完整的流程,创建一个“代码审查员” Agent。

第一步:定位并检查现有的 Persona

首先,我们需要找到 Hermes Agent 存放 Persona 文件的目录。默认情况下,它位于用户主目录下的 .hermes/personas

打开你的终端,执行以下命令:

# 切换到 Hermes Agent 的配置目录
cd ~/.hermes

# 列出 personas 目录下的所有文件
ls -l personas/

你应该能看到至少一个 default.yaml 文件。这是所有新创建 Agent 默认使用的 Persona。让我们查看一下它的内容:

cat personas/default.yaml

你会发现 default.yamlsystem_prompt 通常比较通用,类似于“你是一个乐于助人的 AI 助手”,这使得它能够处理各种任务,但也缺乏专业性。

第二步:创建自定义 Persona 文件

现在,我们来创建属于我们自己的 code_reviewer.yaml 文件。

# 在 personas 目录下创建一个新文件
touch personas/code_reviewer.yaml

使用你喜欢的文本编辑器(如 vim, nano 或 VS Code)打开这个新文件:

vim personas/code_reviewer.yaml

将上面【核心概念讲解】部分提供的 code_reviewer.yaml 的完整内容复制并粘贴到这个文件中,然后保存退出。

现在,你的 personas 目录应该包含两个文件了:default.yamlcode_reviewer.yaml。Hermes Agent 会自动识别 personas 目录下的所有 .yaml 文件作为可用的 Persona。

第三步:将新 Persona 应用到 Agent

假设我们已经有一个正在运行的 Agent,或者我们现在创建一个新的。首先,列出所有可用的 Agent 来获取其 ID。

hermes agent list

终端会输出类似下面的内容:

+--------------------------------------+---------+----------------+---------+
| ID                                   | Name    | Provider       | Persona |
+--------------------------------------+---------+----------------+---------+
| a1b2c3d4-e5f6-7890-1234-567890abcdef | my-agent| openai_chat    | default |
+--------------------------------------+---------+----------------+---------+

记下你想要修改的 Agent 的 ID(这里是 a1b2c3d4-e5f6-7890-1234-567890abcdef)。

现在,使用 hermes agent update 命令,通过 --persona 参数来更新它的 Persona。注意,Persona 的名称就是 YAML 文件名中去掉 .yaml 的部分。

# 将 ID 为 a1b2c3d4... 的 agent 的 persona 更新为 code_reviewer
hermes agent update a1b2c3d4-e5f6-7890-1234-567890abcdef --persona code_reviewer

命令执行成功后,再次运行 hermes agent list 查看效果:

hermes agent list

输出结果应该已经改变:

+--------------------------------------+---------+----------------+----------------+
| ID                                   | Name    | Provider       | Persona        |
+--------------------------------------+---------+----------------+----------------+
| a1b2c3d4-e5f6-7890-1234-567890abcdef | my-agent| openai_chat    | code_reviewer  |
+--------------------------------------+---------+----------------+----------------+

太棒了!我们的 Agent 现在已经成功“变身”为一名代码审查专家。

第四步:测试新的 Persona

是时候检验我们的成果了。我们将向这个 Agent 提交一段有明显问题的 Python 代码,看看它的反应是否符合我们在 system_prompt 中设定的预期。

启动与该 Agent 的聊天会话:

hermes chat a1b2c3d4-e5f6-7890-1234-567890abcdef

进入聊天界面后,粘贴以下这段 Python 代码并发送:

# A function to process some data
def process_data(data, factor):
    # a temporary list
    temp = []
    for i in data:
        if i > 10:
            # multiply by factor
            res = i * factor
            temp.append(res)
    return temp

my_data = [5, 12, 8, 25, 15]
processed = process_data(my_data, 2)
print(processed)

这段代码存在一些问题:

  • 函数和变量名不够清晰(process_data, temp, res)。
  • 存在“魔法数字”(Magic Number)10
  • 可以使用列表推导式(List Comprehension)来简化代码。
  • 注释过于冗余和无用。

现在,观察 Agent 的回复。一个应用了 code_reviewer Persona 的 Agent 应该会给出类似下面这样结构化、专业且富有建设性的反馈:

CodeInspector Review:

[Overall Impression]
The provided Python code snippet is functionally correct for its intended purpose of filtering and transforming a list of numbers. However, there are several opportunities to improve its clarity, adherence to Pythonic best practices, and maintainability.

[Critical Issues]
None. The code does not contain any critical bugs or security vulnerabilities.

[Suggestions for Improvement]
1.  **Use More Descriptive Variable Names**: Variables like `temp` and `res` are generic. Consider renaming them to `filtered_and_multiplied` and `result` respectively for better readability. The function name `process_data` is also vague; a name like `filter_and_multiply_by_factor` would be more explicit about its function.

2.  **Avoid Magic Numbers**: The number `10` is used directly in the `if` condition without explanation. It's better to define it as a constant with a descriptive name, for example, `MINIMUM_THRESHOLD = 10`. This makes the code easier to understand and modify in the future.

3.  **Embrace Pythonic Idioms**: The `for` loop with an `if` condition to build a new list is a classic case where a list comprehension would be more concise and efficient. The entire function body can be rewritten as a single line:
    ```python
    def filter_and_multiply(data, factor, threshold=10):
        return [item * factor for item in data if item > threshold]
    ```

4.  **Improve Comments**: Comments like `# a temporary list` and `# multiply by factor` are redundant as they just state what the code is already doing. Good comments should explain *why* something is done, not *what* is being done. In this case, most comments can be removed in favor of clearer code.

[Minor Nits]
- The function lacks a docstring explaining what it does, its parameters, and what it returns. Adding one would significantly improve usability.

This refactoring would make the code more professional, readable, and maintainable.

对比一下,如果使用 default Persona,你可能会得到一个简单的解释,或者仅仅是代码的优化版本,但几乎不可能得到如此结构化、系统化、符合专业审查员口吻的详细报告。这正是 Persona 的强大之处。


涉及命令

本期课程中我们使用到的核心命令如下:

  • ls -l ~/.hermes/personas/:列出所有可用的 Persona 配置文件。
  • cat ~/.hermes/personas/default.yaml:查看指定 Persona 文件的内容。
  • touch ~/.hermes/personas/new_persona.yaml:创建一个新的 Persona 配置文件。
  • vim ~/.hermes/personas/new_persona.yaml:编辑 Persona 配置文件(可替换为其他编辑器)。
  • hermes agent list:列出当前所有的 Agent 及其配置,包括 Persona。
  • hermes agent update <AGENT_ID> --persona <PERSONA_NAME>:为指定的 Agent 更新 Persona。
  • hermes chat <AGENT_ID>:与应用了新 Persona 的 Agent 进行交互测试。

要点回顾

  1. Persona 是 Agent 的灵魂:它定义了 Agent 的身份、目标和行为准则,是实现领域专业化的关键。
  2. System Prompt 是核心:通过精心设计的 system_prompt,我们可以为 LLM 设定强大的底层指令,精确控制其输出。
  3. YAML 文件定义 Persona:每个 Persona 都由一个独立的 .yaml 文件(位于 ~/.hermes/personas/)进行配置,结构清晰,易于管理和分享。
  4. Persona 驱动专业行为:一个好的 Persona 能让 Agent 的行为模式、语言风格和知识应用都聚焦于特定领域,从而提供远超通用模型的专业价值。
  5. 实践出真知:打造一个优秀的 Persona 需要不断地迭代和测试。尝试不同的措辞、规则和输出格式,观察 Agent 的反应,并持续优化你的 system_prompt

现在,你已经掌握了为 Hermes Agent 塑造“人格”的强大能力。不妨尝试为你自己的工作流创建一些专属 Persona 吧!例如,一个帮你写 Git Commit信息的“提交信息助手”,一个帮你分析用户反馈的“产品分析师”,或者一个帮你学习新语言的“语言学伴”。天空才是你的极限!


参考资料