Skip to content

Commit

Permalink
fix(storage) Update docstrings and document query method
Browse files Browse the repository at this point in the history
  • Loading branch information
VVoruganti committed Nov 15, 2024
1 parent d6e1776 commit cd49cec
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 302 deletions.
51 changes: 5 additions & 46 deletions src/routers/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,7 @@

@router.get("/{app_id}", response_model=schemas.App)
async def get_app(app_id: str, db=db):
"""Get an App by ID
Args:
app_id (str): The ID of the app
Returns:
schemas.App: App object
"""
"""Get an App by ID"""
app = await crud.get_app(db, app_id=app_id)
if app is None:
raise HTTPException(status_code=404, detail="App not found")
Expand All @@ -33,15 +25,7 @@ async def get_app(app_id: str, db=db):

@router.get("/name/{name}", response_model=schemas.App)
async def get_app_by_name(name: str, db=db):
"""Get an App by Name
Args:
app_name (str): The name of the app
Returns:
schemas.App: App object
"""
"""Get an App by Name"""
app = await crud.get_app_by_name(db, name=name)
if app is None:
raise HTTPException(status_code=404, detail="App not found")
Expand All @@ -50,15 +34,7 @@ async def get_app_by_name(name: str, db=db):

@router.post("", response_model=schemas.App)
async def create_app(app: schemas.AppCreate, db=db):
"""Create an App
Args:
app (schemas.AppCreate): The App object containing any metadata
Returns:
schemas.App: Created App object
"""
"""Create a new App"""
try:
honcho_app = await crud.create_app(db, app=app)
return honcho_app
Expand All @@ -73,15 +49,7 @@ async def create_app(app: schemas.AppCreate, db=db):

