Skip to content

Conversation

cpsievert
Copy link
Collaborator

@cpsievert cpsievert commented Aug 20, 2024

Fixes (the currently internal) NormalizerRegistry.register() so that when a new strategy is registered, it is actually the first strategy to be considered. Discovered while investigating #1610. Here's a minimal example:

from shiny.express import ui
from shiny.ui._chat_normalize import BaseMessageNormalizer, message_normalizer_registry

class CustomResponseObject:
    def __init__(self):
        self.object = {"content": "response", "role": "assistant"}

class CustomResponseNormalizer(BaseMessageNormalizer):
    def normalize(self, message: CustomResponseObject):
        return message.object

    def can_normalize(self, message):
        return isinstance(message, CustomResponseObject)

    def normalize_chunk(self, chunk):
        pass

    def can_normalize_chunk(self, chunk):
        pass

message_normalizer_registry.register(
    "custom-response", CustomResponseNormalizer(), force=True
)

# Create and display empty chat
chat = ui.Chat(id="chat")
chat.ui()

@chat.on_user_submit
async def _():
    await chat.append_message(CustomResponseObject())

@cpsievert cpsievert marked this pull request as ready for review August 20, 2024 20:51
@cpsievert cpsievert merged commit d90c0bf into main Aug 20, 2024
55 of 56 checks passed
@cpsievert cpsievert deleted the fix/chat-register-strategies branch August 20, 2024 21:20
schloerke added a commit that referenced this pull request Aug 22, 2024
* main:
  refactor: `shiny create` and `shiny add test` supporting functions (#1629)
  Update CHANGELOG.md
  Fix #1601: Force text/javascript for .js files
  Work around griffe 1.0 breaking changes
  feat(shiny create): Support simpler syntax for `--github` flag (#1623)
  Make sure `Chat.messages(format='google')` converts role assistant -> model (#1622)
  Fix `NormalizerRegistry.register()` (#1619)
  Update JS/CSS from shiny and bslib (#1617)
  Add ability to update `Chat()`'s input placeholder (#1594)
  docs: Add double-quotes to shiny[theme] (#1605)
  `json.dump()` each websocket message once instead of twice (#1597)
schloerke added a commit to machow/py-shiny that referenced this pull request Sep 5, 2024
* main:
  setup.cfg -> pyproject.toml (posit-dev#1625)
  docs(navset): Add server function to navset docs (posit-dev#1596)
  Pin syrupy to avoid pytest-rerunfailures incompatibility (posit-dev#1632)
  refactor: `shiny create` and `shiny add test` supporting functions (posit-dev#1629)
  Update CHANGELOG.md
  Fix posit-dev#1601: Force text/javascript for .js files
  Work around griffe 1.0 breaking changes
  feat(shiny create): Support simpler syntax for `--github` flag (posit-dev#1623)
  Make sure `Chat.messages(format='google')` converts role assistant -> model (posit-dev#1622)
  Fix `NormalizerRegistry.register()` (posit-dev#1619)
  Update JS/CSS from shiny and bslib (posit-dev#1617)
  Add ability to update `Chat()`'s input placeholder (posit-dev#1594)
  docs: Add double-quotes to shiny[theme] (posit-dev#1605)
  `json.dump()` each websocket message once instead of twice (posit-dev#1597)
  docs: Update note on style keys allowing for kebab-case (posit-dev#1595)
  docs: fix navset function links (posit-dev#1590)
  Get `ui.Chat()` working inside Shiny modules (posit-dev#1582)
  bug(notification): Allow for duration to be `None` and passed to client (posit-dev#1577)
  Fix logic for detection of support files in docs (posit-dev#1580)
  docs(navset): Add examples for navset (posit-dev#1579)
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.

1 participant