Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development: Add better error reporting #9

Merged
merged 4 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 48 additions & 11 deletions app/core/custom_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,63 @@ class RequiresAuthenticationException(HTTPException):
def __init__(self):
super().__init__(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Requires authentication",
detail={
"type": "not_authenticated",
"errorMessage": "Requires authentication",
},
)


class PermissionDeniedException(HTTPException):
def __init__(self):
super().__init__(
status_code=status.HTTP_403_FORBIDDEN, detail="Permission denied"
status_code=status.HTTP_403_FORBIDDEN,
detail={
"type": "not_authorized",
"errorMessage": "Permission denied",
},
)


class BadDataException(HTTPException):
class InternalServerException(HTTPException):
def __init__(self, error_message: str):
super().__init__(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=[
{
"loc": [],
"msg": error_message,
"type": "value_error.bad_data",
}
],
Hialus marked this conversation as resolved.
Show resolved Hide resolved
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail={
"type": "other",
"errorMessage": error_message,
},
)


class MissingParameterException(HTTPException):
def __init__(self, error_message: str):
super().__init__(
status_code=status.HTTP_400_BAD_REQUEST,
detail={
"type": "missing_parameter",
"errorMessage": error_message,
},
)


class InvalidTemplateException(HTTPException):
def __init__(self, error_message: str):
super().__init__(
status_code=status.HTTP_400_BAD_REQUEST,
detail={
"type": "invalid_template",
"errorMessage": error_message,
},
)


class InvalidModelException(HTTPException):
def __init__(self, error_message: str):
super().__init__(
status_code=status.HTTP_404_NOT_FOUND,
detail={
"type": "invalid_model",
"errorMessage": error_message,
},
)
2 changes: 1 addition & 1 deletion app/models/dtos.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Template(BaseModel):
content: str

template: Template
preferred_model: LLMModel = Field(..., alias="preferredModel")
preferred_model: str = Field(..., alias="preferredModel")
parameters: dict


Expand Down
26 changes: 21 additions & 5 deletions app/routes/messages.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from fastapi import APIRouter, Depends
from datetime import datetime, timezone

from app.core.custom_exceptions import BadDataException
from parsimonious.exceptions import IncompleteParseError

from app.core.custom_exceptions import (
MissingParameterException,
InvalidTemplateException,
InternalServerException,
InvalidModelException,
)
from app.dependencies import PermissionsValidator
from app.models.dtos import SendMessageRequest, SendMessageResponse
from app.models.dtos import SendMessageRequest, SendMessageResponse, LLMModel
from app.services.guidance_wrapper import GuidanceWrapper

router = APIRouter(tags=["messages"])
Expand All @@ -13,16 +20,25 @@
"/api/v1/messages", dependencies=[Depends(PermissionsValidator())]
)
def send_message(body: SendMessageRequest) -> SendMessageResponse:
try:
model = LLMModel(body.preferred_model)
except ValueError as e:
raise InvalidModelException(str(e))

guidance = GuidanceWrapper(
model=body.preferred_model,
model=model,
handlebars=body.template.content,
parameters=body.parameters,
)

try:
content = guidance.query()
except (KeyError, ValueError) as e:
raise BadDataException(str(e))
except KeyError as e:
raise MissingParameterException(str(e))
except (SyntaxError, IncompleteParseError) as e:
raise InvalidTemplateException(str(e))
except BaseException as e:
Hialus marked this conversation as resolved.
Show resolved Hide resolved
raise InternalServerException(str(e))

# Turn content into an array if it's not already
if not isinstance(content, list):
Expand Down
36 changes: 18 additions & 18 deletions tests/routes/messages_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,12 @@ def test_send_message_raise_value_error(test_client, headers, mocker):
"parameters": {"query": "Some query"},
}
response = test_client.post("/api/v1/messages", headers=headers, json=body)
assert response.status_code == 422
assert response.status_code == 500
assert response.json() == {
"detail": [
{
"loc": [],
"msg": "value error message",
"type": "value_error.bad_data",
}
]
"detail": {
"type": "other",
"errorMessage": "value error message",
}
}


Expand All @@ -101,26 +98,29 @@ def test_send_message_raise_key_error(test_client, headers, mocker):
"parameters": {"query": "Some query"},
}
response = test_client.post("/api/v1/messages", headers=headers, json=body)
assert response.status_code == 422
assert response.status_code == 400
assert response.json() == {
"detail": [
{
"loc": [],
"msg": "'key error message'",
"type": "value_error.bad_data",
}
]
"detail": {
"type": "missing_parameter",
"errorMessage": "'key error message'",
}
}


def test_send_message_with_wrong_api_key(test_client):
headers = {"Authorization": "wrong api key"}
response = test_client.post("/api/v1/messages", headers=headers, json={})
assert response.status_code == 403
assert response.json()["detail"] == "Permission denied"
assert response.json()["detail"] == {
"type": "not_authorized",
"errorMessage": "Permission denied",
}


def test_send_message_without_authorization_header(test_client):
response = test_client.post("/api/v1/messages", json={})
assert response.status_code == 401
assert response.json()["detail"] == "Requires authentication"
assert response.json()["detail"] == {
"type": "not_authenticated",
"errorMessage": "Requires authentication",
}