Skip to content

Commit

Permalink
docs(backend): add docstrings (QuivrHQ#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-romanenko authored Jul 10, 2023
1 parent a6658cd commit 1be71e9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
30 changes: 25 additions & 5 deletions backend/models/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
from fastapi import UploadFile
from langchain.text_splitter import RecursiveCharacterTextSplitter
from logger import get_logger
from pydantic import BaseModel
from utils.file import compute_sha1_from_file

from models.brains import Brain
from models.settings import CommonsDep, common_dependencies
from pydantic import BaseModel
from utils.file import compute_sha1_from_file

logger = get_logger(__name__)

Expand All @@ -19,9 +18,9 @@ class File(BaseModel):
id: Optional[UUID] = None
file: Optional[UploadFile]
file_name: Optional[str] = ""
file_size: Optional[int] = "" # pyright: ignore reportPrivateUsage=none
file_size: Optional[int] = None
file_sha1: Optional[str] = ""
vectors_ids: Optional[int] = [] # pyright: ignore reportPrivateUsage=none
vectors_ids: Optional[list] = []
file_extension: Optional[str] = ""
content: Optional[Any] = None
chunk_size: int = 500
Expand All @@ -42,6 +41,9 @@ def __init__(self, **kwargs):
)[-1].lower()

async def compute_file_sha1(self):
"""
Compute the sha1 of the file using a temporary file
"""
with tempfile.NamedTemporaryFile(
delete=False,
suffix=self.file.filename, # pyright: ignore reportPrivateUsage=none
Expand All @@ -57,6 +59,12 @@ async def compute_file_sha1(self):
os.remove(tmp_file.name)

def compute_documents(self, loader_class):
"""
Compute the documents from the file
Args:
loader_class (class): The class of the loader to use to load the file
"""
logger.info(f"Computing documents from file {self.file_name}")

documents = []
Expand Down Expand Up @@ -118,6 +126,12 @@ def file_already_exists(self):
return True

def file_already_exists_in_brain(self, brain_id):
"""
Check if file already exists in a brain
Args:
brain_id (str): Brain id
"""
commons = common_dependencies()
self.set_file_vectors_ids()
# Check if file exists in that brain
Expand All @@ -136,13 +150,19 @@ def file_already_exists_in_brain(self, brain_id):
return True

def file_is_empty(self):
"""
Check if file is empty by checking if the file pointer is at the beginning of the file
"""
return (
self.file.file._file.tell() < 1 # pyright: ignore reportPrivateUsage=none
)

def link_file_to_brain(self, brain: Brain):
self.set_file_vectors_ids()

if self.vectors_ids is None:
return

for vector_id in self.vectors_ids: # pyright: ignore reportPrivateUsage=none
brain.create_brain_vector(vector_id["id"], self.file_sha1)
print(f"Successfully linked file {self.file_sha1} to brain {brain.id}")
15 changes: 15 additions & 0 deletions backend/models/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class User(BaseModel):

# [TODO] Rename the user table and its references to 'user_usage'
def create_user(self, date):
"""
Create a new user entry in the database
Args:
date (str): Date of the request
"""
commons = common_dependencies()
logger.info(f"New user entry in db document for user {self.email}")

Expand All @@ -34,6 +40,9 @@ def create_user(self, date):
)

def get_user_request_stats(self):
"""
Fetch the user request stats from the database
"""
commons = common_dependencies()
requests_stats = (
commons["supabase"]
Expand All @@ -45,6 +54,9 @@ def get_user_request_stats(self):
return requests_stats.data

def fetch_user_requests_count(self, date):
"""
Fetch the user request count from the database
"""
commons = common_dependencies()
response = (
commons["supabase"]
Expand All @@ -59,6 +71,9 @@ def fetch_user_requests_count(self, date):
return userItem["requests_count"]

def increment_user_request_count(self, date):
"""
Increment the user request count in the database
"""
commons = common_dependencies()
requests_count = self.fetch_user_requests_count(date) + 1
logger.info(f"User {self.email} request count updated to {requests_count}")
Expand Down

0 comments on commit 1be71e9

Please sign in to comment.