| name | ontology |
| description | Typed knowledge graph for structured agent memory and composable skills. Use when creating/querying entities (Person, Project, Task, Event, Document), linking related objects, enforcing constraints, planning multi-step actions as graph transformations, or when skills need to share state. Trigger on "remember", "what do I know about", "link X to Y", "show dependencies", entity CRUD, or cross-skill data access. |
| triggers | ["ontology","知识图谱","实体关系","remember","记住","what do I know about","link X to Y","show dependencies","实体CRUD","graph","图谱","结构化知识"] |
Ontology - 知识图谱技能
类型化的词汇表 + 约束系统,用于将知识表示为可验证的图结构。
🎯 核心概念
一切都是具有类型、属性和与其他实体的关系的实体。每次变更都会在提交前根据类型约束进行验证。
实体: { id, type, properties, relations, created, updated }
关系: { from_id, relation_type, to_id, properties }
📋 何时使用
| 触发词 | 动作 |
|---|
| "记住..." | 创建/更新实体 |
| "关于X我知道什么?" | 查询图谱 |
| "将X链接到Y" | 创建关系 |
| "显示项目Z的所有任务" | 图遍历 |
| "什么依赖于X?" | 依赖查询 |
| 规划多步工作 | 建模为图转换 |
| 技能需要共享状态 | 读取/写入本体对象 |
🚀 快速开始
基本使用
mkdir -p memory/ontology
touch memory/ontology/graph.jsonl
python3 scripts/ontology.py schema-append --data '{
"types": {
"Task": { "required": ["title", "status"] },
"Project": { "required": ["name"] },
"Person": { "required": ["name"] }
}
}'
python3 scripts/ontology.py create --type Person --props '{"name":"Alice"}'
python3 scripts/ontology.py list --type Person
📖 使用示例
中文示例
python3 scripts/ontology.py create --type Project --props '{"name":"CoPaw技能优化", "status":"进行中"}'
python3 scripts/ontology.py create --type Task --props '{"title":"优化ontology技能", "status":"进行中", "priority":"高"}'
python3 scripts/ontology.py relate --from <task_id> --relation belongs_to --to <project_id>
python3 scripts/ontology.py list --type Task
python3 scripts/ontology.py query --type Project --where 'name="CoPaw技能优化"'
英文示例
python3 scripts/ontology.py create --type Person --props '{"name":"Alice", "role":"developer"}'
python3 scripts/ontology.py create --type Task --props '{"title":"Review code", "status":"pending"}'
python3 scripts/ontology.py relate --from <task_id> --relation assigned_to --to <person_id>
python3 scripts/ontology.py list --type Task --where 'status="pending"'
🔧 核心功能
实体操作
python3 scripts/ontology.py create --type <type> --props '{"key":"value"}'
python3 scripts/ontology.py get --id <entity_id>
python3 scripts/ontology.py update --id <entity_id> --props '{"new_key":"new_value"}'
python3 scripts/ontology.py delete --id <entity_id>
关系操作
python3 scripts/ontology.py relate --from <from_id> --relation <relation_type> --to <to_id>
python3 scripts/ontology.py unrelate --from <from_id> --relation <relation_type> --to <to_id>
查询操作
python3 scripts/ontology.py list --type <type>
python3 scripts/ontology.py query --type <type> --where 'condition'
python3 scripts/ontology.py traverse --from <entity_id> --relation <relation_type>
🛠️ 模式定义
定义实体类型
types:
Task:
required: ["title", "status"]
properties:
title: { type: "string" }
status: { type: "string", enum: ["todo", "in-progress", "done"] }
priority: { type: "string", enum: ["low", "medium", "high"] }
relations:
belongs_to: { to: "Project", cardinality: "many-to-one" }
assigned_to: { to: "Person", cardinality: "many-to-one" }
🔍 高级查询
复杂查询示例
query = {
"type": "Task",
"where": "status != 'done' and priority = 'high'",
"traverse": {
"from": "<project_id>",
"relation": "has_task"
}
}
图遍历
python3 scripts/ontology.py traverse --from <entity_id> --relation depends_on --depth 3
python3 scripts/ontology.py traverse --from <entity_id> --relation affects --direction outgoing
📁 文件结构
ontology/
├── SKILL.md # 技能说明文档
├── _meta.json # 元数据
├── scripts/
│ └── ontology.py # 主脚本
└── references/
├── schema.md # 完整类型定义
└── queries.md # 查询语言示例
⚠️ 故障排除
常见问题
-
实体创建失败
python3 scripts/ontology.py validate --id <entity_id>
-
关系验证失败
python3 scripts/ontology.py schema-show
-
查询返回空结果
python3 scripts/ontology.py stats
错误处理
python3 scripts/ontology.py debug --id <entity_id>
rm memory/ontology/graph.jsonl
touch memory/ontology/graph.jsonl
🔄 与其他技能集成
与GitHub技能集成
python3 scripts/ontology.py create --type Task --props '{
"title": "GitHub Issue #123",
"status": "open",
"source": "github",
"url": "https://github.com/owner/repo/issues/123"
}'
与飞书技能集成
python3 scripts/ontology.py create --type Task --props '{
"title": "飞书待办:项目评审",
"status": "pending",
"source": "feishu",
"due_date": "2024-12-31"
}'
📊 性能优化
索引优化
python3 scripts/ontology.py index-create --property "status"
python3 scripts/ontology.py index-list
批量操作
python3 scripts/ontology.py batch-create --file entities.json
python3 scripts/ontology.py batch-relate --file relations.json
📚 参考文档
完整文档
references/schema.md — 完整类型定义和约束模式
references/queries.md — 查询语言和遍历示例
API参考
python3 scripts/ontology.py --help
python3 scripts/ontology.py create --help
🎯 最佳实践
- 始终定义模式:避免数据不一致
- 使用约束:确保数据质量
- 定期备份:防止数据丢失
- 监控性能:及时优化查询
- 版本控制:跟踪模式变更
🔮 未来计划
v2.0.0 计划功能
- 实时同步功能
- 可视化界面
- 自动模式推导
- 机器学习集成
- 分布式存储支持
✨ 让知识结构化,让智能更强大!