def handle_message_simple(payload, state):# payload contains a dict in the form { "role": "user", "message": "hello"}state["conversation"] += [payload]state["conversation"] += [{ "role": "assistant", "content": "Hello human" if payload == "Hello" else "I don't understand"}]# Handle streaming by appending to the last messageimport timefor i in range(10): conv = state["conversation"] conv[-1]["content"] += f" {i}" state["conversation"] = conv time.sleep(0.5)
wf-chatbot-action-click
Handle clicks on actions.
Copy
def handle_action_simple(payload, state):# payload contains the "data" property of the actionif payload == "change_title": state["app_background_color"] = "red"# Make an action available when adding a messagedef handle_message_with_action(payload, state):state["conversation"] += [payload]state["conversation"] += [{ "role": "assistant", "content": "I don't know, but check this out.", "actions": [{ "subheading": "Resource", "name": "Surprise", "desc": "Click to be surprised", "data": "change_title" }]}]
wf-file-change
Triggered when files are uploaded
Copy
def handle_file_upload(state, payload):# An array of dictionaries is provided in the payload# The dictionaries have the properties name, type and data# The data property is a file-like objectuploaded_files = payloadfor i, uploaded_file in enumerate(uploaded_files): name = uploaded_file.get("name") file_data = uploaded_file.get("data") with open(f"{name}-{i}.jpeg", "wb") as file_handle: file_handle.write(file_data)