add /api/v3/llm/chat/completions

This commit is contained in:
朱潮 2026-06-07 10:57:34 +08:00
parent f18d966123
commit f45f55b50a

View File

@ -1018,6 +1018,10 @@ async def llm_passthrough_v3(request: ChatRequestV3, authorization: Optional[str
- stream: bool - whether to stream the output, default false - stream: bool - whether to stream the output, default false
- user_identifier: str - used to resolve the api_key owner - user_identifier: str - used to resolve the api_key owner
Authentication:
- Authorization header is required: Bearer <token>
- token = md5(MASTERKEY:bot_id), same scheme as the v2 API
Returns: Returns:
Union[dict, StreamingResponse]: OpenAI-compatible completion or stream 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: if not bot_id:
raise HTTPException(status_code=400, detail="bot_id is required") raise HTTPException(status_code=400, detail="bot_id is required")
# Optional auth check (consistent with v3, non-blocking) # Authentication validation (same auth logic as v2: token = md5(MASTERKEY:bot_id))
if authorization: expected_token = generate_v2_auth_token(bot_id)
expected_token = generate_v2_auth_token(bot_id) provided_token = extract_api_key_from_auth(authorization)
provided_token = extract_api_key_from_auth(authorization)
if provided_token and provided_token != expected_token: if not provided_token:
logger.warning("Invalid auth token provided for LLM passthrough API, but continuing anyway") 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 # Build the LLM client from db config
llm, model_name = await build_llm_from_bot_config(bot_id, request.user_identifier) llm, model_name = await build_llm_from_bot_config(bot_id, request.user_identifier)