Skip to main content

rag_with_oag

Ask questions about your documents, including questions whose answer is spread over several documents. rag_with_oag is a Python library with three modes.

  • RAG finds passages similar to the question.
  • OAG (ontology-augmented generation) builds a knowledge graph from your ontology and walks its relations.
  • Hybrid (default) gives the model both graph facts and source text.

Status: v0.2, pre-alpha. The API may still change.

Why a graph?

Who is the largest shareholder of the largest shareholder of the customer in contract DS-2023-008? The answer is three hops away across the contract and two shareholder records. Plain RAG can pick a similar but incomplete passage.

Diagram: view it on GitHub.

Results

Share of questions answered correctly, hybrid versus RAG (vector plus BM25) at the same token budget. Synthetic Turkish contracts, 160 held-out questions: hybrid 94%, RAG 39%; single fact 100% vs 100%, two hops 98% vs 18%, three hops 78% vs 0%, free-text clause 100% vs 40%. Real Wikipedia, 2WikiMultiHopQA, 500 questions: hybrid 74%, RAG 50%; two hops 58% vs 34%, family relations 71% vs 65%, comparison 99% vs 99%, bridge comparison 67% vs 2%.

On held-out synthetic Turkish contracts, hybrid answered 94% correctly and RAG answered 39%. On 500 real Wikipedia questions from 2WikiMultiHopQA, the scores were 74% and 50%. Both modes used the same token budget, with deepseek-v4.1-flash and bge-m3.

Tables, method and caveats.

How it works

Diagram: view it on GitHub.

Mode Gives the model
rag Source passages
oag Graph facts
hybrid Graph facts and source passages

Details of ingest, retrieval and deletion.

Install

pip install rag_with_oag

Install the development version with pip install "rag_with_oag @ git+https://github.com/Sami-Ozsoy/rag_with_oag".

Python 3.12+. Add [pdf] for PDF loading or [local] for CPU embeddings without a server.

The embedded database runs on Linux and macOS (macOS needs brew install libomp). On Windows, run FalkorDB in Docker (docker run -p 6379:6379 falkordb/falkordb) and pass url="redis://localhost:6379".

Quick start

Start Ollama, then clone the repository and run the example.

git clone https://github.com/Sami-Ozsoy/rag_with_oag && cd rag_with_oag
ollama pull qwen2.5:7b-instruct
ollama pull bge-m3
python examples/quickstart/run.py

Run the Python snippet from the clone, too.

from rag_with_oag import EmbedConfig, KnowledgeBase, LLMConfig, Ontology

kb = KnowledgeBase(
    "contracts",
    path="./rag_with_oag.db",
    llm=LLMConfig(base_url="http://localhost:11434/v1", model="qwen2.5:7b-instruct"),
    embedder=EmbedConfig(base_url="http://localhost:11434/v1", model="bge-m3"),
    ontology=Ontology.from_yaml("examples/quickstart/ontology.yaml"),
)

kb.ingest(["examples/quickstart/docs"])
res = kb.query("Where is the company that owns the provider in HS-2024-01 based?")
print(res.answer)
kb.close()
  • res.answer holds the answer with citations such as [F1] and [C1].
  • res.facts holds typed graph facts and res.chunks holds source passages.
  • res.mode_used records the mode, including a fallback from hybrid to rag.
Using OpenAI instead

Set OPENAI_API_KEY before running this version.

from rag_with_oag import EmbedConfig, KnowledgeBase, LLMConfig, Ontology

kb = KnowledgeBase(
    "contracts",
    path="./rag_with_oag_openai.db",
    llm=LLMConfig(model="gpt-4o-mini"),
    embedder=EmbedConfig(model="text-embedding-3-small"),
    ontology=Ontology.from_yaml("examples/quickstart/ontology.yaml"),
)

kb.ingest(["examples/quickstart/docs"])
res = kb.query("Where is the company that owns the provider in HS-2024-01 based?")
print(res.answer)
kb.close()

AsyncKnowledgeBase has the same API with await. See the CLI commands.

Is it a good fit?

Good fit Probably not
Questions that join facts across documents Single-passage lookup over free prose (plain rag mode is enough)
Clear entities and relations, such as contracts, companies, people or products No one can define which entities matter
Someone can review the ontology Very large corpora on a tight LLM budget (every chunk goes through extraction)
Answers that cite facts and passages Windows without Docker

Limitations

  • The API may change between pre-alpha versions.
  • The embedded database has no Windows build. Use a FalkorDB server there.
  • You design the ontology. Reviewing a draft from Ontology.suggest() takes domain knowledge. See Ontology.
  • Every chunk goes to the extraction model, so ingest uses at least as many LLM tokens as the corpus, plus prompts. IngestReport.token_usage reports the actual usage. Results are cached per chunk, so interrupted or repeated ingests can reuse work already done. A small, fast extractor_llm with reasoning_effort="none" keeps it cheap.
  • One entity can end up as two nodes under different names, such as "Howard Hawks" and "Howard Winchester Hawks". A shared value for a key property prevents this.
  • Streaming answers, reembed() and observability hooks are planned. Changing the embedder means rebuilding.
  • Evaluation covers one synthetic domain and one public multi-hop set, with one answer model.

Documentation

Roadmap

v0.3 adds streaming, reembed() and observability. Ontology suggestions, re-ingest and deletion already work.

MIT licensed. See CONTRIBUTING.md to get involved.

Metadata

Release files for rag-with-oag 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 rag-with-oag 0.2.0
File Size Uploaded Provenance Actions
rag_with_oag-0.2.0.tar.gz 1.6 MB With provenance Details for rag_with_oag-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rag-with-oag 0.2.0
File Interpreter ABI Platform Actions
rag_with_oag-0.2.0-py3-none-any.whl Python 3 none any Details for rag_with_oag-0.2.0-py3-none-any.whl

Total release size: 1.7 MB

Release files / rag_with_oag-0.2.0.tar.gz

Download URL rag_with_oag-0.2.0.tar.gz
Size 1.6 MB
Tags Source
SHA-256 checksum
How to use checksums
ff1a00468f280e7677e671728fdfb6cc93abcc8c07fd58c4d323ae9f54264be4
BLAKE2b-256 checksum
How to use checksums
970089223c6eef266f0c841ba6e6cf1af81bebecd77aa729e238a466cc825b56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.

Transparency log

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

Download URL rag_with_oag-0.2.0-py3-none-any.whl
Size 89.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b293815ca723a5be558dcebdf498fa137cb96b468dad732185cd22eebff98b84
BLAKE2b-256 checksum
How to use checksums
d062d9ba7129763ef866501b16333023898be48a217f58c2524a60eececbb7fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.

Transparency log

Security for rag-with-oag 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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for rag_with_oag-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.

Transparency log

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 Full 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