前面我们聊了 Graphiti、Ontop、TrustGraph,它们都比较偏上层系统。
这次我想往下走一层,聊一个更“开发者视角”的项目:Owlready2。
项目地址:
https://github.com/pwin/owlready2官方文档:
https://owlready2.readthedocs.io/一、为什么我想专门写 Owlready2?
前面几篇,我们讨论的更多是:
Graphiti ↓ Agent Memory Ontop ↓ Virtual Knowledge Graph TrustGraph ↓ Context Layer这些系统都已经站在比较高的一层。
但如果你真正想把 Ontology 做进自己的程序里,就会马上遇到一个问题:
本体到底怎么用代码创建?
很多人第一次接触 Ontology,会先看到:
Protégé OWL RDF SPARQL然后发现:
怎么感觉全是学术工具?
而 Owlready2 给了我一个非常工程化的答案:
Python + Ontology官方把 Owlready2 定义为一个面向 Python 的Ontology-Oriented Programming工具,可以加载、修改、保存 OWL 2.0 本体,并把其中的 Class、Instance 和 Property 当成 Python 对象来操作;它还集成了 HermiT 推理器,并内置基于 SQLite3 的优化 quadstore。
这也是为什么我觉得它非常适合今天重新拿出来讲。
二、什么叫 Ontology-Oriented Programming?
我们熟悉:
Object-Oriented Programming也就是:
面向对象编程例如 Python:
classUser:pass然后:
user=User()但是 Owlready2 提出的思路是:
Ontology-Oriented Programming可以简单理解成:
把 Ontology 里面的 Class、Individual、Property,直接映射成 Python 代码。
例如:
Ontology 世界: Person ↓ Developer在 Python 里:
fromowlready2import*onto=get_ontology("http://example.org/dev.owl")withonto:classPerson(Thing):passclassDeveloper(Person):pass看起来是不是非常像普通 Python Class?
这就是 Owlready2 最舒服的地方。
三、我怎么看这件事情?
从我的角度来说,我以前做后端开发时,最熟悉的是:
数据库表 ↓ ORM ↓ Python / Go Object比如:
MySQL ↓ GORM ↓ Go Struct或者:
PostgreSQL ↓ SQLAlchemy ↓ Python Object其实 Owlready2 给我的感觉有点类似:
OWL Ontology ↓ Owlready2 ↓ Python Object当然二者不是同一个概念。
但从开发体验上来看,它们解决的是类似的问题:
让开发者不用一直面对底层格式,而是直接操作对象。
我觉得这是 Owlready2 最重要的价值之一。
四、先安装 Owlready2
安装非常简单:
pipinstallowlready2官方文档目前仍然推荐通过 pip 直接安装。
然后:
fromowlready2import*就可以开始。
五、创建第一个 Ontology
先创建一个本体:
fromowlready2import*onto=get_ontology("http://javapub.net.cn/ontology/ai.owl")这里:
http://javapub.net.cn/ontology/ai.owl叫:
IRI可以简单理解成:
Ontology 在语义世界里的唯一标识。
官方文档中,get_ontology()就是创建或获取 Ontology 的核心入口,IRI 同时也会用于构造其中实体的完整标识。
六、创建 Class
比如我们要建立一个 AI 开发领域的 Ontology。
可以定义:
withonto:classPerson(Thing):passclassDeveloper(Person):passclassCompany(Thing):passclassProject(Thing):pass形成:
Thing ├── Person │ └── Developer ├── Company └── Project这里:
Developer is-a Person也就是说:
Developer ↓ Person这是 Ontology 里非常核心的:
Class Hierarchy七、创建 Individual
Class 是类型。
Individual 就是具体实例。
比如:
shiyu=Developer("wang_shiyu")javapub=Company("javapub")project_a=Project("ontology_project")形成:
Developer | ↓ 王仕宇以及:
Company | ↓ JavaPub官方文档中 Individual 的创建方式和普通 Python Object 很接近,本质就是实例化 Ontology Class。
八、Property 才是 Ontology 真正开始有意思的地方
只有:
Person Company Project其实还只是分类。
真正让世界产生结构的是:
Property比如:
Developer ↓ worksFor ↓ Company可以这样写:
withonto:classworks_for(ObjectProperty):domain=[Developer]range=[Company]然后:
shiyu.works_for=[javapub]现在:
王仕宇 | | worksFor ↓ JavaPub就建立起来了。
九、ObjectProperty 和 DataProperty
Ontology 里面有两个非常重要的 Property。
ObjectProperty
连接:
实体 ↓ 实体例如:
Developer ↓ worksFor ↓ Company代码:
classworks_for(ObjectProperty):domain=[Developer]range=[Company]DataProperty
连接:
实体 ↓ 普通数据例如:
Person ↓ age ↓ 30可以:
withonto:classage(DataProperty):domain=[Person]range=[int]然后:
shiyu.age=[30]形成:
王仕宇 ↓ age ↓ 30十、如果从后端开发的角度理解
我觉得可以这样记:
数据库:
Table Column Foreign KeyOntology:
Class Property Relationship例如数据库:
users------id name company_id后端开发者看到:
company_idOntology 看到:
User ↓ worksFor ↓ Company数据库强调:
数据怎么存。
Ontology 强调:
这件事情到底是什么意思。
这就是二者最大的区别。
十一、保存 Ontology
创建完成后:
onto.save(file="ai.owl",format="rdfxml")就可以保存。
然后生成:
ai.owl这个文件可以:
Owlready2 ↓ Protégé ↓ 其他 OWL 工具继续使用。
Owlready2 官方支持加载和保存 OWL,本身就是为了让 Python 程序能够透明访问本体,而不只是生成一个孤立的数据结构。
十二、加载已有 Ontology
如果别人已经做好:
ecommerce.owl我们也可以直接:
onto=get_ontology("file://ecommerce.owl").load()然后:
print(list(onto.classes()))得到:
Customer Order Product Supplier Payment再:
print(list(onto.individuals()))查看所有实例。
十三、Owlready2 最厉害的地方之一:Reasoning
这也是 Ontology 和普通数据库特别不一样的地方。
数据库通常只保存:
已知数据Ontology 可以:
根据规则推导新知识这叫:
ReasoningOwlready2 可以调用 HermiT Reasoner 进行自动分类和推理。
例如:
Developer is-a Person以及:
王仕宇 is-a Developer推理器可以得到:
王仕宇 is-a Person虽然你没有显式写出来。
十四、再举一个更有意思的例子
我们定义:
AIEngineer = Developer AND uses some AIModel如果:
王仕宇 ↓ uses ↓ GPT同时:
王仕宇 ↓ is-a ↓ DeveloperReasoner 可能推断:
王仕宇 ↓ is-a ↓ AIEngineer这和:
if else不太一样。
它更接近:
根据语义规则推导事实。
十五、调用 Reasoner
例如:
withonto:sync_reasoner()然后查看:
print(shiyu.is_a)推理器可能会加入新的分类结果。
这就是:
Explicit Knowledge + Reasoning = Implicit Knowledge十六、我为什么觉得这个能力对 AI Agent 很重要?
大模型擅长:
模糊推理 自然语言理解 概率判断Ontology Reasoner 擅长:
确定性规则 类型约束 逻辑一致性所以我越来越觉得:
未来 Agent 不一定只是:
LLM而可能是:
LLM + Ontology + Reasoner比如:
用户是 VIP VIP 可以使用高级模型 高级模型禁止匿名用户使用这种规则:
完全可以通过结构化语义进行判断。
而不是每次都让 LLM:
猜。十七、Owlready2 + SPARQL
Owlready2 也支持 SPARQL 查询,而且官方文档现在包含自己的 Native SPARQL Engine,以及和 RDFLib 配合的查询方式。
例如:
results=list(default_world.sparql(""" SELECT ?x WHERE { ?x rdf:type <http://example.org/Developer> } """))这时候:
Python ↓ SPARQL ↓ Ontology就连接起来了。
十八、为什么既要 Python,又要 SPARQL?
Python 更适合:
业务逻辑 API 自动化 AgentSPARQL 更适合:
图查询 关系查询 语义查询两者结合:
FastAPI ↓ Python Service ↓ Owlready2 ↓ SPARQL ↓ Ontology就可以直接做一个:
Ontology API Server十九、甚至可以自己做一个 Ontology Service
比如:
POST /ontology/entities创建实体。
GET /ontology/query查询。
POST /ontology/reason推理。
整体:
AI Agent ↓ FastAPI ↓ Owlready2 / \ ↓ ↓ SPARQL Reasoner \ / ↓ Ontology这已经非常像一个:
Semantic Backend二十、Owlready2 自带 Quadstore
Owlready2 并不是每次都在内存里处理所有东西。
它内部有自己的:
Triple Store / Quadstore而且使用:
SQLite3作为底层存储。
官方文档明确说明 Owlready2 Version 2 包含一个针对性能和内存优化的 SQLite3 quadstore,因此可以处理比第一代更大的 Ontology。
这意味着:
Python Object背后其实还有:
Graph Storage二十一、什么叫 Triple?
知识图谱最基础的结构:
Subject Predicate Object比如:
王仕宇 ↓ worksFor ↓ JavaPub可以写成:
Subject: 王仕宇 Predicate: worksFor Object: JavaPub这就是:
Triple二十二、什么叫 Quad?
Quad:
Subject Predicate Object Graph比 Triple 多:
Graph例如:
王仕宇 worksFor JavaPub AI-Business-Graph这样可以把:
不同 Ontology 不同 Dataset 不同 Context区分开。
二十三、Owlready2 + LLM,我觉得才是真正有意思的方向
到这里,其实 Owlready2 本身已经是一个比较成熟的 Ontology 工具。
但是如果只是:
手写 Class我觉得没有那么有意思。
真正有意思的是:
LLM + Owlready2例如:
用户上传数据库 SchemaAI:
理解表 ↓ 识别业务概念 ↓ 生成 Ontology ↓ Owlready2 写入 OWL架构:
Database Schema ↓ LLM ↓ Concept Extraction ↓ Relationship Extraction ↓ Owlready2 ↓ OWL Ontology二十四、举一个实际例子
数据库:
CREATETABLEusers(idBIGINT,nameVARCHAR(100));CREATETABLEorders(idBIGINT,user_idBIGINT);LLM 分析:
users ↓ User orders ↓ Order关系:
User ↓ places ↓ Order然后自动生成:
withonto:classUser(Thing):passclassOrder(Thing):passclassplaces(ObjectProperty):domain=[User]range=[Order]最后保存:
business.owl这已经是:
AI 自动生成 Ontology二十五、再进一步:从文档生成 Ontology
比如输入:
公司客户购买产品后, 订单由销售负责跟进, 产品由供应商提供。LLM 提取:
Customer Product Order Salesperson Supplier关系:
Customer ↓ purchases Product Salesperson ↓ manages Order Supplier ↓ supplies ProductOwlready2:
创建 Class ↓ 创建 Property ↓ 保存 OWL最终:
自然语言 ↓ Ontology我觉得这个方向非常值得做。
二十六、从我的角度,我会怎么使用 Owlready2?
如果让我现在用 Owlready2 做一个真实项目,我不会先做一个:
Ontology 编辑器因为 Protégé 已经很好用了。
我更倾向做:
AI Ontology Generator也就是:
PDF Database Schema API Schema Business Document ↓ LLM ↓ Ontology Extraction ↓ Owlready2 ↓ OWL然后再:
OWL ↓ GraphRAG ↓ AI Agent这个路线我觉得更有现实价值。
二十七、Owlready2 + Graphiti
前面我们讲:
Graphiti主要解决:
Temporal Knowledge Graph也就是:
世界怎么变化。
而 Owlready2 可以负责:
Ontology也就是:
世界应该是什么结构。
组合:
Owlready2 ↓ Ontology ↓ Graphiti ↓ Temporal Knowledge Graph ↓ Agent Memory这两个其实可以很好地分工。
二十八、Owlready2 + Ontop
Ontop:
Database ↓ Virtual Knowledge GraphOwlready2:
Python ↓ Ontology于是可以:
Database Schema ↓ LLM ↓ Owlready2 ↓ Ontology ↓ Ontop ↓ Virtual Knowledge Graph ↓ SPARQL也就是说:
Owlready2 甚至可以成为:
Ontology Generation Layer二十九、Owlready2 + TrustGraph
TrustGraph:
Documents Database API ↓ Context Graph如果前面加:
Owlready2可以先定义:
Domain Ontology然后:
Owlready2 ↓ Ontology ↓ TrustGraph ↓ Ontology RAG ↓ Context Graph ↓ Agent这样 TrustGraph 在抽取实体时,就不是:
随便抽而是:
按照业务 Ontology 抽。三十、这几个项目其实已经可以串起来了
我们现在已经讲了:
Owlready2 Ontop TrustGraph Graphiti可以组合:
LLM ↓ Owlready2 ↓ Ontology ┌──────────┼──────────┐ ↓ ↓ Ontop TrustGraph ↓ ↓ Enterprise DB Context Graph ↓ Graphiti ↓ Agent Memory ↓ AI Agent我觉得这张图已经非常接近:
Ontology-driven Agent Architecture
三十一、为什么我越来越关注 Ontology?
从我自己的角度来说,我以前做后端时,关注的是:
API Database Redis MQ Docker K8s这些东西解决的是:
系统怎么运行。
进入 AI Agent 之后,我发现出现了一个新的问题:
AI 到底怎么理解系统?
数据库 Schema:
告诉程序怎么存数据。API:
告诉程序怎么调用能力。但是还缺一个东西:
告诉 AI: 这个业务世界是什么。我觉得这可能正是:
Ontology重新变得重要的原因。
三十二、数据库 Schema 和 Ontology 的本质区别
数据库:
users orders products描述:
数据结构Ontology:
Customer places Order contains Product描述:
世界结构我觉得这句话非常值得记住:
数据库 Schema 描述数据怎么存,Ontology 描述世界怎么理解。
三十三、未来 AI Agent 很可能需要两套 Schema
第一套:
Technical Schema比如:
Database Schema API Schema JSON Schema第二套:
Semantic Schema比如:
Ontology于是:
AI Agent | ↓ Semantic Schema | ↓ Business World | ↓ Technical Schema | ↓ Database / API我认为这是未来 Agent 架构非常值得研究的一层。
三十四、Owlready2 的优点
1. Python 生态
这是最大优势。
可以非常方便接:
FastAPI LLM LangChain LlamaIndex Agent2. OWL 支持
它不是自己发明一个图格式。
直接使用:
OWL 2.03. Reasoner
可以:
自动分类 逻辑推理 一致性检查4. SPARQL
可以做真正的:
Semantic Query5. 和 Python Object 很接近
开发者上手成本比较低。
三十五、它的缺点也很明显
Owlready2 并不是一个:
完整企业知识图谱平台它更像:
Ontology Programming Library如果你需要:
亿级 Graph 分布式存储 高并发查询 复杂图分析那肯定需要:
Neo4j GraphDB 其他图数据库Owlready2 更适合:
Ontology Modeling Reasoning Python Integration Prototype AI Ontology Pipeline三十六、我最推荐的学习方式
如果第一次学,我建议不要一上来研究:
Description Logic也不要先背:
OWL 规范直接做一个:
电商 Ontology比如:
Customer Order Product Supplier关系:
Customer ↓ places Order Order ↓ contains Product Supplier ↓ supplies Product然后用 Owlready2 写出来。
真正跑起来之后,再理解:
Class Individual ObjectProperty DataProperty Reasoner SPARQL会快很多。
三十七、总结
一句话总结 Owlready2:
Owlready2 是一个 Python Ontology 编程框架,它让开发者能够像操作 Python 对象一样创建、修改、查询和推理 OWL Ontology。
如果:
Protégé更像:
Ontology IDE那么:
Owlready2更像:
Ontology SDK它最大的价值不是:
再做一个知识图谱而是让:
Ontology真正进入:
Python AI Agent Automation这一套工程体系里。
我现在更关注的一条路线是:
Business Data ↓ LLM ↓ Ontology Generation ↓ Owlready2 ↓ OWL Ontology ↓ GraphRAG / TrustGraph / Ontop ↓ Agent ↓ Reasoning & Action如果未来 Agent 不只是:
会聊天而是真正需要:
理解业务 理解关系 理解规则 理解世界那 Ontology 很可能会重新成为 AI 基础设施里非常重要的一层。
而 Owlready2,可能就是 Python 开发者进入这一层最简单、最直接的入口之一。
项目地址
Owlready2:
https://github.com/pwin/owlready2官方文档:
https://owlready2.readthedocs.io/王仕宇 JavaPub
https://javapub.net.cn/