feat: initial commit of Enterprise PDF RAG Assistant
This commit is contained in:
+24
@@ -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
|
||||
@@ -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`.
|
||||
@@ -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})
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user