From f45f55b50abb6b2502bab3b6dfc16cf405566dc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Sun, 7 Jun 2026 10:57:34 +0800 Subject: [PATCH] add /api/v3/llm/chat/completions --- routes/chat.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/routes/chat.py b/routes/chat.py index b6b29e4..07819a3 100644 --- a/routes/chat.py +++ b/routes/chat.py @@ -1018,6 +1018,10 @@ async def llm_passthrough_v3(request: ChatRequestV3, authorization: Optional[str - stream: bool - whether to stream the output, default false - user_identifier: str - used to resolve the api_key owner + Authentication: + - Authorization header is required: Bearer + - token = md5(MASTERKEY:bot_id), same scheme as the v2 API + Returns: Union[dict, StreamingResponse]: OpenAI-compatible completion or stream """ @@ -1026,12 +1030,21 @@ async def llm_passthrough_v3(request: ChatRequestV3, authorization: Optional[str if not bot_id: raise HTTPException(status_code=400, detail="bot_id is required") - # Optional auth check (consistent with v3, non-blocking) - if authorization: - expected_token = generate_v2_auth_token(bot_id) - provided_token = extract_api_key_from_auth(authorization) - if provided_token and provided_token != expected_token: - logger.warning("Invalid auth token provided for LLM passthrough API, but continuing anyway") + # Authentication validation (same auth logic as v2: token = md5(MASTERKEY:bot_id)) + expected_token = generate_v2_auth_token(bot_id) + provided_token = extract_api_key_from_auth(authorization) + + if not provided_token: + raise HTTPException( + status_code=401, + detail="Authorization header is required" + ) + + if provided_token != expected_token: + raise HTTPException( + status_code=403, + detail=f"Invalid authentication token. Expected: {expected_token[:8]}..., Provided: {provided_token[:8]}..." + ) # Build the LLM client from db config llm, model_name = await build_llm_from_bot_config(bot_id, request.user_identifier)