yym68686 commited on
Commit
1ad99a8
·
1 Parent(s): 1d0ce62

Fix a possible bug that occurs when attempting to encode a dictionary (dict) object as a string in a FastAPI application.

Browse files
Files changed (2) hide show
  1. main.py +8 -2
  2. utils.py +1 -1
main.py CHANGED
@@ -58,7 +58,7 @@ async def process_request(request: RequestModel, provider: Dict):
58
 
59
  if provider.get("engine"):
60
  engine = provider["engine"]
61
- print("provider: ", provider['provider'], "engine: ", engine)
62
 
63
  url, headers, payload = await get_payload(request, engine, provider)
64
 
@@ -165,7 +165,13 @@ model_handler = ModelRequestHandler()
165
 
166
  @app.post("/v1/chat/completions")
167
  async def request_model(request: RequestModel, token: str = Depends(verify_api_key)):
168
- return await model_handler.request_model(request, token)
 
 
 
 
 
 
169
 
170
  @app.options("/v1/chat/completions")
171
  async def options_handler():
 
58
 
59
  if provider.get("engine"):
60
  engine = provider["engine"]
61
+ print("provider:", provider['provider'], "engine:", engine)
62
 
63
  url, headers, payload = await get_payload(request, engine, provider)
64
 
 
165
 
166
  @app.post("/v1/chat/completions")
167
  async def request_model(request: RequestModel, token: str = Depends(verify_api_key)):
168
+ try:
169
+ return await model_handler.request_model(request, token)
170
+ except Exception as e:
171
+ print('\033[31m')
172
+ print(f"request_model Error: {str(e)}")
173
+ traceback.print_exc()
174
+ print('\033[0m')
175
 
176
  @app.options("/v1/chat/completions")
177
  async def options_handler():
utils.py CHANGED
@@ -45,7 +45,7 @@ async def error_handling_wrapper(generator, status_code=200):
45
  first_item_str = json.loads(first_item_str)
46
  if isinstance(first_item_str, dict) and 'error' in first_item_str:
47
  # 如果第一个 yield 的项是错误信息,抛出 HTTPException
48
- raise HTTPException(status_code=status_code, detail=f"{first_item_str}"[:200])
49
 
50
  # 如果不是错误,创建一个新的生成器,首先yield第一个项,然后yield剩余的项
51
  async def new_generator():
 
45
  first_item_str = json.loads(first_item_str)
46
  if isinstance(first_item_str, dict) and 'error' in first_item_str:
47
  # 如果第一个 yield 的项是错误信息,抛出 HTTPException
48
+ raise HTTPException(status_code=status_code, detail=f"{first_item_str}"[:300])
49
 
50
  # 如果不是错误,创建一个新的生成器,首先yield第一个项,然后yield剩余的项
51
  async def new_generator():