2023-09-20 10:35:37 +03:00
|
|
|
from uuid import UUID
|
|
|
|
|
2023-11-27 18:47:13 +03:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
2023-09-20 10:35:37 +03:00
|
|
|
from logger import get_logger
|
2023-11-14 16:31:02 +03:00
|
|
|
from middlewares.auth import AuthBearer, get_current_user
|
2023-12-01 00:29:28 +03:00
|
|
|
from modules.brain.entity.brain_entity import RoleEnum
|
|
|
|
from modules.brain.service.brain_authorization_service import (
|
2023-09-20 10:35:37 +03:00
|
|
|
has_brain_authorization,
|
|
|
|
validate_brain_authorization,
|
|
|
|
)
|
2023-12-01 00:29:28 +03:00
|
|
|
from modules.brain.service.brain_vector_service import BrainVectorService
|
|
|
|
from modules.knowledge.service.knowledge_service import KnowledgeService
|
|
|
|
from modules.user.entity.user_identity import UserIdentity
|
2024-04-20 11:42:35 +03:00
|
|
|
from repository.files.generate_file_signed_url import generate_file_signed_url
|
2023-09-20 10:35:37 +03:00
|
|
|
|
|
|
|
knowledge_router = APIRouter()
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
2023-11-29 11:04:03 +03:00
|
|
|
knowledge_service = KnowledgeService()
|
|
|
|
|
2023-09-20 10:35:37 +03:00
|
|
|
|
|
|
|
@knowledge_router.get(
|
2023-09-25 13:13:48 +03:00
|
|
|
"/knowledge", dependencies=[Depends(AuthBearer())], tags=["Knowledge"]
|
2023-09-20 10:35:37 +03:00
|
|
|
)
|
|
|
|
async def list_knowledge_in_brain_endpoint(
|
|
|
|
brain_id: UUID = Query(..., description="The ID of the brain"),
|
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Retrieve and list all the knowledge in a brain.
|
|
|
|
"""
|
|
|
|
|
|
|
|
validate_brain_authorization(brain_id=brain_id, user_id=current_user.id)
|
|
|
|
|
2023-11-29 11:04:03 +03:00
|
|
|
knowledges = knowledge_service.get_all_knowledge(brain_id)
|
2023-09-20 10:35:37 +03:00
|
|
|
|
2023-09-22 17:06:04 +03:00
|
|
|
return {"knowledges": knowledges}
|
2023-09-20 10:35:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
@knowledge_router.delete(
|
2023-09-25 13:13:48 +03:00
|
|
|
"/knowledge/{knowledge_id}",
|
2023-09-20 10:35:37 +03:00
|
|
|
dependencies=[
|
|
|
|
Depends(AuthBearer()),
|
|
|
|
Depends(has_brain_authorization(RoleEnum.Owner)),
|
|
|
|
],
|
|
|
|
tags=["Knowledge"],
|
|
|
|
)
|
|
|
|
async def delete_endpoint(
|
2023-09-22 17:06:04 +03:00
|
|
|
knowledge_id: UUID,
|
2023-09-20 10:35:37 +03:00
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
|
|
|
brain_id: UUID = Query(..., description="The ID of the brain"),
|
|
|
|
):
|
|
|
|
"""
|
2023-09-22 17:06:04 +03:00
|
|
|
Delete a specific knowledge from a brain.
|
2023-09-20 10:35:37 +03:00
|
|
|
"""
|
|
|
|
|
2023-11-29 11:04:03 +03:00
|
|
|
knowledge = knowledge_service.get_knowledge(knowledge_id)
|
2023-09-22 17:06:04 +03:00
|
|
|
file_name = knowledge.file_name if knowledge.file_name else knowledge.url
|
2023-11-29 11:04:03 +03:00
|
|
|
knowledge_service.remove_knowledge(knowledge_id)
|
2023-09-22 17:06:04 +03:00
|
|
|
|
2023-12-01 00:29:28 +03:00
|
|
|
brain_vector_service = BrainVectorService(brain_id)
|
2023-09-22 17:06:04 +03:00
|
|
|
if knowledge.file_name:
|
2023-12-01 00:29:28 +03:00
|
|
|
brain_vector_service.delete_file_from_brain(knowledge.file_name)
|
2023-09-22 17:06:04 +03:00
|
|
|
elif knowledge.url:
|
2023-12-01 00:29:28 +03:00
|
|
|
brain_vector_service.delete_file_url_from_brain(knowledge.url)
|
2023-09-20 10:35:37 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
"message": f"{file_name} of brain {brain_id} has been deleted by user {current_user.email}."
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@knowledge_router.get(
|
2023-09-22 17:06:04 +03:00
|
|
|
"/knowledge/{knowledge_id}/signed_download_url",
|
2023-09-20 10:35:37 +03:00
|
|
|
dependencies=[Depends(AuthBearer())],
|
|
|
|
tags=["Knowledge"],
|
|
|
|
)
|
|
|
|
async def generate_signed_url_endpoint(
|
2023-09-22 17:06:04 +03:00
|
|
|
knowledge_id: UUID,
|
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
2023-09-20 10:35:37 +03:00
|
|
|
):
|
|
|
|
"""
|
|
|
|
Generate a signed url to download the file from storage.
|
|
|
|
"""
|
|
|
|
|
2023-11-29 11:04:03 +03:00
|
|
|
knowledge = knowledge_service.get_knowledge(knowledge_id)
|
2023-09-20 10:35:37 +03:00
|
|
|
|
2023-09-22 17:06:04 +03:00
|
|
|
validate_brain_authorization(brain_id=knowledge.brain_id, user_id=current_user.id)
|
2023-09-20 10:35:37 +03:00
|
|
|
|
2023-09-22 17:06:04 +03:00
|
|
|
if knowledge.file_name == None:
|
2023-11-27 18:47:13 +03:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=404,
|
|
|
|
detail=f"Knowledge with id {knowledge_id} is not a file.",
|
|
|
|
)
|
2023-09-20 10:35:37 +03:00
|
|
|
|
2023-09-22 17:06:04 +03:00
|
|
|
file_path_in_storage = f"{knowledge.brain_id}/{knowledge.file_name}"
|
2023-09-20 10:35:37 +03:00
|
|
|
|
|
|
|
file_signed_url = generate_file_signed_url(file_path_in_storage)
|
|
|
|
|
|
|
|
return file_signed_url
|