Skip to main content

Chatbot / Events/ Stop

Use this card for chatbot interactions. Streaming can be stopped by the user.

from h2o_wave import main, app, Q, ui, data
import asyncio

async def stream_message(q):
stream = ''['example'].data += [stream, False]
# Show the "Stop generating" button['example'].generating = True
for w in 'I am a fake chatbot. Sorry, I cannot help you.'.split():
await asyncio.sleep(0.3)
stream += w + ' '['example'].data[-1] = [stream, False]
# Hide the "Stop generating" button['example'].generating = False

async def serve(q: Q):
if not q.client.initialized:['example'] = ui.chatbot_card(
box='1 1 5 5',
data=data(fields='content from_user', t='list'),
q.client.initialized = True

# Handle the stop event.
if and
# Cancel the streaming task.
# Hide the "Stop generating" button.['example'].generating = False
# A new message arrived.
elif q.args.chatbot:
# Append user message.['example'].data += [q.args.chatbot, True]
# Run the streaming within cancelable asyncio task.
q.client.task = asyncio.create_task(stream_message(q))


Tags:  chatboteventsstop