12 蒸馏产物 → 虚拟人:知识注入与技能绑定

这是《Git 仓库蒸馏术:从代码仓库到 OpenClaw 虚拟人》系列的第 12 篇。前 11 篇我们完成了两件事:蒸馏(把仓库变成知识产物)和虚拟人机制(persona/memory/skills 三要素)。这一篇是"临门一脚"——把蒸馏产物真正注入虚拟人,完成从"知识产物"到"虚拟人能力"的最后转化。


一、蒸馏产物到虚拟人的映射

1.1 映射总览

前 9 篇产出的 8 类知识产物,逐一映射到虚拟人三要素:

蒸馏产物来源篇映射目标说明
仓库画像03memory/project.md项目概览
演进报告04memory/history.md演进脉络
架构文档05memory/architecture.md架构知识
知识摘要06memory/knowledge.md核心知识
模式库07skills/analyze.md模式识别能力
决策记录08memory/decisions.md决策知识
知识图谱09memory/graph.json关系网络
工具链方案10skills/run.md执行分析能力

1.2 映射原则

映射不是"复制粘贴",而是结构化转化

  1. 文档类产物 → memory:直接作为知识文档,但需按 memory 格式重排
  2. 模式/工具类产物 → skills:转化为"可执行的能力",而非静态知识
  3. 图谱类产物 → graph.json:转为机器可读的关系数据

二、知识注入:memory 构建

2.1 注入流程

[蒸馏产物] → [格式转换] → [结构重排] → [关联索引] → [memory 文件]

2.2 注入脚本

#!/usr/bin/env python3
# scripts/inject_memory.py
# 将蒸馏产物注入虚拟人 memory
import json
import os
import shutil

def inject_memory(distill_dir, memory_dir):
    """将蒸馏产物注入 memory 目录"""
    os.makedirs(memory_dir, exist_ok=True)

    # 产物 → memory 文件映射
    mapping = {
        "repo-profile.md": "project.md",        # 仓库画像 → 项目概览
        "evolution-report.md": "history.md",    # 演进报告 → 演进脉络
        "architecture.md": "architecture.md",   # 架构文档 → 架构知识
        "knowledge-summary.md": "knowledge.md", # 知识摘要 → 核心知识
        "adr/": "decisions.md",                 # 决策记录 → 决策知识
    }

    for src, dst in mapping.items():
        src_path = os.path.join(distill_dir, src)
        if os.path.isdir(src_path):
            # 目录(如 adr/)合并为单个文件
            merge_dir_to_file(src_path, os.path.join(memory_dir, dst))
        elif os.path.exists(src_path):
            shutil.copy(src_path, os.path.join(memory_dir, dst))
            print(f"注入: {src}{dst}")

    # 知识图谱单独处理(JSON 格式)
    graph_src = os.path.join(distill_dir, "knowledge-graph.json")
    if os.path.exists(graph_src):
        shutil.copy(graph_src, os.path.join(memory_dir, "graph.json"))
        print("注入: knowledge-graph.json → graph.json")

def merge_dir_to_file(src_dir, dst_file):
    """将目录下多个文件合并为一个 memory 文件"""
    with open(dst_file, "w", encoding="utf-8") as out:
        for fname in sorted(os.listdir(src_dir)):
            fpath = os.path.join(src_dir, fname)
            if fname.endswith(".md"):
                with open(fpath, encoding="utf-8") as f:
                    out.write(f.read() + "\n\n")
    print(f"合并注入: {src_dir}{dst_file}")

if __name__ == "__main__":
    inject_memory("output/", ".openclaw/personas/repo-guru/memory/")

2.3 注入后的 memory 结构

# .openclaw/personas/repo-guru/memory/
# 注入完成后
memory/
├── project.md        # 来自仓库画像
├── history.md        # 来自演进报告
├── architecture.md   # 来自架构文档
├── knowledge.md      # 来自知识摘要
├── decisions.md      # 来自 ADR 合并
└── graph.json        # 来自知识图谱

三、技能绑定:skills 配置

3.1 技能绑定流程

技能不是"文档",而是"能力"。绑定分三步:

[模式库/工具链] → [能力定义] → [触发规则] → [执行步骤] → [skills 配置]

3.2 技能配置示例

# .openclaw/personas/repo-guru/skills/analyze.yaml
# 技能:代码模式分析(来自模式库)
name: analyze
description: 分析代码是否符合项目既有模式
trigger:
  - 用户询问"这段代码符合项目模式吗"
  - 用户询问"应该用哪种模式实现"
steps:
  - 从 memory/patterns.md 加载模式库
  - 解析用户提供的代码片段
  - 与模式库逐条比对
  - 输出匹配的模式 + 差异说明
  - 引用模式文档来源
# .openclaw/personas/repo-guru/skills/run.yaml
# 技能:执行蒸馏分析(来自工具链)
name: run
description: 对仓库执行蒸馏分析,生成最新知识
trigger:
  - 用户询问"仓库最近有什么变化"
  - 用户要求"重新分析仓库"
