The Message class is designed to send, stream, edit or remove messages in the chatbot user interface.

Attributes

content
str

The content of the message.

author
str

The author of the message, defaults to the chatbot name defined in your config file.

elements
Element[]

The list of elements scoped to this message.

actions
Action[]

The list of actions scoped to this message.

prompt
Prompt

The prompt used to generate the message. If provided, enables the prompt playground for this message.

disable_human_feedback
bool

Hide the feedback buttons for this specific message.

language
str

Language of the code if the content is code. See https://react-code-blocks-rajinwonderland.vercel.app/?path=/story/codeblock—supported-languages for a list of supported languages.

Send a message

Send a new message to the UI.

Code Example
import chainlit as cl


@cl.on_message
async def main(message: cl.Message):
    await cl.Message(
        content=f"Received: {message.content}",
    ).send()

Nest a message

Send a nested message to the UI. Ideal for chain of thoughts.

Code Example
import chainlit as cl

@cl.on_chat_start
async def start():
    id = await cl.Message(content="Hello!").send()
    await cl.Message(content="Hello nested!", parent_id=id).send()

Stream a message

Send a message token by token to the UI.

Code Example
import chainlit as cl

token_list = ["the", "quick", "brown", "fox"]


@cl.on_chat_start
async def main():
    msg = cl.Message(content="")
    for token in token_list:
        await msg.stream_token(token)

    await msg.send()

Update a message

Update a message that already has been sent.

Code Example
import chainlit as cl


@cl.on_chat_start
async def main():
    msg = cl.Message(content="Hello!")
    await msg.send()

    await cl.sleep(2)

    msg.content = "Hello again!"
    await msg.update()

Remove a message

Remove a message from the UI.

Code Example
import chainlit as cl


@cl.on_chat_start
async def main():
    msg = cl.Message(content="Message 1")
    await msg.send()
    await cl.sleep(2)
    await msg.remove()