2023-09-20 10:35:37 +03:00
|
|
|
from fastapi import HTTPException
|
2023-11-29 11:04:03 +03:00
|
|
|
from models.settings import get_supabase_client
|
|
|
|
from modules.knowledge.dto.outputs import DeleteKnowledgeResponse
|
|
|
|
from modules.knowledge.entity.knowledge import Knowledge
|
|
|
|
from modules.knowledge.repository.knowledge_interface import KnowledgeInterface
|
2023-09-20 10:35:37 +03:00
|
|
|
|
|
|
|
|
2023-11-29 11:04:03 +03:00
|
|
|
class Knowledges(KnowledgeInterface):
|
|
|
|
def __init__(self):
|
|
|
|
supabase_client = get_supabase_client()
|
2023-09-20 10:35:37 +03:00
|
|
|
self.db = supabase_client
|
|
|
|
|
2023-11-29 11:04:03 +03:00
|
|
|
def insert_knowledge(self, knowledge):
|
2023-09-20 10:35:37 +03:00
|
|
|
"""
|
|
|
|
Add a knowledge
|
|
|
|
"""
|
|
|
|
response = (self.db.from_("knowledge").insert(knowledge.dict()).execute()).data
|
|
|
|
return Knowledge(**response[0])
|
|
|
|
|
|
|
|
def remove_knowledge_by_id(
|
2023-09-22 17:06:04 +03:00
|
|
|
# todo: update remove brain endpoints to first delete the knowledge
|
2023-09-20 10:35:37 +03:00
|
|
|
self,
|
2023-11-29 11:04:03 +03:00
|
|
|
knowledge_id,
|
|
|
|
):
|
2023-09-20 10:35:37 +03:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
knowledge_id (UUID): The id of the knowledge
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: Status message
|
|
|
|
"""
|
|
|
|
response = (
|
|
|
|
self.db.from_("knowledge")
|
|
|
|
.delete()
|
|
|
|
.filter("id", "eq", knowledge_id)
|
|
|
|
.execute()
|
|
|
|
.data
|
|
|
|
)
|
|
|
|
|
|
|
|
if response == []:
|
|
|
|
raise HTTPException(404, "Knowledge not found")
|
|
|
|
|
|
|
|
return DeleteKnowledgeResponse(
|
|
|
|
# change to response[0].brain_id and knowledge_id[0].brain_id
|
|
|
|
status="deleted",
|
|
|
|
knowledge_id=knowledge_id,
|
|
|
|
)
|
|
|
|
|
2023-11-29 11:04:03 +03:00
|
|
|
def get_knowledge_by_id(self, knowledge_id):
|
2023-09-20 10:35:37 +03:00
|
|
|
"""
|
|
|
|
Get a knowledge by its id
|
|
|
|
Args:
|
|
|
|
brain_id (UUID): The id of the brain
|
|
|
|
"""
|
|
|
|
knowledge = (
|
|
|
|
self.db.from_("knowledge")
|
|
|
|
.select("*")
|
2023-09-22 17:06:04 +03:00
|
|
|
.filter("id", "eq", str(knowledge_id))
|
2023-09-20 10:35:37 +03:00
|
|
|
.execute()
|
|
|
|
).data
|
|
|
|
|
|
|
|
return Knowledge(**knowledge[0])
|
2023-09-22 17:06:04 +03:00
|
|
|
|
2023-11-29 11:04:03 +03:00
|
|
|
def get_all_knowledge_in_brain(self, brain_id):
|
2023-09-22 17:06:04 +03:00
|
|
|
"""
|
|
|
|
Get all the knowledge in a brain
|
|
|
|
Args:
|
|
|
|
brain_id (UUID): The id of the brain
|
|
|
|
"""
|
|
|
|
all_knowledge = (
|
|
|
|
self.db.from_("knowledge")
|
|
|
|
.select("*")
|
|
|
|
.filter("brain_id", "eq", str(brain_id))
|
|
|
|
.execute()
|
|
|
|
).data
|
|
|
|
|
2023-11-06 18:58:03 +03:00
|
|
|
return [Knowledge(**knowledge) for knowledge in all_knowledge]
|
|
|
|
|
2023-11-29 11:04:03 +03:00
|
|
|
def remove_brain_all_knowledge(self, brain_id):
|
2023-11-06 18:58:03 +03:00
|
|
|
"""
|
|
|
|
Remove all knowledge in a brain
|
|
|
|
Args:
|
|
|
|
brain_id (UUID): The id of the brain
|
|
|
|
"""
|
|
|
|
all_knowledge = self.get_all_knowledge_in_brain(brain_id)
|
|
|
|
knowledge_to_delete_list = []
|
|
|
|
|
|
|
|
for knowledge in all_knowledge:
|
|
|
|
if knowledge.file_name:
|
|
|
|
knowledge_to_delete_list.append(f"{brain_id}/{knowledge.file_name}")
|
|
|
|
|
2023-11-13 17:02:10 +03:00
|
|
|
if knowledge_to_delete_list:
|
|
|
|
self.db.storage.from_("quivr").remove(knowledge_to_delete_list)
|
2023-11-06 18:58:03 +03:00
|
|
|
|
|
|
|
self.db.from_("knowledge").delete().filter(
|
|
|
|
"brain_id", "eq", str(brain_id)
|
|
|
|
).execute()
|