-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathinfo.py
More file actions
85 lines (72 loc) · 2.66 KB
/
info.py
File metadata and controls
85 lines (72 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""Handler for REST API call to provide info."""
import logging
from typing import Annotated, Any
from fastapi import APIRouter, HTTPException, Request, status
from fastapi import Depends
from llama_stack_client import APIConnectionError
from authentication.interface import AuthTuple
from authentication import get_auth_dependency
from authorization.middleware import authorize
from configuration import configuration
from client import AsyncLlamaStackClientHolder
from models.config import Action
from models.responses import InfoResponse
from version import __version__
logger = logging.getLogger("app.endpoints.handlers")
router = APIRouter(tags=["info"])
auth_dependency = get_auth_dependency()
get_info_responses: dict[int | str, dict[str, Any]] = {
200: {
"name": "Service name",
"service_version": "Service version",
"llama_stack_version": "Llama Stack version",
},
500: {
"detail": {
"response": "Unable to connect to Llama Stack",
"cause": "Connection error.",
}
},
}
@router.get("/info", responses=get_info_responses)
@authorize(Action.INFO)
async def info_endpoint_handler(
auth: Annotated[AuthTuple, Depends(auth_dependency)],
request: Request,
) -> InfoResponse:
"""
Handle request to the /info endpoint.
Process GET requests to the /info endpoint, returning the
service name, version and Llama-stack version.
Returns:
InfoResponse: An object containing the service's name and version.
"""
# Used only for authorization
_ = auth
# Nothing interesting in the request
_ = request
logger.info("Response to /v1/info endpoint")
try:
# try to get Llama Stack client
client = AsyncLlamaStackClientHolder().get_client()
# retrieve version
llama_stack_version_object = await client.inspect.version()
llama_stack_version = llama_stack_version_object.version
logger.debug("Service name: %s", configuration.configuration.name)
logger.debug("Service version: %s", __version__)
logger.debug("LLama Stack version: %s", llama_stack_version)
return InfoResponse(
name=configuration.configuration.name,
service_version=__version__,
llama_stack_version=llama_stack_version,
)
# connection to Llama Stack server
except APIConnectionError as e:
logger.error("Unable to connect to Llama Stack: %s", e)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail={
"response": "Unable to connect to Llama Stack",
"cause": str(e),
},
) from e