Oontology

ontology结构化 Agent 记忆的类型化知识图谱技能

浏览32下载0
资料更新2026-08-31来源观测2026-09-03

介绍

快速判断

结构化 Agent 记忆与可组合技能的类型化知识图谱。适用于创建/查询实体(Person、Project、Task、Event、Document)、关联相关对象、实施约束、将多步骤操作规划为图转换,或技能间共享状态。触发词包括:"remember"、"what do I know about"、"link X to Y"、"show dependencies"、实体 CRUD,或跨技能数据访问。

适合谁
知识图谱构建者、Agent 规则设计者、语义建模研究者、自动化工作流搭建者
使用门槛
低,可直接试用
部署方式
本地运行
接入方式
来源
ontology

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

核心概念

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

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

使用场景

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

核心类型

# Agents & People
Person: { name, email?, phone?, notes? }
Organization: { name, type?, members[] }

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

# Time & Place
Event: { title, start, end?, location?, attendees[], recurrence? }
Location: { name, address?, coordinates? }

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

# Resources
Account: { service, username, credential_ref? }
Device: { name, type, identifiers[] }
Credential: { service, secret_ref }  # Never store secrets directly

# Meta
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":"[email protected]"}'

查询

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  # Check all constraints

约束

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]  # Force indirection

relations:
  has_owner:
    from_types: [Project, Task]
    to_types: [Person]
    cardinality: many_to_one
  
  blocks:
    from_types: [Task]
    to_types: [Task]
    acyclic: true  # No circular dependencies

技能契约

使用本体的技能应声明:

# In SKILL.md frontmatter or header
ontology:
  reads: [Task, Project, Person]
  writes: [Task, Action]
  preconditions:
    - "Task.assignee must exist"
  postconditions:
    - "Created Task has status=open"

作为图转换的规划

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

Plan: "Schedule team meeting and create follow-up tasks"

1. CREATE Event { title: "Team Sync", attendees: [p_001, p_002] }
2. RELATE Event -> has_project -> proj_001
3. CREATE Task { title: "Prepare agenda", assignee: p_001 }
4. RELATE Task -> for_event -> event_001
5. CREATE Task { title: "Send summary", assignee: p_001, blockers: [task_001] }

每一步在执行前都会进行验证。约束违规时回滚。

集成模式

与因果推理结合

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

# When creating/updating entities, also log to causal action log
action = {
    "action": "create_entity",
    "domain": "ontology", 
    "context": {"type": "Task", "project": "proj_001"},
    "outcome": "created"
}

跨技能通信

# Email skill creates commitment
commitment = ontology.create("Commitment", {
    "source_message": msg_id,
    "description": "Send report by Friday",
    "due": "2026-01-31"
})

# Task skill picks it up
tasks = ontology.query("Commitment", {"status": "pending"})
for c in tasks:
    ontology.create("Task", {
        "title": c.description,
        "due": c.due,
        "source": c.id
    })

快速开始

# Initialize ontology storage
mkdir -p memory/ontology
touch memory/ontology/graph.jsonl

# Create schema (optional but recommended)
python3 scripts/ontology.py schema-append --data '{
  "types": {
    "Task": { "required": ["title", "status"] },
    "Project": { "required": ["name"] },
    "Person": { "required": ["name"] }
  }
}'

# Start using
python3 scripts/ontology.py create --type Person --props '{"name":"Alice"}'
python3 scripts/ontology.py list --type Person

参考资料

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

指令范围

运行时指令在本地文件(memory/ontology/graph.jsonlmemory/ontology/schema.yaml)上运行,并提供 create/query/relate/validate 的 CLI 用法;这在范围内。该技能读写工作区文件,并在使用时创建 memory/ontology 目录。验证包括属性/enum/forbidden 检查、关系类型/基数验证、标记为 acyclic: true 的关系的无环性检查,以及 Event end >= start 检查;其他更高级别的约束可能仍仅为文档性质,除非在代码中实现。

@oswalpalash

@oswalpalash

ClawHub 创作者
1文章0粉丝32浏览

资源档案