quivr/backend/routes/explore_routes.py
Mamadou DICKO 59fe7b089b
feat(chat): use openai function for answer (#354)
* feat(chat): use openai function for answer (backend)

* feat(chat): use openai function for answer (frontend)

* chore: refacto BrainPicking

* feat: update chat creation logic

* feat: simplify chat system logic

* feat: set default method to gpt-3.5-turbo-0613

* feat: use user own openai key

* feat(chat): slightly improve prompts

* feat: add global error interceptor

* feat: remove unused endpoints

* docs: update chat system doc

* chore(linter): add unused import remove config

* feat: improve dx

* feat: improve OpenAiFunctionBasedAnswerGenerator prompt
2023-06-22 17:50:06 +02:00

78 lines
2.7 KiB
Python

from auth.auth_bearer import AuthBearer, get_current_user
from fastapi import APIRouter, Depends
from models.settings import CommonsDep, common_dependencies
from models.users import User
explore_router = APIRouter()
def get_unique_user_data(commons, user):
"""
Retrieve unique user data 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 = 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
unique_data = [dict(t) for t in set(tuple(d.items()) for d in documents)]
return unique_data
@explore_router.get("/explore", dependencies=[Depends(AuthBearer())], tags=["Explore"])
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(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(
{"metadata->>file_name": file_name}
).execute()
commons["supabase"].table("vectors").delete().match(
{"metadata->>file_name": file_name, "user_id": user.email}
).execute()
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(
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
return {"documents": documents}