2023-06-11 00:59:16 +03:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
from uuid import UUID
|
|
|
|
|
2023-06-14 22:21:13 +03:00
|
|
|
from auth.auth_bearer import AuthBearer, get_current_user
|
2023-06-11 00:59:16 +03:00
|
|
|
from fastapi import APIRouter, Depends, Request
|
|
|
|
from models.chats import ChatMessage
|
2023-06-19 23:46:25 +03:00
|
|
|
from models.settings import CommonsDep, common_dependencies
|
2023-06-11 00:59:16 +03:00
|
|
|
from models.users import User
|
2023-06-17 00:36:53 +03:00
|
|
|
from utils.chats import (create_chat, get_chat_name_from_first_question,
|
|
|
|
update_chat)
|
|
|
|
from utils.users import (create_user, fetch_user_id_from_credentials,
|
|
|
|
update_user_request_count)
|
|
|
|
from utils.vectors import get_answer
|
2023-06-11 00:59:16 +03:00
|
|
|
|
|
|
|
chat_router = APIRouter()
|
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
def get_user_chats(commons, user_id):
|
|
|
|
response = commons['supabase'].from_('chats').select('chatId:chat_id, chatName:chat_name').filter("user_id", "eq", user_id).execute()
|
|
|
|
return response.data
|
|
|
|
|
|
|
|
def get_chat_details(commons, chat_id):
|
|
|
|
response = commons['supabase'].from_('chats').select('*').filter("chat_id", "eq", chat_id).execute()
|
|
|
|
return response.data
|
|
|
|
|
|
|
|
def delete_chat_from_db(commons, chat_id):
|
|
|
|
commons['supabase'].table("chats").delete().match({"chat_id": chat_id}).execute()
|
|
|
|
|
|
|
|
def fetch_user_stats(commons, user, date):
|
|
|
|
response = commons['supabase'].from_('users').select('*').filter("email", "eq", user.email).filter("date", "eq", date).execute()
|
|
|
|
userItem = next(iter(response.data or []), {"requests_count": 0})
|
|
|
|
return userItem
|
|
|
|
|
2023-06-11 00:59:16 +03:00
|
|
|
# get all chats
|
2023-06-15 15:43:40 +03:00
|
|
|
@chat_router.get("/chat", dependencies=[Depends(AuthBearer())], tags=["Chat"])
|
2023-06-19 23:46:25 +03:00
|
|
|
async def get_chats(current_user: User = Depends(get_current_user)):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Retrieve all chats for the current user.
|
|
|
|
|
|
|
|
- `current_user`: The current authenticated user.
|
|
|
|
- Returns a list of all chats for the user.
|
|
|
|
|
|
|
|
This endpoint retrieves all the chats associated with the current authenticated user. It returns a list of chat objects
|
|
|
|
containing the chat ID and chat name for each chat.
|
|
|
|
"""
|
2023-06-19 23:46:25 +03:00
|
|
|
commons = common_dependencies()
|
2023-06-17 00:36:53 +03:00
|
|
|
user_id = fetch_user_id_from_credentials(commons, {"email": current_user.email})
|
2023-06-12 18:58:05 +03:00
|
|
|
chats = get_user_chats(commons, user_id)
|
2023-06-11 00:59:16 +03:00
|
|
|
return {"chats": chats}
|
|
|
|
|
|
|
|
# get one chat
|
2023-06-15 15:43:40 +03:00
|
|
|
@chat_router.get("/chat/{chat_id}", dependencies=[Depends(AuthBearer())], tags=["Chat"])
|
2023-06-19 23:46:25 +03:00
|
|
|
async def get_chats( chat_id: UUID):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Retrieve details of a specific chat by chat ID.
|
|
|
|
|
|
|
|
- `chat_id`: The ID of the chat to retrieve details for.
|
|
|
|
- Returns the chat ID and its history.
|
|
|
|
|
|
|
|
This endpoint retrieves the details of a specific chat identified by the provided chat ID. It returns the chat ID and its
|
|
|
|
history, which includes the chat messages exchanged in the chat.
|
|
|
|
"""
|
2023-06-19 23:46:25 +03:00
|
|
|
commons = common_dependencies()
|
2023-06-12 18:58:05 +03:00
|
|
|
chats = get_chat_details(commons, chat_id)
|
|
|
|
if len(chats) > 0:
|
|
|
|
return {"chatId": chat_id, "history": chats[0]['history']}
|
|
|
|
else:
|
|
|
|
return {"error": "Chat not found"}
|
2023-06-11 00:59:16 +03:00
|
|
|
|
|
|
|
# delete one chat
|
2023-06-15 15:43:40 +03:00
|
|
|
@chat_router.delete("/chat/{chat_id}", dependencies=[Depends(AuthBearer())], tags=["Chat"])
|
2023-06-19 23:46:25 +03:00
|
|
|
async def delete_chat( chat_id: UUID):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Delete a specific chat by chat ID.
|
|
|
|
"""
|
2023-06-19 23:46:25 +03:00
|
|
|
commons = common_dependencies()
|
2023-06-12 18:58:05 +03:00
|
|
|
delete_chat_from_db(commons, chat_id)
|
2023-06-11 00:59:16 +03:00
|
|
|
return {"message": f"{chat_id} has been deleted."}
|
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
# helper method for update and create chat
|
|
|
|
def chat_handler(request, commons, chat_id, chat_message, email, is_new_chat=False):
|
2023-06-11 00:59:16 +03:00
|
|
|
date = time.strftime("%Y%m%d")
|
2023-06-17 00:36:53 +03:00
|
|
|
user_id = fetch_user_id_from_credentials(commons, {"email": email})
|
2023-06-11 00:59:16 +03:00
|
|
|
max_requests_number = os.getenv("MAX_REQUESTS_NUMBER")
|
|
|
|
user_openai_api_key = request.headers.get('Openai-Api-Key')
|
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
userItem = fetch_user_stats(commons, User(email=email), date)
|
2023-06-11 00:59:16 +03:00
|
|
|
old_request_count = userItem['requests_count']
|
|
|
|
|
|
|
|
history = chat_message.history
|
|
|
|
history.append(("user", chat_message.question))
|
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
|
2023-06-11 00:59:16 +03:00
|
|
|
if old_request_count == 0:
|
2023-06-17 00:36:53 +03:00
|
|
|
create_user(commons, email= email, date=date)
|
2023-06-12 18:58:05 +03:00
|
|
|
else:
|
2023-06-17 00:36:53 +03:00
|
|
|
update_user_request_count(commons,email, date, requests_count=old_request_count + 1)
|
2023-06-12 18:58:05 +03:00
|
|
|
if user_openai_api_key is None and old_request_count >= float(max_requests_number):
|
2023-06-11 00:59:16 +03:00
|
|
|
history.append(('assistant', "You have reached your requests limit"))
|
2023-06-17 00:36:53 +03:00
|
|
|
update_chat(commons, chat_id=chat_id, history=history)
|
2023-06-12 18:58:05 +03:00
|
|
|
return {"history": history}
|
2023-06-11 00:59:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
answer = get_answer(commons, chat_message, email, user_openai_api_key)
|
|
|
|
history.append(("assistant", answer))
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
if is_new_chat:
|
|
|
|
chat_name = get_chat_name_from_first_question(chat_message)
|
2023-06-17 00:36:53 +03:00
|
|
|
new_chat = create_chat(commons, user_id, history, chat_name)
|
2023-06-12 18:58:05 +03:00
|
|
|
chat_id = new_chat.data[0]['chat_id']
|
|
|
|
else:
|
2023-06-17 00:36:53 +03:00
|
|
|
update_chat(commons, chat_id=chat_id, history=history)
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
return {"history": history, "chatId": chat_id}
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
# update existing chat
|
2023-06-15 15:43:40 +03:00
|
|
|
@chat_router.put("/chat/{chat_id}", dependencies=[Depends(AuthBearer())], tags=["Chat"])
|
2023-06-12 18:58:05 +03:00
|
|
|
async def chat_endpoint(request: Request, commons: CommonsDep, chat_id: UUID, chat_message: ChatMessage, current_user: User = Depends(get_current_user)):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Update an existing chat with new chat messages.
|
|
|
|
"""
|
2023-06-12 18:58:05 +03:00
|
|
|
return chat_handler(request, commons, chat_id, chat_message, current_user.email)
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
# create new chat
|
2023-06-15 15:43:40 +03:00
|
|
|
@chat_router.post("/chat", dependencies=[Depends(AuthBearer())], tags=["Chat"])
|
2023-06-12 18:58:05 +03:00
|
|
|
async def chat_endpoint(request: Request, commons: CommonsDep, chat_message: ChatMessage, current_user: User = Depends(get_current_user)):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Create a new chat with initial chat messages.
|
|
|
|
"""
|
2023-06-12 18:58:05 +03:00
|
|
|
return chat_handler(request, commons, None, chat_message, current_user.email, is_new_chat=True)
|