This project is a Django-based REST API for a chatbot application that allows users to have an interactive session with the chatbot using prompt messages. The chat history is stored with timestamps and user information.
- Implementation of the Flan-T5 Model: The base version of the Flan-T5 model is used to optimize for performance and avoid heavy computation. To switch to a more powerful variant, such as the 'xxl' version, update the configuration in chatbot\chatbot\utils.py.
- User Authentication (Django Token Authentication).
- SQLite database used.
- Start a new chat session by sending a message.
- Receive response from the bot.
- Continue conversation while persisting the chat session.
- Chat history is preserves.
- Continue or end an existing chat session.
- Edit specific messages in chat history using chat id and message id.
- Delete specific chat room based on id.
- Timestamps are stored in PST time zone and formatted as
DD/MM/YYYY HH:MM:SS.
- Python 3.10+
- Django 5.1.2
- Django REST Framework
-
Clone the repository
git clone https://github.com/Alijaffery5/chatbot.git cd chatbot -
Create a virtual environment
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
-
Install dependencies
pip install -r requirements.txt
-
Run migrations
python manage.py makemigrations python manage.py migrate
-
Create a superuser
python manage.py createsuperuser
-
Run the server
python manage.py runserver
-
Access the API
- The API will be available at
http://127.0.0.1:8000/api/
- The API will be available at
All Apis require these in the Header section except login and register.
{
"Content-Type":"application/json",
"Authorization":"Token <enter_your_token_here>"
}POST /api/login/: Authenticate and Login the user.POST /api/register/: Register a new user.
-
Register a new User
POST http://127.0.0.1:8000/api/register/Request Body:
{ "username": "john_doe", "email": "john.doe@example.com", "password": "your_password" }- Example Response:
{ "message": "Registered successfully!", "token": "token_displayed_here", "user": { "id": 1, "username": "john_doe", "email": "xyz@gmail.com", "password": "test12345678" } }
- Example Response:
-
Login Endpoint
POST http://127.0.0.1:8000/api/login/Request Body:
{ "username": "john_doe", "password": "your_password" }- Example Response:
{ "message": "Logged-In successfully!", "token": "your_token_here", "user": { "id": 1, "username": "john_doe", "email": "xyz@gmail.com" } }
- Example Response:
-
Start or Continue a Chat Session
POST /api/chats/Request Body:
{ "content": "Your message here", "end_session": false }content: The message content or body (Mandatory field).end_session: Set totrueto end the current session, orfalseto continue the chat (Optional - False by default).- Note: Each json request body must have content field and upon sending multiple requests with content, the chatbot will continue conversation with responses in the current session or chat room.
Response:
{ "chat_id": 118, "user_id": 2, "conversation": [ { "message_id": "670a8acf-24af-4831-8c86-3f5863437b9a", "You": "How many plants are there in the universe?", "Bot": "seven" }, { "id": "a4debb18-3fda-4bc2-afa7-ff330d339c03", "You": "What happens when coke is mixed with icecubes?", "Bot": "the ice cubes melt" } ], "start_time": "2024-10-24T22:47:26.507862Z", "end_time": null, "session_identifier": "b7d6a8f2-7986-4820-bd29-056cff5e32ec" }
The read_chats endpoint allows users to retrieve their chat history, including all the messages exchanged during each chat session. User can also filter search chats using session_id.
- Endpoint:
/api/chats/ORchats/?session_id=<session_id> - Method:
GET - Authentication Required: Yes (Token Authentication or Session Authentication)
Authorization: Token your_auth_token_hereExample Response:
[
{
"chat_id": 101,
"start_time": "25/10/2024 12:30:00",
"end_time": "25/10/2024 13:00:00",
"session_id": "c1f8f5e1-8e42-4ef3-bb35-9a62c5271c82",
"conversation": [
{
"message_id": "670a8acf-24af-4831-8c86-3f5863437b9a",
"You": "How many planets are there in the universe",
"Bot": "seven",
"timestamp": "25/10/2024 04:20:03"
},
{
"id": "a4debb18-3fda-4bc2-afa7-ff330d339c03",
"You": "What happens when coke is mixed with icecubes?",
"Bot": "the ice cubes melt",
"timestamp": "25/10/2024 04:20:38"
}
]
},
{
"chat_id": 102,
"user_id": 1,
"start_time": "26/10/2024 09:00:00",
"end_time": null,
"session_id": "b2f8a3c9-8d2f-4f3c-bb35-9a62c5272d34",
"conversation": [
{
"message_id": "670a8acf-24af-4831-8c86-3f5863437b9a",
"You": "How many planets are there in the universe",
"Bot": "seven"
},
{
"id": "a4debb18-3fda-4bc2-afa7-ff330d339c03",
"You": "What happens when coke is mixed with icecubes?",
"Bot": "the ice cubes melt"
}
]
}
]
-
PATCH /api/chats/<chat_id>/messages/<message_id>/Request Body:
{ "content": "Updated message content" }Response:
{ "message": "Message updated successfully" } -
Delete a specific chat session based on chat_id
DELETE /api/chats/<int:chat_id>/Response:
{ "message": "Chat deleted successfully" }
-
End the Current Chat Session
POST /api/chats/Request Body:
{ "content": "xyz", "end_session": true }Response:
{ "message": "Chat session ended successfully", "start_time": "24/10/2024 10:00:00", "end_time": "24/10/2024 10:30:00", "session_id": "b7d6a8f2-7986-4820-bd29-056cff5e32ec" }
-
Time Zone Configuration
The API stores timestamps in PST time zone (
Asia/Karachi). You can change this configuration in the code where the timezone is set.
- Chat
user: ForeignKey to the User model.content: JSONField to store chat history (list of dictionaries).start_time: DateTime when the chat started.end_time: DateTime when the chat ended (nullable).session_id: Unique identifier for each chat session.
- Custom decorators for authentication and permissions (
@authenticated_view) can be used to enforce authentication on specific endpoints.
-
Run Tests
python manage.py test
- Fork the repository
- Create a new branch
- Make your changes
- Submit a pull request
This project is licensed under the MIT License.
- Common Errors
"AttributeError: 'str' object has no attribute 'append'": Ensure that thecontentfield in the database is a JSON array, not a string."JSONDecodeError": Check that the JSON being sent in the request is valid.
- Django REST Framework for providing the tools to build the API.