Chainlit / chainlit

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

Custom API endpoints not working anymore #1099

Open Jimmy-Newtron opened 5 months ago

Jimmy-Newtron commented 5 months 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 5 months 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 5 months 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)
Physium commented 2 months ago

@Jimmy-Newtron where would this patch suppose to be included in? the chainlit repo itself or your own chainlit app?

Jimmy-Newtron commented 2 months ago

My patch is on the your own app code, but it would be nice to avoid patching the code by having a chainlit fix