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
- Supports
.txt
,.docx
, and.csv
files. - Uses LLaMA 3.2 for natural language understanding.
- Checks if the Ollama server is running and provides setup instructions if not.
- Handles unanswerable questions gracefully.
- Command-line interface with a user-friendly greeting.
Requirements
- Python 3.8 or higher
- Libraries:
ollama
,python-docx
,pandas
- Ollama (latest version, e.g., 0.3.12+)
- LLaMA 3.2 model (installed via
ollama pull llama3.2
)
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:
- "What is Walmart's stock price?"
- "Who is working on the AI Development project?"
Sample Files
Download sample files to test the chatbot:
- technical_report.txt: A technical analysis report on stock trading indicators (~200 lines).
- project_contacts.csv: Contact data with names, emails, projects (~100 lines).
Download
Get the script and start building your document-based chatbot:
Download doc_based_chatbot.pyExample 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!