2023-12-04 20:38:54 +03:00
|
|
|
from models.settings import get_supabase_client
|
|
|
|
from modules.chat.entity.chat import Chat
|
|
|
|
from modules.chat.repository.chats_interface import ChatsInterface
|
2023-08-10 11:25:08 +03:00
|
|
|
|
|
|
|
|
2023-12-04 20:38:54 +03:00
|
|
|
class Chats(ChatsInterface):
|
|
|
|
def __init__(self):
|
|
|
|
supabase_client = get_supabase_client()
|
2023-08-02 00:03:47 +03:00
|
|
|
self.db = supabase_client
|
|
|
|
|
|
|
|
def create_chat(self, new_chat):
|
|
|
|
response = self.db.table("chats").insert(new_chat).execute()
|
|
|
|
return response
|
|
|
|
|
|
|
|
def get_chat_by_id(self, chat_id: str):
|
|
|
|
response = (
|
|
|
|
self.db.from_("chats")
|
|
|
|
.select("*")
|
|
|
|
.filter("chat_id", "eq", chat_id)
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
2023-12-04 20:38:54 +03:00
|
|
|
def add_question_and_answer(self, chat_id, question_and_answer):
|
2023-10-12 10:39:56 +03:00
|
|
|
response = (
|
|
|
|
self.db.table("chat_history")
|
|
|
|
.insert(
|
|
|
|
{
|
|
|
|
"chat_id": str(chat_id),
|
|
|
|
"user_message": question_and_answer.question,
|
|
|
|
"assistant": question_and_answer.answer,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.execute()
|
|
|
|
).data
|
|
|
|
if len(response) > 0:
|
|
|
|
response = Chat(response[0])
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2023-08-02 00:03:47 +03:00
|
|
|
def get_chat_history(self, chat_id: str):
|
|
|
|
reponse = (
|
|
|
|
self.db.from_("chat_history")
|
|
|
|
.select("*")
|
|
|
|
.filter("chat_id", "eq", chat_id)
|
|
|
|
.order("message_time", desc=False) # Add the ORDER BY clause
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
|
|
|
|
return reponse
|
|
|
|
|
2023-12-04 20:38:54 +03:00
|
|
|
def get_user_chats(self, user_id):
|
2023-08-02 00:03:47 +03:00
|
|
|
response = (
|
|
|
|
self.db.from_("chats")
|
|
|
|
.select("chat_id,user_id,creation_time,chat_name")
|
|
|
|
.filter("user_id", "eq", user_id)
|
2023-09-18 19:25:28 +03:00
|
|
|
.order("creation_time", desc=False)
|
2023-08-02 00:03:47 +03:00
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
2023-12-04 20:38:54 +03:00
|
|
|
def update_chat_history(self, chat_history):
|
2023-08-02 00:03:47 +03:00
|
|
|
response = (
|
|
|
|
self.db.table("chat_history")
|
|
|
|
.insert(
|
|
|
|
{
|
2023-08-10 11:25:08 +03:00
|
|
|
"chat_id": str(chat_history.chat_id),
|
|
|
|
"user_message": chat_history.user_message,
|
|
|
|
"assistant": chat_history.assistant,
|
|
|
|
"prompt_id": str(chat_history.prompt_id)
|
|
|
|
if chat_history.prompt_id
|
|
|
|
else None,
|
|
|
|
"brain_id": str(chat_history.brain_id)
|
|
|
|
if chat_history.brain_id
|
|
|
|
else None,
|
2023-08-02 00:03:47 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
def update_chat(self, chat_id, updates):
|
|
|
|
response = (
|
|
|
|
self.db.table("chats").update(updates).match({"chat_id": chat_id}).execute()
|
|
|
|
)
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
def update_message_by_id(self, message_id, updates):
|
|
|
|
response = (
|
|
|
|
self.db.table("chat_history")
|
|
|
|
.update(updates)
|
|
|
|
.match({"message_id": message_id})
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
def delete_chat(self, chat_id):
|
2023-08-03 13:15:33 +03:00
|
|
|
self.db.table("chats").delete().match({"chat_id": chat_id}).execute()
|
2023-08-02 00:03:47 +03:00
|
|
|
|
|
|
|
def delete_chat_history(self, chat_id):
|
|
|
|
self.db.table("chat_history").delete().match({"chat_id": chat_id}).execute()
|