Elffuss BraidHub

kikocis/llama-stack public

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