SynfraCore
Synfracore
Start Learning
Navigation

Academies

Platform

RoadmapsLabsCertificationsInterviewPYQsAI AssistantCareer
Start Learning Free🗺️ Learning Roadmaps
Blog/AI

Build a RAG System in Python: Step-by-Step with SynfraAI

SynfraCore·March 2025·15 min read

What is RAG and Why You Need It

LLMs do not know your internal documents. RAG (Retrieval-Augmented Generation) retrieves relevant content from YOUR documents at query time and gives it to the LLM as context. The LLM answers based on YOUR data, not just its training.

Install Dependencies

bash
pip install anthropic langchain langchain-community chromadb pypdf sentence-transformers

Complete RAG System

python
import anthropic
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma

client = anthropic.Anthropic()

def load_documents(paths):
    all_docs = []
    for p in paths:
        all_docs.extend(PyPDFLoader(p).load())
    chunks = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50).split_documents(all_docs)
    embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
    return Chroma.from_documents(chunks, embeddings)

def answer(vectorstore, question):
    docs = vectorstore.similarity_search(question, k=4)
    context = "\n\n".join([d.page_content for d in docs])
    r = client.messages.create(
        model="claude-haiku-4-5-20251001",
        max_tokens=800,
        system="Answer ONLY from the provided context. Say so if the answer is not there.",
        messages=[{"role": "user", "content": f"Context:\n{context}\n\nQuestion: {question}"}]
    )
    return r.content[0].text

vs = load_documents(["company_handbook.pdf"])
print(answer(vs, "What is the refund policy?"))

Add Streamlit UI

python
import streamlit as st
st.title("Document Q&A")
uploaded = st.file_uploader("Upload PDFs", type="pdf", accept_multiple_files=True)
if uploaded:
    # save to temp files, load_documents(), show chat interface
    pass
bash
streamlit run app.py

What Makes a Production RAG Better

This is a basic implementation. Production systems add: hybrid search (BM25 + vectors), reranking, query rewriting, and evaluation pipelines. See the RAG section in the AI Academy for advanced patterns.

Found this useful? Share it:

Twitter / X LinkedIn WhatsApp Telegram

Weekly DevOps & Cloud digest

Every Sunday — tutorials, interview questions, tips, and what changed in DevOps and Cloud this week.

Join our Community
Daily tips, job alerts, interview help — join engineers learning together
← All articlesStart Learning AI
Build a RAG System in Python: Step-by-Step with SynfraAI — Blog | SynfraCore