thinkcol/chainlit-example
2
1import asyncio2 3import chainlit as cl4 5from chain import Chain6 7 8@cl.on_chat_start9async def start_chat():10 chain = Chain(None)11 await chain.text("I will count to 5. How many concurrent times should I count?")12 13 14@cl.on_message15async def on_message(message: str, message_id: str):16 chain = Chain(message_id)17 18 try:19 num = int(message)20 except ValueError:21 await chain.text_stream("Sorry, that doesn't look like an integer to me.", final=True)22 return23 24 if num > 10:25 await chain.text_stream("Whoa, let's try a smaller number. (Max 10.)", final=True)26 return27 28 await chain.text("Alright, here we go:")29 coroutines = []30 for i in range(num):31 coroutines.append(chain.text_stream("1 2 3 4 5", delay=1, name=f"Counter {i + 1}"))32 await asyncio.gather(*coroutines)33 await chain.text_stream("Okay, I'm done counting now.", final=True)34 