Skip to content

feat(pkg-py): Add new message_content() and message_content_chunk() #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 8, 2025

Conversation

cpsievert
Copy link
Collaborator

@cpsievert cpsievert commented Aug 7, 2025

Adds new message_content() and message_content_chunk() generic functions (via singledispatch). They aren't intended to be called directly by users, but instead, provide an opportunity to teach Chat.append_message()/Chat.append_message_stream() how to extract a ChatMessage() from different bespoke objects.

from shiny import reactive
import shiny.express

from shinychat import message_content
from shinychat.express import Chat
from shinychat.types import ChatMessage

class MyObject:
    foo = "bar"

@message_content.register
def _(message: MyObject):
    return ChatMessage(content=message.foo, role="assistant")

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

@reactive.effect
async def _():
    await chat.append_message(MyObject())

These new functions are the singledispatch equivalent of the previous (internal) normalize_message() and normalize_message_chunk() helpers. Although not quite as flexible as the previous strategies pattern, this approach much simpler (implementation wise and also for those registering new methods) and aligns much better with shinychat::contents_shinychat() (#52).

@cpsievert cpsievert force-pushed the feat/python-contents-shinychat branch from 0552907 to b7edff7 Compare August 7, 2025 16:59
@cpsievert cpsievert requested a review from Copilot August 7, 2025 17:00
Copilot

This comment was marked as resolved.

…hinychat_chunk()

These new functions are the singledispatch equivalent of the previous normalize_message() and normalize_message_chunk(). Although not quite as flexible as the previous strategies pattern, it's much simpler (implementation wise and also for those registering new methods) and aligns much better with the vision for the R package
@cpsievert cpsievert force-pushed the feat/python-contents-shinychat branch from b7edff7 to a30a722 Compare August 7, 2025 18:21
@cpsievert cpsievert changed the title feat(contents_shinychat): Add new contents_shinychat() and contents_shinychat_chunk() feat(pkg-py): Add new get_message_content() and get_message_chunk_content() Aug 7, 2025
@cpsievert cpsievert requested a review from Copilot August 7, 2025 21:30
Copilot

This comment was marked as resolved.

@cpsievert cpsievert force-pushed the feat/python-contents-shinychat branch from 0bcbcd0 to ba7e201 Compare August 7, 2025 21:40
@cpsievert cpsievert force-pushed the feat/python-contents-shinychat branch from 4be7753 to f4a6950 Compare August 7, 2025 22:12
@cpsievert cpsievert marked this pull request as ready for review August 7, 2025 22:13
@cpsievert cpsievert requested a review from gadenbuie August 7, 2025 22:13
@cpsievert cpsievert force-pushed the feat/python-contents-shinychat branch from f4a6950 to 959fb61 Compare August 7, 2025 22:14
@cpsievert cpsievert changed the title feat(pkg-py): Add new get_message_content() and get_message_chunk_content() feat(pkg-py): Add new message_content() and message_chunk_content() Aug 7, 2025
Copy link
Collaborator

@gadenbuie gadenbuie left a comment

Choose a reason for hiding this comment

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

Looks great, just one small name adjustment comment. Thanks!

@cpsievert cpsievert changed the title feat(pkg-py): Add new message_content() and message_chunk_content() feat(pkg-py): Add new message_content() and message_content_chunk() Aug 8, 2025
@cpsievert cpsievert merged commit 3d9bc44 into main Aug 8, 2025
5 checks passed
@cpsievert cpsievert deleted the feat/python-contents-shinychat branch August 8, 2025 15:58
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