Chainlit / chainlit

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

From 0.4.0 to 0.4.1, the root module no longer shows up in sys.modules #121

Closed frankwanicka closed 1 year ago

frankwanicka commented 1 year ago

I am hoping this is another dumb mistake on my part. I am new to Python so bear with me. I am using the OpenAI functions and am calling the function dynamically based on the name. The code is:

func = getattr(sys.modules[__name__], function_name)

This seems to be the recommended way to do this based on what I have read. It works fine in 0.4.0. In 0.4.1, I get a KeyError, because the root file is no longer in sys.modules, so sys.modules[name] fails. Was that intentional? I have looked through the commits from 0.4.0 to 0.4.1 and nothing jumps out. Thanks in advance.

willydouhard commented 1 year ago

Can you try to update to 0.5.0?

frankwanicka commented 1 year ago

I am sorry for not being more specific. It changed in 0.4.1. I have tested with 0.4.2, 0.4.3 and 0.5.0 and the same behavior exists in all of those. This will demonstrate it:

import chainlit as cl
import sys

@cl.on_chat_start
async def start_chat():
    print(__name__)
    print(__name__ in sys.modules)

@cl.on_message
async def main(message: str):

    # Send a response back to the user
    await cl.Message(
        content=f"Received: {message}",
    ).send()

The name of the module will be printed and then True or False. You should get True in 0.4.0 and prior versions and False in 0.4.1 and later versions.

willydouhard commented 1 year ago

This might be the change that changed that behavior. Looking into it

frankwanicka commented 1 year ago

Thanks very much for the quick response and all the work you are doing.