Return 422 when model field is missing and no force_model is set
Prevents an opaque Ollama error from reaching the client by failing fast with a clear message before the request is forwarded.
This commit is contained in:
parent
34b108f4df
commit
f823e7d314
@ -92,6 +92,8 @@ async def generate(request: Request, db: Session = Depends(get_db)):
|
||||
force_model = crud.get_setting(db, "force_model") or None
|
||||
if force_model:
|
||||
body = {**body, "model": force_model}
|
||||
if not body.get("model"):
|
||||
raise HTTPException(status_code=422, detail="Field 'model' is required")
|
||||
prompt_tokens = crud.count_tokens(body.get("prompt", ""))
|
||||
|
||||
if not crud.check_and_increment_quota(db, request.state.api_key_id, tokens=prompt_tokens, requests=1):
|
||||
@ -119,6 +121,8 @@ async def chat(request: Request, db: Session = Depends(get_db)):
|
||||
force_model = crud.get_setting(db, "force_model") or None
|
||||
if force_model:
|
||||
body = {**body, "model": force_model}
|
||||
if not body.get("model"):
|
||||
raise HTTPException(status_code=422, detail="Field 'model' is required")
|
||||
messages = body.get("messages", [])
|
||||
prompt_tokens = sum(crud.count_tokens(_content_to_str(msg.get("content"))) for msg in messages)
|
||||
|
||||
@ -165,13 +169,15 @@ async def openai_chat_completions(request: Request, db: Session = Depends(get_db
|
||||
force_model = crud.get_setting(db, "force_model") or None
|
||||
if force_model:
|
||||
body = {**body, "model": force_model}
|
||||
if not body.get("model"):
|
||||
raise HTTPException(status_code=422, detail="Field 'model' is required")
|
||||
messages = body.get("messages", [])
|
||||
prompt_tokens = sum(crud.count_tokens(_content_to_str(msg.get("content"))) for msg in messages)
|
||||
|
||||
if not crud.check_and_increment_quota(db, request.state.api_key_id, tokens=prompt_tokens, requests=1):
|
||||
raise HTTPException(status_code=429, detail="Quota exceeded")
|
||||
|
||||
model_name = body.get("model", "?")
|
||||
model_name = body["model"]
|
||||
|
||||
usage_log.info('%s | /v1/chat/completions | %s | ~%d tokens | "%s"',
|
||||
request.state.api_key_name, model_name, prompt_tokens, _last_user_msg(messages))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user