Skip to content

Conversation

wch
Copy link
Collaborator

@wch wch commented Aug 26, 2024

This allows the @chat.transform_assistant_response function to return None, in which case nothing is sent to the client (and it will not update the output).

It also fixes a small issue where, when streaming, the last message would not be displayed. (This issue only manifests when the transform function returns None, because previously the last piece of text would get sent twice, once with chunk_type=None and once with chunk_type="message_end".

@wch wch requested a review from cpsievert August 26, 2024 18:32

if (message.chunk_type === "message_end") {
lastMessage.removeAttribute("is_streaming");
lastMessage.setAttribute("content", message.content);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After chatting with @wch, we discovered/remembered this is needed for @wch's hand-rolled throttling implementation, but it's also useful if want to transform the content when the message is done, for example:

from shiny.express import ui

chat = ui.Chat(id="chat")
chat.ui()


@chat.transform_assistant_response
def transform(content: str, chunk: str, done: bool) -> str | None:
    if done:
        return content + "...DONE!"
    else:
        return content


@chat.on_user_submit
async def _():
    await chat.append_message_stream(("Basic ", "response"))

@cpsievert cpsievert merged commit a632159 into main Aug 27, 2024
42 of 48 checks passed
@cpsievert cpsievert deleted the chat-content-none branch August 27, 2024 23:45
schloerke added a commit to machow/py-shiny that referenced this pull request Sep 5, 2024
* main:
  CI(deploy): Add more installation configs to surface failure cause (posit-dev#1658)
  `Chat.messages()` no longer trims messages by default (posit-dev#1657)
  tests(controllers): Split _controls.py into separate files (posit-dev#1652)
  Chat tweaks (posit-dev#1607)
  docs: Add modal_show/remove examples (posit-dev#1628)
  Delay sending of chat UI messages until reactive graph is flushed (posit-dev#1593)
  ci(remove future behavior warning): Set the `asyncio_default_fixture_loop_scope` to `fixture` (posit-dev#1655)
  bug: Verify mypy can run on CI (posit-dev#1650)
  Fix CI install failures on Windows (posit-dev#1651)
  Quartodoc 0.7.6 (posit-dev#1636)
  Allow `@chat.transform_assistant_response` function to return `None` (posit-dev#1641)
  feat: Support templates with `_template.json` metadata (posit-dev#1631)
  Fix KeyError when serving static files (posit-dev#1648)
  tests(navsets): Add navsets kitchensink tests (posit-dev#1602)
  Change default claude model to 3.5 sonnet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants