Skip to content

kshirajahere/RAG_discord_bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 

Repository files navigation

Discord RAG Bot

A Discord bot that uses Retrieval-Augmented Generation (RAG) to answer questions based on your document collection. The bot processes PDF and text files, creates a searchable vector database, and provides intelligent responses using Groq's LLaMA model.

Features

  • Document Processing: Automatically processes PDF and text files from your data directory
  • Vector Search: Uses FAISS for efficient similarity search across document chunks
  • AI-Powered Responses: Leverages Groq's LLaMA 3 model for intelligent question answering
  • Discord Integration: Responds to mentions with formatted embed messages
  • Source Attribution: Shows which documents were used to generate each answer

Prerequisites

  • Python 3.8 or higher
  • Discord Bot Token
  • Groq API Key

Installation

  1. Clone the repository

    git clone <your-repo-url>
    cd discord-rag-bot
  2. Create a virtual environment

    python -m venv .venv
    .venv\Scripts\activate  # On Windows
    # source .venv/bin/activate  # On macOS/Linux
  3. Install dependencies

    pip install -r requirements.txt
  4. Set up environment variables

    Create a .env file in the root directory:

    DISCORD_BOT_TOKEN=your_discord_bot_token_here
    GROQ_API_KEY=your_groq_api_key_here

Setup Guide

1. Discord Bot Setup

  1. Go to the Discord Developer Portal
  2. Create a new application and bot
  3. Copy the bot token to your .env file
  4. Invite the bot to your server with the following permissions:
    • Read Messages
    • Send Messages
    • Use Slash Commands
    • Embed Links

2. Groq API Setup

  1. Sign up at Groq Console
  2. Generate an API key
  3. Add it to your .env file

3. Document Preparation

  1. Place your PDF or text files in the data/ directory
  2. The bot will automatically process all .pdf and .txt files

Usage

1. Create Vector Database

Before running the bot, create the vector database from your documents:

python rag_pipeline.py

This will:

  • Load all documents from the data/ directory
  • Split them into chunks
  • Create embeddings using HuggingFace's BGE model
  • Save the FAISS vector database

2. Run the Bot

python bot.py

3. Interact with the Bot

In Discord, mention the bot with your question:

@YourBot What is the refund policy?
@YourBot How do I submit an assignment?

The bot will respond with an answer and show which documents were used as sources.

Project Structure

discord-rag-bot/
├── bot.py                 # Main Discord bot implementation
├── rag_pipeline.py        # RAG pipeline and vector store creation
├── requirements.txt       # Python dependencies
├── .env                  # Environment variables (create this)
├── data/                 # Place your documents here
│   └── PUT THE PDFS HERE
└── vectorstore/          # Generated vector database
    └── db_faiss/

Configuration

Model Settings

The bot uses the following default models:

  • Embedding Model: BAAI/bge-small-en-v1.5 (HuggingFace)
  • LLM Model: llama3-8b-8192 (Groq)

You can modify these in rag_pipeline.py:

MODEL_NAME = "BAAI/bge-small-en-v1.5"  # Embedding model
# In create_rag_chain():
model_name="llama3-8b-8192"  # LLM model

Text Chunking

Documents are split with these parameters:

  • Chunk Size: 1000 characters
  • Chunk Overlap: 150 characters

Adjust in rag_pipeline.py:

text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=1000, 
    chunk_overlap=150
)

Troubleshooting

Common Issues

  1. "Vector database not found" error

    • Run python rag_pipeline.py first to create the database
  2. "No documents found" error

    • Ensure you have PDF or text files in the data/ directory
  3. Discord bot not responding

    • Check that the bot has proper permissions in your server
    • Verify the bot token in your .env file
  4. Groq API errors

    • Verify your API key is correct
    • Check your Groq API usage limits

Performance Tips

  • For large document collections, consider increasing chunk size
  • The bot loads the RAG chain once at startup for efficiency
  • Vector database is created locally and persists between runs

Dependencies

  • langchain - LangChain framework
  • langchain-groq - Groq integration
  • langchain-community - Community integrations
  • faiss-cpu - Vector similarity search
  • sentence-transformers - Embedding models
  • python-dotenv - Environment variable management
  • discord.py - Discord API wrapper

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly
  5. Submit a pull request

License

This project is open source. Please check the license file for details.

Support

If you encounter issues:

  1. Check the troubleshooting section above
  2. Review the console output for error messages
  3. Ensure all dependencies are properly installed
  4. Verify your API keys and bot permissions

Note: This bot processes documents locally and uses external APIs (Groq) for language model inference. Ensure you comply with your organization's data handling policies when using sensitive documents.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages