| 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 llama_toolchain.agentic_system.api import AgenticSystem |
| 12 | from llama_toolchain.inference.api import Inference |
| 13 | from llama_toolchain.memory.api import Memory |
| 14 | from llama_toolchain.safety.api import Safety |
| 15 | from llama_toolchain.telemetry.api import Telemetry |
| 16 | |
| 17 | from .datatypes import Api, ApiEndpoint, ProviderSpec, remote_provider_spec |
| 18 | |
| 19 | # These are the dependencies needed by the distribution server. |
| 20 | # `llama-toolchain` is automatically installed by the installation script. |
| 21 | SERVER_DEPENDENCIES = [ |
| 22 | "fastapi", |
| 23 | "uvicorn", |
| 24 | ] |
| 25 | |
| 26 | |
| 27 | def stack_apis() -> List[Api]: |
| 28 | return [v for v in Api] |
| 29 | |
| 30 | |
| 31 | def api_endpoints() -> Dict[Api, List[ApiEndpoint]]: |
| 32 | apis = {} |
| 33 | |
| 34 | protocols = { |
| 35 | Api.inference: Inference, |
| 36 | Api.safety: Safety, |
| 37 | Api.agentic_system: AgenticSystem, |
| 38 | Api.memory: Memory, |
| 39 | Api.telemetry: Telemetry, |
| 40 | } |
| 41 | |
| 42 | for api, protocol in protocols.items(): |
| 43 | endpoints = [] |
| 44 | protocol_methods = inspect.getmembers(protocol, predicate=inspect.isfunction) |
| 45 | |
| 46 | for name, method in protocol_methods: |
| 47 | if not hasattr(method, "__webmethod__"): |
| 48 | continue |
| 49 | |
| 50 | webmethod = method.__webmethod__ |
| 51 | route = webmethod.route |
| 52 | |
| 53 | if webmethod.method == "GET": |
| 54 | method = "get" |
| 55 | elif webmethod.method == "DELETE": |
| 56 | method = "delete" |
| 57 | else: |
| 58 | method = "post" |
| 59 | endpoints.append(ApiEndpoint(route=route, method=method, name=name)) |
| 60 | |
| 61 | apis[api] = endpoints |
| 62 | |
| 63 | return apis |
| 64 | |
| 65 | |
| 66 | def api_providers() -> Dict[Api, Dict[str, ProviderSpec]]: |
| 67 | ret = {} |
| 68 | for api in stack_apis(): |
| 69 | name = api.name.lower() |
| 70 | module = importlib.import_module(f"llama_toolchain.{name}.providers") |
| 71 | ret[api] = { |
| 72 | "remote": remote_provider_spec(api), |
| 73 | **{a.provider_id: a for a in module.available_providers()}, |
| 74 | } |
| 75 | |
| 76 | return ret |