Chainlit / chainlit

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

Websocket server fails on hot reload #480

Closed mikeengland closed 1 month ago

mikeengland commented 1 year ago

Hi, I am trying to use Chainlit inside of a Replit REPL but I am having a lot of trouble with the websocket connection when the server refreshes (-w is used):

2023-10-16 11:01:24 - message async handler error Traceback (most recent call last): File "/home/runner/myproject/venv/lib/python3.10/site-packages/socketio/asyncio_server.py", line 463, in _handle_connect success = await self._trigger_event( File "/home/runner/myproject/venv/lib/python3.10/site-packages/socketio/asyncio_server.py", line 549, in _trigger_event ret = await handler(*args) TypeError: connect() missing 1 required positional argument: 'auth'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/runner/myproject/venv/lib/python3.10/site-packages/engineio/asyncio_server.py", line 475, in run_async_handler return await self.handlersevent File "/home/runner/myproject/venv/lib/python3.10/site-packages/socketio/asyncio_server.py", line 585, in _handle_eio_message await self._handle_connect(eio_sid, pkt.namespace, pkt.data) File "/home/runner/myproject/venv/lib/python3.10/site-packages/socketio/asyncio_server.py", line 466, in _handle_connect success = await self._trigger_event( File "/home/runner/myproject/venv/lib/python3.10/site-packages/socketio/asyncio_server.py", line 549, in _trigger_event ret = await handler(*args) File "/home/runner/myproject/venv/lib/python3.10/site-packages/chainlit/socket.py", line 50, in connect raise ConnectionRefusedError("No websocket endpoint configured") ConnectionRefusedError: No websocket endpoint configured

Has anyone else encountered this?

I am using chainlit 0.7.2 and a Python 3.10 virtual environment.

Cheers

willydouhard commented 1 year ago

This error message is raised when no on_chat_start and on_message are defined and a websocket client tries to connect. Does your code have at least one of the two?

mikeengland commented 1 year ago

Hi @willydouhard - it does, it just contains the LangChain demo code from here: https://docs.chainlit.io/integrations/langchain

mikeengland commented 1 year ago

For extra context, I am trying to use Replit to build out a small workshop which uses Chainlit as the interface. I have a few exercises that I'd like people to run through. However, pasting in new exercise code (with the on_chat_start and on_message decorated code) restarts the webserver (expected), but if I then restart my Chainlit UI in the browser, I can't type in the input box and the errors sent in the original post appear.

If the web server hot reloads, the webserver connection is lost.

If I kill the Chainlit process inside Replit and it is auto-restarted, then the app works as expected.

willydouhard commented 1 year ago

I confirm there are issues. Will investigate later, for the time being you will have to restart the app manually or maybe run a small bash script instead of running the chainlit command directly. Gpt-4 wrote this script for me (have not tried it myself):

Replit does not have a built-in feature to automatically rerun your repl when a file changes. However, you can use a workaround by using a bash script and the inotify-tools package. Here's how you can do it:

  1. Create a new Bash repl.
  2. Install inotify-tools by running apt-get install inotify-tools in the shell.
  3. Create a bash script with the following content:
#!/bin/bash

while true; do
  change=$(inotifywait -e close_write,moved_to,create .)
  change=${change#./ * }
  if [ "$change" = "your_file_name" ]; then ./your_script.sh; fi
done

Replace your_file_name with the name of the file you want to watch, and replace your_script.sh with the script you want to run when the file changes.

  1. Run the bash script.

This script will watch for changes in the current directory and rerun your script whenever the specified file is modified.