DBGraph - Building schema graph with LLM Assistance
DBGraph aims to help data scientist with exploring and finding relevant data assets in a huge and complex database.
Quick Start
Here is a quick example of how to build and query a schema graph with DBGraph:
# imports
from dbgraph.builder.sqlite.sqlite_graph_builder import SQLiteGraphBuilder
from dbgraph.io.json_graph_writer import JSONGraphWriter
from dbgraph.io.json_graph_loader import JSONGraphLoader
from dbgraph.search.bm25_search_engine import BM25SearchEngine
# initiate GraphBuilder on an SQLite database
graph_builder = SQLiteGraphBuilder(Path("data/northwind.db"))
# build the schema graph (include profiling databases)
graph = graph_builder.build_graph()
# generate descriptions for assets
graph_descriptor = GraphDescriptorV1(
llm=OAICompatibleLLM(
model=...,
base_url=...,
api_key=...,
),
system_prompt=...,
formating_prompt=...,
target_prompt=...
)
graph = graph_descriptor.rfill_semantic_aspects(graph)
# save the graph
graph_writer = JSONGraphWriter(
json_path=Path("data/northwind-graph.json"), indent=2
)
graph_writer.write()
# load the graph
graph_loader = JSONGraphLoader(json_path=Path("data/northwind-graph.json"))
graph = self.graph_loader.load()
# index the graph using BM25
search_engine = BM25SearchEngine(Path("data/northwind-index"))
semantic_aspects = {
a.asset_id: cast(SemanticAspect, a.aspects["semantic_properties"])
for a in graph.assets
}
search_engine.index(semantic_aspects)
# retrieve assets using BM25
assets_ids = search_engine.search(
"Give me the total count of orders in each categories"
)
For visualization purpose, this is the graph saved in JSON:
{
"assets": [
{
"asset_id": "8ab5a624-0596-497e-a0ee-3996d95dbe63",
"name": "Categories",
"type": "table",
"aspects": {
"schema_properties": {
"name": "Categories_table_schema",
"pks": ["CategoryID"],
"indices": {}
},
"statistical_properties": {
"name": "Categories_table_stats",
"num_columns": 4,
"num_rows": 8
},
"semantic_properties": {
"name": "Categories_semantic",
"description": "Stores product category definitions and metadata, serving as a lookup table for classifying products in the inventory system.",
"keywords": [
"categories",
"product classification",
"category definitions",
"inventory groups",
"product types"
]
}
}
},
...
],
"links": [
{
"link_id": "db6bea93-a02c-4426-a2db-449e4a7bba8f",
"name": "Categories_CategoryID",
"type": "contain",
"source_id": "8ab5a624-0596-497e-a0ee-3996d95dbe63",
"destination_id": "04c20046-2808-4021-bbf1-99876e0eea6e",
"aspects": {}
},
...
]
}
Usecases
There are 4 main groups of usecases where DBGraph is applicable:
- Manipulating database schema: Build the schema graph, store it, and use it to traverse around the database, find JOIN path, get references tables, ...
- Profiling database: Use the concept of
Aspectto represent different types of properties attached to a single data asset. Each data asset can have a statistics aspects, semantical aspects, ... - Render graph: Output schema graph to Markdown or text as context for LLM
- LLM Assistance: Leverage LLM to generate data assets' descriptions and tags. Furthermore, LLM could also be used in the process of SQL generation.
- Search for data assets: Search for wanted data assets based on their descriptions. The descriptions are indexed and retrieved with BM25 algorithms.
Architecture
To encourage open-ness and extension, DBGraph is designed in a way that is very easy to extend.
-
The core classes (entities) define the shared business logic of database graphs (traversal, neighborhoods, ...) and core operations within the application (building graphs, profiling databases, ...). The prefix "R..." stands for "Relational", as the class is dedicated Relational databases only. The same stands for "D.." (Document), "V..." (Vector), "G..." (Graph). However, at this point, the only supported paradigm is Relational database.
-
The interfaces (extensions) define a part of the system that should be pluggable. For example:
RProfilerandRGraphBuildershould works on multiple types of RDBMS, not just SQLite. Hence the abstraction ofRDataGateway.- We want to support multiple LLM providers. Hence the abstraction of
LLM. - There are many ways to render a graph into Markdown or text. Hence the abstraction of
RGraphRenderer. - To store and load the graph's data from some storages, we have multiple options. Hence the abstraction of
GraphGateway. - To index and retrieve graph's data from some search engines, we also have multiple options. Hence the abstraction of
SearchEngine
Metadata
Release files for dbgraph 0.2.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dbgraph-0.2.9.tar.gz | 13.7 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dbgraph-0.2.9-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.8 MB
Release files / dbgraph-0.2.9.tar.gz
| Download URL | dbgraph-0.2.9.tar.gz |
|---|---|
| Size | 13.7 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a9762a455ebf9acf6f7502cc0f40197eaf5d7a49039f42d03fbd6e18cc83d371
|
|
BLAKE2b-256 checksum How to use checksums |
66507dfb3bf3223119603780d69d876437fbbad140db4e42461937a496a7bef3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / dbgraph-0.2.9-py3-none-any.whl
| Download URL | dbgraph-0.2.9-py3-none-any.whl |
|---|---|
| Size | 31.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f6e504e230bd284a7070bfd88974bd8cd25f9bb994e66afc4fed88d3fcfaa133
|
|
BLAKE2b-256 checksum How to use checksums |
25204c0086feb943691078c484d51c86301aa9e35d1fecc2b821222494161fa3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|