|
| 1 | +"""Handler for REST API call to list available shields.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import Annotated, Any |
| 5 | + |
| 6 | +from fastapi import APIRouter, HTTPException, Request, status |
| 7 | +from fastapi.params import Depends |
| 8 | +from llama_stack_client import APIConnectionError |
| 9 | + |
| 10 | +from authentication import get_auth_dependency |
| 11 | +from authentication.interface import AuthTuple |
| 12 | +from authorization.middleware import authorize |
| 13 | +from client import AsyncLlamaStackClientHolder |
| 14 | +from configuration import configuration |
| 15 | +from models.config import Action |
| 16 | +from models.responses import ShieldsResponse |
| 17 | +from utils.endpoints import check_configuration_loaded |
| 18 | + |
| 19 | +logger = logging.getLogger(__name__) |
| 20 | +router = APIRouter(tags=["shields"]) |
| 21 | + |
| 22 | + |
| 23 | +shields_responses: dict[int | str, dict[str, Any]] = { |
| 24 | + 200: { |
| 25 | + "shields": [ |
| 26 | + { |
| 27 | + "identifier": "lightspeed_question_validity-shield", |
| 28 | + "provider_resource_id": "lightspeed_question_validity-shield", |
| 29 | + "provider_id": "lightspeed_question_validity", |
| 30 | + "type": "shield", |
| 31 | + "params": {}, |
| 32 | + } |
| 33 | + ] |
| 34 | + }, |
| 35 | + 500: {"description": "Connection to Llama Stack is broken"}, |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +@router.get("/shields", responses=shields_responses) |
| 40 | +@authorize(Action.GET_SHIELDS) |
| 41 | +async def shields_endpoint_handler( |
| 42 | + request: Request, |
| 43 | + auth: Annotated[AuthTuple, Depends(get_auth_dependency())], |
| 44 | +) -> ShieldsResponse: |
| 45 | + """ |
| 46 | + Handle requests to the /shields endpoint. |
| 47 | +
|
| 48 | + Process GET requests to the /shields endpoint, returning a list of available |
| 49 | + shields from the Llama Stack service. |
| 50 | +
|
| 51 | + Raises: |
| 52 | + HTTPException: If unable to connect to the Llama Stack server or if |
| 53 | + shield retrieval fails for any reason. |
| 54 | +
|
| 55 | + Returns: |
| 56 | + ShieldsResponse: An object containing the list of available shields. |
| 57 | + """ |
| 58 | + # Used only by the middleware |
| 59 | + _ = auth |
| 60 | + |
| 61 | + # Nothing interesting in the request |
| 62 | + _ = request |
| 63 | + |
| 64 | + check_configuration_loaded(configuration) |
| 65 | + |
| 66 | + llama_stack_configuration = configuration.llama_stack_configuration |
| 67 | + logger.info("Llama stack config: %s", llama_stack_configuration) |
| 68 | + |
| 69 | + try: |
| 70 | + # try to get Llama Stack client |
| 71 | + client = AsyncLlamaStackClientHolder().get_client() |
| 72 | + # retrieve shields |
| 73 | + shields = await client.shields.list() |
| 74 | + s = [dict(s) for s in shields] |
| 75 | + return ShieldsResponse(shields=s) |
| 76 | + |
| 77 | + # connection to Llama Stack server |
| 78 | + except APIConnectionError as e: |
| 79 | + logger.error("Unable to connect to Llama Stack: %s", e) |
| 80 | + raise HTTPException( |
| 81 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
| 82 | + detail={ |
| 83 | + "response": "Unable to connect to Llama Stack", |
| 84 | + "cause": str(e), |
| 85 | + }, |
| 86 | + ) from e |
| 87 | + # any other exception that can occur during shield listing |
| 88 | + except Exception as e: |
| 89 | + logger.error("Unable to retrieve list of shields: %s", e) |
| 90 | + raise HTTPException( |
| 91 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
| 92 | + detail={ |
| 93 | + "response": "Unable to retrieve list of shields", |
| 94 | + "cause": str(e), |
| 95 | + }, |
| 96 | + ) from e |
0 commit comments