Chainlit / chainlit

Build Conversational AI in minutes ⚡️
https://docs.chainlit.io
Apache License 2.0
6.06k stars 777 forks source link

Custom API endpoints not working anymore #1099

Open Jimmy-Newtron opened 1 week ago

Jimmy-Newtron commented 1 week ago
          > This new way to handle resources access is breaking the custom endpoints that I add to the server

This is my solution to patch and temporarily fix the issue caused by the serve route

from chainlit.server import app
from starlette.routing import BaseRoute, Route

from src.core.routers import admin

routes: list[BaseRoute] = [
    r for r in app.router.routes if isinstance(r, Route) and r.name != "serve"
]

serve_route = [
    r for r in app.router.routes if isinstance(r, Route) and r.name == "serve"
]

app.router.routes = routes

app.include_router(admin.router)

app.router.routes.extend(serve_route)

Originally posted by @Jimmy-Newtron in https://github.com/Chainlit/chainlit/issues/1064#issuecomment-2186946521

haidersultancrewlogix commented 1 week ago

I am also facing issues with Custom Endpoints with the new release. I can't use GET endpoints. They route the base URL https:{HOST_NAME}:8000/ which is Chainlit Frontend.

Jimmy-Newtron commented 1 week ago

The first code works partially, but the following forks better:

from chainlit.server import app
from prometheus_fastapi_instrumentator import Instrumentator
from starlette.routing import BaseRoute, Route

from src.core.routers import your_routers

serve_route: list[BaseRoute] = [
    r for r in app.router.routes if isinstance(r, Route) and r.name == "serve"
]

for route in serve_route:
    app.router.routes.remove(route)

app.include_router(your_routers)

Instrumentator().instrument(app).expose(app, tags=["monitoring"])

app.router.routes.extend(serve_route)