mirror of
https://github.com/fauxpilot/fauxpilot.git
synced 2025-08-19 21:04:08 -07:00
Reformat the error to match OpenAI's
This commit is contained in:
parent
e2486698e0
commit
b7b85461af
2 changed files with 33 additions and 4 deletions
|
@ -2,12 +2,14 @@ import logging
|
|||
import os
|
||||
|
||||
import uvicorn
|
||||
from fastapi import FastAPI, Response, HTTPException
|
||||
from fastapi import FastAPI, Request, Response
|
||||
from fastapi.responses import JSONResponse
|
||||
from sse_starlette.sse import EventSourceResponse
|
||||
|
||||
from config.log_config import uvicorn_logger
|
||||
from models import OpenAIinput
|
||||
from utils.codegen import CodeGenProxy
|
||||
from utils.errors import FauxPilotException
|
||||
|
||||
logging.config.dictConfig(uvicorn_logger)
|
||||
|
||||
|
@ -25,6 +27,12 @@ app = FastAPI(
|
|||
swagger_ui_parameters={"defaultModelsExpandDepth": -1}
|
||||
)
|
||||
|
||||
@app.exception_handler(FauxPilotException)
|
||||
async def fauxpilot_handler(request: Request, exc: FauxPilotException):
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content=exc.json()
|
||||
)
|
||||
|
||||
@app.post("/v1/engines/codegen/completions")
|
||||
@app.post("/v1/completions")
|
||||
|
@ -33,9 +41,11 @@ async def completions(data: OpenAIinput):
|
|||
try:
|
||||
content = codegen(data=data)
|
||||
except codegen.TokensExceedsMaximum as E:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=str(E)
|
||||
raise FauxPilotException(
|
||||
message=str(E),
|
||||
type="invalid_request_error",
|
||||
param=None,
|
||||
code=None,
|
||||
)
|
||||
|
||||
if data.get("stream") is not None:
|
||||
|
|
19
copilot_proxy/utils/errors.py
Normal file
19
copilot_proxy/utils/errors.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
from typing import *
|
||||
|
||||
class FauxPilotException(Exception):
|
||||
def __init__(self, message: str, type: Optional[str] = None, param: Optional[str] = None, code: Optional[int] = None):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
self.type = type
|
||||
self.param = param
|
||||
self.code = code
|
||||
|
||||
def json(self):
|
||||
return {
|
||||
'error': {
|
||||
'message': self.message,
|
||||
'type': self.type,
|
||||
'param': self.param,
|
||||
'code': self.code
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue