Elffuss BraidHub

kikocis/llama-stack public

3fc339af16ee · 3.6 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
21# We do some monkey-patching to ensure our definitions only use the minimal
22# (json_schema_type, webmethod) definitions from the llama_models package. For
23# generation though, we need the full definitions and implementations from the
24# (json-strong-typing) package.
25
26from strong_typing.schema import json_schema_type
27
28from .pyopenapi.options import Options
29from .pyopenapi.specification import Info, Server
30from .pyopenapi.utility import Specification
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
46
47
48class LlamaStack(
49 Inference,
50 BatchInference,
51 Agents,
52 RewardScoring,
53 Safety,
54 SyntheticDataGeneration,
55 Datasets,
56 Telemetry,
57 PostTraining,
58 Memory,
59 Evaluations,
60):
61 pass
62
63
64# TODO: this should be fixed in the generator itself so it reads appropriate annotations
65STREAMING_ENDPOINTS = [
66 "/agentic_system/turn/create",
67 "/inference/chat_completion",
68]
69
70
71def patch_sse_stream_responses(spec: Specification):
72 for path, path_item in spec.document.paths.items():
73 if path in STREAMING_ENDPOINTS:
74 content = path_item.post.responses["200"].content.pop("application/json")
75 path_item.post.responses["200"].content["text/event-stream"] = content
76
77
78def main(output_dir: str):
79 output_dir = Path(output_dir)
80 if not output_dir.exists():
81 raise ValueError(f"Directory {output_dir} does not exist")
82
83 now = str(datetime.now())
84 print(
85 "Converting the spec to YAML (openapi.yaml) and HTML (openapi.html) at " + now
86 )
87 print("")
88 spec = Specification(
89 LlamaStack,
90 Options(
91 server=Server(url="http://any-hosted-llama-stack.com"),
92 info=Info(
93 title="[DRAFT] Llama Stack Specification",
94 version="0.0.1",
95 description="""This is the specification of the llama stack that provides
96 a set of endpoints and their corresponding interfaces that are tailored to
97 best leverage Llama Models. The specification is still in draft and subject to change.
98 Generated at """
99 + now,
100 ),
101 ),
102 )
103
104 patch_sse_stream_responses(spec)
105
106 with open(output_dir / "llama-stack-spec.yaml", "w", encoding="utf-8") as fp:
107 yaml.dump(spec.get_json(), fp, allow_unicode=True)
108
109 with open(output_dir / "llama-stack-spec.html", "w") as fp:
110 spec.write_html(fp, pretty_print=True)
111
112
113if __name__ == "__main__":
114 fire.Fire(main)