|
| 1 | + |
| 2 | + |
| 3 | +# Chatbot with LLM Integration and Database Storage |
| 4 | + |
| 5 | +This chatbot application integrates LLM (Large Language Model) API services, **Together** and **Groq**(you can use any one of them), to generate AI-driven responses. It stores conversation history in a MySQL database and manages chat sessions with triggers that update the status of conversations automatically. |
| 6 | + |
| 7 | +## Features |
| 8 | +- Supports LLM response generation using **Together** and **Groq** APIs. |
| 9 | +- Stores chat sessions and message exchanges in MySQL database tables. |
| 10 | +- Automatically updates chat session status using database triggers. |
| 11 | +- Manages conversation history with user-assistant interaction. |
| 12 | + |
| 13 | +## Requirements |
| 14 | + |
| 15 | +Before running the application, ensure the following dependencies are installed: |
| 16 | + |
| 17 | +- Python 3.13+ |
| 18 | +- MySQL Server |
| 19 | +- The following Python libraries: |
| 20 | + ```bash |
| 21 | + pip3 install -r requirements.txt |
| 22 | + ``` |
| 23 | + |
| 24 | +## Setup Instructions |
| 25 | + |
| 26 | +### Step 1: Set Up Environment Variables |
| 27 | + |
| 28 | +Create a `.env` file in the root directory of your project and add the following entries for your database credentials and API keys: |
| 29 | + |
| 30 | +``` |
| 31 | +# Together API key |
| 32 | +TOGETHER_API_KEY="YOUR_API_KEY" |
| 33 | +
|
| 34 | +# Groq API key |
| 35 | +GROQ_API_KEY = "YOUR_API_KEY" |
| 36 | +
|
| 37 | +# MySQL connectionDB (if you're running locally) |
| 38 | +DB_USER = "<DB_USER_NAME>" |
| 39 | +DB_PASSWORD = "<DB_USER_NAME>" |
| 40 | +DB_HOST = "127.0.0.1" |
| 41 | +DB_NAME = "ChatDB" |
| 42 | +PORT = "3306" |
| 43 | +
|
| 44 | +# API service to you(or use "Together") |
| 45 | +API_SERVICE = "Groq" |
| 46 | +``` |
| 47 | + |
| 48 | +### Step 2: Create MySQL Tables and Trigger |
| 49 | + |
| 50 | +The `create_tables()` function in the script automatically creates the necessary tables and a trigger for updating chat session statuses. To ensure the database is set up correctly, the function is called at the beginning of the script. |
| 51 | + |
| 52 | +Ensure that your MySQL server is running and accessible before running the code. |
| 53 | + |
| 54 | +### Step 3: Run the Application |
| 55 | + |
| 56 | +To start the chatbot: |
| 57 | + |
| 58 | +1. Ensure your MySQL server is running. |
| 59 | +2. Open a terminal and run the Python script: |
| 60 | + |
| 61 | +```bash |
| 62 | +python3 chat_db.py |
| 63 | +``` |
| 64 | + |
| 65 | +The chatbot will initialize, and you can interact with it by typing your inputs. Type `/stop` to end the conversation. |
| 66 | + |
| 67 | +### Step 4: Test and Validate Code |
| 68 | + |
| 69 | +This project uses doctests to ensure that the functions work as expected. To run the doctests: |
| 70 | + |
| 71 | +```bash |
| 72 | +python3 -m doctest -v chatbot.py |
| 73 | +``` |
| 74 | + |
| 75 | +Make sure to add doctests to all your functions where applicable, to validate both valid and erroneous inputs. |
| 76 | + |
| 77 | +### Key Functions |
| 78 | + |
| 79 | +- **create_tables()**: Sets up the MySQL tables (`Chat_history` and `Chat_data`) and the `update_is_stream` trigger. |
| 80 | +- **insert_chat_history()**: Inserts a new chat session into the `Chat_history` table. |
| 81 | +- **insert_chat_data()**: Inserts user-assistant message pairs into the `Chat_data` table. |
| 82 | +- **generate_llm_response()**: Generates a response from the selected LLM API service, either **Together** or **Groq**. |
| 83 | + |
0 commit comments