Skip to main content

Chatbot / Events/ Suggestions

Use suggestions to simplify user interaction.

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

# Dictionary containing label, caption and icon for each suggestion.
suggestions = {
'sug1': ["Write a poem", "about H2O Wave", "Edit"],
'sug2': ["Plan a trip", "to Europe", "Airplane"],
'sug3': ["Give me ideas", "for a new project", "Lightbulb"],
'sug4': ["Explain me", "CSS preprocessors", "Code"]

async def stream_bot_response(q: Q, message: str):
stream = ''
# Fake bot "thinking" time.
await q.sleep(0.5)
# Stream bot response.
for w in 'I am a fake chatbot. Sorry, I cannot help you.'.split():
await q.sleep(0.1)
stream += w + ' '['example'].data[-1] = [stream, 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'),
placeholder='Ask me anything...',
suggestions=[ui.chat_suggestion(name, label=value[0], caption=value[1], icon=value[2]) for
name, value in suggestions.items()]
q.client.initialized = True

elif or q.args.chatbot:
# Clear suggestions.['example'].suggestions = []

# Handle user input.
if q.args.chatbot:
# Append user message typed manually.['example'].data += [q.args.chatbot, True]
label, caption, icon = suggestions[]
# Append user message based on the suggestion event.['example'].data += [label + ' ' + caption, True]

# Append bot response.['example'].data += ['', False]
# Update UI.
# Stream bot response.
await stream_bot_response(q, 'I am a fake chatbot. Sorry, I cannot help you.')


Tags:  chatboteventssuggestions