mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-17 20:19:48 +00:00
Update README.md
This commit is contained in:
parent
a1d4cd9839
commit
508249e47a
@ -1,55 +1,83 @@
|
|||||||
# Chatbot with Chat history stored in Database
|
|
||||||
|
|
||||||
This project is a simple chatbot application built using Python, integrating a database for chat history storage and a language model service to generate responses. The chatbot can handle user messages, manage chat history, and terminate conversations upon receiving a `/stop` command.
|
|
||||||
|
# Chatbot with LLM Integration and Database Storage
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
- **Conversation Handling**: The bot processes user inputs and generates responses using a language model service.
|
- Supports LLM response generation using **Together** and **Groq** APIs.
|
||||||
- **Database Integration**: Stores chat data (user messages and bot responses) and maintains chat history.
|
- Stores chat sessions and message exchanges in MySQL database tables.
|
||||||
- **Session Management**: Supports starting and terminating chat sessions, including proper logging of start and end times.
|
- Automatically updates chat session status using database triggers.
|
||||||
- **Message Truncation**: Limits conversation history to the last few messages if the conversation exceeds a large number of entries.
|
- Manages conversation history with user-assistant interaction.
|
||||||
|
|
||||||
## Components
|
## Requirements
|
||||||
- **`Chatbot` Class**: Core logic for handling user messages and managing the chat lifecycle.
|
|
||||||
- **`Database` (Mocked in tests)**: Handles chat data storage (methods for inserting and retrieving data).
|
|
||||||
- **`LLM Service` (Mocked in tests)**: Generates responses to user input based on conversation history.
|
|
||||||
|
|
||||||
## Installation
|
Before running the application, ensure the following dependencies are installed:
|
||||||
1. Clone the repository:
|
|
||||||
2. Install the necessary dependencies
|
- Python 3.13+
|
||||||
```bash
|
- MySQL Server
|
||||||
pip3 install requirements.txt
|
- The following Python libraries:
|
||||||
```
|
```bash
|
||||||
4. Run the bot or test it using `doctest`:
|
pip3 install -r requirements.txt
|
||||||
```bash
|
```
|
||||||
python3 -m doctest -v chatbot.py
|
|
||||||
```
|
## Setup Instructions
|
||||||
|
|
||||||
|
### Step 1: Set Up Environment Variables
|
||||||
|
|
||||||
|
Create a `.env` file in the root directory of your project and add the following entries for your database credentials and API keys:
|
||||||
|
|
||||||
## Usage
|
|
||||||
1. **Create Database**: Create a databse named `ChatDB` in Mysql
|
|
||||||
2. **Create .env**:
|
|
||||||
```
|
```
|
||||||
# Together API key
|
# Together API key
|
||||||
TOGETHER_API_KEY="YOUR_API_KEY"
|
TOGETHER_API_KEY="YOUR_API_KEY"
|
||||||
|
|
||||||
# Groq API key
|
# Groq API key
|
||||||
GROQ_API_KEY = "YOUR_API_KEY"
|
GROQ_API_KEY = "YOUR_API_KEY"
|
||||||
|
|
||||||
# MySQL connectionDB (if you're running locally)
|
# MySQL connectionDB (if you're running locally)
|
||||||
DB_USER = "<DB_USER_NAME>"
|
DB_USER = "<DB_USER_NAME>"
|
||||||
DB_PASSWORD = "<DB_USER_NAME>"
|
DB_PASSWORD = "<DB_USER_NAME>"
|
||||||
DB_HOST = "127.0.0.1"
|
DB_HOST = "127.0.0.1"
|
||||||
DB_NAME = "ChatDB"
|
DB_NAME = "ChatDB"
|
||||||
PORT = "3306"
|
PORT = "3306"
|
||||||
```
|
|
||||||
7. **Handling Messages**: run below command to start the chat in console, you can login to your Database to check the chat history
|
# API service to you(or use "Together")
|
||||||
```python
|
API_SERVICE = "Groq"
|
||||||
python3 main.py
|
|
||||||
```
|
```
|
||||||
10. **Ending the Chat**: When the user sends `/stop`, the chat will terminate and log the end of the conversation with the message 'conversation-terminated'
|
|
||||||
|
|
||||||
## Testing
|
### Step 2: Create MySQL Tables and Trigger
|
||||||
The code includes basic `doctests` to verify the chatbot's functionality using mock services for the database and language model:
|
|
||||||
- Run the tests:
|
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.
|
||||||
```bash
|
|
||||||
python3 -m doctest -v chatbot.py
|
Ensure that your MySQL server is running and accessible before running the code.
|
||||||
```
|
|
||||||
|
### Step 3: Run the Application
|
||||||
|
|
||||||
|
To start the chatbot:
|
||||||
|
|
||||||
|
1. Ensure your MySQL server is running.
|
||||||
|
2. Open a terminal and run the Python script:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python3 chat_db.py
|
||||||
|
```
|
||||||
|
|
||||||
|
The chatbot will initialize, and you can interact with it by typing your inputs. Type `/stop` to end the conversation.
|
||||||
|
|
||||||
|
### Step 4: Test and Validate Code
|
||||||
|
|
||||||
|
This project uses doctests to ensure that the functions work as expected. To run the doctests:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python3 -m doctest -v chatbot.py
|
||||||
|
```
|
||||||
|
|
||||||
|
Make sure to add doctests to all your functions where applicable, to validate both valid and erroneous inputs.
|
||||||
|
|
||||||
|
### Key Functions
|
||||||
|
|
||||||
|
- **create_tables()**: Sets up the MySQL tables (`Chat_history` and `Chat_data`) and the `update_is_stream` trigger.
|
||||||
|
- **insert_chat_history()**: Inserts a new chat session into the `Chat_history` table.
|
||||||
|
- **insert_chat_data()**: Inserts user-assistant message pairs into the `Chat_data` table.
|
||||||
|
- **generate_llm_response()**: Generates a response from the selected LLM API service, either **Together** or **Groq**.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user