commit 1c09cb29e1736d7fa2fbb1e7613ca197e583ebf8 Author: om_karande Date: Tue Oct 6 12:43:30 2026 +0530 feat: initial commit of Enterprise PDF RAG Assistant diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e282e3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +ο»Ώ# Environments +.venv/ +venv/ +ENV/ +env/ + +# Python cache +__pycache__/ +*.py[cod] +*$py.class + +# ChromaDB & Vector Store local data +chroma_db/ +.chroma/ + +# Uploads & User documents +*.pdf +!samples/*.pdf +temp/ +*.tmp + +# Secrets & Local Config +.env +*.log \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..7b71f8c --- /dev/null +++ b/README.md @@ -0,0 +1,79 @@ +ο»Ώ# Enterprise PDF RAG Assistant (Llama 3 8B & ChromaDB) + +A privacy-focused, 100% on-premise **Retrieval-Augmented Generation (RAG)** application built with **Streamlit**, **LangChain**, **ChromaDB**, and **Ollama**. + +Upload any PDF document (resumes, project briefs, specifications, reports) to extract structured detailsβ€”such as candidate names, skills, project milestones, and metricsβ€”and converse directly with your document through natural language Q&A. + +--- + +## πŸ“Œ Architecture & Pipeline + +```text +[ Upload PDF ] ───> Streamlit File Uploader + β”‚ + β–Ό +[ PyPDFLoader ] ───> Extract text across all pages + β”‚ + β–Ό +[ RecursiveCharacterTextSplitter ] ───> Chunking (size: 1000, overlap: 200) + β”‚ + β–Ό +[ OllamaEmbeddings (nomic-embed-text) ] ───> Vector embeddings + β”‚ + β–Ό +[ ChromaDB ] ───> In-memory vector store indexing + β”‚ + β–Ό +[ User Query ] ───> Top-k retrieval (k=3) ───> LCEL Prompt ───> OllamaLLM (llama3:8b) + β”‚ + β–Ό +[ Streamlit Chat UI ] ───> Real-time conversational response +``` + +--- + +## πŸš€ Features + +- **Document Extraction & Q&A**: Extracts project names, roles, technical skills, and key metrics directly from PDFs. +- **Dual Chat Modes**: + - **RAG Mode**: Answers grounded strictly in your uploaded PDF content with citation context. + - **Direct Chat Mode**: General AI assistant mode when no document is uploaded. +- **100% Local & Confidential**: All embeddings (`nomic-embed-text`) and LLM inference (`llama3:8b`) run on your local machine via Ollama. No data is sent to external clouds or APIs. +- **Modern Streamlit Chat Interface**: Built-in `st.chat_message` and `st.chat_input` for a responsive UI. + +--- + +## πŸ› οΈ Quick Start + +### 1. Prerequisites (Ollama) + +Install [Ollama](https://ollama.com/) and download the required models: + +```bash +# Pull the LLM and embedding models +ollama pull llama3:8b +ollama pull nomic-embed-text + +# Ensure the Ollama server is running +ollama serve +``` + +### 2. Environment Setup & Dependencies + +```bash +python -m venv venv +# Windows: +.\venv\Scripts\activate +# Linux/macOS: +source venv/bin/activate + +pip install -r requirements.txt +``` + +### 3. Run the Application + +```bash +streamlit run app.py +``` + +The web dashboard will open automatically in your browser at `http://localhost:8501`. \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..2631419 --- /dev/null +++ b/app.py @@ -0,0 +1,87 @@ +import streamlit as st +import tempfile +import os + +from langchain_community.document_loaders import PyPDFLoader +from langchain_text_splitters import RecursiveCharacterTextSplitter +from langchain_community.vectorstores import Chroma +from langchain_ollama import OllamaEmbeddings, OllamaLLM +from langchain_core.prompts import ChatPromptTemplate + +# 1. Page Configuration +st.set_page_config(page_title="Enterprise RAG Assistant", page_icon="πŸ€–", layout="wide") +st.title("πŸ€– Enterprise RAG Assistant (Llama 3 8B)") +st.caption("Powered by Ollama, LangChain & ChromaDB | 100% On-Premise & Secure") + +# 2. Sidebar for Document Upload +with st.sidebar: + st.header("πŸ“„ Document Management") + uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"]) + +# Initialize Vector DB in Session State +if "vector_store" not in st.session_state: + st.session_state.vector_store = None + +# 3. Process PDF Document +if uploaded_file and st.session_state.vector_store is None: + with st.spinner("Processing PDF and generating embeddings..."): + # Save uploaded file to a temporary directory + with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file: + tmp_file.write(uploaded_file.read()) + tmp_file_path = tmp_file.name + + # Load Document + loader = PyPDFLoader(tmp_file_path) + docs = loader.load() + + # Split Document into Chunks + text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) + splits = text_splitter.split_documents(docs) + + # Generate Embeddings & Store in ChromaDB + embeddings = OllamaEmbeddings(model="nomic-embed-text") + st.session_state.vector_store = Chroma.from_documents(documents=splits, embedding=embeddings) + + # Clean up temporary file + os.remove(tmp_file_path) + st.sidebar.success("PDF processed and indexed successfully!") + +# 4. Chat Interface +if "messages" not in st.session_state: + st.session_state.messages = [] + +# Display Chat History +for message in st.session_state.messages: + with st.chat_message(message["role"]): + st.markdown(message["content"]) + +# User Query Processing +if user_prompt := st.chat_input("Ask a question about your document or anything else..."): + # Display User Message + st.chat_message("user").markdown(user_prompt) + st.session_state.messages.append({"role": "user", "content": user_prompt}) + + with st.chat_message("assistant"): + with st.spinner("Thinking..."): + llm = OllamaLLM(model="llama3:8b") + + if st.session_state.vector_store: + # RAG Mode: Retrieve Context & Format Prompt directly via LCEL + retriever = st.session_state.vector_store.as_retriever(search_kwargs={"k": 3}) + retrieved_docs = retriever.invoke(user_prompt) + + context = "\n\n".join([doc.page_content for doc in retrieved_docs]) + + prompt_template = ChatPromptTemplate.from_messages([ + ("system", "You are an expert AI assistant. Use the following context to answer the question concisely.\n\nContext:\n{context}"), + ("human", "{input}") + ]) + + chain = prompt_template | llm + ai_response = chain.invoke({"context": context, "input": user_prompt}) + else: + # Direct Chat Mode (No PDF Uploaded) + ai_response = llm.invoke(user_prompt) + + st.markdown(ai_response) + st.session_state.messages.append({"role": "assistant", "content": ai_response}) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..295d90d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,8 @@ +ο»Ώstreamlit>=1.35.0 +langchain>=0.2.0 +langchain-community>=0.2.0 +langchain-core>=0.2.0 +langchain-text-splitters>=0.2.0 +langchain-ollama>=0.1.0 +pypdf>=4.0.0 +chromadb>=0.5.0 \ No newline at end of file