From 5e82b7eb693cdbf82d8d5bfacc1f3e6a1eeb3d57 Mon Sep 17 00:00:00 2001 From: kingbri Date: Sun, 7 Jul 2024 21:34:18 -0400 Subject: [PATCH] API: Add standalone method to fetch OpenAPI docs Generates and stores an export of the openapi.json file for use in static websites. Signed-off-by: kingbri --- .gitignore | 3 +++ endpoints/server.py | 8 ++++++-- generate_openapi.py | 13 +++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 generate_openapi.py diff --git a/.gitignore b/.gitignore index 1c597fa..ebc96c7 100644 --- a/.gitignore +++ b/.gitignore @@ -199,3 +199,6 @@ sampler_overrides/* # Gpu lib preferences file gpu_lib.txt + +# OpenAPI JSON +openapi.json diff --git a/endpoints/server.py b/endpoints/server.py index f6515c2..2cdaa72 100644 --- a/endpoints/server.py +++ b/endpoints/server.py @@ -25,6 +25,10 @@ app.add_middleware( ) +def setup_app(): + app.include_router(OAIRouter) + + async def start_api(host: str, port: int): """Isolated function to start the API server""" @@ -33,8 +37,8 @@ async def start_api(host: str, port: int): logger.info(f"Completions: http://{host}:{port}/v1/completions") logger.info(f"Chat completions: http://{host}:{port}/v1/chat/completions") - # Add OAI router - app.include_router(OAIRouter) + # Setup app + setup_app() config = uvicorn.Config( app, diff --git a/generate_openapi.py b/generate_openapi.py new file mode 100644 index 0000000..0ef4f80 --- /dev/null +++ b/generate_openapi.py @@ -0,0 +1,13 @@ +import json +from endpoints import server + + +if __name__ == "__main__": + """Uses the FastAPI server to write an OpenAPI JSON documentation file.""" + + server.setup_app() + openapi_json = json.dumps(server.app.openapi()) + + # Write JSON to a file + with open("openapi.json", "w") as f: + f.write(openapi_json)