2023-08-10 11:25:08 +03:00
|
|
|
from typing import List, Optional
|
|
|
|
from uuid import UUID
|
2023-07-10 15:27:49 +03:00
|
|
|
|
2023-08-21 13:25:16 +03:00
|
|
|
from models import ChatHistory, get_supabase_db
|
2023-08-10 11:25:08 +03:00
|
|
|
from pydantic import BaseModel
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-08-21 13:25:16 +03:00
|
|
|
from repository.brain import get_brain_by_id
|
|
|
|
from repository.prompt import get_prompt_by_id
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-08-10 11:25:08 +03:00
|
|
|
|
|
|
|
class GetChatHistoryOutput(BaseModel):
|
|
|
|
chat_id: UUID
|
|
|
|
message_id: UUID
|
|
|
|
user_message: str
|
|
|
|
assistant: str
|
|
|
|
message_time: str
|
|
|
|
prompt_title: Optional[str] | None
|
|
|
|
brain_name: Optional[str] | None
|
|
|
|
|
|
|
|
def dict(self, *args, **kwargs):
|
|
|
|
chat_history = super().dict(*args, **kwargs)
|
2023-08-10 19:35:30 +03:00
|
|
|
chat_history["chat_id"] = str(chat_history.get("chat_id"))
|
2023-08-10 11:25:08 +03:00
|
|
|
chat_history["message_id"] = str(chat_history.get("message_id"))
|
|
|
|
|
|
|
|
return chat_history
|
|
|
|
|
|
|
|
|
|
|
|
def get_chat_history(chat_id: str) -> List[GetChatHistoryOutput]:
|
2023-08-03 21:24:42 +03:00
|
|
|
supabase_db = get_supabase_db()
|
2023-08-10 11:25:08 +03:00
|
|
|
history: List[dict] = supabase_db.get_chat_history(chat_id).data
|
2023-06-22 18:50:06 +03:00
|
|
|
if history is None:
|
|
|
|
return []
|
|
|
|
else:
|
2023-08-10 11:25:08 +03:00
|
|
|
enriched_history: List[GetChatHistoryOutput] = []
|
|
|
|
for message in history:
|
|
|
|
message = ChatHistory(message)
|
|
|
|
brain = None
|
|
|
|
if message.brain_id:
|
|
|
|
brain = get_brain_by_id(message.brain_id)
|
|
|
|
|
|
|
|
prompt = None
|
|
|
|
if message.prompt_id:
|
|
|
|
prompt = get_prompt_by_id(message.prompt_id)
|
|
|
|
|
|
|
|
enriched_history.append(
|
|
|
|
GetChatHistoryOutput(
|
|
|
|
chat_id=(UUID(message.chat_id)),
|
|
|
|
message_id=(UUID(message.message_id)),
|
|
|
|
user_message=message.user_message,
|
|
|
|
assistant=message.assistant,
|
|
|
|
message_time=message.message_time,
|
|
|
|
brain_name=brain.name if brain else None,
|
|
|
|
prompt_title=prompt.title if prompt else None,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return enriched_history
|