Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
phact committed Mar 19, 2024
1 parent 9d5fddc commit 3408e18
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.1.5
v0.1.6
4 changes: 3 additions & 1 deletion impl/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import httpx
import openai
import uvicorn
from fastapi import FastAPI, Request
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import JSONResponse
from prometheus_client import Counter, Summary, Histogram
from prometheus_fastapi_instrumentator import Instrumentator
Expand Down Expand Up @@ -158,6 +158,8 @@ async def generic_exception_handler(request: Request, exc: Exception):
# Log the error
logger.error(f"Unexpected error: {exc}")

if isinstance(exc, HTTPException):
raise exec
# Return an error response, not sure if we want to return all errors but at least this surfaces things like bad embedding model. Though that should be a 4xx error?
return JSONResponse(
status_code=501, content={"message": str(exc)}
Expand Down
3 changes: 2 additions & 1 deletion impl/routes/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
Path,
Request,
Query,
UploadFile, HTTPException,
UploadFile,
HTTPException,
)
from litellm import utils

Expand Down
18 changes: 12 additions & 6 deletions impl/services/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

import docx2txt
import pptx
from fastapi import UploadFile
from fastapi import UploadFile, HTTPException
from loguru import logger
from PyPDF2 import PdfReader

from impl.astra_vector import HandledResponse
from impl.models import Document


Expand All @@ -32,7 +33,7 @@ def extract_text_from_filepath(filepath: str, mimetype: Optional[str] = None) ->
if filepath.endswith(".md"):
mimetype = "text/markdown"
else:
raise Exception("Unsupported file type")
raise HTTPException(status_code=400, detail="Unsupported file type")

try:
with open(filepath, "rb") as file:
Expand Down Expand Up @@ -82,12 +83,17 @@ def extract_text_from_file(file: BufferedReader, mimetype: str) -> str:
extracted_text += run.text + " "
extracted_text += "\n"
else:
extension = mimetypes.guess_extension(mimetype)[1:]
if extension in ("c", "cpp", "css", "html", "java", "js", "json", "md", "php", "py", "rb", "ts", "xml"):
extracted_text = file.read().decode("utf-8")
raw_extension = mimetypes.guess_extension(mimetype)
if raw_extension is not None:
extension = raw_extension[1:]
if extension in ("c", "cpp", "css", "html", "java", "js", "json", "md", "php", "py", "rb", "ts", "xml"):
extracted_text = file.read().decode("utf-8")
else:
# Unsupported file type
raise ValueError("Unsupported file type: {}".format(mimetype))
raise HTTPException(
status_code=400,
detail="Unsupported file type: {}".format(mimetype),
)
return extracted_text


Expand Down
Empty file added tests/fixtures/invalid
Empty file.
16 changes: 15 additions & 1 deletion tests/streaming-assistants/test_file_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,18 @@ def test_file_embedding_python(patched_openai_client):
purpose="assistants",
embedding_model="text-embedding-3-large",
)
logger.info(file)
logger.info(file)

def test_file_embedding_python(patched_openai_client):
try:
patched_openai_client.files.create(
file=open(
"./tests/fixtures/invalid",
"rb",
),
purpose="assistants",
embedding_model="text-embedding-3-large",
)
except Exception as e:
assert e.status_code == 400
logger.info(e)

0 comments on commit 3408e18

Please sign in to comment.