Skip to main content

Topology-Aware Retrieval for RAG — structure-guided vector search with progressive fallback

Project description

tar-rag

Topology-Aware Retrieval for RAG — a vector-store-agnostic Python library that adds structural navigation to any RAG pipeline.

Description

Most RAG pipelines do flat top-K semantic search — every query scans the entire vector space, mixing chunks from unrelated parts of the corpus and diluting the top result. Many teams patch this with an extra LLM call that "routes" the query to a filter; that costs tokens, adds latency, and can hallucinate filters that don't exist.

tar-rag does the routing with math instead of an LLM. It builds a topology map from your corpus's directory layout, scores each branch lexically against the query, and runs ANN search scoped to the hottest branch first — broadening only if confidence isn't high enough.

# Example
query: "How does the OAuth token refresh flow work?"

  ┌────────────────────────────────────────────────────────────┐
  │ auth/oauth        ████████████   0.92   ← start ANN here   │
  │ auth/sessions     █████          0.41                      │
  │ billing/refunds   █              0.08                      │
  │ billing/invoices                 0.02                      │
  └────────────────────────────────────────────────────────────┘

No extra LLM call, no per-query token cost, sub-millisecond on the hot path, no hallucination, fully deterministic. tar-rag does not own embeddings, chunking, the vector store, or the LLM — it decides where retrieval starts and gates weak results before they reach your LLM.

See more about tar-rag on Github

Install

pip install tar-rag

Includes every bundled vector-store adapter (OpenAI, Pinecone, Qdrant, Chroma) and every file extractor (PDF, DOCX, HTML, JSON, CSV, plaintext / source code) — works out of the box.

Use cases

Documentation portals · source code repositories · enterprise knowledge bases · product manuals · SOP trees · API docs · compliance repositories · engineering archives · any filesystem-organized corpus where the directory layout encodes meaning.

Usage example

Index a directory of documents, upload them to an OpenAI Vector Store, and run a query through tar-rag's structural filter + fallback pipeline.

Step 1 — Crawl your corpus

from tar_rag import DirectoryCrawler, build_artifacts

crawler = DirectoryCrawler(
    root="./my-corpus",
    level_names=["service", "module"],  # or None to auto-infer
)
documents = crawler.crawl()
bundle = build_artifacts(documents, level_names=crawler.level_names)
bundle.write("./tar_rag_output/")

print(f"Indexed {len(documents)} document(s).")
print("Tune ./tar_rag_output/confidence_config.json before first query if needed.")

Step 2 — Upload to a vector store (OpenAI shown)

import openai
from tar_rag.manifest import MetadataManifest

client = openai.OpenAI()
manifest = MetadataManifest.load("./tar_rag_output/metadata_manifest.json")

vs = client.vector_stores.create(name=f"my-kb-{manifest.version}")
for doc in manifest:
    with open(doc.relative_path, "rb") as f:
        uploaded = client.files.create(file=f, purpose="assistants")
    client.vector_stores.files.create(
        vector_store_id=vs.id,
        file_id=uploaded.id,
        attributes={k: v for k, v in doc.metadata.items() if v is not None},
    )

print(f"Uploaded {len(manifest)} document(s) to vector store {vs.id}.")

Step 3 — Query through tar-rag

import openai
from tar_rag import TarRag
from tar_rag.adapters import OpenAIVectorStoreAdapter

tar = TarRag.from_artifacts("./tar_rag_output/")
tar.set_adapter(OpenAIVectorStoreAdapter(
    client=openai.OpenAI(),
    vector_store_id="vs_xxx",
    top_k=6,
))

result = tar.search("How does the OAuth token refresh flow work?")

print(f"confidence={result.confidence}  top_score={result.top_score:.2f}")
print(f"reason={result.reason}  attempts_made={result.attempts_made}")

if result.should_answer:
    for chunk in result.results:
        print(chunk.score, chunk.snippet[:200])
else:
    print("Confidence below the gate — forwarding zero chunks to the LLM.")

The same tar.search(...) call works against Pinecone, Qdrant, Chroma, or any custom adapter — only the constructor changes. See the full GitHub README for the system architecture diagram, the how-to guide for tuning / custom adapters / async, and benchmarks/benchmark.md for measured comparisons.


"Data should empower, not overwhelm"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tar_rag-0.2.0.tar.gz (88.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tar_rag-0.2.0-py3-none-any.whl (58.6 kB view details)

Uploaded Python 3

File details

Details for the file tar_rag-0.2.0.tar.gz.

File metadata

  • Download URL: tar_rag-0.2.0.tar.gz
  • Upload date:
  • Size: 88.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tar_rag-0.2.0.tar.gz
Algorithm Hash digest
SHA256 557cca1863115c4d061cbd1a22806a7b460c181a1c9d7911003df1f59aad5689
MD5 35d57823388a2fb9267677a059f52133
BLAKE2b-256 839e7dd98bb21c612eb2570f751bacc7c52998dc67d4a1f25ab6c4d043910e35

See more details on using hashes here.

Provenance

The following attestation bundles were made for tar_rag-0.2.0.tar.gz:

Publisher: publish.yml on vamsi-karnam/tar-rag

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tar_rag-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tar_rag-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 58.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tar_rag-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0928459824a592db0a399bafe640a6c843620b625e5a1e427b9cd2943a7cf21c
MD5 9e21582b26c8550b0558a52207caedea
BLAKE2b-256 ebf17b5656b707eaee1f2c5c1a3a70655458ac0097da2a46f00b175cc549f76d

See more details on using hashes here.

Provenance

The following attestation bundles were made for tar_rag-0.2.0-py3-none-any.whl:

Publisher: publish.yml on vamsi-karnam/tar-rag

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page