@router.get("/get_or_create/{name}", response_model=schemas.App)
async def get_or_create_app(name: str, db=db):
"""Get or Create an App
Args:
app_name (str): The name of the app
Returns:
schemas.App: App object
"""
"""Get or Create an App"""
print("name", name)
app = await crud.get_app_by_name(db=db, name=name)
if app is None:
Expand All @@ -95,16 +63,7 @@ async def update_app(
app: schemas.AppUpdate,
db=db,
):
"""Update an App
Args:
app_id (str): The ID of the app to update
app (schemas.AppUpdate): The App object containing any new metadata
Returns:
schemas.App: The App object of the updated App
"""
"""Update an App"""
honcho_app = await crud.update_app(db, app_id=app_id, app=app)
if honcho_app is None:
raise HTTPException(status_code=404, detail="App not found")
Expand Down
17 changes: 6 additions & 11 deletions src/routers/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,7 @@ async def get_collections(
reverse: Optional[bool] = False,
db=db,
):
"""Get All Collections for a User
Args:
app_id (str): The ID of the app representing the client
application using honcho
user_id (str): The User ID representing the user, managed by the user
Returns:
list[schemas.Collection]: List of Collection objects
"""
"""Get All Collections for a User"""
return await paginate(
db,
await crud.get_collections(
Expand All @@ -49,6 +39,7 @@ async def get_collection_by_name(
name: str,
db=db,
) -> schemas.Collection:
"""Get a Collection by Name"""
honcho_collection = await crud.get_collection_by_name(
db, app_id=app_id, user_id=user_id, name=name
)
Expand All @@ -66,6 +57,7 @@ async def get_collection_by_id(
collection_id: str,
db=db,
) -> schemas.Collection:
"""Get a Collection by ID"""
honcho_collection = await crud.get_collection_by_id(
db, app_id=app_id, user_id=user_id, collection_id=collection_id
)
Expand All @@ -83,6 +75,7 @@ async def create_collection(
collection: schemas.CollectionCreate,
db=db,
):
"""Create a new Collection"""
if collection.name == "honcho":
raise HTTPException(
status_code=406,
Expand All @@ -107,6 +100,7 @@ async def update_collection(
collection: schemas.CollectionUpdate,
db=db,
):
"Update a Collection's name or metadata"
if collection.name is None and collection.metadata is None:
raise HTTPException(
status_code=406,
Expand Down Expand Up @@ -140,6 +134,7 @@ async def delete_collection(
collection_id: str,
db=db,
):
"""Delete a Collection and its documents"""
try:
await crud.delete_collection(
db, app_id=app_id, user_id=user_id, collection_id=collection_id
Expand Down
39 changes: 24 additions & 15 deletions src/routers/documents.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from collections.abc import Sequence
from typing import Optional

Expand Down Expand Up @@ -25,6 +26,7 @@ async def get_documents(
reverse: Optional[bool] = False,
db=db,
):
"""Get all of the Documents in a Collection"""
try:
return await paginate(
db,
Expand Down Expand Up @@ -56,6 +58,7 @@ async def get_document(
document_id: str,
db=db,
):
"""Get a document by ID"""
honcho_document = await crud.get_document(
db,
app_id=app_id,
Expand All @@ -70,30 +73,33 @@ async def get_document(
return honcho_document


@router.get("/query", response_model=Sequence[schemas.Document])
@router.post("/query", response_model=Sequence[schemas.Document])
async def query_documents(
app_id: str,
user_id: str,
collection_id: str,
query: str,
options: schemas.DocumentQuery,
top_k: int = 5,
filter: Optional[str] = None,
db=db,
):
"""Cosiner Similarity Search for Documents"""
if top_k is not None and top_k > 50:
top_k = 50 # TODO see if we need to paginate this
data = None
if filter is not None:
data = json.loads(filter)
return await crud.query_documents(
db=db,
app_id=app_id,
user_id=user_id,
collection_id=collection_id,
query=query,
filter=data,
top_k=top_k,
)
try:
filter = options.filter
if options.filter == {}:
filter = None
return await crud.query_documents(
db=db,
app_id=app_id,
user_id=user_id,
collection_id=collection_id,
query=options.query,
filter=filter,
top_k=top_k,
)
except ValueError as e:
raise HTTPException(status_code=400, detail="Error Query Documents") from e


@router.post("", response_model=schemas.Document)
Expand All @@ -104,6 +110,7 @@ async def create_document(
document: schemas.DocumentCreate,
db=db,
):
"""Embed text as a vector and create a Document"""
try:
return await crud.create_document(
db,
Expand All @@ -130,6 +137,7 @@ async def update_document(
document: schemas.DocumentUpdate,
db=db,
):
"""Update the content and/or the metadata of a Document"""
if document.content is None and document.metadata is None:
raise HTTPException(
status_code=400, detail="content and metadata cannot both be None"
Expand Down Expand Up @@ -157,6 +165,7 @@ async def delete_document(
document_id: str,
db=db,
):
"""Delete a Document by ID"""
response = await crud.delete_document(
db,
app_id=app_id,
Expand Down
37 changes: 4 additions & 33 deletions src/routers/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,7 @@ async def create_message_for_session(
background_tasks: BackgroundTasks,
db=db,
):
"""Adds a message to a session
Args:
app_id (str): The ID of the app representing the client application using honcho
user_id (str): The User ID representing the user, managed by the user
session_id (int): The ID of the Session to add the message to
message (schemas.MessageCreate): The Message object to add containing the message content and type
Returns:
schemas.Message: The Message object of the added message
Raises:
HTTPException: If the session is not found
"""
"""Adds a message to a session"""
try:
honcho_message = await crud.create_message(
db, message=message, app_id=app_id, user_id=user_id, session_id=session_id
Expand Down Expand Up @@ -110,22 +96,7 @@ async def get_messages(
reverse: Optional[bool] = False,
db=db,
):
"""Get all messages for a session
Args:
app_id (str): The ID of the app representing the client application using
honcho
user_id (str): The User ID representing the user, managed by the user
session_id (int): The ID of the Session to retrieve
reverse (bool): Whether to reverse the order of the messages
Returns:
list[schemas.Message]: List of Message objects
Raises:
HTTPException: If the session is not found
"""
"""Get all messages for a session"""
try:
filter = options.filter
if options.filter == {}:
Expand Down Expand Up @@ -153,7 +124,7 @@ async def get_message(
message_id: str,
db=db,
):
""" """
"""Get a Message by ID"""
honcho_message = await crud.get_message(
db, app_id=app_id, session_id=session_id, user_id=user_id, message_id=message_id
)
Expand All @@ -171,7 +142,7 @@ async def update_message(
message: schemas.MessageUpdate,
db=db,
):
"""Update's the metadata of a message"""
"""Update the metadata of a Message"""
if message.metadata is None:
raise HTTPException(status_code=400, detail="Message metadata cannot be empty")
try:
Expand Down
64 changes: 4 additions & 60 deletions src/routers/metamessages.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,7 @@ async def create_metamessage(
metamessage: schemas.MetamessageCreate,
db=db,
):
"""Adds a message to a session
Args:
app_id (str): The ID of the app representing the client application using
honcho
user_id (str): The User ID representing the user, managed by the user
session_id (int): The ID of the Session to add the message to
metamessage (schemas.MeteamessageCreate): The metamessage creation object
Returns:
schemas.Metamessage: The Metamessage object of the added metamessage
Raises:
HTTPException: If the session is not found
"""
"""Adds a message to a session"""
try:
return await crud.create_metamessage(
db,
Expand All @@ -66,22 +51,7 @@ async def get_metamessages(
reverse: Optional[bool] = False,
db=db,
):
"""Get all messages for a session
Args:
app_id (str): The ID of the app representing the client application using
honcho
user_id (str): The User ID representing the user, managed by the user
session_id (int): The ID of the Session to retrieve
reverse (bool): Whether to reverse the order of the metamessages
Returns:
list[schemas.Message]: List of Message objects
Raises:
HTTPException: If the session is not found
"""
"""Get all messages for a session"""
try:
return await paginate(
db,
Expand All @@ -108,20 +78,7 @@ async def get_metamessages_by_user(
reverse: Optional[bool] = False,
db=db,
):
"""Paginate through the user metamessages for a user
Args:
app_id (str): The ID of the app representing the client application using honcho
user_id (str): The User ID representing the user, managed by the user
reverse (bool): Whether to reverse the order of the metamessages
Returns:
list[schemas.Message]: List of Message objects
Raises:
HTTPException: If the session is not found
"""
"""Paginate through the user metamessages for a user"""
try:
return await paginate(
db,
Expand Down Expand Up @@ -150,20 +107,7 @@ async def get_metamessage(
metamessage_id: str,
db=db,
):
"""Get a specific Metamessage by ID
Args:
app_id (str): The ID of the app representing the client application using
honcho
user_id (str): The User ID representing the user, managed by the user
session_id (int): The ID of the Session to retrieve
Returns:
schemas.Session: The Session object of the requested Session
Raises:
HTTPException: If the session is not found
"""
"""Get a specific Metamessage by ID"""
honcho_metamessage = await crud.get_metamessage(
db,
app_id=app_id,
Expand Down
Loading

0 comments on commit cd49cec

Please sign in to comment.