前言¶
在 DeepSeek Harness 的生態中,智能體常用的記憶方式往往是自由文本。記錄下來的內容隨時間推移可能產生矛盾,或者不再符合最初的定義。更重要的是,文本記憶無法自動推導隱含關係,導致知識碎片化。
dsh-ontology 插件爲智能體提供了一個結構化的知識框架。它將領域知識定義爲類型化的本體(TBox + ABox),強制智能體在聲明類別和關係時遵守模式約束。通過這種方式,斷言會在存儲前被驗證,推導出的隱含事實會在查詢時即時生成,而非重複存儲。
插件定位¶
這是一個爲 DeepSeek Harness 提供的類型化、可推理本體插件。它包含 TBox(概念定義與關係類型)和 ABox(實例與事實斷言)兩部分。智能體可以在其中聲明領域知識、斷言事實,並在模式約束下進行查詢。
核心功能¶
- 類型化與推理:支持基於模式約束的驗證,以及 transitive(傳遞性)、symmetric(對稱性)和 inverseOf(逆關係)的推理。
- 依賴感知的撤回:在移除事實時,插件會自動處理依賴關係,防止孤立數據殘留。
- 多模式查詢:支持 schema(模式)、stats(統計)、entities(實體)、facts(事實)、neighbors(鄰居)和 path(路徑)等多種查詢方式。
安裝與啓用¶
使用官方命令將插件添加到指定的 Profile 中:
dsh plugin --profile <name> add dsh-ontology
dsh --profile <name>
配置與使用¶
安裝後,可以通過 Profile 的 cordis.patch.yml 覆蓋默認配置項,例如存儲域名、嚴格模式開關、推理開關等。
1. 定義模式 (TBox)¶
使用 ontology_define 聲明類和類型化關係。
// ontology_define
{
"classes": [
{ "id": "Service", "subClassOf": ["Component"] },
{ "id": "Component", "comment": "A deployable unit of the system" },
{ "id": "Person" }
],
"relations": [
{ "id": "depends_on", "domain": ["Component"], "range": ["Component"], "characteristics": ["transitive"] },
{ "id": "owns", "domain": ["Person"], "range": ["Component"], "inverseOf": "owned_by" },
{ "id": "owned_by", "domain": ["Component"], "range": ["Person"] },
{ "id": "version", "domain": ["Component"], "rangeKind": "literal" }
]
}
2. 斷言事實 (ABox)¶
使用 ontology_assert 記錄實體和事實。在此步驟中,系統會驗證斷言是否符合已定義的模式。
// ontology_assert
{
"entities": [
{ "id": "api", "classes": ["Service"] },
{ "id": "auth", "classes": ["Service"] },
{ "id": "pg", "classes": ["Component"] },
{ "id": "ada", "classes": ["Person"] }
],
"facts": [
{ "subject": "api", "predicate": "depends_on", "object": "auth" },
{ "subject": "auth", "predicate": "depends_on", "object": "pg" },
{ "subject": "ada", "predicate": "owns", "object": "api", "source": "CODEOWNERS" },
{ "subject": "api", "predicate": "version", "object": "2.1.0" }
]
}
3. 查詢與推理¶
使用 ontology_query 進行查找。由於 depends_on 被定義爲 transitive(傳遞性),查詢 api 的依賴時會自動包含 pg。
// ontology_query
{ "mode": "facts", "subject": "api", "predicate": "depends_on", "includeInferred": true }
結果將包含推導出的事實:
api depends_on auth
api depends_on pg (inferred: transitive)
4. 安全撤回¶
使用 ontology_retract 移除事實。如果移除某個實體,所有依賴於此實體的推導事實也會隨之消失。
注意事項¶
- 構建權限:如果通過
github:安裝,需要 pnpm ≥10。必須在 Profile 的pnpm-workspace.yaml中添加allowBuilds: { dsh-ontology: true }。 - 命名規範:存儲域名必須匹配
/^[a-z][a-z0-9_]*$/。 - 隔離運行:運行隔離圖需要單獨的域名。
- 字面量限制:取值爲字面量的關係不能被設爲對稱、傳遞或逆關係,否則定義會被拒絕。
總結¶
dsh-ontology 將智能體的記憶從“文本袋”轉變爲受控的知識圖譜。它通過嚴格的模式約束和自動推理,確保了領域知識的可靠性和一致性。對於需要構建長期記憶、處理複雜依賴關係的智能體開發場景,這是一個實用的工具。