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
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.
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.answerholds the answer with citations such as[F1]and[C1].res.factsholds typed graph facts andres.chunksholds source passages.res.mode_usedrecords the mode, including a fallback fromhybridtorag.
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 suggestion and preview. - Every chunk goes to the extraction model, so ingest uses at least as many LLM
tokens as the corpus, plus prompts.
IngestReport.token_usagereports the actual usage. Results are cached per chunk, so interrupted or repeated ingests can reuse work already done. A small, fastextractor_llmwithreasoning_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
keyproperty 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
- Ontology
- Ontology Studio opens with
rag-with-oag ui. - Configuration and CLI
- How it works
- Evaluation
- Quick start example
- Changelog
- Contributing
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.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| rag_with_oag-0.3.0.tar.gz | 1.8 MB | With provenance | Details for rag_with_oag-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| rag_with_oag-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for rag_with_oag-0.3.0-py3-none-any.whl |
Total release size: 2.0 MB
Release files / rag_with_oag-0.3.0.tar.gz
| Download URL | rag_with_oag-0.3.0.tar.gz |
|---|---|
| Size | 1.8 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
77f1a2da1dbb43859ae9ff428eef48155a24e90156db130dc8cc444ff599c4cb
|
|
BLAKE2b-256 checksum How to use checksums |
c730e497815b609850327286108c44e43dd0791e6f5cfc413478a35cb8b7fd55
|
| 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 28, 2026.
Transparency logRelease files / rag_with_oag-0.3.0-py3-none-any.whl
| Download URL | rag_with_oag-0.3.0-py3-none-any.whl |
|---|---|
| Size | 265.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7dd8c1c74f457dc3a7b1190b84b1af7a66c25cc69d18edc977cb3f4cfc57542e
|
|
BLAKE2b-256 checksum How to use checksums |
7c62fcf9e1e77eb572febd0b73280da697f261ce98326dd5b40c3601447b7d25
|
| 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 28, 2026.
Transparency logSecurity for rag-with-oag 0.3.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.
Attestation for rag_with_oag-0.3.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 28, 2026.
Transparency logA 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.