Skip to content

Commit

Permalink
Fix a possible bug that occurs when attempting to encode a dictionary…
Browse files Browse the repository at this point in the history
… (dict) object as a string in a FastAPI application.
  • Loading branch information
yym68686 committed Jul 24, 2024
1 parent 1d0ce62 commit 1ad99a8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async def process_request(request: RequestModel, provider: Dict):

if provider.get("engine"):
engine = provider["engine"]
print("provider: ", provider['provider'], "engine: ", engine)
print("provider:", provider['provider'], "engine:", engine)

url, headers, payload = await get_payload(request, engine, provider)

Expand Down Expand Up @@ -165,7 +165,13 @@ async def try_all_providers(self, request: RequestModel, providers: List[Dict],

@app.post("/v1/chat/completions")
async def request_model(request: RequestModel, token: str = Depends(verify_api_key)):
return await model_handler.request_model(request, token)
try:
return await model_handler.request_model(request, token)
except Exception as e:
print('\033[31m')
print(f"request_model Error: {str(e)}")
traceback.print_exc()
print('\033[0m')

@app.options("/v1/chat/completions")
async def options_handler():
Expand Down
2 changes: 1 addition & 1 deletion utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def error_handling_wrapper(generator, status_code=200):
first_item_str = json.loads(first_item_str)
if isinstance(first_item_str, dict) and 'error' in first_item_str:
# 如果第一个 yield 的项是错误信息,抛出 HTTPException
raise HTTPException(status_code=status_code, detail=f"{first_item_str}"[:200])
raise HTTPException(status_code=status_code, detail=f"{first_item_str}"[:300])

# 如果不是错误,创建一个新的生成器,首先yield第一个项,然后yield剩余的项
async def new_generator():
Expand Down

0 comments on commit 1ad99a8

Please sign in to comment.