Skip to content

Commit c6d7196

Browse files
committed
chore: chop fastapi_generator into its module
Decoupled the large script with distinct files and purpose. Signed-off-by: Sébastien Han <[email protected]>
1 parent 06dd5f8 commit c6d7196

File tree

11 files changed

+2319
-2206
lines changed

11 files changed

+2319
-2206
lines changed

scripts/fastapi_generator.py

Lines changed: 0 additions & 2205 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
"""
8+
OpenAPI generator module for Llama Stack.
9+
10+
This module provides functionality to generate OpenAPI specifications
11+
from FastAPI applications.
12+
"""
13+
14+
from .main import generate_openapi_spec, main
15+
16+
__all__ = ["generate_openapi_spec", "main"]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
"""
8+
Entry point for running the openapi_generator module as a package.
9+
"""
10+
11+
from .main import main
12+
13+
if __name__ == "__main__":
14+
main()

scripts/openapi_generator/app.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
"""
8+
FastAPI app creation for OpenAPI generation.
9+
"""
10+
11+
import inspect
12+
from typing import Any
13+
14+
from fastapi import FastAPI
15+
16+
from llama_stack.apis.datatypes import Api
17+
from llama_stack.core.resolver import api_protocol_map
18+
19+
from .state import _protocol_methods_cache
20+
21+
22+
def _get_protocol_method(api: Api, method_name: str) -> Any | None:
23+
"""
24+
Get a protocol method function by API and method name.
25+
Uses caching to avoid repeated lookups.
26+
27+
Args:
28+
api: The API enum
29+
method_name: The method name (function name)
30+
31+
Returns:
32+
The function object, or None if not found
33+
"""
34+
global _protocol_methods_cache
35+
36+
if _protocol_methods_cache is None:
37+
_protocol_methods_cache = {}
38+
protocols = api_protocol_map()
39+
from llama_stack.apis.tools import SpecialToolGroup, ToolRuntime
40+
41+
toolgroup_protocols = {
42+
SpecialToolGroup.rag_tool: ToolRuntime,
43+
}
44+
45+
for api_key, protocol in protocols.items():
46+
method_map: dict[str, Any] = {}
47+
protocol_methods = inspect.getmembers(protocol, predicate=inspect.isfunction)
48+
for name, method in protocol_methods:
49+
method_map[name] = method
50+
51+
# Handle tool_runtime special case
52+
if api_key == Api.tool_runtime:
53+
for tool_group, sub_protocol in toolgroup_protocols.items():
54+
sub_protocol_methods = inspect.getmembers(sub_protocol, predicate=inspect.isfunction)
55+
for name, method in sub_protocol_methods:
56+
if hasattr(method, "__webmethod__"):
57+
method_map[f"{tool_group.value}.{name}"] = method
58+
59+
_protocol_methods_cache[api_key] = method_map
60+
61+
return _protocol_methods_cache.get(api, {}).get(method_name)
62+
63+
64+
def create_llama_stack_app() -> FastAPI:
65+
"""
66+
Create a FastAPI app that represents the Llama Stack API.
67+
This uses the existing route discovery system to automatically find all routes.
68+
"""
69+
app = FastAPI(
70+
title="Llama Stack API",
71+
description="A comprehensive API for building and deploying AI applications",
72+
version="1.0.0",
73+
servers=[
74+
{"url": "http://any-hosted-llama-stack.com"},
75+
],
76+
)
77+
78+
# Get all API routes
79+
from llama_stack.core.server.routes import get_all_api_routes
80+
81+
api_routes = get_all_api_routes()
82+
83+
# Create FastAPI routes from the discovered routes
84+
from . import endpoints
85+
86+
for api, routes in api_routes.items():
87+
for route, webmethod in routes:
88+
# Convert the route to a FastAPI endpoint
89+
endpoints._create_fastapi_endpoint(app, route, webmethod, api)
90+
91+
return app

0 commit comments

Comments
 (0)