ontology

ontology

热门

类型化知识图谱,用于结构化智能体记忆和可组合技能。在创建/查询实体(Person、Project、Task、Event、Document)、链接相关对象、强制执行约束、将多步操作规划为图转换,或技能需要共享状态时使用。触发词包括“记住”、“我知道关于X的什么”、“链接X到Y”、“显示依赖关系”、实体CRUD或跨技能数据访问。

632Star
89Fork
更新于 2026/3/7
SKILL.md
只读
名称
ontology
描述

类型化知识图谱,用于结构化智能体记忆和可组合技能。在创建/查询实体(Person、Project、Task、Event、Document)、链接相关对象、强制执行约束、将多步操作规划为图转换,或技能需要共享状态时使用。触发词包括“记住”、“我知道关于X的什么”、“链接X到Y”、“显示依赖关系”、实体CRUD或跨技能数据访问。

本体(Ontology)

一个类型化词汇表+约束系统,用于将知识表示为可验证的图。

核心概念

一切都是具有类型属性和与其他实体关系实体。每次变更在提交前都会根据类型约束进行验证。

Entity: { id, type, properties, relations, created, updated }
Relation: { from_id, relation_type, to_id, properties }

何时使用

触发词 操作
“记住...” 创建/更新实体
“我知道关于X的什么?” 查询图
“链接X到Y” 创建关系
“显示项目Z的所有任务” 图遍历
“什么依赖于X?” 依赖查询
规划多步工作 建模为图转换
技能需要共享状态 读/写本体对象

核心类型

# 智能体与人员
Person: { name, email?, phone?, notes? }
Organization: { name, type?, members[] }

# 工作
Project: { name, status, goals[], owner? }
Task: { title, status, due?, priority?, assignee?, blockers[] }
Goal: { description, target_date?, metrics[] }

# 时间与地点
Event: { title, start, end?, location?, attendees[], recurrence? }
Location: { name, address?, coordinates? }

# 信息
Document: { title, path?, url?, summary? }
Message: { content, sender, recipients[], thread? }
Thread: { subject, participants[], messages[] }
Note: { content, tags[], refs[] }

# 资源
Account: { service, username, credential_ref? }
Device: { name, type, identifiers[] }
Credential: { service, secret_ref }  # 切勿直接存储密钥

# 元数据
Action: { type, target, timestamp, outcome? }
Policy: { scope, rule, enforcement }

存储

默认路径:memory/ontology/graph.jsonl

{"op":"create","entity":{"id":"p_001","type":"Person","properties":{"name":"Alice"}}}
{"op":"create","entity":{"id":"proj_001","type":"Project","properties":{"name":"Website Redesign","status":"active"}}}
{"op":"relate","from":"proj_001","rel":"has_owner","to":"p_001"}

通过脚本或直接文件操作进行查询。对于复杂图,可迁移至SQLite。

工作流

创建实体

python3 scripts/ontology.py create --type Person --props '{"name":"Alice","email":"alice@example.com"}'

查询

python3 scripts/ontology.py query --type Task --where '{"status":"open"}'
python3 scripts/ontology.py get --id task_001
python3 scripts/ontology.py related --id proj_001 --rel has_task

链接实体

python3 scripts/ontology.py relate --from proj_001 --rel has_task --to task_001

验证

python3 scripts/ontology.py validate  # 检查所有约束

约束

定义在 memory/ontology/schema.yaml 中:

types:
  Task:
    required: [title, status]
    status_enum: [open, in_progress, blocked, done]
  
  Event:
    required: [title, start]
    validate: "end >= start if end exists"

  Credential:
    required: [service, secret_ref]
    forbidden_properties: [password, secret, token]  # 强制间接引用

relations:
  has_owner:
    from_types: [Project, Task]
    to_types: [Person]
    cardinality: many_to_one
  
  blocks:
    from_types: [Task]
    to_types: [Task]
    acyclic: true  # 无循环依赖

技能契约

使用本体的技能应声明:

# 在 SKILL.md 的 frontmatter 或头部
ontology:
  reads: [Task, Project, Person]
  writes: [Task, Action]
  preconditions:
    - "Task.assignee 必须存在"
  postconditions:
    - "创建的 Task 状态为 open"

作为图转换的规划

将多步计划建模为一系列图操作:

计划:“安排团队会议并创建后续任务”

1. CREATE Event { title: "团队同步", attendees: [p_001, p_002] }
2. RELATE Event -> has_project -> proj_001
3. CREATE Task { title: "准备议程", assignee: p_001 }
4. RELATE Task -> for_event -> event_001
5. CREATE Task { title: "发送总结", assignee: p_001, blockers: [task_001] }

每一步在执行前都经过验证。违反约束时回滚。

集成模式

与因果推断结合

将本体变更记录为因果动作:

# 创建/更新实体时,同时记录到因果动作日志
action = {
    "action": "create_entity",
    "domain": "ontology", 
    "context": {"type": "Task", "project": "proj_001"},
    "outcome": "created"
}

跨技能通信

# 邮件技能创建承诺
commitment = ontology.create("Commitment", {
    "source_message": msg_id,
    "description": "周五前发送报告",
    "due": "2026-01-31"
})

# 任务技能拾取
pending = ontology.query("Commitment", {"status": "pending"})
for c in pending:
    ontology.create("Task", {
        "title": c.description,
        "due": c.due,
        "source": c.id
    })

快速开始

# 初始化本体存储
mkdir -p memory/ontology
touch memory/ontology/graph.jsonl

# 创建模式(可选但推荐)
cat > memory/ontology/schema.yaml << 'EOF'
types:
  Task:
    required: [title, status]
  Project:
    required: [name]
  Person:
    required: [name]
EOF

# 开始使用
python3 scripts/ontology.py create --type Person --props '{"name":"Alice"}'
python3 scripts/ontology.py list --type Person

参考

  • references/schema.md — 完整类型定义和约束模式
  • references/queries.md — 查询语言和遍历示例