|
| 1 | +"""Handler for RHEL Lightspeed rlsapi v1 REST API endpoints. |
| 2 | +
|
| 3 | +This module provides the /infer endpoint for stateless inference requests |
| 4 | +from the RHEL Lightspeed Command Line Assistant (CLA). |
| 5 | +""" |
| 6 | + |
| 7 | +import logging |
| 8 | +from typing import Annotated, Any |
| 9 | + |
| 10 | +from fastapi import APIRouter, Depends |
| 11 | + |
| 12 | +from authentication import get_auth_dependency |
| 13 | +from authentication.interface import AuthTuple |
| 14 | +from authorization.middleware import authorize |
| 15 | +from models.config import Action |
| 16 | +from models.responses import ( |
| 17 | + ForbiddenResponse, |
| 18 | + UnauthorizedResponse, |
| 19 | + UnprocessableEntityResponse, |
| 20 | +) |
| 21 | +from models.rlsapi.requests import RlsapiV1InferRequest |
| 22 | +from models.rlsapi.responses import RlsapiV1InferData, RlsapiV1InferResponse |
| 23 | +from utils.suid import get_suid |
| 24 | + |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | +router = APIRouter(tags=["rlsapi-v1"]) |
| 27 | + |
| 28 | + |
| 29 | +infer_responses: dict[int | str, dict[str, Any]] = { |
| 30 | + 200: RlsapiV1InferResponse.openapi_response(), |
| 31 | + 401: UnauthorizedResponse.openapi_response( |
| 32 | + examples=["missing header", "missing token"] |
| 33 | + ), |
| 34 | + 403: ForbiddenResponse.openapi_response(examples=["endpoint"]), |
| 35 | + 422: UnprocessableEntityResponse.openapi_response(), |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +@router.post("/infer", responses=infer_responses) |
| 40 | +@authorize(Action.RLSAPI_V1_INFER) |
| 41 | +async def infer_endpoint( |
| 42 | + infer_request: RlsapiV1InferRequest, |
| 43 | + auth: Annotated[AuthTuple, Depends(get_auth_dependency())], |
| 44 | +) -> RlsapiV1InferResponse: |
| 45 | + """Handle rlsapi v1 /infer requests for stateless inference. |
| 46 | +
|
| 47 | + This endpoint serves requests from the RHEL Lightspeed Command Line Assistant (CLA). |
| 48 | +
|
| 49 | + Accepts a question with optional context (stdin, attachments, terminal output, |
| 50 | + system info) and returns an LLM-generated response. |
| 51 | +
|
| 52 | + Args: |
| 53 | + infer_request: The inference request containing question and context. |
| 54 | + auth: Authentication tuple from the configured auth provider. |
| 55 | +
|
| 56 | + Returns: |
| 57 | + RlsapiV1InferResponse containing the generated response text and request ID. |
| 58 | + """ |
| 59 | + # Authentication enforced by get_auth_dependency(), authorization by @authorize decorator. |
| 60 | + _ = auth |
| 61 | + |
| 62 | + # Generate unique request ID |
| 63 | + request_id = get_suid() |
| 64 | + |
| 65 | + logger.info("Processing rlsapi v1 /infer request %s", request_id) |
| 66 | + |
| 67 | + # Combine all input sources (question, stdin, attachments, terminal) |
| 68 | + input_source = infer_request.get_input_source() |
| 69 | + logger.debug("Combined input source length: %d", len(input_source)) |
| 70 | + |
| 71 | + # NOTE(major): Placeholder until we wire up the LLM integration. |
| 72 | + response_text = ( |
| 73 | + "Inference endpoint is functional. " |
| 74 | + "LLM integration will be added in a subsequent update." |
| 75 | + ) |
| 76 | + |
| 77 | + return RlsapiV1InferResponse( |
| 78 | + data=RlsapiV1InferData( |
| 79 | + text=response_text, |
| 80 | + request_id=request_id, |
| 81 | + ) |
| 82 | + ) |
0 commit comments