Chainlit / chainlit

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

chainlit send message not being called in async setup #466

Open poojitharamachandra opened 9 months ago

poojitharamachandra commented 9 months ago

hi,

i have a code something like this:

@cl.on_message async def run(input_str): ........................... ............................... await play_answer(res['result'])

await cl.Message(content=fnl_ans).send()

but only play_answer is getting executed, but i dont see the fnl_ans on the chainlit UI. the execution of the whole code is also blocked

willydouhard commented 9 months ago

What does the play_answer function look like? Hard to debug without the code.

poojitharamachandra commented 9 months ago

import pygame async def play_soundfile():

#playsound.playsound('./ans.wav') 

# Initialize the mixer module for audio playback
pygame.mixer.init()

# Load the audio file
pygame.mixer.music.load(wavefile)

# Play the audio
pygame.mixer.music.play()

# Wait for the audio to finish playing (optional)
while pygame.mixer.music.get_busy():
    pygame.time.Clock().tick(10)

# Release the audio resources
pygame.mixer.quit()
willydouhard commented 9 months ago

Can you try to wrap the function call like this await cl.make_async(play_answer)(res['result'])?

poojitharamachandra commented 9 months ago

if i wrap the function, the soundclip is not being played and i get the following error :

RuntimeWarning: coroutine 'play_answer' was never awaited self.run() RuntimeWarning: Enable tracemalloc to get the object allocation traceback

willydouhard commented 9 months ago

is the play_answer function marked as async? If that is the case are you doing any await in it?

poojitharamachandra commented 9 months ago

async def play_answer(text): await play_soundfile()