|
| 1 | +import json |
| 2 | +import tempfile |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from mlflow.gateway.app import create_app_from_path |
| 6 | + |
| 7 | +# This HTML was obtained by sending a request to the `/docs` route and saving the response. |
| 8 | +# To hide the "try it out" button, we set `supportedSubmitMethods` to an empty list. |
| 9 | +# The url was changed to "./openapi.json" from "/openapi.json" because `api.html` and `openapi.json` |
| 10 | +# are served from the same directory. |
| 11 | +API_HTML = """ |
| 12 | +<!DOCTYPE html> |
| 13 | +<html> |
| 14 | + <head> |
| 15 | + <link |
| 16 | + type="text/css" |
| 17 | + rel="stylesheet" |
| 18 | + href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui.css" |
| 19 | + /> |
| 20 | + <link |
| 21 | + rel="shortcut icon" |
| 22 | + href="../_static/favicon.ico" |
| 23 | + /> |
| 24 | + <title>MLflow Gateway API - Swagger UI</title> |
| 25 | + </head> |
| 26 | + <body> |
| 27 | + <div id="swagger-ui"></div> |
| 28 | + <script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui-bundle.js"></script> |
| 29 | + <!-- `SwaggerUIBundle` is now available on the page --> |
| 30 | + <script> |
| 31 | + const ui = SwaggerUIBundle({ |
| 32 | + supportedSubmitMethods: [], |
| 33 | + url: "./openapi.json", |
| 34 | + dom_id: "#swagger-ui", |
| 35 | + layout: "BaseLayout", |
| 36 | + deepLinking: true, |
| 37 | + showExtensions: true, |
| 38 | + showCommonExtensions: true, |
| 39 | + oauth2RedirectUrl: window.location.origin + "/docs/oauth2-redirect", |
| 40 | + presets: [ |
| 41 | + SwaggerUIBundle.presets.apis, |
| 42 | + SwaggerUIBundle.SwaggerUIStandalonePreset, |
| 43 | + ], |
| 44 | + }); |
| 45 | + </script> |
| 46 | + </body> |
| 47 | +</html> |
| 48 | +""" |
| 49 | + |
| 50 | + |
| 51 | +def main(): |
| 52 | + config = """ |
| 53 | +routes: |
| 54 | + - name: chat |
| 55 | + route_type: llm/v1/chat |
| 56 | + model: |
| 57 | + provider: openai |
| 58 | + name: gpt-3.5-turbo |
| 59 | + config: |
| 60 | + openai_api_base: https://api.openai.com/v1 |
| 61 | + openai_api_key: key |
| 62 | +
|
| 63 | + - name: completions |
| 64 | + route_type: llm/v1/completions |
| 65 | + model: |
| 66 | + provider: openai |
| 67 | + name: gpt-3.5-turbo |
| 68 | + config: |
| 69 | + openai_api_base: https://api.openai.com/v1 |
| 70 | + openai_api_key: key |
| 71 | +
|
| 72 | + - name: embeddings |
| 73 | + route_type: llm/v1/embeddings |
| 74 | + model: |
| 75 | + provider: openai |
| 76 | + name: text-embedding-ada-002 |
| 77 | + config: |
| 78 | + openai_api_base: https://api.openai.com/v1 |
| 79 | + openai_api_key: key |
| 80 | +""" |
| 81 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 82 | + config_path = Path(tmpdir).joinpath("config.yaml") |
| 83 | + config_path.write_text(config) |
| 84 | + |
| 85 | + app = create_app_from_path(config_path) |
| 86 | + docs_build = Path("build/html/gateway") |
| 87 | + docs_build.mkdir(parents=True, exist_ok=True) |
| 88 | + with docs_build.joinpath("openapi.json").open("w") as f: |
| 89 | + json.dump(app.openapi(), f) |
| 90 | + |
| 91 | + with docs_build.joinpath("api.html").open("w") as f: |
| 92 | + f.write(API_HTML) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + main() |
0 commit comments