Hybrid pipeline: LLM extraction → optional Redis entity linking → predicate handling (FAISS over the full DBpedia ontology in canonical mode, or ontology-constrained IDs in eval mode) → LLM disambiguation over candidate sets.
pipenv install
pipenv shell # or: pipenv run python …Optional: copy .env and set variables below. run.py loads .env via python-dotenv; you can export the same vars for nef.py.
These paths are gitignored; clone the OWL once and either generate embeddings locally or copy in a matching embeddings.npy + predicates.csv pair from a trusted build.
| Path | Role |
|---|---|
dbpedia-20250806.owl.rdf |
DBpedia ontology (labels/comments for predicates, domain/range for joint disambiguation). Place in the repo root (same directory as Emeddings.py / default --owl for nef.py). |
predicates.csv |
One URI per line (header predicate), row-aligned with embeddings.npy. |
embeddings.npy |
float32 matrix (N, 768) from gemini-embedding-001 (not L2-normalized until index build in runtime). |
predicate_labels.csv |
Optional debug export: URI + exact embedded text (from Emeddings.py). |
Obtain the OWL: use a current DBpedia ontology release (filename may differ — rename or symlink to dbpedia-20250806.owl.rdf, or pass --owl / set paths in nef.py to your file). Official material is linked from DBpedia Ontology.
Regenerate predicate index files (requires network + GEMINI_API_KEY; uses REST batch embed like the paper’s index recipe):
# OWL must exist at ./dbpedia-20250806.owl.rdf (or edit path inside Emeddings.py)
export GEMINI_API_KEY='…'
pipenv run python Emeddings.pyWrites predicates.csv, predicate_labels.csv, and embeddings.npy in the repo root. Then canonical nef.py runs with defaults.
If you previously committed these files and want Git to stop tracking them: git rm --cached embeddings.npy predicates.csv predicate_labels.csv dbpedia-20250806.owl.rdf (keep local copies), then commit .gitignore.
| Variable | Purpose |
|---|---|
OPENAI_MODEL |
Chat model, e.g. local/gpt-4o-mini or openai/gpt-4o-mini (see llm.py). |
OPENAI_BASE_URL / OPENAI_API_KEY |
Required when model prefix is local/…. |
OPENROUTER_API_KEY |
Required for non-local/ models (OpenRouter). |
REDIS_HOST, REDIS_PORT |
Required with --ground. |
REDIS_PASSWORD |
Optional. |
NEF_GROUND_CANDIDATES |
Max Redis candidates per surface (default 5). |
NEF_REDIRECT_CAP |
Redirect fan-out cap (default 20). |
Canonical mode only (query embeddings for predicate retrieval):
| Variable | Purpose |
|---|---|
GEMINI_API_KEY |
Or pass --gemini-api-key to nef.py. Used by google-genai for gemini-embedding-001. |
Benchmark data (run.py, score.py):
| Variable | Purpose |
|---|---|
TEXT2KGBENCH_PATH |
Root of the cloned Text2KGBench repo (same as run.py --data). |
- Input: natural language; predicates are phrases (not ontology IDs).
- Predicates: FAISS cosine search over
embeddings.npy+predicates.csv(see Canonical artifacts above), with metadata from the OWL file (--owl, defaultdbpedia-20250806.owl.rdf). - Disambiguation: joint choice of predicate URI + subject/object URIs from Redis candidates.
pipenv run python nef.py \
-s "Apple was founded by Steve Jobs." \
--ground --completenessUseful flags: -v / --verbose, --trace-file path.log (with verbose, writes stderr trace to that file), --predicate-threshold, --top-k-predicates, paths --embeddings, --predicates, --owl.
Stdout JSON (single object):
{
"input": "…",
"triples": [ { "subject": "http://…", "predicate": "http://…", "object": "http://…", "meta": { … } } ],
"retrieval_report": [ { "input_predicate_phrase": "…", "subject_text": "…", "object_text": "…" } ]
}Without --ground, entities are not linked; output is still JSON but surfaces may be ungrounded depending on pipeline exit.
Predicates must be IDs from that ontology; disambiguation is entity-only over Redis (same shared extract() path as the benchmark).
pipenv run python nef.py --eval \
--ontology-json /path/to/Text2KGBench/.../ontologies/ont_1_university.json \
-s "Your test sentence." \
--ground --completenessStdout JSON:
{ "input": "…", "triples": [ { "sub": "…", "rel": "…", "obj": "…" } ] }(triples shape matches Text2KGBench / run.py predictions.)
run.py always requires --eval. It walks Text2KGBench test JSONL files, calls extract() with retrieved few-shot examples, and writes JSONL under --out (default predictions/).
export TEXT2KGBENCH_PATH=/path/to/Text2KGBench
pipenv run python run.py --eval --ground
# one domain, dry run size:
pipenv run python run.py --eval --domain 1_university --limit 10 --groundSee pipenv run python run.py --help for --shots, --parallel, --retrieval, --mmr-lambda, etc.
Scoring (score.py): expects prediction JSONL per domain next to ground truth under TEXT2KGBENCH_PATH (or override in score.py).
export TEXT2KGBENCH_PATH=/path/to/Text2KGBench
pipenv run python score.py predictions_fullpipenv run python nef.py --help
pipenv run python run.py --help| File | Role |
|---|---|
nef.py |
CLI: canonical vs --eval; shared extract() for eval. |
canonical.py |
Canonical-only orchestration (FAISS + joint disambiguation). |
predicate_index.py |
Embeddings index + Gemini query embeddings. |
redis_index.py |
Redis surface → URI candidates. |
Emeddings.py |
Batch-embeds ontology predicates via Gemini REST; writes predicates.csv, predicate_labels.csv, embeddings.npy. (Typo in filename is historical.) |