This project demonstrates applications showcasing interaction with a local Ollama instance π¦ for various AI tasks using Python and LangChain.
This project implements a Retrieval-Augmented Generation (RAG) system using:
- A local Large Language Model (LLM) served by Ollama π¦
- A Chroma vector database πΎ for document storage and retrieval
- A Gradio web interface π for interaction.
It allows you to upload your documents (PDF, TXT, MD), ask questions about their content, and receive answers generated by a local LLM, ensuring your data stays private π. The system also keeps track of your chat history π.
- π Local Processing: Uses Ollama to run LLMs locally, keeping your data private.
- π€ Document Upload: Supports uploading PDF, TXT, and Markdown (.md) files.
- πΎ Vector Storage: Uses ChromaDB to store document embeddings for efficient retrieval.
- π¬ Chat Interface: Provides a user-friendly Gradio interface to chat with your documents.
- π Source Citation: Displays the source documents used to generate the answer.
- π’ Token Count: Shows the number of tokens used in the user query and the assistant's response.
- π Chat History: Saves and loads conversation history (also stored in ChromaDB).
- βοΈ Management: Allows listing stored documents, deleting all documents, and deleting chat history.
- Python 3.8+ π
pip(Python package installer) π¦- Git (for cloning the repository) π
- Ollama installed and running π¦π¨.
-
Clone the Repository: π»
git clone github.com:maurizioorani/python_ai.git
-
Create a Virtual Environment (Recommended): π±
# Linux/macOS python3 -m venv venv source venv/bin/activate # Windows python -m venv venv .\venv\Scripts\activate
-
Create the Requirements File (
RAG_requirements.txt): π Create a file namedRAG_requirements.txtin the project directory with the following content:gradio langchain langchain-chroma langchain-huggingface langchain-openai # Used for Ollama's OpenAI-compatible API tiktoken pypdf unstructured[md] # Installs markdown parsing extras for unstructured sentence-transformers # For HuggingFaceEmbeddings chromadb >= 0.4.20 # Ensure a recent version for compatibility ollama # Optional, but useful for managing Ollama via command line
Note: You might need to adjust versions based on compatibility.
-
Install Dependencies: β¬οΈ Ensure your virtual environment is active, then run:
pip install -r RAG_requirements.txt
Alternatively, if an
environment.ymlis provided for Conda:conda env create -f environment.yml # Or conda install --file environment.yml -
Setup Ollama: π¦
- Install Ollama: Follow instructions on the Ollama website.
- Run Ollama: Ensure the Ollama service is running (e.g., run the installed application or use
ollama servein the terminal). - Verify Ollama: Check if Ollama is running by opening a terminal and typing:
This command should list your installed models if Ollama is running and accessible.
ollama list
-
Pull the LLM Model: π§ The RAG script is configured by default to use
llama3.2. Pull this model using the Ollama CLI:ollama pull llama3.2
Wait for the download to complete. You can verify the model is installed using
ollama listagain. Note: If you wish to use a different model, update themodelvalue within the Python script (likely in anLLM_CONFIGdictionary) and pull the corresponding model usingollama pull <new_model_name>.
Once the setup is complete and Ollama is running with the required model:
python RAG_example.pyThen, open your web browser and navigate to http://localhost:7860 π. You should see the Gradio interface where you can upload documents and interact with the RAG system.
Please note: For optimal performance in languages other than English, consider using larger models or those specifically trained for multilingual tasks, as smaller CPU-only models may produce unexpected results due to limited non-English training.
This project provides a more sophisticated Retrieval-Augmented Generation (RAG) system, enhanced with document management capabilities using metadata for precise file deletion. See its dedicated Readme for details.
A minimalist command-line interface for interacting with locally hosted Ollama models. Perfect for:
β
Learning LLM interaction fundamentals
β
Testing model responses
β
Prototyping without UI complexity
Advanced web analysis tool featuring:
- Website Scraping - Automatically harvest all hyperlinks
- AI Analysis - Local Ollama-powered relevance scoring π§
- Business Intelligence - Identifies core business sections:
- About Us
- Services/Products
- Contact Information
- Company Values
Perfect for competitive analysis and website mapping!
π¦ Ollama Running:
ollama serve # Keep running in background