Document-Based Chatbot

A Python script for answering questions based on documents using LLaMA 3.2

Back to Home

Overview

The doc_based_chatbot.py script creates a chatbot that answers questions based solely on the content of provided .txt, .docx, or .csv files. It uses a locally installed LLaMA 3.2 model via Ollama to process documents and generate responses. The chatbot opens a command-line chat interface, issues a greeting, and responds with "I do not have information for that question" if a question cannot be answered from the documents.

Features

Requirements

Installation

# Install Python 3.8+ (if not already installed)
# Linux
sudo apt-get install python3
# macOS
brew install python3

# Install required libraries
pip install ollama python-docx pandas

# Install Ollama
# Linux/macOS
curl -fsSL https://ollama.com/install.sh | sh
# Windows: Download from https://ollama.com/download

# Pull LLaMA 3.2 model
ollama pull llama3.2

# Start Ollama server (run in a separate terminal)
ollama serve
            

Usage

Run the script with one or more document files as command-line arguments. The chatbot will read the documents, open a chat interface, and answer questions based on their content.

python3 doc_based_chatbot.py technical_report.txt project_contacts.csv
            

Type your question in the chat interface, or 'exit' to quit. Example questions:

Sample Files

Download sample files to test the chatbot:

Download

Get the script and start building your document-based chatbot:

Download doc_based_chatbot.py

Example Output

=== Document-Based Chatbot ===
Hello! I'm ready to answer questions based on the provided documents.
Type your question, or 'exit' to quit.
================================

You: What is Walmart's stock price?
Bot: Walmart's stock price is $98.55.

You: Who is working on Contract-001?
Bot: John Smith is working on Contract-001 for the AI Development project.

You: What is the capital of France?
Bot: I do not have information for that question.

You: exit
Goodbye!