steps:
  - 调用蒸馏工具链(make all)
  - 读取最新产物
  - 更新 memory 中对应文件
  - 汇报更新内容

3.3 技能绑定脚本

#!/usr/bin/env python3
# scripts/bind_skills.py
# 将模式库/工具链绑定为虚拟人技能
import json
import os

def bind_skills(distill_dir, skills_dir):
    """将蒸馏产物绑定为虚拟人技能"""
    os.makedirs(skills_dir, exist_ok=True)

    # 模式库 → analyze 技能
    patterns = os.path.join(distill_dir, "patterns/")
    if os.path.isdir(patterns):
        skill = {
            "name": "analyze",
            "description": "分析代码是否符合项目既有模式",
            "trigger": ["模式匹配", "代码审查"],
            "source": "patterns/",
            "steps": ["加载模式库", "比对代码", "输出结果"]
        }
        write_skill(skills_dir, "analyze.yaml", skill)
        print("绑定技能: patterns/ → analyze")

    # 工具链 → run 技能
    toolchain = os.path.join(distill_dir, "toolchain/")
    if os.path.isdir(toolchain):
        skill = {
            "name": "run",
            "description": "执行蒸馏分析,更新知识",
            "trigger": ["重新分析", "仓库变化"],
            "source": "toolchain/",
            "steps": ["执行工具链", "读取产物", "更新 memory"]
        }
        write_skill(skills_dir, "run.yaml", skill)
        print("绑定技能: toolchain/ → run")

def write_skill(skills_dir, fname, skill):
    """写入技能配置(YAML 格式)"""
    with open(os.path.join(skills_dir, fname), "w", encoding="utf-8") as f:
        for key, val in skill.items():
            if isinstance(val, list):
                f.write(f"{key}:\n")
                for item in val:
                    f.write(f"  - {item}\n")
            else:
                f.write(f"{key}: {val}\n")

if __name__ == "__main__":
    bind_skills("output/", ".openclaw/personas/repo-guru/skills/")

四、输出虚拟人配置

4.1 完整配置结构

注入和绑定完成后,虚拟人配置完整:

.openclaw/personas/repo-guru/
├── persona.yaml          # 人格(来自项目定位)
├── memory/               # 记忆(来自蒸馏产物)
│   ├── project.md
│   ├── history.md
│   ├── architecture.md
│   ├── knowledge.md
│   ├── decisions.md
│   └── graph.json
└── skills/               # 技能(来自模式库/工具链)
    ├── analyze.yaml
    └── run.yaml

4.2 配置验证

# 1. 验证目录完整性
tree .openclaw/personas/repo-guru/

# 2. 验证 YAML 语法
python -c "
import yaml, glob
for f in glob.glob('.openclaw/**/*.yaml', recursive=True):
    yaml.safe_load(open(f))
    print(f'OK: {f}')
"

# 3. 验证 memory 非空
find .openclaw/personas/repo-guru/memory/ -name "*.md" -size +0 | wc -l

# 4. 端到端测试
openclaw ask "项目架构是怎样的?"
openclaw ask "这段代码符合项目模式吗?"

4.3 常见问题与排查

问题原因排查
虚拟人回答"不知道"memory 未注入或格式错误检查 memory 文件是否非空、格式是否规范
虚拟人回答不准确知识图谱关联缺失检查 graph.json 是否完整
技能不触发trigger 规则不匹配检查 skills 的 trigger 配置
回答风格不对persona 配置未生效检查 persona.yaml 是否被正确加载

五、小结

这一篇的核心收获:

  1. 映射:8 类蒸馏产物逐一映射到 memory 或 skills,文档类进 memory、能力类进 skills。
  2. 知识注入:用脚本把产物结构化注入 memory,含格式转换、结构重排、关联索引。
  3. 技能绑定:把模式库/工具链转化为可执行技能,定义触发规则与执行步骤。
  4. 输出配置:形成完整虚拟人配置,并给出验证与排查方法。

下一篇,我们把虚拟人真正"用起来":[13 虚拟人落地:从配置到实战](13-Git 仓库蒸馏术:从代码仓库到 OpenClaw 虚拟人-虚拟人落地.md)——让虚拟人在真实场景中发挥作用。


上一篇:[11 OpenClaw 虚拟人机制:persona、memory、skills](11-Git 仓库蒸馏术:从代码仓库到 OpenClaw 虚拟人-OpenClaw虚拟人机制.md)
下一篇:[13 虚拟人落地:从配置到实战](13-Git 仓库蒸馏术:从代码仓库到 OpenClaw 虚拟人-虚拟人落地.md)

Logo

中国智能体开发者社区,聚焦智能体与大模型开发,提供前沿资讯、实用工具链、开源项目及行业案例。通过技术沙龙、开发者大赛等活动,促进经验交流与协作,助力开发者快速构建创新智能应用。

更多推荐