Skip to content

A demo Jupyter Notebook showcasing a simple local RAG (Retrieval Augmented Generation) pipeline to chat with your PDFs.

License

Notifications You must be signed in to change notification settings

aghoshpro/OllamaRAG

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

38 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿค– Chat with PDF locally using Ollama + LangChain

A powerful local RAG (Retrieval Augmented Generation) application that lets you chat with your PDF documents using Ollama and LangChain. This project includes both a Jupyter notebook for experimentation and a Streamlit web interface for easy interaction. Shout out to Tony Kipkemboi!

๐Ÿ“‚ Project File Structure

ollama_pdf_rag/
โ”œโ”€โ”€ src/                      # Source code
โ”‚   โ”œโ”€โ”€ app/                  # Streamlit application
โ”‚   โ”‚   โ”œโ”€โ”€ components/       # UI components
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ chat.py       # Chat interface
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ pdf_viewer.py # PDF display
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ sidebar.py    # Sidebar controls
โ”‚   โ”‚   โ””โ”€โ”€ main.py           # Main app
โ”‚   โ””โ”€โ”€ core/                 # Core functionality
โ”‚       โ”œโ”€โ”€ document.py       # Document processing
โ”‚       โ”œโ”€โ”€ embeddings.py     # Vector embeddings
โ”‚       โ”œโ”€โ”€ llm.py            # LLM setup
โ”‚       โ””โ”€โ”€ rag.py            # RAG pipeline
โ”œโ”€โ”€ data/                     # Data storage
โ”‚   โ”œโ”€โ”€ pdfs/                 # PDF storage
โ”‚   โ”‚   โ””โ”€โ”€ sample/           # Sample PDFs
โ”‚   โ””โ”€โ”€ vectors/              # Vector DB storage
โ”œโ”€โ”€ notebooks/                # Jupyter notebooks
โ”‚   โ””โ”€โ”€ experiments/          # Experimental notebooks
โ”œโ”€โ”€ tests/                    # Unit tests
โ”œโ”€โ”€ docs/                     # Documentation
โ””โ”€โ”€ run.py                    # Application runner

โœจ Features

  • ๐Ÿ”’ Fully local processing - no data leaves your machine
  • ๐Ÿ“„ PDF processing with intelligent chunking
  • ๐Ÿง  Multi-query retrieval for better context understanding
  • ๐ŸŽฏ Advanced RAG implementation using LangChain
  • ๐Ÿ–ฅ๏ธ Clean Streamlit interface
  • ๐Ÿ““ Jupyter notebook for experimentation

๐Ÿš€ Getting Started

Prerequisites

  1. Install Ollama

    • Visit Ollama's website to download and install
    • Pull required models:
      ollama pull llama3.2  # or your preferred model
      ollama pull nomic-embed-text
  2. Clone Repository

    git clone https://github.com/aghoshpro/OllamaRAG.git
    cd ollama_pdf_rag
  3. Set Up Environment

    python -m venv myvenv
    # On Windows
    .\myvenv\Scripts\activate
    
    **OR**
    
    # On Linux or Mac
     source myvenv/bin/activate
    
    pip install -r requirements.txt
    

๐ŸŽฎ Run the App

Option 1: Streamlit Interface

python run.py

Then open your browser to local url http://localhost:8501

Streamlit interface showing ChatPDFx StreamLit App

Option 2: Jupyter Notebook

jupyter notebook

Open updated_rag_notebook.ipynb to experiment with the code

๐Ÿ’ก Usage Tips

  1. Upload PDF: Use the file uploader in the Streamlit interface or try the sample PDF
  2. Select Model: Choose from your locally available Ollama models
  3. Ask Questions: Start chatting with your PDF through the chat interface
  4. Adjust Display: Use the zoom slider to adjust PDF visibility
  5. Clean Up: Use the "Delete Collection" button when switching documents

๐Ÿ› ๏ธ Troubleshooting

  • Ensure Ollama is running in the background
  • Check that required models are downloaded
  • Verify Python environment is activated
  • For Windows users, ensure WSL2 is properly configured if using Ollama

โš ๏ธ Common Errors

๐ŸŸก ONNX DLL Error

DLL load failed while importing onnx_copy2py_export: a dynamic link Library (DLL) initialization routine failed.

Try these solutions:

  1. Install Microsoft Visual C++ Redistributable:

  2. If the error persists, try installing ONNX Runtime manually:

    pip uninstall onnxruntime onnxruntime-gpu
    pip install onnxruntime

CPU-Only Systems

If you're running on a CPU-only system:

  1. Ensure you have the CPU version of ONNX Runtime:

    pip uninstall onnxruntime-gpu  # Remove GPU version if installed
    pip install onnxruntime  # Install CPU-only version
  2. You may need to modify the chunk size in the code to prevent memory issues:

    • Reduce chunk_size to 500-1000 if you experience memory problems
    • Increase chunk_overlap for better context preservation

Note: The application will run slower on CPU-only systems, but it will still work effectively.

๐ŸŸก TesseractNotFoundError

TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.

Try this solution from stackoverflow:

  • Install tesseract using windows installer available at: https://github.com/UB-Mannheim/tesseract/wiki

  • Note the tesseract path from the installation C:\Program Files\Tesseract-OCR add it to system environmental variables path.

    • Restart the system to adapt the variable effect

    • Activate the myenv and run the app again.

  • pip install pytesseract [OPTIONAL]

  • Set the tesseract path in the script before calling image_to_string [OPTIONAL]

    pytesseract.pytesseract.tesseract_cmd = r'C:\Users\USER\AppData\Local\Tesseract-OCR\tesseract.exe'
    

๐ŸŸก Lookup Error with NLTK

Lookup error ... nltk.download('averaged_perceptron_tagger_eng') is not found
  • Add the following to main.py and run again

    import nltk
    nltk.download('averaged_perceptron_tagger_eng')
    

๐Ÿงช Testing

Running Tests

# Run all tests
python -m unittest discover tests

# Run tests verbosely
python -m unittest discover tests -v

Pre-commit Hooks

The project uses pre-commit hooks to ensure code quality. To set up:

pip install pre-commit
pre-commit install

This will:

  • Run tests before each commit
  • Run linting checks
  • Ensure code quality standards are met

Continuous Integration

The project uses GitHub Actions for CI. On every push and pull request:

  • Tests are run on multiple Python versions (3.9, 3.10, 3.11)
  • Dependencies are installed
  • Ollama models are pulled
  • Test results are uploaded as artifacts

๐Ÿค Contributing

Feel free to:

  • Open issues for bugs or suggestions
  • Submit pull requests
  • Comment on the YouTube video for questions
  • Star the repository if you find it useful!

๐Ÿ“ License

This project is open source and available under the MIT License.

About

A demo Jupyter Notebook showcasing a simple local RAG (Retrieval Augmented Generation) pipeline to chat with your PDFs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Jupyter Notebook 74.1%
  • Python 25.9%