Chainlit / cookbook

Chainlit's cookbook repo
https://github.com/Chainlit/chainlit
726 stars 270 forks source link

The values obtained within async def on_action(action): cannot be returned to async def main(message: str) #23

Closed Franckegao closed 1 year ago

Franckegao commented 1 year ago

After invoking the action class, the values obtained within async def on_action(action): cannot be returned to async def main(message: str); this is my code.

@cl.action_callback("selector")
async def on_action(action):
    await cl.Message(content=f"you are picking {action.label}").send()
    await cl.Message(content=f"looking into {action.label},wait.....").send()
    user_choice = int(action.value)
    cl.user_session.set("user_choice", user_choice)

@cl.on_message
async def main(message: str):
    # Set Avatar
    await cl.Avatar(
        name="EMM",
        path="./Avatar/avatar.png",
    ).send()
    pick_template = []
    for i, document in enumerate(law_doc_titles):
        pick_template.append(cl.Action(name="selector", value=str(i), label=document.page_content, description=document.page_content))

    await cl.Message(content="pick an option:", actions=pick_template).send()
    pick = cl.user_session.get("user_choice")
    chosen_document = law_doc_titles[pick]

I have tried global variables, and the async def main won't wait until the correct value of user_choice is passed through. if i put while pick == none cl.sleep(1) after " await cl.Message(content="pick an option:", actions=pick_template).send() It would stop the button from clicking as well. Also tried asyncio.Event(), same problem, as the while method, whatever I want to use to pause until the right value is processed, it freezes the button from click.

willydouhard commented 1 year ago

When a message is sent, the await means wait until the message is sent, not until the an action is clicked. At the moment it is not possible to freeze the UI until an action has been clicked.

The user session is the right way to pass data between action callbacks and the main function.

ryanshrott commented 1 year ago

@willydouhard how to use open ai azure with this app?

willydouhard commented 1 year ago

@willydouhard how to use open ai azure with this app? Which example in the cookbook are you referring to?