mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-15 09:32:22 +03:00
feat(refacto): removed commons
This commit is contained in:
parent
72a6ae3dc0
commit
c7fdbf6dec
@ -7,7 +7,7 @@ from auth.auth_bearer import AuthBearer, get_current_user
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from logger import get_logger
|
||||
from models.brains import Brain, BrainToUpdate
|
||||
from models.settings import CommonsDep
|
||||
from models.settings import CommonsDep, common_dependencies
|
||||
from models.users import User
|
||||
from utils.users import fetch_user_id_from_credentials
|
||||
|
||||
@ -71,7 +71,7 @@ def update_brain_with_file(commons,brain_id:UUID , file_sha1: str ):
|
||||
|
||||
# get all brains
|
||||
@brain_router.get("/brain", dependencies=[Depends(AuthBearer())], tags=["Brain"])
|
||||
async def brain_endpoint(commons: CommonsDep, current_user: User = Depends(get_current_user)):
|
||||
async def brain_endpoint( current_user: User = Depends(get_current_user)):
|
||||
"""
|
||||
Retrieve all brains for the current user.
|
||||
|
||||
@ -81,14 +81,14 @@ async def brain_endpoint(commons: CommonsDep, current_user: User = Depends(get_c
|
||||
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.
|
||||
"""
|
||||
|
||||
commons = common_dependencies()
|
||||
user_id = fetch_user_id_from_credentials(commons, {"email": current_user.email})
|
||||
brains = get_user_brains(commons, user_id)
|
||||
return {"brains": brains}
|
||||
|
||||
# get one brain
|
||||
@brain_router.get("/brain/{brain_id}", dependencies=[Depends(AuthBearer())], tags=["Brain"])
|
||||
async def brain_endpoint(commons: CommonsDep, brain_id: UUID):
|
||||
async def brain_endpoint( brain_id: UUID):
|
||||
"""
|
||||
Retrieve details of a specific brain by brain ID.
|
||||
|
||||
@ -98,6 +98,7 @@ async def brain_endpoint(commons: CommonsDep, brain_id: UUID):
|
||||
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.
|
||||
"""
|
||||
commons = common_dependencies()
|
||||
brains = get_brain_details(commons, brain_id)
|
||||
if len(brains) > 0:
|
||||
return {"brainId": brain_id, "brainName": brains[0]['brain_name'], "status": brains[0]['status']}
|
||||
@ -106,17 +107,18 @@ async def brain_endpoint(commons: CommonsDep, brain_id: UUID):
|
||||
|
||||
# delete one brain
|
||||
@brain_router.delete("/brain/{brain_id}", dependencies=[Depends(AuthBearer())], tags=["Brain"])
|
||||
async def brain_endpoint(commons: CommonsDep, brain_id: UUID):
|
||||
async def brain_endpoint( brain_id: UUID):
|
||||
"""
|
||||
Delete a specific brain by brain ID.
|
||||
"""
|
||||
commons = common_dependencies()
|
||||
delete_brain(commons, brain_id)
|
||||
return {"message": f"{brain_id} has been deleted."}
|
||||
|
||||
|
||||
# create new brain
|
||||
@brain_router.post("/brain", dependencies=[Depends(AuthBearer())], tags=["Brain"])
|
||||
async def brain_endpoint(request: Request, commons: CommonsDep, brain: Brain, current_user: User = Depends(get_current_user)):
|
||||
async def brain_endpoint(request: Request, brain: Brain, current_user: User = Depends(get_current_user)):
|
||||
"""
|
||||
Create a new brain with given
|
||||
brain_name
|
||||
@ -126,7 +128,7 @@ async def brain_endpoint(request: Request, commons: CommonsDep, brain: Brain, cu
|
||||
temperature
|
||||
In the brains table & in the brains_users table and put the creator user as 'Owner'
|
||||
"""
|
||||
|
||||
commons = common_dependencies()
|
||||
user_id = fetch_user_id_from_credentials(commons, {"email": current_user.email})
|
||||
created_brain = create_brain(commons, brain)
|
||||
# create a brain
|
||||
@ -136,7 +138,7 @@ async def brain_endpoint(request: Request, commons: CommonsDep, brain: Brain, cu
|
||||
|
||||
# update existing brain
|
||||
@brain_router.put("/brain/{brain_id}", dependencies=[Depends(AuthBearer())], tags=["Brain"])
|
||||
async def brain_endpoint(request: Request, commons: CommonsDep, brain_id: UUID, brain: BrainToUpdate, fileName: Optional[str], current_user: User = Depends(get_current_user)):
|
||||
async def brain_endpoint(request: Request, brain_id: UUID, brain: BrainToUpdate, fileName: Optional[str], current_user: User = Depends(get_current_user)):
|
||||
"""
|
||||
Update an existing brain with new brain parameters/files.
|
||||
If the file is contained in Add file to brain :
|
||||
@ -145,7 +147,7 @@ async def brain_endpoint(request: Request, commons: CommonsDep, brain_id: UUID,
|
||||
name, status, model, max_tokens, temperature
|
||||
Return modified brain ? No need -> do an optimistic update
|
||||
"""
|
||||
|
||||
commons = common_dependencies()
|
||||
# Add new file to brain , il file_sha1 already exists in brains_vectors -> out (not now)
|
||||
if brain.file_sha1 :
|
||||
# add all the vector Ids to the brains_vectors with the given brain.brain_id
|
||||
|
@ -5,7 +5,7 @@ from tempfile import SpooledTemporaryFile
|
||||
from auth.auth_bearer import AuthBearer, get_current_user
|
||||
from crawl.crawler import CrawlWebsite
|
||||
from fastapi import APIRouter, Depends, Request, UploadFile
|
||||
from models.settings import CommonsDep
|
||||
from models.settings import CommonsDep, common_dependencies
|
||||
from models.users import User
|
||||
from parsers.github import process_github
|
||||
from utils.file import convert_bytes
|
||||
@ -27,10 +27,11 @@ def get_unique_user_data(commons, user):
|
||||
return user_unique_vectors
|
||||
|
||||
@crawl_router.post("/crawl/", dependencies=[Depends(AuthBearer())], tags=["Crawl"])
|
||||
async def crawl_endpoint(request: Request,commons: CommonsDep, crawl_website: CrawlWebsite, enable_summarization: bool = False, current_user: User = Depends(get_current_user)):
|
||||
async def crawl_endpoint(request: Request, crawl_website: CrawlWebsite, enable_summarization: bool = False, current_user: User = Depends(get_current_user)):
|
||||
"""
|
||||
Crawl a website and process the crawled data.
|
||||
"""
|
||||
commons = common_dependencies()
|
||||
max_brain_size = os.getenv("MAX_BRAIN_SIZE")
|
||||
if request.headers.get('Openai-Api-Key'):
|
||||
max_brain_size = os.getenv("MAX_BRAIN_SIZE_WITH_KEY",209715200)
|
||||
|
@ -1,6 +1,6 @@
|
||||
from auth.auth_bearer import AuthBearer, get_current_user
|
||||
from fastapi import APIRouter, Depends
|
||||
from models.settings import CommonsDep
|
||||
from models.settings import CommonsDep, common_dependencies
|
||||
from models.users import User
|
||||
|
||||
explore_router = APIRouter()
|
||||
@ -17,20 +17,22 @@ def get_unique_user_data(commons, user):
|
||||
return unique_data
|
||||
|
||||
@explore_router.get("/explore", dependencies=[Depends(AuthBearer())], tags=["Explore"])
|
||||
async def explore_endpoint(commons: CommonsDep, current_user: User = Depends(get_current_user)):
|
||||
async def explore_endpoint( current_user: User = Depends(get_current_user)):
|
||||
"""
|
||||
Retrieve and explore unique user data vectors.
|
||||
"""
|
||||
commons = common_dependencies()
|
||||
unique_data = get_unique_user_data(commons, current_user)
|
||||
unique_data.sort(key=lambda x: int(x['size']), reverse=True)
|
||||
return {"documents": unique_data}
|
||||
|
||||
|
||||
@explore_router.delete("/explore/{file_name}", dependencies=[Depends(AuthBearer())], tags=["Explore"])
|
||||
async def delete_endpoint(commons: CommonsDep, file_name: str, credentials: dict = Depends(AuthBearer())):
|
||||
async def delete_endpoint( file_name: str, credentials: dict = Depends(AuthBearer())):
|
||||
"""
|
||||
Delete a specific user file by file name.
|
||||
"""
|
||||
commons = common_dependencies()
|
||||
user = User(email=credentials.get('email', 'none'))
|
||||
# Cascade delete the summary from the database first, because it has a foreign key constraint
|
||||
commons['supabase'].table("summaries").delete().match(
|
||||
@ -40,10 +42,11 @@ async def delete_endpoint(commons: CommonsDep, file_name: str, credentials: dict
|
||||
return {"message": f"{file_name} of user {user.email} has been deleted."}
|
||||
|
||||
@explore_router.get("/explore/{file_name}", dependencies=[Depends(AuthBearer())], tags=["Explore"])
|
||||
async def download_endpoint(commons: CommonsDep, file_name: str, current_user: User = Depends(get_current_user)):
|
||||
async def download_endpoint( file_name: str, current_user: User = Depends(get_current_user)):
|
||||
"""
|
||||
Download a specific user file by file name.
|
||||
"""
|
||||
commons = common_dependencies()
|
||||
response = commons['supabase'].table("vectors").select(
|
||||
"metadata->>file_name, metadata->>file_size, metadata->>file_extension, metadata->>file_url", "content").match({"metadata->>file_name": file_name, "user_id": current_user.email}).execute()
|
||||
documents = response.data
|
||||
|
@ -2,7 +2,7 @@ import os
|
||||
|
||||
from auth.auth_bearer import AuthBearer, get_current_user
|
||||
from fastapi import APIRouter, Depends, Request, UploadFile
|
||||
from models.settings import CommonsDep
|
||||
from models.settings import CommonsDep, common_dependencies
|
||||
from models.users import User
|
||||
from utils.file import convert_bytes, get_file_size
|
||||
from utils.processors import filter_file
|
||||
@ -24,7 +24,7 @@ def calculate_remaining_space(request, max_brain_size, max_brain_size_with_own_k
|
||||
return remaining_free_space
|
||||
|
||||
@upload_router.post("/upload", dependencies=[Depends(AuthBearer())], tags=["Upload"])
|
||||
async def upload_file(request: Request, commons: CommonsDep, file: UploadFile, enable_summarization: bool = False, current_user: User = Depends(get_current_user)):
|
||||
async def upload_file(request: Request, file: UploadFile, enable_summarization: bool = False, current_user: User = Depends(get_current_user)):
|
||||
"""
|
||||
Upload a file to the user's storage.
|
||||
|
||||
@ -37,6 +37,7 @@ async def upload_file(request: Request, commons: CommonsDep, file: UploadFile,
|
||||
and ensures that the file size does not exceed the maximum capacity. If the file is within the allowed size limit,
|
||||
it can optionally apply summarization to the file's content. The response message will indicate the status of the upload.
|
||||
"""
|
||||
commons = common_dependencies()
|
||||
max_brain_size = os.getenv("MAX_BRAIN_SIZE")
|
||||
max_brain_size_with_own_key = os.getenv("MAX_BRAIN_SIZE_WITH_KEY", 209715200)
|
||||
|
||||
|
@ -3,7 +3,7 @@ import time
|
||||
|
||||
from auth.auth_bearer import AuthBearer, get_current_user
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from models.settings import CommonsDep
|
||||
from models.settings import CommonsDep, common_dependencies
|
||||
from models.users import User
|
||||
|
||||
user_router = APIRouter()
|
||||
@ -28,7 +28,7 @@ def get_user_request_stats(commons, email):
|
||||
return requests_stats.data
|
||||
|
||||
@user_router.get("/user", dependencies=[Depends(AuthBearer())], tags=["User"])
|
||||
async def get_user_endpoint(request: Request, commons: CommonsDep, current_user: User = Depends(get_current_user)):
|
||||
async def get_user_endpoint(request: Request, current_user: User = Depends(get_current_user)):
|
||||
"""
|
||||
Get user information and statistics.
|
||||
|
||||
@ -40,6 +40,7 @@ async def get_user_endpoint(request: Request, commons: CommonsDep, current_user:
|
||||
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.
|
||||
"""
|
||||
commons = common_dependencies()
|
||||
user_vectors = get_user_vectors(commons, current_user.email)
|
||||
user_unique_vectors = get_unique_documents(user_vectors)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user