Skip to content

chat.append_message() should allow choosing content_type #1780

@gadenbuie

Description

@gadenbuie

Currently, chat.append_message() allows a message to be a dictionary with items "role" and "content" and the message's content_type is ultimately determined from the role.

I believe we should make it possible to customize the content_type directly in the message dictionary.

Here's a small reprex from shiny/api-examples/chat/app-core.py:

from shiny import App, ui

app_ui = ui.page_fillable(
    ui.panel_title("Hello Shiny Chat"),
    ui.chat_ui("chat"),
    fillable_mobile=True,
)

# Create a welcome message
welcome = ui.markdown(
    """
    Hi! This is a simple Shiny `Chat` UI. Enter a message below and I will
    simply repeat it back to you. For more examples, see this
    [folder of examples](https://github.com/posit-dev/py-shiny/tree/main/examples/chat).
    """
)


def server(input, output, session):
    chat = ui.Chat(id="chat", messages=[welcome])

    # Define a callback to run when the user submits a message
    @chat.on_user_submit
    async def _():
        # Get the user's input
        user = chat.user_input()
        await chat.append_message({
            "role": "assistant",
            "content": f"You said:\n\n{user}",
            "content_type": "html"
        })
        await chat.append_message({
            "role": "user",
            "content": f"You said:\n\n{user}",
            "content_type": "html"
        })


app = App(app_ui, server)

Put any text in the prompt in the browser and then run this in the dev console:

$$("shiny-user-message, shiny-chat-message").map(el => el.getAttribute("content_type"))
// ['html', null, 'markdown', 'markdown']

Ideally, the content type of the two added message could be customized based on the content_type of the message dictionary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions