RAG for Enterprise

A RAG demo answers from a few files in memory. A production one keeps its indexes in a database, re-indexes what changed, searches by meaning and by keyword, shows each user only what they may read, and is measured rather than eyeballed. llm4s-rag ships each of these as configuration of one RAG pipeline.

  1. A production pipeline
  2. Ingesting large document sets
  3. Retrieval quality
  4. Who may see what
  5. Measuring it
  6. See also

A production pipeline

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import org.llm4s.chunking.ChunkerFactory
import org.llm4s.config.Llm4sConfig
import org.llm4s.llmconnect.LLMClient
import org.llm4s.model.ModelRegistryService
import org.llm4s.rag.RAG
import org.llm4s.rag.RAG.RAGConfigOps
import org.llm4s.security.Secret
import org.llm4s.trace.Tracing
import org.llm4s.types.Result

def companyDocs(client: LLMClient, dbPassword: Secret, tracing: Tracing): Result[RAG] = for {
  registryService <- Llm4sConfig.modelRegistryService()
  given ModelRegistryService = registryService
  embeddingConfig <- Llm4sConfig.embeddings()               // EMBEDDING_MODEL, e.g. openai/text-embedding-3-small
  rag <- RAG
    .builder()
    .withEmbeddings(embeddingConfig.providerId.asString)
    .withPgHybrid("jdbc:postgresql://db:5432/rag", "rag", dbPassword) // vectors and keywords both in PostgreSQL
    .withChunking(ChunkerFactory.Strategy.Markdown, 800, 150)          // target size and overlap, in characters
    .withRRF(60)                                                       // fuse vector and keyword results
    .withTopK(8)
    .withBatchSize(32)                                                 // documents per embedding batch
    .withParallelism(4)                                                // documents processed concurrently
    .withLLM(client)
    .withTracing(tracing)
    .build(_ => Right(embeddingConfig))
} yield rag

withPgHybrid keeps both the vector index (pgvector) and the keyword index (PostgreSQL full-text search) in the database; withPgVector alone keeps the keyword index in memory, and the default keeps everything in memory. The RAG in Scala page builds the minimal pipeline and explains queryWithAnswer, streaming answers, chat history and question condensing.

Ingesting large document sets

Need Component
Files, directories, URLs, a crawled site, S3 DirectoryLoader, UrlLoader, WebCrawlerLoader, S3Loader, … (Document Loaders)
Re-index only what changed, remove what was deleted rag.sync(loader); rag.refresh(loader) re-ingests everything
Remember what was indexed across restarts RAGConfig.withDocumentRegistry
Ingest without blocking ingestAsync, syncAsync, refreshAsync
Chunk by structure, by sentence or by meaning ChunkerFactory.Strategy.Markdown, Sentence, Semantic or Simple
Never pay twice to embed the same text CachedEmbeddingClient (Caching)

Loaders that fetch URLs refuse private and internal addresses: see SSRF protection.

Retrieval quality

  • Hybrid search is the default: vector similarity and BM25 keyword search, fused with reciprocal rank fusion (withRRF) or a weighted score (withWeightedScore); vectorOnly and keywordOnly turn one channel off. See hybrid search.
  • Reranking re-orders the fused candidates: withCohereReranking() (a cross-encoder) or withLLMReranking, with withRerankTopK candidates. See reranking.
  • Query transformers rewrite a question before retrieval: Query transformers.
  • Other stores - Qdrant, Elasticsearch, OpenSearch, Redis and Valkey - are in the vector store guide.

Who may see what

Permission-based RAG adds collections with queryableBy permissions and documents with readableBy permissions, on PostgreSQL, and filters retrieval by the asking user before the model sees anything. See Permission-based RAG. For erasure requests, see deleting a user’s data.

Measuring it

Faithfulness, answer relevancy, context precision and context recall (RAGAS), and a benchmark harness that compares chunking strategies, fusion strategies and embedding providers on your dataset, are in RAG evaluation. Change one setting at a time and compare the scores. In production, withTracing traces each model call the pipeline makes - answering, reranking, rewriting and condensing - with its token usage tagged by operation (Production Monitoring).

See also