🔍 Grounding LLMs in External Knowledge: The Anatomy of Modern RAG Systems
Pre-trained large language models suffer from static knowledge cutoffs and lack access to proprietary enterprise data, leading to costly hallucinations when answering domain-specific queries. Retrieval-Augmented Generation (RAG) solves this by decoupling internal parametric memory from external non-parametric document stores. When a user submits a query, a high-performance vector pipeline retrieves the top-$k$ most semantically relevant document chunks, injects them into the prompt context window, and prompts the LLM to generate grounded answers with precise inline citations.
import os
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Pinecone
from pinecone import Pinecone as PineconeClient
# 1. Initialize Vector Database Client
pc = PineconeClient(api_key=os.environ.get("PINECONE_API_KEY"))
index_name = "enterprise-kb-prod"
# 2. Configure Semantic Chunking
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=512,
chunk_overlap=64,
separators=["\n\n", "\n", " ", ""]
)
# 3. Load & Split Source Documents
raw_document_text = "..." # Unstructured corporate policy handbook
chunks = text_splitter.create_documents([raw_document_text])
# 4. Generate Embeddings & Upsert to Vector Store
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
vector_store = Pinecone.from_documents(chunks, embeddings, index_name=index_name)
print(f"[✔] Successfully embedded and indexed {len(chunks)} semantic chunks.")
def reciprocal_rank_fusion(vector_results, bm25_results, k=60):
"""
RRF algorithm merges rankings from dense vector search and sparse BM25 search.
Score = sum( 1 / (k + rank_i) ) for each retriever.
"""
fused_scores = {}
for rank, doc in enumerate(vector_results):
doc_id = doc.metadata["id"]
fused_scores[doc_id] = fused_scores.get(doc_id, 0) + 1 / (k + rank + 1)
for rank, doc in enumerate(bm25_results):
doc_id = doc.metadata["id"]
fused_scores[doc_id] = fused_scores.get(doc_id, 0) + 1 / (k + rank + 1)
sorted_docs = sorted(fused_scores.items(), key=lambda x: x[1], reverse=True)
return sorted_docs
from sentence_transformers import CrossEncoder
# Load high-precision cross-encoder model
reranker = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")
query = "What are the contraindications of prescribing beta-blockers for asthma patients?"
initial_retrieved_passages = [
"Beta-blockers can cause bronchospasm in patients with reactive airway disease.",
"Aspirin is commonly used for cardiovascular inflammation prevention.",
"Asthma management requires inhaled corticosteroids and bronchodilators."
]
# Format pairs for cross-encoder scoring
pairs = [[query, passage] for passage in initial_retrieved_passages]
scores = reranker.predict(pairs)
# Sort by reranker score descending
ranked_results = sorted(zip(initial_retrieved_passages, scores), key=lambda x: x[1], reverse=True)
for passage, score in ranked_results:
print(f"Score: {score:.4f} | Passage: {passage}")
# Complex Query: "Compare Q3 2025 cloud infrastructure spending of AWS vs Azure and calculate the percentage variance."
# Sub-Question 1: "What was AWS cloud infrastructure spending in Q3 2025?" -> Vector Search 1
# Sub-Question 2: "What was Azure cloud infrastructure spending in Q3 2025?" -> Vector Search 2
# Synthesis: Pass both retrieved contexts into LLM to compute variance: ((AWS - Azure) / Azure) * 100.
# Chat History:
# User: "I bought a MacBook Pro yesterday."
# Assistant: "That's great! How can I help you with it?"
# User: "How do I return it for a refund?"
# Standalone Query Rewritten by LLM:
# "What is the return policy and refund procedure for a MacBook Pro purchased yesterday?"
# -> Executed against vector store instead of raw pronoun-heavy user input.
# Metadata-filtered retrieval query:
results = vector_store.similarity_search(
query="Database connection pool exhausted",
k=3,
filter={"service": "payment-gateway", "environment": "production"}
)
# Ensuring Tenant A cannot retrieve documents belonging to Tenant B
tenant_id = "tenant_acme_corp_882"
secure_results = vector_store.similarity_search(
query="Q4 revenue projections",
k=5,
filter={"tenant_id": tenant_id}
)
# Prompt template forcing explicit source attribution:
prompt = """
Answer the query based ONLY on the provided context. Include source document names and page numbers in citations.
Context: {retrieved_chunks}
Query: {user_query}
"""
# CLIP projects images and text into a shared 512-dimensional vector space.
# User uploads image of red running shoes -> Vector DB retrieves matching product descriptions & SKUs.
from ragas import evaluate
from ragas.metrics import faithfulness, answer_relevance, context_precision
# Evaluates test dataset of queries, retrieved contexts, and generated LLM answers
evaluation_results = evaluate(
dataset=eval_test_dataset,
metrics=[faithfulness, answer_relevance, context_precision]
)
print(evaluation_results)