Chainlit / chainlit

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

LookupError for run_sync when trying to update UI via the FastAPI endpoint #1356

Closed MA3CIN closed 1 month ago

MA3CIN commented 1 month ago

I'm running FastAPI with Chainlit, and running into issues when trying to enable UI updates (sending messages) via a FastAPI Endpoint. The examples in documentation are non-existent, and a cookbook example with fastAPI also doesn't exist. I'm trying to serve endpoints via FastAPI (for example, display message) that would update the UI, but whenever i try to call a function (wrapped in run_sync) that would update the ui (await cl.Message(...).send()), either the routine is not awaited 'coroutine 'send_message' was never awaited', or i get a LookupError:

image

To Reproduce chain.py:

import chainlit as cl

@cl.on_chat_start
async def main():
    await cl.Message(content="Hello World").send()

async def send_message(msg):
    await cl.Message(content=msg).send()

main.py:

from chainlit import run_sync
from chainlit.utils import mount_chainlit
from chain import send_message

app = FastAPI()

@app.get("/app")
def read_main():
    run_sync(send_message('test'))
    return {"message": "Hello World from main app"}

mount_chainlit(app=app, target="chain.py", path="/chainlit")

Expected behavior More documentation on updating the UI via FastAPI endpoints (or on run_sync) is needed

MA3CIN commented 1 month ago

If i try to call the await cl.Message function directly from the FastAPI file, i get "chainlit.context.ChainlitContextException: Chainlit context not found" And if i call it via a function imported from the ChainLit file, i get the "coroutine 'send_message' was never awaited" error

nethi commented 1 month ago

Take a look at this documentation pointer on how to integrate HTTP and websocket context into FastAPI. Without appropriate websocket context, emitter won't know what to do.

https://docs.chainlit.io/deploy/api#use-chainlit-apis-in-your-endpoint

dokterbob commented 1 month ago

Closing this for now as I consider the question answered. @MA3CIN If you have more trouble with this, feel free to open again (providing additional clarification for reproduction).