
前面我们聊了 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也就是面向对象编程例如 PythonclassUser:pass然后userUser()但是 Owlready2 提出的思路是Ontology-Oriented Programming可以简单理解成把 Ontology 里面的 Class、Individual、Property直接映射成 Python 代码。例如Ontology 世界 Person ↓ Developer在 Python 里fromowlready2import*ontoget_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*ontoget_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七、创建 IndividualClass 是类型。Individual 就是具体实例。比如shiyuDeveloper(wang_shiyu)javapubCompany(javapub)project_aProject(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 和 DataPropertyOntology 里面有两个非常重要的 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 KeyOntologyClass Property Relationship例如数据库users------id name company_id后端开发者看到company_idOntology 看到User ↓ worksFor ↓ Company数据库强调数据怎么存。Ontology 强调这件事情到底是什么意思。这就是二者最大的区别。十一、保存 Ontology创建完成后onto.save(fileai.owl,formatrdfxml)就可以保存。然后生成ai.owl这个文件可以Owlready2 ↓ Protégé ↓ 其他 OWL 工具继续使用。Owlready2 官方支持加载和保存 OWL本身就是为了让 Python 程序能够透明访问本体而不只是生成一个孤立的数据结构。十二、加载已有 Ontology如果别人已经做好ecommerce.owl我们也可以直接ontoget_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 SPARQLOwlready2 也支持 SPARQL 查询而且官方文档现在包含自己的 Native SPARQL Engine以及和 RDFLib 配合的查询方式。例如resultslist(default_world.sparql( SELECT ?x WHERE { ?x rdf:type http://example.org/Developer } ))这时候Python ↓ SPARQL ↓ Ontology就连接起来了。十八、为什么既要 Python又要 SPARQLPython 更适合业务逻辑 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 自带 QuadstoreOwlready2 并不是每次都在内存里处理所有东西。它内部有自己的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二十二、什么叫 QuadQuadSubject 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 OntopOntopDatabase ↓ Virtual Knowledge GraphOwlready2Python ↓ Ontology于是可以Database Schema ↓ LLM ↓ Owlready2 ↓ Ontology ↓ Ontop ↓ Virtual Knowledge Graph ↓ SPARQL也就是说Owlready2 甚至可以成为Ontology Generation Layer二十九、Owlready2 TrustGraphTrustGraphDocuments 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描述数据结构OntologyCustomer 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会快很多。三十七、总结一句话总结 Owlready2Owlready2 是一个 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 开发者进入这一层最简单、最直接的入口之一。项目地址Owlready2https://github.com/pwin/owlready2官方文档https://owlready2.readthedocs.io/王仕宇 JavaPubhttps://javapub.net.cn/