Skip to main content

LDrag - Ontology-Based Retrieval Augmented Generation

LDrag is a Python library for creating, managing, and querying knowledge graphs through an ontology-based approach combined with Large Language Models for Retrieval Augmented Generation (RAG).

Disclaimer

This project is a work in progress and is not yet ready for production use. The codebase is under active development and subject to change. The Project is a Proof of Concept and is not intended for any specific use case. It is developed for the Carl-Zeiss Research Project KIDZ.

Overview

This library provides tools to represent domain knowledge as an ontology, with machine learning models, datasets, and other entities as nodes within this knowledge graph. It enables:

  • Converting trained scikit-learn models into ontology structures
  • Adding dataset metadata from pandas DataFrames to the ontology
  • Calculating and incorporating SHAP values for model explainability
  • Converting between JSON and OWL ontology formats
  • Visualizing ontology structures as interactive graphs
  • Querying the ontology using natural language with GPT-powered retrieval

Features

Ontology Management

  • Create and manipulate ontological structures with nodes, relationships, and classes
  • Deserialize from and serialize to JSON
  • Convert between JSON and OWL formats
  • Generate graphical representations (static and interactive)

Model Integration

  • Convert sklearn models into ontology nodes
  • Store model performance metrics (accuracy, precision, recall, F1, ROC AUC)
  • Capture model weights and parameters
  • Link models to their training datasets and tasks

Dataset Handling

  • Store dataset metadata including statistics for numerical attributes
  • Link datasets to their attributes and models

Explainability

  • Calculate and store SHAP values for model explainability
  • Link explanations to models and features

Retrieval Augmented Generation

  • Query the ontology using natural language
  • Traverse the knowledge graph to find relevant information
  • Visualize query exploration paths
  • Use GPT to interpret and reason over ontology data

Installation

# Installation instructions to be added

Dependencies

  • networkx
  • matplotlib
  • pyvis
  • rdflib
  • numpy
  • pandas
  • scikit-learn
  • shap
  • openai

Usage Examples

Loading an Ontology

from ldrag.ontology import Ontology

# Load from JSON file
ontology = Ontology()
ontology.deserialize("ontology_base.json")

Adding a Model to the Ontology

from ldrag.ontology_io import sklearn_model_to_ontology
import sklearn

# Train a model
model = sklearn.ensemble.RandomForestClassifier()
model.fit(X_train, y_train)

# Add to ontology
sklearn_model_to_ontology(
    model=model,
    model_id="forest_model_1",
    dataset_id="my_dataset",
    task_id="classification_task",
    X_train=X_train,
    X_test=X_test,
    y_test=y_test,
    output_file="ontology.json"
)

Adding Dataset Metadata

from ldrag.ontology_io import add_dataset_metadata_from_dataframe
import pandas as pd

# Load dataset
df = pd.read_csv("data.csv")

# Add to ontology
add_dataset_metadata_from_dataframe(
    dataset_id="my_dataset",
    df=df,
    domain="finance",
    location="database",
    date="2023-01-01",
    models=["forest_model_1"],
    output_file="ontology.json"
)

Visualizing the Ontology

from ldrag.ontology import Ontology

ontology = Ontology()
ontology.deserialize("ontology.json")

# Create dynamic HTML visualization
ontology.create_dynamic_instance_graph("my_graph")

Querying with Natural Language

from ldrag.ontology import Ontology
from ldrag.retriever import information_retriever

ontology = Ontology()
ontology.deserialize("ontology.json")

# Query the ontology
result = information_retriever(
    ontology=ontology,
    user_query="How many rows does the dataset from September have?"
)

Project Structure

  • ontology.py - Core ontology classes and data structures
  • ontology_io.py - Input/output operations for the ontology including ML model integration
  • retriever.py - Retrieval Augmented Generation functionality
  • gptconnector.py - OpenAI API connector for GPT model integration
  • config.py - Configuration settings

License

[Add license information]

Contributing

[Add contribution guidelines]

Metadata

Release files for ldrag 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ldrag 0.2.0
File Size Uploaded Provenance
ldrag-0.2.0.tar.gz 20.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for ldrag 0.2.0
File Interpreter ABI Platform
ldrag-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 41.4 kB

Release files / ldrag-0.2.0.tar.gz

Download URL ldrag-0.2.0.tar.gz
Size 20.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4d3e1a658e48bc4ea8da68cfc5e1148dfd89cbeb36921ecabb81aca875dd747c
BLAKE2b-256 checksum
How to use checksums
aa2f79d622ed1c620762928b114955492406833779ee0bb7e06805b83e7c075f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.11

Release files / ldrag-0.2.0-py3-none-any.whl

Download URL ldrag-0.2.0-py3-none-any.whl
Size 20.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
75c6313a52c33bd8f0a2c7eee8572857a9af7136fafe957123c517b4dd163c82
BLAKE2b-256 checksum
How to use checksums
9dc519d48f873d741a4271d4e4f3d5829246fb10d7223188d39bce4671fb7ed5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.11

Security for ldrag 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page