Elffuss BraidHub

kikocis/llama-stack public

3b6b4dbcc978 · 2.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
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_stack.apis.stack import LlamaStack
35
36
37# TODO: this should be fixed in the generator itself so it reads appropriate annotations
38STREAMING_ENDPOINTS = [
39 "/agentic_system/turn/create",
40 "/inference/chat_completion",
41]
42
43
44def patch_sse_stream_responses(spec: Specification):
45 for path, path_item in spec.document.paths.items():
46 if path in STREAMING_ENDPOINTS:
47 content = path_item.post.responses["200"].content.pop("application/json")
48 path_item.post.responses["200"].content["text/event-stream"] = content
49
50
51def main(output_dir: str):
52 output_dir = Path(output_dir)
53 if not output_dir.exists():
54 raise ValueError(f"Directory {output_dir} does not exist")
55
56 now = str(datetime.now())
57 print(
58 "Converting the spec to YAML (openapi.yaml) and HTML (openapi.html) at " + now
59 )
60 print("")
61 spec = Specification(
62 LlamaStack,
63 Options(
64 server=Server(url="http://any-hosted-llama-stack.com"),
65 info=Info(
66 title="[DRAFT] Llama Stack Specification",
67 version="0.0.1",
68 description="""This is the specification of the llama stack that provides
69 a set of endpoints and their corresponding interfaces that are tailored to
70 best leverage Llama Models. The specification is still in draft and subject to change.
71 Generated at """
72 + now,
73 ),
74 ),
75 )
76
77 patch_sse_stream_responses(spec)
78
79 with open(output_dir / "llama-stack-spec.yaml", "w", encoding="utf-8") as fp:
80 yaml.dump(spec.get_json(), fp, allow_unicode=True)
81
82 with open(output_dir / "llama-stack-spec.html", "w") as fp:
83 spec.write_html(fp, pretty_print=True)
84
85
86if __name__ == "__main__":
87 fire.Fire(main)