Spaces:
Running
Running
| """FastAPI application factory for NLProxy. | |
| Author: IntelliDeep Labs Team | |
| License: BSL 1.1 | |
| """ | |
| from __future__ import annotations | |
| from contextlib import asynccontextmanager | |
| from fastapi import FastAPI | |
| from prometheus_fastapi_instrumentator import Instrumentator, metrics | |
| from nlproxy.server.config import settings | |
| from nlproxy.server.logger import setup_logging | |
| from nlproxy.server.middleware import configure_middlewares, configure_request_id | |
| from nlproxy.server.dependencies import startup, shutdown | |
| from nlproxy.server.apis.health import router as health_router | |
| from nlproxy.server.apis.models import router as models_router | |
| from nlproxy.server.apis.chat import router as chat_router | |
| from nlproxy.server.apis.errors import register_exception_handlers | |
| def create_app() -> FastAPI: | |
| setup_logging(settings.log_level) | |
| async def lifespan(app: FastAPI): | |
| await startup() | |
| try: | |
| yield | |
| finally: | |
| await shutdown() | |
| app = FastAPI( | |
| title="nlproxy Enterprise Proxy", | |
| description="Production-grade prompt compression and safety proxy for LLMs", | |
| version="1.0.0", | |
| docs_url="/docs", | |
| redoc_url="/redoc", | |
| openapi_url="/openapi.json", | |
| lifespan=lifespan, | |
| ) | |
| configure_middlewares(app) | |
| configure_request_id(app) | |
| app.include_router(health_router) | |
| app.include_router(models_router) | |
| app.include_router(chat_router) | |
| register_exception_handlers(app) | |
| if settings.enable_metrics: | |
| Instrumentator( | |
| should_group_status_codes=False, | |
| should_ignore_untemplated=True, | |
| should_respect_env_var=True, | |
| env_var_name="nlproxy_ENABLE_METRICS", | |
| ).add( | |
| metrics.requests(metric_namespace="nlproxy", metric_subsystem="api"), | |
| metrics.request_size(metric_namespace="nlproxy", metric_subsystem="api"), | |
| metrics.response_size(metric_namespace="nlproxy", metric_subsystem="api"), | |
| metrics.latency( | |
| buckets=(0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0), | |
| metric_namespace="nlproxy", | |
| metric_subsystem="api", | |
| ), | |
| ).instrument(app).expose( | |
| app, | |
| endpoint=settings.metrics_path, | |
| include_in_schema=False, | |
| ) | |
| return app | |
| app = create_app() | |