You ask your AI: "What's our company's revenue for Q3 2026?"
You get a confident, detailed answer. Total fabrication.
This is hallucination. The model makes up answers when it doesn't have information.
RAG solves this by giving your AI access to real data before answering.
RAG = Retrieval-Augmented Generation
Traditional AI: Question β Model β Answer (no context)
RAG: Question β Search knowledge base β Retrieve relevant documents β Model reads documents β Answer
It's like giving your AI access to reference materials before an exam.
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.document_s import PDF
= PDF("company_docs.pdf")
docs = .load()
embeddings = OpenAIEmbeddings()
vector_store = FAISS.from_documents(docs, embeddings)
retriever = vector_store.as_retriever()
python
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI
qa = RetrievalQA.from_chain_type(
llm=OpenAI(),
chain_type="stuff",
retriever=retriever
)
result = qa.run("What's our Q3 revenue?")
print(result) # Now grounded in real data!
python
from langchain.text_splitter import RecursiveCharacterTextSplitter
splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
separators=["\n\n", "\n", " ", ""]
)
chunks = splitter.split_documents(docs)
vector_store = FAISS.from_documents(chunks, embeddings)
retriever = vector_store.as_retriever(
search_type="similarity_score_threshold",
search_kwargs={"score_threshold": 0.7}
)
Customer Support: AI references your knowledge base while answering
Legal Discovery: Search contract database, cite sources
Medical: AI consults latest research papers
Finance: Real-time market data access
HR: Company policy retrieval
results = vector_store.similarity_search(query, k=10)
keyword_results = bm25_search(query)
combined = merge_results(results, keyword_results)
original_query = "stuff about money"
rewritten = llm.predict(
f"Rewrite this for a database search: {original_query}"
)
retrieved = retriever.get_relevant_documents(query) # Get 100
ranked = rerank(retrieved, query, top_k=5) # Keep 5 best
context = "\n".join([d.page_content for d in ranked])
Mistake 1: Poor chunking β Broken context
Solution: Experiment with chunk size
Mistake 2: Outdated documents β Stale answers
Solution: Implement refresh schedule
Mistake 3: No deduplication β Waste tokens
Solution: Remove duplicate documents
Mistake 4: Bad embeddings β Poor retrieval
Solution: Use domain-specific embedding models
hallucinations = 0
for question, expected_answer in test_cases:
response = qa.run(question)
if not verify_against_docs(response):
hallucinations += 1
hallucination_rate = hallucinations / len(test_cases)
print(f"Hallucination rate: {hallucination_rate*100}%")
In 2026, RAG is becoming standard for:
Companies not using RAG will face:
Take your most important company document and:
You'll never trust hallucinating AI again.
Are you using RAG? What's your document source?