vantadb 0.1.4

VantaDB: An embedded persistent memory and vector retrieval engine for local-first AI applications.
Documentation
"""
Experimental / not part of the v0.1.x MVP.

This legacy RAG sketch is retained as an integration idea. The production-facing
Python path is `import vantadb_py as vantadb` with namespace-scoped memory APIs;
see docs/QUICKSTART.md for the supported first-run flow.
"""

import time
import uuid

# In a real environment, you would use standard LangChain modules:
# from langchain.llms import Ollama
# from langchain.embeddings import SentenceTransformerEmbeddings
print("[Setup] Loading VantaDB & Emulated LangChain Modules...")
import vantadb_py as vantadb

# ---------------------------------------------------------
# 1. INITIALIZE VantaDB (In-Process, Zero-Network)
# ---------------------------------------------------------
# Like SQLite, it lives in your python heap.
# We set a strict 128MB limit for this script to demo resource governance.
db = vantadb.VantaDB("./local_vanta_data", memory_limit_bytes=128_000_000, read_only=False)

# ---------------------------------------------------------
# 2. DOCUMENT INGESTION (Ollama Embeddings Pipeline)
# ---------------------------------------------------------
raw_documents = [
    "VantaDB is a deeply embedded database written in Rust.",
    "Using multiple databases (Vector, Graph, Relational) creates a glue-code nightmare.",
    "By compiling the database via PyO3, Python apps can query vectors with Zero-Copy overhead.",
    "Resource governance automatically shifts HNSW heaps to Disk (MMAP) when RAM is low.",
]


def hook_embed_documents(texts):
    """
    Mocking a local embedding model (e.g. all-MiniLM-L6-v2 or Llama3 via Ollama).
    In reality, this returns a float32 array per text.
    """
    return [[0.1, 0.4, 0.6] for _ in texts]  # Dummy vectors for brevity


print("[RAG] Ingesting documents...")
start_time = time.perf_counter()

embeddings = hook_embed_documents(raw_documents)
for i, text in enumerate(raw_documents):
    db.insert(
        i + 1,
        text,
        embeddings[i],
        fields={
            "doc_id": str(uuid.uuid4()),
            "content": text,
            "category": "architecture",
            "processed": True,
        },
    )

print(
    f"[RAG] Ingestion completed in {(time.perf_counter() - start_time) * 1000:.2f} ms"
)

# ---------------------------------------------------------
# 3. SEMANTIC RETRIEVAL (Zero-Latency Hybrid Query)
# ---------------------------------------------------------
user_query = "How does it avoid the glue-code problem in Python?"
query_embedding = hook_embed_documents([user_query])[0]

print("\n[RAG] Searching for context...")
search_start = time.perf_counter()

# This call maps directly to Rust C-ABI. No HTTP parsing, no TCP overhead.
results = db.search(query_embedding, top_k=2)
search_end = time.perf_counter()

print(f"[RAG] Semantic search took {(search_end - search_start) * 1000:.3f} ms.")

# ---------------------------------------------------------
# 4. LLM GENERATION (Augmentation)
# ---------------------------------------------------------
# We pass the zero-latency results directly to the LLM prompt.
context_chunks = []
for (node_id, _distance) in results:
    node = db.get(node_id) or {}
    fields = node.get("fields") or {}
    content = fields.get("content") or ""
    if content:
        context_chunks.append(str(content))
context = "\n".join(context_chunks)
prompt = f"Answer the user's question using the context.\n\nContext:\n{context}\n\nQuestion: {user_query}"

print(f"\n[Generated Prompt Output to LLM]\n{prompt}\n")
print("✅ Local RAG pipeline executed successfully without leaving the Python process.")