Welcome to the Custom Data-Driven Chatbot repository! This project allows you to create a chatbot that leverages your own data (PDFs, TXT files, CSVs, etc.) to provide context-aware responses. The chatbot is built using LangChain and Gradio, and it supports memory, document retrieval, and database management.
- Custom Data Integration: Load and process your own documents (PDFs, TXTs, CSVs, DOCX, XLSX).
- Memory: An option to update the database with the conversation history.
- Database Management: Create and update the chatbot's database.
- User-Friendly Interface: Built with Gradio for an intuitive and interactive UI.
Before you begin, ensure you have the following installed:
- Python 3.8 or higher
- OpenAI API Key: You need an API key from OpenAI to use the LLM models.
-
Clone the Repository:
git clone https://github.com/your-username/custom-data-driven-chatbot.git cd custom-data-driven-chatbot -
Set Up a Virtual Environment (recommended):
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
-
Install Dependencies:
pip install gradio langchain openai pypdf unstructured python-docx pandas numpy tqdm sentence-transformers
-
Set Up Your OpenAI API Key:
- Add your OpenAPI key in the
openai_api_keyvariable in the script "chatbot.py" with your actual OpenAI API key.
- Add your OpenAPI key in the
- Place your data files (PDFs, TXTs, CSVs, DOCX, XLSX) in the
Datafolder.
- Start the chatbot by running the script:
python chatbot.py
- The chatbot will launch a Gradio interface in your browser at
http://127.0.0.1:7860/.
- For the first time you use the chatbot, press on the button
Create/Update Database (Using your own files)to create the database. - For any new files/docs you want to incroporate in the database, add these to the
Datafolder and press again on theCreate/Update Database (Using your own files)button, you can delete the files/docs fromDatafolder after updating the database.
- Type your questions in the chat interface.
- The chatbot will retrieve relevant information from your data and provide responses.
- Create/Update Database: Click the
Create/Update Databasebutton to process your data and update the chatbot's knowledge base. - Update Memory: Click the "Update Memory" button to save the conversation history to the database.
- Use the "Clear History" button to delete the conversation history. Note that If you have previously updated memory, the database will have to be deleted to clear the data and re-created from scratch.
Data: Place your data files (PDFs, TXTs, CSVs, DOCX, XLSX) here.Memory: Stores the conversation history and memory files.chatbot.py: The main script for the chatbot.Run_ChatBot.bat: To run the Chatbot with a.batFile (Windows).Database: This will be automatically created once you click onCreate/Update Database (Using your own files)
- Modify the
titleanddescriptionparameters in thegr.ChatInterfacesection of the script:chatbot_ui = gr.ChatInterface( fn=chatbot_response, title="Your Custom Title", description="Your custom description.", theme="soft" )
- To support additional file types, add new
DirectoryLoaderinstances in thecreate_update_databasefunction.
- Replace
"gpt-4o"in theChatOpenAIinitialization with another model (e.g.,"gpt-4", "gpt-4o-mini").
- Ensure your data files are in the
Datafolder and have valid content. - Check the console for any error messages.
- Verify that your OpenAI API key is correct and has sufficient credits.
- Ensure the database is created/updated before interacting with the chatbot.
- If the browser does not open automatically, manually navigate to
http://127.0.0.1:7860/.
- Open
chatbot.py, and editChatWeb.launch(last line of the code) by switchingsharetoTrueas:
ChatWeb.launch(server_name="0.0.0.0", server_port=7860, share=False)To make it easier to run the chatbot on your computer you can use .bat file, by editing Run_ChatBot.bat file. Here’s how:
-
Edit
Run_ChatBot.batFile:- Open
Run_ChatBot.batusing Notepad or any text editor. - Below is the script included:
@echo off set BAT_DIR=%~dp0 "C:\Users\XYZ\miniconda3\python.exe" "%BAT_DIR%chatbot.py" timeout /t 5 /nobreak >nul start http://127.0.0.1:7860/
1.1 Replace the Python Directory
Replace the placeholderC:\Users\XYZ\miniconda3\python.exewith your actual Python installation path.
You can find your Python directory using one of the following methods:-
Via Python Script in IDE: Run the following script in your Python IDE or interpreter:
import sys print("Python Directory:", sys.executable)
-
Using the
whichCommand (Linux/Mac) orwhereCommand (Windows):- Linux/Mac:
which python
- Windows:
where python
- Linux/Mac:
- Open
-
Save the File:
- Save the file with a
.batextension, e.g.,Run_ChatBot.bat. - Make sure to select "All Files" in the "Save as type" dropdown to avoid saving it as a
.txtfile. - Make sure the
.batfile is saved in the same directory as thechatbot.py.
- Save the file with a
-
Run the
.batFile:- Double-click the
Run_ChatBot.batfile to run the chatbot. - The script will:
- Run the
chatbot.pyscript. - Wait for 5 seconds to ensure the server is ready.
- Open your default browser to
http://127.0.0.1:7860/.
- Run the
- Double-click the
Contributions are welcome!
Enjoy using your Custom Data-Driven Chatbot! If you have any questions or issues, feel free to open an issue on GitHub.
Let me know if you need further assistance! 😊