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-07-18 10:47:59 +03:00
|
|
|
from models.brains import (Brain, get_default_user_brain,
|
|
|
|
get_default_user_brain_or_create_new)
|
2023-06-22 18:50:06 +03:00
|
|
|
from models.settings import common_dependencies
|
2023-06-17 00:36:53 +03:00
|
|
|
from models.users import User
|
2023-07-14 22:02:26 +03:00
|
|
|
from routes.authorizations.brain_authorization import has_brain_authorization
|
2023-07-11 11:00:06 +03:00
|
|
|
|
2023-06-17 00:36:53 +03:00
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
brain_router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
# get all brains
|
2023-06-30 10:08:40 +03:00
|
|
|
@brain_router.get("/brains/", dependencies=[Depends(AuthBearer())], tags=["Brain"])
|
2023-06-20 17:17:13 +03:00
|
|
|
async def brain_endpoint(current_user: User = Depends(get_current_user)):
|
2023-06-17 00:36:53 +03:00
|
|
|
"""
|
|
|
|
Retrieve all brains for the current user.
|
|
|
|
|
|
|
|
- `current_user`: The current authenticated user.
|
|
|
|
- Returns a list of all brains registered for the user.
|
|
|
|
|
|
|
|
This endpoint retrieves all the brains associated with the current authenticated user. It returns a list of brains objects
|
|
|
|
containing the brain ID and brain name for each brain.
|
|
|
|
"""
|
2023-06-21 11:16:44 +03:00
|
|
|
brain = Brain()
|
2023-06-28 20:39:27 +03:00
|
|
|
brains = brain.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-07-14 22:02:26 +03:00
|
|
|
# get default brain
|
2023-07-02 03:19:30 +03:00
|
|
|
@brain_router.get(
|
|
|
|
"/brains/default/", dependencies=[Depends(AuthBearer())], tags=["Brain"]
|
|
|
|
)
|
2023-06-28 20:39:27 +03:00
|
|
|
async def get_default_brain_endpoint(current_user: User = Depends(get_current_user)):
|
|
|
|
"""
|
2023-07-04 18:56:54 +03:00
|
|
|
Retrieve the default brain for the current user. If the user doesnt have one, it creates one.
|
2023-06-28 20:39:27 +03:00
|
|
|
|
|
|
|
- `current_user`: The current authenticated user.
|
|
|
|
- Returns the default brain for the user.
|
|
|
|
|
2023-07-02 03:19:30 +03:00
|
|
|
This endpoint retrieves the default brain associated with the current authenticated user.
|
2023-06-28 20:39:27 +03:00
|
|
|
The default brain is defined as the brain marked as default in the brains_users table.
|
|
|
|
"""
|
|
|
|
|
2023-07-14 22:02:26 +03:00
|
|
|
brain = get_default_user_brain_or_create_new(current_user)
|
|
|
|
return {
|
|
|
|
"id": brain.id,
|
|
|
|
"name": brain.name,
|
|
|
|
}
|
2023-06-28 20:39:27 +03:00
|
|
|
|
2023-06-30 10:08:40 +03:00
|
|
|
|
2023-07-14 22:02:26 +03:00
|
|
|
# get one brain - Currently not used in FE
|
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-07-11 11:00:06 +03:00
|
|
|
async def get_brain_endpoint(
|
|
|
|
brain_id: UUID,
|
|
|
|
):
|
2023-06-17 00:36:53 +03:00
|
|
|
"""
|
|
|
|
Retrieve details of a specific brain by brain ID.
|
|
|
|
|
|
|
|
- `brain_id`: The ID of the brain to retrieve details for.
|
|
|
|
- Returns the brain ID and its history.
|
|
|
|
|
|
|
|
This endpoint retrieves the details of a specific brain identified by the provided brain ID. It returns the brain ID and its
|
|
|
|
history, which includes the brain messages exchanged in the brain.
|
|
|
|
"""
|
2023-06-28 20:39:27 +03:00
|
|
|
brain = Brain(id=brain_id)
|
2023-06-21 11:16:44 +03:00
|
|
|
brains = brain.get_brain_details()
|
2023-06-17 00:36:53 +03:00
|
|
|
if len(brains) > 0:
|
2023-06-21 11:16:44 +03:00
|
|
|
return {
|
2023-07-14 22:02:26 +03:00
|
|
|
"id": brain_id,
|
|
|
|
"name": brains[0]["name"],
|
2023-06-21 11:16:44 +03:00
|
|
|
"status": brains[0]["status"],
|
|
|
|
}
|
2023-06-17 00:36:53 +03:00
|
|
|
else:
|
2023-07-14 22:02:26 +03:00
|
|
|
return HTTPException(
|
|
|
|
status_code=404,
|
|
|
|
detail="Brain not found",
|
|
|
|
)
|
2023-06-21 11:16:44 +03:00
|
|
|
|
2023-06-17 00:36:53 +03:00
|
|
|
|
|
|
|
# create new brain
|
2023-06-30 10:08:40 +03:00
|
|
|
@brain_router.post("/brains/", dependencies=[Depends(AuthBearer())], tags=["Brain"])
|
|
|
|
async def create_brain_endpoint(
|
2023-07-14 22:02:26 +03:00
|
|
|
brain: Brain,
|
2023-06-21 11:16:44 +03:00
|
|
|
current_user: User = Depends(get_current_user),
|
|
|
|
):
|
2023-06-17 00:36:53 +03:00
|
|
|
"""
|
2023-06-21 11:16:44 +03:00
|
|
|
Create a new brain with given
|
2023-06-20 17:17:13 +03:00
|
|
|
name
|
2023-06-17 00:36:53 +03:00
|
|
|
status
|
|
|
|
model
|
|
|
|
max_tokens
|
|
|
|
temperature
|
|
|
|
In the brains table & in the brains_users table and put the creator user as 'Owner'
|
|
|
|
"""
|
|
|
|
|
2023-07-10 15:27:49 +03:00
|
|
|
brain = Brain(name=brain.name) # pyright: ignore reportPrivateUsage=none
|
2023-07-02 03:19:30 +03:00
|
|
|
|
2023-07-10 15:27:49 +03:00
|
|
|
brain.create_brain() # pyright: ignore reportPrivateUsage=none
|
2023-06-28 20:39:27 +03:00
|
|
|
default_brain = get_default_user_brain(current_user)
|
|
|
|
if default_brain:
|
2023-06-29 19:26:03 +03:00
|
|
|
logger.info(f"Default brain already exists for user {current_user.id}")
|
2023-07-10 15:27:49 +03:00
|
|
|
brain.create_brain_user( # pyright: ignore reportPrivateUsage=none
|
2023-07-02 03:19:30 +03:00
|
|
|
user_id=current_user.id, rights="Owner", default_brain=False
|
|
|
|
)
|
2023-06-28 20:39:27 +03:00
|
|
|
else:
|
2023-07-02 03:19:30 +03:00
|
|
|
logger.info(
|
|
|
|
f"Default brain does not exist for user {current_user.id}. It will be created."
|
|
|
|
)
|
2023-07-10 15:27:49 +03:00
|
|
|
brain.create_brain_user( # pyright: ignore reportPrivateUsage=none
|
2023-07-02 03:19:30 +03:00
|
|
|
user_id=current_user.id, rights="Owner", default_brain=True
|
|
|
|
)
|
2023-06-29 19:26:03 +03:00
|
|
|
|
2023-07-10 15:27:49 +03:00
|
|
|
return {
|
|
|
|
"id": brain.id, # pyright: ignore reportPrivateUsage=none
|
|
|
|
"name": brain.name,
|
|
|
|
}
|
2023-06-17 00:36:53 +03:00
|
|
|
|
2023-06-30 10:08:40 +03:00
|
|
|
|
2023-06-17 00:36:53 +03:00
|
|
|
# update existing brain
|
2023-06-21 11:16:44 +03:00
|
|
|
@brain_router.put(
|
2023-07-11 11:00:06 +03:00
|
|
|
"/brains/{brain_id}/",
|
|
|
|
dependencies=[
|
|
|
|
Depends(
|
|
|
|
AuthBearer(),
|
|
|
|
),
|
2023-07-13 18:54:23 +03:00
|
|
|
Depends(has_brain_authorization()),
|
2023-07-11 11:00:06 +03:00
|
|
|
],
|
|
|
|
tags=["Brain"],
|
2023-06-21 11:16:44 +03:00
|
|
|
)
|
2023-06-22 18:50:06 +03:00
|
|
|
async def update_brain_endpoint(
|
2023-06-21 11:16:44 +03:00
|
|
|
brain_id: UUID,
|
|
|
|
input_brain: Brain,
|
|
|
|
):
|
2023-06-17 00:36:53 +03:00
|
|
|
"""
|
|
|
|
Update an existing brain with new brain parameters/files.
|
2023-06-21 11:16:44 +03:00
|
|
|
If the file is contained in Add file to brain :
|
|
|
|
if given a fileName/ file sha1 / -> add all the vector Ids to the brains_vectors
|
|
|
|
Modify other brain fields:
|
|
|
|
name, status, model, max_tokens, temperature
|
|
|
|
Return modified brain ? No need -> do an optimistic update
|
2023-06-17 00:36:53 +03:00
|
|
|
"""
|
2023-06-19 23:54:01 +03:00
|
|
|
commons = common_dependencies()
|
2023-06-28 20:39:27 +03:00
|
|
|
brain = Brain(id=brain_id)
|
2023-06-21 11:16:44 +03:00
|
|
|
|
2023-06-17 00:36:53 +03:00
|
|
|
# Add new file to brain , il file_sha1 already exists in brains_vectors -> out (not now)
|
2023-07-10 15:27:49 +03:00
|
|
|
if brain.file_sha1: # pyright: ignore reportPrivateUsage=none
|
2023-06-17 00:36:53 +03:00
|
|
|
# add all the vector Ids to the brains_vectors with the given brain.brain_id
|
2023-07-10 15:27:49 +03:00
|
|
|
brain.update_brain_with_file(
|
|
|
|
file_sha1=input_brain.file_sha1 # pyright: ignore reportPrivateUsage=none
|
|
|
|
)
|
2023-06-17 00:36:53 +03:00
|
|
|
|
2023-07-10 15:27:49 +03:00
|
|
|
brain.update_brain_fields(commons, brain) # pyright: ignore reportPrivateUsage=none
|
2023-06-17 00:36:53 +03:00
|
|
|
return {"message": f"Brain {brain_id} has been updated."}
|