Elffuss BraidHub

kikocis/llama-stack public

bb9be718c121 · 3.8 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
7# Copyright (c) Meta Platforms, Inc. and affiliates.
8# All rights reserved.
9#
10# This source code is licensed under the terms described found in the
11# LICENSE file in the root directory of this source tree.
12
13from datetime import datetime
14from pathlib import Path
15
16import fire
17import yaml
18
19from llama_models import schema_utils
20
21from .pyopenapi.options import Options
22from .pyopenapi.specification import Info, Server
23from .pyopenapi.utility import Specification
24
25# We do some monkey-patching to ensure our definitions only use the minimal
26# (json_schema_type, webmethod) definitions from the llama_models package. For
27# generation though, we need the full definitions and implementations from the
28# (json-strong-typing) package.
29
30from .strong_typing.schema import json_schema_type
31
32schema_utils.json_schema_type = json_schema_type
33
34from llama_models.llama3.api.datatypes import * # noqa: F403
35from llama_stack.apis.agents import * # noqa: F403
36from llama_stack.apis.dataset import * # noqa: F403
37from llama_stack.apis.evals import * # noqa: F403
38from llama_stack.apis.inference import * # noqa: F403
39from llama_stack.apis.batch_inference import * # noqa: F403
40from llama_stack.apis.memory import * # noqa: F403
41from llama_stack.apis.telemetry import * # noqa: F403
42from llama_stack.apis.post_training import * # noqa: F403
43from llama_stack.apis.reward_scoring import * # noqa: F403
44from llama_stack.apis.synthetic_data_generation import * # noqa: F403
45from llama_stack.apis.safety import * # noqa: F403
46from llama_stack.apis.models import * # noqa: F403
47from llama_stack.apis.memory_banks import * # noqa: F403
48from llama_stack.apis.shields import * # noqa: F403
49
50
51class LlamaStack(
52 MemoryBanks,
53 Inference,
54 BatchInference,
55 Agents,
56 RewardScoring,
57 Safety,
58 SyntheticDataGeneration,
59 Datasets,
60 Telemetry,
61 PostTraining,
62 Memory,
63 Evaluations,
64 Models,
65 Shields,
66):
67 pass
68
69
70# TODO: this should be fixed in the generator itself so it reads appropriate annotations
71STREAMING_ENDPOINTS = [
72 "/agentic_system/turn/create",
73 "/inference/chat_completion",
74]
75
76
77def patch_sse_stream_responses(spec: Specification):
78 for path, path_item in spec.document.paths.items():
79 if path in STREAMING_ENDPOINTS:
80 content = path_item.post.responses["200"].content.pop("application/json")
81 path_item.post.responses["200"].content["text/event-stream"] = content
82
83
84def main(output_dir: str):
85 output_dir = Path(output_dir)
86 if not output_dir.exists():
87 raise ValueError(f"Directory {output_dir} does not exist")
88
89 now = str(datetime.now())
90 print(
91 "Converting the spec to YAML (openapi.yaml) and HTML (openapi.html) at " + now
92 )
93 print("")
94 spec = Specification(
95 LlamaStack,
96 Options(
97 server=Server(url="http://any-hosted-llama-stack.com"),
98 info=Info(
99 title="[DRAFT] Llama Stack Specification",
100 version="0.0.1",
101 description="""This is the specification of the llama stack that provides
102 a set of endpoints and their corresponding interfaces that are tailored to
103 best leverage Llama Models. The specification is still in draft and subject to change.
104 Generated at """
105 + now,
106 ),
107 ),
108 )
109
110 patch_sse_stream_responses(spec)
111
112 with open(output_dir / "llama-stack-spec.yaml", "w", encoding="utf-8") as fp:
113 yaml.dump(spec.get_json(), fp, allow_unicode=True)
114
115 with open(output_dir / "llama-stack-spec.html", "w") as fp:
116 spec.write_html(fp, pretty_print=True)
117
118
119if __name__ == "__main__":
120 fire.Fire(main)