mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-09-11 14:36:35 +03:00
✏️ add / in all explore url (#415)
* ✏️ add / in all explore url * 🐛 fix user routes * ⚰️ remove dead code * ✨ add name to active brain
This commit is contained in:
parent
4d9bd512ec
commit
b016413099
@ -2,7 +2,7 @@ from typing import Optional
|
||||
from uuid import UUID
|
||||
|
||||
from auth.auth_bearer import AuthBearer, get_current_user
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from fastapi import APIRouter, Depends
|
||||
from logger import get_logger
|
||||
from models.brains import Brain, get_default_user_brain
|
||||
from models.settings import common_dependencies
|
||||
@ -25,7 +25,7 @@ class BrainToUpdate(BaseModel):
|
||||
|
||||
|
||||
# get all brains
|
||||
@brain_router.get("/brains", dependencies=[Depends(AuthBearer())], tags=["Brain"])
|
||||
@brain_router.get("/brains/", dependencies=[Depends(AuthBearer())], tags=["Brain"])
|
||||
async def brain_endpoint(current_user: User = Depends(get_current_user)):
|
||||
"""
|
||||
Retrieve all brains for the current user.
|
||||
@ -56,6 +56,7 @@ async def get_default_brain_endpoint(current_user: User = Depends(get_current_us
|
||||
default_brain = get_default_user_brain(current_user)
|
||||
return default_brain
|
||||
|
||||
|
||||
# get one brain
|
||||
@brain_router.get(
|
||||
"/brains/{brain_id}", dependencies=[Depends(AuthBearer())], tags=["Brain"]
|
||||
@ -96,7 +97,6 @@ async def delete_brain_endpoint(brain_id: UUID, current_user: User = Depends(get
|
||||
brain = Brain(id=brain_id)
|
||||
brain.delete_brain()
|
||||
|
||||
|
||||
return {"message": f"{brain_id} has been deleted."}
|
||||
|
||||
|
||||
@ -111,8 +111,8 @@ class BrainObject(BaseModel):
|
||||
|
||||
|
||||
# create new brain
|
||||
@brain_router.post("/brains", dependencies=[Depends(AuthBearer())], tags=["Brain"])
|
||||
async def brain_endpoint(
|
||||
@brain_router.post("/brains/", dependencies=[Depends(AuthBearer())], tags=["Brain"])
|
||||
async def create_brain_endpoint(
|
||||
brain: BrainObject,
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
@ -132,24 +132,21 @@ async def brain_endpoint(
|
||||
default_brain = get_default_user_brain(current_user)
|
||||
if default_brain:
|
||||
logger.info(f"Default brain already exists for user {current_user.id}")
|
||||
brain.create_brain_user(user_id = current_user.id, rights="Owner", default_brain=False)
|
||||
brain.create_brain_user(user_id=current_user.id, rights="Owner", default_brain=False)
|
||||
else:
|
||||
logger.info(f"Default brain does not exist for user {current_user.id}. It will be created.")
|
||||
brain.create_brain_user(user_id = current_user.id, rights="Owner", default_brain=True)
|
||||
brain.create_brain_user(user_id=current_user.id, rights="Owner", default_brain=True)
|
||||
|
||||
|
||||
return {"id": brain.id, "name": brain.name}
|
||||
|
||||
|
||||
# update existing brain
|
||||
@brain_router.put(
|
||||
"/brains/{brain_id}", dependencies=[Depends(AuthBearer())], tags=["Brain"]
|
||||
)
|
||||
async def update_brain_endpoint(
|
||||
request: Request,
|
||||
brain_id: UUID,
|
||||
input_brain: Brain,
|
||||
fileName: Optional[str],
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""
|
||||
Update an existing brain with new brain parameters/files.
|
||||
|
@ -22,7 +22,7 @@ async def explore_endpoint(brain_id: UUID = Query(..., description="The ID of th
|
||||
|
||||
|
||||
@explore_router.delete(
|
||||
"/explore/{file_name}", dependencies=[Depends(AuthBearer())], tags=["Explore"]
|
||||
"/explore/{file_name}/", dependencies=[Depends(AuthBearer())], tags=["Explore"]
|
||||
)
|
||||
async def delete_endpoint(file_name: str, current_user: User = Depends(get_current_user), brain_id: UUID = Query(..., description="The ID of the brain")):
|
||||
"""
|
||||
@ -35,7 +35,7 @@ async def delete_endpoint(file_name: str, current_user: User = Depends(get_curre
|
||||
|
||||
|
||||
@explore_router.get(
|
||||
"/explore/{file_name}", dependencies=[Depends(AuthBearer())], tags=["Explore"]
|
||||
"/explore/{file_name}/", dependencies=[Depends(AuthBearer())], tags=["Explore"]
|
||||
)
|
||||
async def download_endpoint(
|
||||
file_name: str, current_user: User = Depends(get_current_user)
|
||||
|
@ -12,16 +12,6 @@ from utils.processors import filter_file
|
||||
|
||||
upload_router = APIRouter()
|
||||
|
||||
def get_user_vectors(commons, user):
|
||||
user_vectors_response = commons['supabase'].table("vectors").select(
|
||||
"name:metadata->>file_name, size:metadata->>file_size", count="exact") \
|
||||
.filter("user_id", "eq", user.email)\
|
||||
.execute()
|
||||
documents = user_vectors_response.data # Access the data from the response
|
||||
# Convert each dictionary to a tuple of items, then to a set to remove duplicates, and then back to a dictionary
|
||||
user_unique_vectors = [dict(t) for t in set(tuple(d.items()) for d in documents)]
|
||||
return user_unique_vectors
|
||||
|
||||
|
||||
@upload_router.post("/upload", dependencies=[Depends(AuthBearer())], tags=["Upload"])
|
||||
async def upload_file(request: Request, uploadFile: UploadFile, brain_id: UUID = Query(..., description="The ID of the brain"), enable_summarization: bool = False, current_user: User = Depends(get_current_user)):
|
||||
@ -49,8 +39,6 @@ async def upload_file(request: Request, uploadFile: UploadFile, brain_id: UUID
|
||||
brain.max_brain_size = os.getenv("MAX_BRAIN_SIZE_WITH_KEY",209715200)
|
||||
remaining_free_space = brain.remaining_brain_size
|
||||
|
||||
|
||||
|
||||
file_size = get_file_size(uploadFile)
|
||||
|
||||
file = File(file=uploadFile)
|
||||
|
@ -15,17 +15,6 @@ def get_unique_documents(vectors):
|
||||
# Convert each dictionary to a tuple of items, then to a set to remove duplicates, and then back to a dictionary
|
||||
return [dict(t) for t in set(tuple(d.items()) for d in vectors)]
|
||||
|
||||
|
||||
def get_user_vectors(commons, email):
|
||||
# Access the supabase table and get the vectors
|
||||
user_vectors_response = commons['supabase'].table("vectors").select(
|
||||
"name:metadata->>file_name, size:metadata->>file_size", count="exact") \
|
||||
.filter("user_id", "eq", email)\
|
||||
.execute()
|
||||
return user_vectors_response.data
|
||||
|
||||
|
||||
|
||||
@user_router.get("/user", dependencies=[Depends(AuthBearer())], tags=["User"])
|
||||
async def get_user_endpoint(request: Request, current_user: User = Depends(get_current_user)):
|
||||
"""
|
||||
@ -39,10 +28,6 @@ async def get_user_endpoint(request: Request, current_user: User = Depends(get_c
|
||||
user's uploaded vectors, and the maximum brain size is obtained from the environment variables. The requests statistics provide
|
||||
information about the user's API usage.
|
||||
"""
|
||||
# user_vectors = get_user_vectors(commons, current_user.email)
|
||||
# user_unique_vectors = get_unique_documents(user_vectors)
|
||||
|
||||
# current_brain_size = sum(float(doc.get('size', 0)) for doc in user_unique_vectors)
|
||||
|
||||
max_brain_size = int(os.getenv("MAX_BRAIN_SIZE", 0))
|
||||
if request.headers.get('Openai-Api-Key'):
|
||||
@ -51,13 +36,17 @@ async def get_user_endpoint(request: Request, current_user: User = Depends(get_c
|
||||
date = time.strftime("%Y%m%d")
|
||||
max_requests_number = os.getenv("MAX_REQUESTS_NUMBER")
|
||||
requests_stats = current_user.get_user_request_stats()
|
||||
default_brain = get_default_user_brain(current_user)
|
||||
|
||||
defaultBrain = Brain(id=get_default_user_brain(current_user)['id'])
|
||||
|
||||
return {"email": current_user.email,
|
||||
"max_brain_size": max_brain_size,
|
||||
"current_brain_size": defaultBrain.brain_size,
|
||||
if default_brain:
|
||||
defaul_brain_size = Brain(id=default_brain.id).size
|
||||
else:
|
||||
defaul_brain_size = 0
|
||||
|
||||
return {"email": current_user.email,
|
||||
"max_brain_size": max_brain_size,
|
||||
"current_brain_size": defaul_brain_size,
|
||||
"max_requests_number": max_requests_number,
|
||||
"requests_stats" : requests_stats,
|
||||
"requests_stats": requests_stats,
|
||||
"date": date,
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ const ExplorePage = (): JSX.Element => {
|
||||
const fetchAndSetActiveBrain = async () => {
|
||||
const storedBrain = getBrainFromLocalStorage();
|
||||
if (storedBrain) {
|
||||
setActiveBrain(storedBrain.id);
|
||||
setActiveBrain({ ...storedBrain });
|
||||
return storedBrain;
|
||||
} else {
|
||||
const defaultBrain = await setDefaultBrain();
|
||||
|
@ -24,9 +24,11 @@ export const BrainsDropDown = (): JSX.Element => {
|
||||
setNewBrainName(""); // Reset the new brain name input
|
||||
};
|
||||
|
||||
const handleClickOutside = (event : MouseEvent) => {
|
||||
const handleClickOutside = (event: MouseEvent) => {
|
||||
if (
|
||||
dropdownRef.current && !dropdownRef.current.contains(event.target as Node | null)) {
|
||||
dropdownRef.current &&
|
||||
!dropdownRef.current.contains(event.target as Node | null)
|
||||
) {
|
||||
setShowDropdown(false);
|
||||
}
|
||||
};
|
||||
@ -78,7 +80,7 @@ export const BrainsDropDown = (): JSX.Element => {
|
||||
className={`block w-full text-left px-4 py-2 text-sm leading-5 ${
|
||||
currentBrain?.id === brain.id ? "bg-blue-100" : ""
|
||||
} text-gray-900 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700 focus:bg-gray-100 dark:focus:bg-gray-700 focus:outline-none`}
|
||||
onClick={() => setActiveBrain(brain.id)}
|
||||
onClick={() => setActiveBrain({ ...brain })}
|
||||
>
|
||||
{brain.name}
|
||||
</button>
|
||||
|
@ -23,7 +23,7 @@ export interface BrainStateProps {
|
||||
allBrains: Brain[];
|
||||
createBrain: (name: string) => Promise<UUID | undefined>;
|
||||
deleteBrain: (id: UUID) => Promise<void>;
|
||||
setActiveBrain: (id: UUID) => void;
|
||||
setActiveBrain: ({ id, name }: { id: UUID; name: string }) => void;
|
||||
getBrainWithId: (brainId: UUID) => Promise<Brain>;
|
||||
fetchAllBrains: () => Promise<void>;
|
||||
setDefaultBrain: () => Promise<void>;
|
||||
@ -84,19 +84,15 @@ export const useBrainState = (): BrainStateProps => {
|
||||
}
|
||||
}, [axiosInstance]);
|
||||
|
||||
const setActiveBrain = useCallback((id: UUID) => {
|
||||
//get brain with id from BE ?
|
||||
|
||||
const newActiveBrain = { id, name: "Default Brain" };
|
||||
// if (newActiveBrain) {
|
||||
console.log("newActiveBrain", newActiveBrain);
|
||||
saveBrainInLocalStorage(newActiveBrain);
|
||||
setCurrentBrainId(id);
|
||||
console.log("Setting active brain", newActiveBrain);
|
||||
// } else {
|
||||
// console.warn(`No brain found with ID ${id}`);
|
||||
// }
|
||||
}, []);
|
||||
const setActiveBrain = useCallback(
|
||||
({ id, name }: { id: UUID; name: string }) => {
|
||||
const newActiveBrain = { id, name };
|
||||
saveBrainInLocalStorage(newActiveBrain);
|
||||
setCurrentBrainId(id);
|
||||
console.log("Setting active brain", newActiveBrain);
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
const setDefaultBrain = useCallback(async () => {
|
||||
console.log("Setting default brain");
|
||||
@ -104,7 +100,7 @@ export const useBrainState = (): BrainStateProps => {
|
||||
console.log("defaultBrain", defaultBrain);
|
||||
if (defaultBrain) {
|
||||
saveBrainInLocalStorage(defaultBrain);
|
||||
setActiveBrain(defaultBrain.id);
|
||||
setActiveBrain({ ...defaultBrain });
|
||||
} else {
|
||||
console.warn("No brains found");
|
||||
}
|
||||
@ -118,7 +114,7 @@ export const useBrainState = (): BrainStateProps => {
|
||||
if (storedBrain?.id !== undefined) {
|
||||
console.log("Setting active brain from local storage");
|
||||
console.log("storedBrain", storedBrain);
|
||||
setActiveBrain(storedBrain.id);
|
||||
setActiveBrain({ ...storedBrain });
|
||||
} else {
|
||||
console.log("Setting default brain for first time");
|
||||
await setDefaultBrain();
|
||||
|
Loading…
Reference in New Issue
Block a user