2023-06-17 00:36:53 +03:00
|
|
|
from uuid import UUID
|
|
|
|
|
2023-07-04 18:56:54 +03:00
|
|
|
from auth import AuthBearer, get_current_user
|
2023-07-14 22:02:26 +03:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
2023-06-17 00:36:53 +03:00
|
|
|
from logger import get_logger
|
2023-09-13 14:47:12 +03:00
|
|
|
from models import UserIdentity, UserUsage
|
2023-09-21 10:35:53 +03:00
|
|
|
from models.brain_entity import PublicBrain
|
2023-09-13 14:47:12 +03:00
|
|
|
from models.databases.supabase.brains import (
|
|
|
|
BrainQuestionRequest,
|
|
|
|
BrainUpdatableProperties,
|
|
|
|
CreateBrainProperties,
|
|
|
|
)
|
|
|
|
from repository.brain import (
|
|
|
|
create_brain,
|
|
|
|
create_brain_user,
|
2023-10-19 16:52:20 +03:00
|
|
|
delete_brain_users,
|
2023-09-13 14:47:12 +03:00
|
|
|
get_brain_details,
|
|
|
|
get_default_user_brain_or_create_new,
|
2023-10-19 16:52:20 +03:00
|
|
|
get_public_brains,
|
2023-09-13 14:47:12 +03:00
|
|
|
get_question_context_from_brain,
|
|
|
|
get_user_brains,
|
|
|
|
get_user_default_brain,
|
|
|
|
set_as_default_brain_for_user,
|
|
|
|
update_brain_by_id,
|
|
|
|
)
|
2023-08-21 15:05:13 +03:00
|
|
|
from repository.prompt import delete_prompt_by_id, get_prompt_by_id
|
|
|
|
from routes.authorizations.brain_authorization import has_brain_authorization
|
2023-08-03 11:37:13 +03:00
|
|
|
from routes.authorizations.types import RoleEnum
|
2023-07-11 11:00:06 +03:00
|
|
|
|
2023-06-17 00:36:53 +03:00
|
|
|
logger = get_logger(__name__)
|
|
|
|
brain_router = APIRouter()
|
|
|
|
|
|
|
|
|
2023-06-30 10:08:40 +03:00
|
|
|
@brain_router.get("/brains/", dependencies=[Depends(AuthBearer())], tags=["Brain"])
|
2023-10-19 16:52:20 +03:00
|
|
|
async def retrieve_all_brains_for_user(
|
2023-09-20 17:24:56 +03:00
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
|
|
|
):
|
2023-10-19 16:52:20 +03:00
|
|
|
"""Retrieve all brains for the current user."""
|
2023-08-03 11:37:13 +03:00
|
|
|
brains = get_user_brains(current_user.id)
|
2023-06-17 00:36:53 +03:00
|
|
|
return {"brains": brains}
|
|
|
|
|
2023-06-21 11:16:44 +03:00
|
|
|
|
2023-09-21 10:35:53 +03:00
|
|
|
@brain_router.get(
|
|
|
|
"/brains/public", dependencies=[Depends(AuthBearer())], tags=["Brain"]
|
|
|
|
)
|
2023-10-19 16:52:20 +03:00
|
|
|
async def retrieve_public_brains() -> list[PublicBrain]:
|
|
|
|
"""Retrieve all Quivr public brains."""
|
2023-09-21 10:35:53 +03:00
|
|
|
return get_public_brains()
|
|
|
|
|
|
|
|
|
2023-07-02 03:19:30 +03:00
|
|
|
@brain_router.get(
|
|
|
|
"/brains/default/", dependencies=[Depends(AuthBearer())], tags=["Brain"]
|
|
|
|
)
|
2023-10-19 16:52:20 +03:00
|
|
|
async def retrieve_default_brain(
|
2023-08-21 15:05:13 +03:00
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
|
|
|
):
|
2023-10-19 16:52:20 +03:00
|
|
|
"""Retrieve or create the default brain for the current user."""
|
2023-07-14 22:02:26 +03:00
|
|
|
brain = get_default_user_brain_or_create_new(current_user)
|
2023-08-03 11:37:13 +03:00
|
|
|
return {"id": brain.brain_id, "name": brain.name, "rights": "Owner"}
|
2023-06-28 20:39:27 +03:00
|
|
|
|
2023-06-30 10:08:40 +03:00
|
|
|
|
2023-06-21 11:16:44 +03:00
|
|
|
@brain_router.get(
|
2023-07-11 11:00:06 +03:00
|
|
|
"/brains/{brain_id}/",
|
2023-07-13 18:54:23 +03:00
|
|
|
dependencies=[Depends(AuthBearer()), Depends(has_brain_authorization())],
|
2023-07-11 11:00:06 +03:00
|
|
|
tags=["Brain"],
|
2023-06-21 11:16:44 +03:00
|
|
|
)
|
2023-10-19 16:52:20 +03:00
|
|
|
async def retrieve_brain_by_id(brain_id: UUID):
|
|
|
|
"""Retrieve details of a specific brain by its ID."""
|
2023-08-03 11:37:13 +03:00
|
|
|
brain_details = get_brain_details(brain_id)
|
2023-08-01 17:25:02 +03:00
|
|
|
if brain_details is None:
|
2023-10-19 16:52:20 +03:00
|
|
|
raise HTTPException(status_code=404, detail="Brain details not found")
|
2023-08-01 17:25:02 +03:00
|
|
|
return brain_details
|
|
|
|
|
2023-06-17 00:36:53 +03:00
|
|
|
|
2023-06-30 10:08:40 +03:00
|
|
|
@brain_router.post("/brains/", dependencies=[Depends(AuthBearer())], tags=["Brain"])
|
2023-10-19 16:52:20 +03:00
|
|
|
async def create_new_brain(
|
|
|
|
brain: CreateBrainProperties, current_user: UserIdentity = Depends(get_current_user)
|
2023-06-21 11:16:44 +03:00
|
|
|
):
|
2023-10-19 16:52:20 +03:00
|
|
|
"""Create a new brain for the user."""
|
2023-08-03 11:37:13 +03:00
|
|
|
user_brains = get_user_brains(current_user.id)
|
2023-10-19 16:52:20 +03:00
|
|
|
user_usage = UserUsage(
|
2023-09-13 14:47:12 +03:00
|
|
|
id=current_user.id,
|
|
|
|
email=current_user.email,
|
|
|
|
openai_api_key=current_user.openai_api_key,
|
|
|
|
)
|
2023-10-19 16:52:20 +03:00
|
|
|
user_settings = user_usage.get_user_settings()
|
2023-07-20 19:17:55 +03:00
|
|
|
|
2023-10-19 16:52:20 +03:00
|
|
|
if len(user_brains) >= user_settings.get("max_brains", 5):
|
2023-07-20 19:17:55 +03:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=429,
|
2023-10-19 16:52:20 +03:00
|
|
|
detail=f"Maximum number of brains reached ({user_settings.get('max_brains', 5)}).",
|
2023-07-20 19:17:55 +03:00
|
|
|
)
|
|
|
|
|
2023-10-19 16:52:20 +03:00
|
|
|
new_brain = create_brain(brain)
|
|
|
|
if get_user_default_brain(current_user.id):
|
2023-06-29 19:26:03 +03:00
|
|
|
logger.info(f"Default brain already exists for user {current_user.id}")
|
2023-08-03 11:37:13 +03:00
|
|
|
create_brain_user(
|
|
|
|
user_id=current_user.id,
|
|
|
|
brain_id=new_brain.brain_id,
|
|
|
|
rights=RoleEnum.Owner,
|
|
|
|
is_default_brain=False,
|
2023-07-02 03:19:30 +03:00
|
|
|
)
|
2023-06-28 20:39:27 +03:00
|
|
|
else:
|
2023-10-19 16:52:20 +03:00
|
|
|
logger.info(f"Creating default brain for user {current_user.id}.")
|
2023-08-03 11:37:13 +03:00
|
|
|
create_brain_user(
|
|
|
|
user_id=current_user.id,
|
|
|
|
brain_id=new_brain.brain_id,
|
|
|
|
rights=RoleEnum.Owner,
|
|
|
|
is_default_brain=True,
|
2023-07-02 03:19:30 +03:00
|
|
|
)
|
2023-06-29 19:26:03 +03:00
|
|
|
|
2023-10-19 16:52:20 +03:00
|
|
|
return {"id": new_brain.brain_id, "name": brain.name, "rights": "Owner"}
|
2023-06-17 00:36:53 +03:00
|
|
|
|
2023-06-30 10:08:40 +03:00
|
|
|
|
2023-06-21 11:16:44 +03:00
|
|
|
@brain_router.put(
|
2023-07-11 11:00:06 +03:00
|
|
|
"/brains/{brain_id}/",
|
|
|
|
dependencies=[
|
2023-10-19 16:52:20 +03:00
|
|
|
Depends(AuthBearer()),
|
2023-07-25 16:22:17 +03:00
|
|
|
Depends(has_brain_authorization([RoleEnum.Editor, RoleEnum.Owner])),
|
2023-07-11 11:00:06 +03:00
|
|
|
],
|
|
|
|
tags=["Brain"],
|
2023-06-21 11:16:44 +03:00
|
|
|
)
|
2023-10-19 16:52:20 +03:00
|
|
|
async def update_existing_brain(
|
|
|
|
brain_id: UUID, brain_update_data: BrainUpdatableProperties
|
2023-06-21 11:16:44 +03:00
|
|
|
):
|
2023-10-19 16:52:20 +03:00
|
|
|
"""Update an existing brain's configuration."""
|
2023-09-26 11:35:52 +03:00
|
|
|
existing_brain = get_brain_details(brain_id)
|
|
|
|
if existing_brain is None:
|
2023-10-19 16:52:20 +03:00
|
|
|
raise HTTPException(status_code=404, detail="Brain not found")
|
2023-09-26 11:35:52 +03:00
|
|
|
|
2023-10-19 16:52:20 +03:00
|
|
|
if brain_update_data.prompt_id is None and existing_brain.prompt_id:
|
|
|
|
prompt = get_prompt_by_id(existing_brain.prompt_id)
|
|
|
|
if prompt and prompt.status == "private":
|
|
|
|
delete_prompt_by_id(existing_brain.prompt_id)
|
2023-08-03 11:37:13 +03:00
|
|
|
|
2023-10-19 16:52:20 +03:00
|
|
|
if brain_update_data.status == "private" and existing_brain.status == "public":
|
2023-09-26 11:35:52 +03:00
|
|
|
delete_brain_users(brain_id)
|
|
|
|
|
2023-10-19 16:52:20 +03:00
|
|
|
update_brain_by_id(brain_id, brain_update_data)
|
2023-07-25 16:22:17 +03:00
|
|
|
return {"message": f"Brain {brain_id} has been updated."}
|
|
|
|
|
|
|
|
|
|
|
|
@brain_router.post(
|
|
|
|
"/brains/{brain_id}/default",
|
2023-10-19 16:52:20 +03:00
|
|
|
dependencies=[Depends(AuthBearer()), Depends(has_brain_authorization())],
|
2023-07-25 16:22:17 +03:00
|
|
|
tags=["Brain"],
|
|
|
|
)
|
2023-10-19 16:52:20 +03:00
|
|
|
async def set_brain_as_default(
|
|
|
|
brain_id: UUID, user: UserIdentity = Depends(get_current_user)
|
2023-07-25 16:22:17 +03:00
|
|
|
):
|
2023-10-19 16:52:20 +03:00
|
|
|
"""Set a brain as the default for the current user."""
|
2023-08-03 11:37:13 +03:00
|
|
|
set_as_default_brain_for_user(user.id, brain_id)
|
2023-07-25 16:22:17 +03:00
|
|
|
return {"message": f"Brain {brain_id} has been set as default brain."}
|
2023-08-27 10:38:41 +03:00
|
|
|
|
|
|
|
|
|
|
|
@brain_router.post(
|
|
|
|
"/brains/{brain_id}/question_context",
|
2023-10-19 16:52:20 +03:00
|
|
|
dependencies=[Depends(AuthBearer()), Depends(has_brain_authorization())],
|
2023-08-27 10:38:41 +03:00
|
|
|
tags=["Brain"],
|
|
|
|
)
|
2023-10-19 16:52:20 +03:00
|
|
|
async def get_question_context_for_brain(brain_id: UUID, request: BrainQuestionRequest):
|
|
|
|
"""Retrieve the question context from a specific brain."""
|
2023-08-27 10:38:41 +03:00
|
|
|
context = get_question_context_from_brain(brain_id, request.question)
|
|
|
|
return {"context": context}
|