Skip to content

Examples

Real-world recipes live in two places:

  • docs/examples.md — RAG pipelines, agent memory, every index type, transactions, batch insert, embedding cache.
  • examples/ — runnable Python scripts you can python examples/<name>.py directly.

Highlights

Minimal RAG loop

python
import numpy as np
from pistadb import PistaDB, Metric, Index

with PistaDB("rag.pst", dim=1536, metric=Metric.COSINE, index=Index.HNSW) as db:
    # 1. Index your chunks (call your embedding model first)
    for chunk_id, vec, text in your_chunks:
        db.insert(chunk_id, vec, label=text[:255])

    # 2. Retrieve at query time
    q_vec = embed(user_question)
    hits = db.search(q_vec, k=5)

    # 3. Stuff into the LLM prompt
    context = "\n---\n".join(h.label for h in hits)
    answer = llm(f"Context:\n{context}\n\nQuestion: {user_question}")

Transactions (ACID-style)

python
with db.transaction() as tx:
    tx.insert(1, vec1)
    tx.delete(99)
    tx.update(2, vec2)
    # Anything raising in this block triggers full rollback.

Batch insert

python
# Multi-threaded ring-buffer ingest — saturates SSD throughput
db.batch_insert(ids, vectors, labels=labels, num_threads=8)

Embedding cache

python
from pistadb import EmbeddingCache

cache = EmbeddingCache("openai_embed.pcc", dim=1536, capacity=1_000_000)
vec = cache.get_or_compute(text, lambda t: openai_embed(t))

A persistent LRU cache that eliminates redundant model calls across runs.

More

For full code — HNSW tuning, IVF training, SQ memory savings, ScaNN two-phase search, agent memory, the Milvus-compatible schema API — head to docs/examples.md in the repository.

Released under the MIT License.