🎉 ai-infra v1.0 is here — Production-ready AI/LLM infrastructure
What's new
nfrax logonfrax

Infrastructure that just works. Ship products, not boilerplate.

Frameworks

  • svc-infra
  • ai-infra
  • fin-infra
  • robo-infra

Resources

  • Getting Started
  • What's New
  • Contributing

Community

  • GitHub

© 2026 nfrax. All rights reserved.

nfrax logonfrax
Start HereWhat's New
GitHub
ai-infra / API Reference

Retriever

from ai_infra.retriever import Retriever
View source
ai_infra.retriever

Semantic search made simple. The Retriever automatically handles: - Embedding generation (via any provider) - Text chunking for long documents - File loading (PDF, DOCX, TXT, CSV, JSON, HTML) - Directory scanning - Vector storage and search Progressive complexity: - Zero-config: `Retriever()` uses memory storage and auto-detected embeddings - Simple: `Retriever(backend="postgres", connection_string="...")` - Advanced: Pass your own `Embeddings` instance for full control Example - Dead simple: >>> r = Retriever() >>> r.add("Your text here") >>> r.add("./documents/") # Add all files from a directory >>> results = r.search("query") # Returns list of strings Example - Production with PostgreSQL: >>> r = Retriever( ... backend="postgres", ... connection_string="postgresql://user:pass@localhost/db", ... ) >>> r.add("./knowledge_base/") >>> results = r.search("query", detailed=True) # Returns SearchResult objects Example - LLM context generation: >>> r = Retriever() >>> r.add("./docs/") >>> context = r.get_context("user question", k=5) >>> prompt = f"Context:\n{context}\n\nQuestion: {question}"

Constructor
Retriever(provider: str | None = None, model: str | None = None, embeddings: Embeddings | None = None, backend: str | None = None, similarity: str = 'cosine', chunk_size: int = 500, chunk_overlap: int = 50, persist_path: str | Path | None = None, auto_save: bool = True, lazy_init: bool = False, auto_configure: bool = True, backend_config: Any = {}) -> None
ParameterTypeDefaultDescription
providerstr|NoneNoneEmbedding provider (openai, google, voyage, cohere, huggingface). If not specified and auto_configure=True, auto-detects from environment (falls back to free huggingface if no API keys).
modelstr|NoneNoneEmbedding model name. Uses provider default if not specified.
embeddingsEmbeddings |NoneNonePre-configured Embeddings instance. If provided, `provider` and `model` are ignored.
backendstr|NoneNoneStorage backend name. Options:
similaritystr'cosine'Similarity metric for search. Options:
chunk_sizeint500Maximum characters per chunk (default 500).
chunk_overlapint50Overlapping characters between chunks (default 50).
persist_pathstr| Path |NoneNonePath to save/load retriever state. If provided and the file exists, the retriever loads from it. Works with memory backend to add persistence.
auto_saveboolTrueIf True (default) and persist_path is set, automatically saves after each add operation.
lazy_initboolFalseIf True, defer loading the embedding model until first use (add or search). Makes server startup faster.
auto_configureboolTrueIf True (default), auto-detect configuration from environment variables: - DATABASE_URL -> backend="postgres" with auto dimension - OPENAI_API_KEY -> provider="openai" - VOYAGE_API_KEY -> provider="voyage" - COHERE_API_KEY -> provider="cohere" - GOOGLE_API_KEY -> provider="google_genai" - No API keys -> provider="huggingface" (free local)
backend_configAny{}—

Methods

On This Page

Constructoraaddasyncaadd_textasyncaddadd_directoryadd_fileadd_from_githubasyncadd_from_github_syncadd_from_loaderasyncadd_from_loader_syncadd_from_urlasyncadd_from_url_syncadd_textaget_contextasyncasearchasynccleardeleteget_contextloadclassmethodmigrateclassmethodsavesearch