| 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 ( |
| 18 | Api, |
| 19 | ApiEndpoint, |
| 20 | DistributionSpec, |
| 21 | InlineProviderSpec, |
| 22 | ProviderSpec, |
| 23 | remote_provider_spec, |
| 24 | ) |
| 25 | |
| 26 | # These are the dependencies needed by the distribution server. |
| 27 | # `llama-toolchain` is automatically installed by the installation script. |
| 28 | SERVER_DEPENDENCIES = [ |
| 29 | "fastapi", |
| 30 | "uvicorn", |
| 31 | ] |
| 32 | |
| 33 | |
| 34 | def distribution_dependencies(distribution: DistributionSpec) -> List[str]: |
| 35 | # only consider InlineProviderSpecs when calculating dependencies |
| 36 | return [ |
| 37 | dep |
| 38 | for provider_spec in distribution.provider_specs.values() |
| 39 | if isinstance(provider_spec, InlineProviderSpec) |
| 40 | for dep in provider_spec.pip_packages |
| 41 | ] + SERVER_DEPENDENCIES |
| 42 | |
| 43 | |
| 44 | def stack_apis() -> List[Api]: |
| 45 | return [v for v in Api] |
| 46 | |
| 47 | |
| 48 | def api_endpoints() -> Dict[Api, List[ApiEndpoint]]: |
| 49 | apis = {} |
| 50 | |
| 51 | protocols = { |
| 52 | Api.inference: Inference, |
| 53 | Api.safety: Safety, |
| 54 | Api.agentic_system: AgenticSystem, |
| 55 | Api.memory: Memory, |
| 56 | Api.telemetry: Telemetry, |
| 57 | } |
| 58 | |
| 59 | for api, protocol in protocols.items(): |
| 60 | endpoints = [] |
| 61 | protocol_methods = inspect.getmembers(protocol, predicate=inspect.isfunction) |
| 62 | |
| 63 | for name, method in protocol_methods: |
| 64 | if not hasattr(method, "__webmethod__"): |
| 65 | continue |
| 66 | |
| 67 | webmethod = method.__webmethod__ |
| 68 | route = webmethod.route |
| 69 | |
| 70 | if webmethod.method == "GET": |
| 71 | method = "get" |
| 72 | elif webmethod.method == "DELETE": |
| 73 | method = "delete" |
| 74 | else: |
| 75 | method = "post" |
| 76 | endpoints.append(ApiEndpoint(route=route, method=method, name=name)) |
| 77 | |
| 78 | apis[api] = endpoints |
| 79 | |
| 80 | return apis |
| 81 | |
| 82 | |
| 83 | def api_providers() -> Dict[Api, Dict[str, ProviderSpec]]: |
| 84 | ret = {} |
| 85 | for api in stack_apis(): |
| 86 | name = api.name.lower() |
| 87 | module = importlib.import_module(f"llama_toolchain.{name}.providers") |
| 88 | ret[api] = { |
| 89 | "remote": remote_provider_spec(api), |
| 90 | **{a.provider_type: a for a in module.available_providers()}, |
| 91 | } |
| 92 | |
| 93 | return ret |