nexus is a lightweight knowledge graph + vectorDB embedded library.
VERSION 0.1
git submodule add https://github.com/arca-research/nexus.git nexus
git submodule update --init --recursive
git commit -m "Add nexus submodule"- download requirements.txt
- create .env and set OPENAI_API_KEY
- tune config.py to your liking
Below is a sample script main.py (note that here, main.py and nexus are at the same level)
from nexus import GraphBuilder, GraphQueryEngine, GraphIndex, GraphConfig
from dataclasses import dataclass
from pathlib import Path
@dataclass
class DocData:
"""Container for document information."""
document_id: int
filepath: Path | str
fp = "data/myfile.txt"
builder = GraphBuilder(debug=True)
# A) Building ---
# optional: see what the prompt looks like
with open(fp, "r", encoding="utf-8") as f:
data = f.read()
print(builder._build_extraction_prompt(data))
# create the document container for the file
doc = DocData(document_id=0, filepath=fp)
# build() accepts list[DocLike] (defined in _schemas)
docs = [doc]
# build() will run LLM extraction and build a graph db in .nexus
builder.build(docs)
# B) Using the query engine ---
query_engine = GraphQueryEngine()
entities = query_engine.list_all_entities()
print(entities)
return # remove this when you're ready
ent = query_engine.Entity("<ENTITY>") # replace <ENTITY> with desired
print(ent.claims)
# C) manually upserting aliases
cfg = GraphConfig()
index = GraphIndex(index_path=cfg.graph_index_path)
index.upsert_alias("FBI", "Federal Bureau of Investigation")