🛡️ AI代理的本体防火墙
   
基于本体的人工智能代理架构的生产就绪实现,通过添加确定性验证层来防止企业系统中的幻觉。
📄 阅读全文: 媒介上的本体防火墙
🎯 问题陈述
企业人工智能代理在生产中失败,因为LLM是概率性的——它们预测的是什么 *可能*,不是什么 *正确*在高风险环境(金融、法律、医疗保健)中,“可能正确”是灾难性的。
常见故障模式:
- 将“毛利率”与“营业利润率”混淆(不同的公式)
- 交替使用“子公司”和“关联公司”(法律区别事项)
- 违反数据库操作中的引用完整性
- 忽略业务规则约束(负数、无效日期)
💡 解决方案
该项目实现了 本体防火墙--位于AI代理和执行之间的语义验证层:
User Request → Ontology Validator → LLM + Constraints → Verified Action → Execution主要优势:
- ✅ 防止类别错误(混淆实体类型)
- ✅ 强制执行业务规则(约束、关系)
- ✅ 提供审核跟踪(为什么允许/拒绝操作)
- ✅ 自我修复(当模式改变时进行调整)
- ✅ 生产就绪(打字、测试、记录)
🚀 快速开始
安装
# Clone the repository
git clone https://github.com/cloudbadal007/ontology-firewall.git
cd ontology-firewall
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Install in development mode
pip install -e .基本用法
from ontology_firewall import OntologyValidator, BusinessRules
from ontology_firewall.core.validator import CommonValidators
# Load your ontology
validator = OntologyValidator("ontologies/sales_domain.owl")
# Define business rules
rules = BusinessRules()
rules.add_constraint("Order", "quantity", CommonValidators.positive, "Quantity must be positive")
rules.add_constraint("Order", "discount", CommonValidators.percentage, "Discount must be 0-100%")
# Validate an action
action = {
"entity_type": "Order",
"operation": "create",
"data": {"quantity": 5, "discount": 0.1, "customer_id": "C123"}
}
result = validator.validate(action)
if result.is_valid:
print("✅ Action allowed:", result.message)
else:
print("❌ Action denied:", result.violations)📚 例子
1.基本本体创建和验证
python examples/01_basic_ontology.py创建简单的销售本体并验证基本操作。
2.从数据库模式中提取本体
python examples/02_database_schema_extraction.py从现有的SQL模式自动生成OWL本体。
3.验证防火墙模式
python examples/03_validation_firewall.py演示AI操作的完整验证流程。
4.MCP服务器设置
python examples/04_mcp_server_setup.py通过模型上下文协议将本体暴露给AI代理。
5.完成工作流程
python examples/05_complete_workflow.py端到端示例:模式提取→ 本体论→ MCP → 经过验证的代理。
🏗️ 建筑
三种核心模式
1.验证防火墙
# Prevents invalid actions before execution
validator.validate(action) → Allow | Deny + Reason2.语义路由器
# Routes requests to appropriate handlers based on ontology
router.route(intent, ontology) → Handler + Context3.自愈模式
# Adapts when database schemas change
schema_monitor.detect_change() → Update Ontology → Notify Agents看 建筑.md 查看详细的图表和模式。
🧪 测试
# Run all tests
pytest
# Run with coverage
pytest --cov=src/ontology_firewall --cov-report=html
# Run specific test suite
pytest tests/unit/test_validator.py
# Run integration tests
pytest tests/integration/📦 Docker部署
# Build image
docker build -t ontology-firewall .
# Run MCP server
docker run -p 8000:8000 ontology-firewall
# Or use docker-compose
docker-compose up🔧 配置
通过环境变量进行配置:
export ONTOLOGY_PATH="ontologies/sales_domain.owl"
export MCP_PORT="8000"
export LOG_LEVEL="INFO"
export ENABLE_CACHING="true"或使用 .env 文件(参见 .env.example).
📖 文档
🤝 贡献
我们欢迎捐款!请参阅 贡献.md 作为指导方针。
贡献方式:
- 🐛 通过以下方式报告错误
- 💡 建议功能或改进
- 📝 改进文档
- 🔧 提交拉取请求
🔐 安全
发现安全问题?请参阅 安全.md 负责披露。
📄 许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件。
🙏 致谢
📚 相关工作
- 文章: 媒介上的本体防火墙
- 辅导的: 30分钟内将BI转化为AI代理
- 分析: 微软vs Palantir:企业本体论的两条路径
📊 基准测试
| 操作 | 无本体 | 有本体 | 改进 |
|---|---|---|---|
| 错误检测 | 45% | 98% | +118% |
| 无效操作 | 12% | \<1% | -92% |
| 调试时间 | 2.5小时 | 15分钟 | -83% |
看 基准测试/ 方法论。
🗺️ 路线图
- \[x\] 核心本体验证
- \[x\] MCP服务器实现
- \[x\] 数据库架构提取
- \[\]GraphRAG集成
- \[\]用于本体可视化的Web UI
- \[\]预构建的领域本体(医疗保健、金融、法律)
- \[\]LangChain/LlamaIdex适配器
💬 社区
- 问题? 打开一个问题
- 领英: 与Pankaj建立联系
- 推特/X: @CloudyPankaj
______________________________________________________________________
内置于❤️ 通过 潘卡·库马尔
⭐ 如果你觉得这个仓库有用,就把它标上!
