| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | import importlib |
| 8 | import inspect |
| 9 | from typing import Dict, List |
| 10 | |
| 11 | from pydantic import BaseModel |
| 12 | |
| 13 | from llama_stack.apis.agents import Agents |
| 14 | from llama_stack.apis.inference import Inference |
| 15 | from llama_stack.apis.memory import Memory |
| 16 | from llama_stack.apis.memory_banks import MemoryBanks |
| 17 | from llama_stack.apis.models import Models |
| 18 | from llama_stack.apis.safety import Safety |
| 19 | from llama_stack.apis.shields import Shields |
| 20 | from llama_stack.apis.telemetry import Telemetry |
| 21 | |
| 22 | from .datatypes import Api, ApiEndpoint, ProviderSpec, remote_provider_spec |
| 23 | |
| 24 | # These are the dependencies needed by the distribution server. |
| 25 | # `llama-stack` is automatically installed by the installation script. |
| 26 | SERVER_DEPENDENCIES = [ |
| 27 | "fastapi", |
| 28 | "fire", |
| 29 | "httpx", |
| 30 | "uvicorn", |
| 31 | ] |
| 32 | |
| 33 | |
| 34 | def stack_apis() -> List[Api]: |
| 35 | return [v for v in Api] |
| 36 | |
| 37 | |
| 38 | class AutoRoutedApiInfo(BaseModel): |
| 39 | routing_table_api: Api |
| 40 | router_api: Api |
| 41 | |
| 42 | |
| 43 | def builtin_automatically_routed_apis() -> List[AutoRoutedApiInfo]: |
| 44 | return [ |
| 45 | AutoRoutedApiInfo( |
| 46 | routing_table_api=Api.models, |
| 47 | router_api=Api.inference, |
| 48 | ), |
| 49 | AutoRoutedApiInfo( |
| 50 | routing_table_api=Api.shields, |
| 51 | router_api=Api.safety, |
| 52 | ), |
| 53 | AutoRoutedApiInfo( |
| 54 | routing_table_api=Api.memory_banks, |
| 55 | router_api=Api.memory, |
| 56 | ), |
| 57 | ] |
| 58 | |
| 59 | |
| 60 | def api_endpoints() -> Dict[Api, List[ApiEndpoint]]: |
| 61 | apis = {} |
| 62 | |
| 63 | protocols = { |
| 64 | Api.inference: Inference, |
| 65 | Api.safety: Safety, |
| 66 | Api.agents: Agents, |
| 67 | Api.memory: Memory, |
| 68 | Api.telemetry: Telemetry, |
| 69 | Api.models: Models, |
| 70 | Api.shields: Shields, |
| 71 | Api.memory_banks: MemoryBanks, |
| 72 | } |
| 73 | |
| 74 | for api, protocol in protocols.items(): |
| 75 | endpoints = [] |
| 76 | protocol_methods = inspect.getmembers(protocol, predicate=inspect.isfunction) |
| 77 | |
| 78 | for name, method in protocol_methods: |
| 79 | if not hasattr(method, "__webmethod__"): |
| 80 | continue |
| 81 | |
| 82 | webmethod = method.__webmethod__ |
| 83 | route = webmethod.route |
| 84 | |
| 85 | if webmethod.method == "GET": |
| 86 | method = "get" |
| 87 | elif webmethod.method == "DELETE": |
| 88 | method = "delete" |
| 89 | else: |
| 90 | method = "post" |
| 91 | endpoints.append(ApiEndpoint(route=route, method=method, name=name)) |
| 92 | |
| 93 | apis[api] = endpoints |
| 94 | |
| 95 | return apis |
| 96 | |
| 97 | |
| 98 | def api_providers() -> Dict[Api, Dict[str, ProviderSpec]]: |
| 99 | ret = {} |
| 100 | routing_table_apis = set( |
| 101 | x.routing_table_api for x in builtin_automatically_routed_apis() |
| 102 | ) |
| 103 | for api in stack_apis(): |
| 104 | if api in routing_table_apis: |
| 105 | continue |
| 106 | |
| 107 | name = api.name.lower() |
| 108 | module = importlib.import_module(f"llama_stack.providers.registry.{name}") |
| 109 | ret[api] = { |
| 110 | "remote": remote_provider_spec(api), |
| 111 | **{a.provider_id: a for a in module.available_providers()}, |
| 112 | } |
| 113 | |
| 114 | return ret |