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 os
|
||||||
|
|
||||||
import uvicorn
|
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 sse_starlette.sse import EventSourceResponse
|
||||||
|
|
||||||
from config.log_config import uvicorn_logger
|
from config.log_config import uvicorn_logger
|
||||||
from models import OpenAIinput
|
from models import OpenAIinput
|
||||||
from utils.codegen import CodeGenProxy
|
from utils.codegen import CodeGenProxy
|
||||||
|
from utils.errors import FauxPilotException
|
||||||
|
|
||||||
logging.config.dictConfig(uvicorn_logger)
|
logging.config.dictConfig(uvicorn_logger)
|
||||||
|
|
||||||
|
@ -25,6 +27,12 @@ app = FastAPI(
|
||||||
swagger_ui_parameters={"defaultModelsExpandDepth": -1}
|
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/engines/codegen/completions")
|
||||||
@app.post("/v1/completions")
|
@app.post("/v1/completions")
|
||||||
|
@ -33,9 +41,11 @@ async def completions(data: OpenAIinput):
|
||||||
try:
|
try:
|
||||||
content = codegen(data=data)
|
content = codegen(data=data)
|
||||||
except codegen.TokensExceedsMaximum as E:
|
except codegen.TokensExceedsMaximum as E:
|
||||||
raise HTTPException(
|
raise FauxPilotException(
|
||||||
status_code=400,
|
message=str(E),
|
||||||
detail=str(E)
|
type="invalid_request_error",
|
||||||
|
param=None,
|
||||||
|
code=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
if data.get("stream") is not 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