Skip to content
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

Chat's .append_message_stream() now returns an ExtendedTask #1846

Merged
merged 13 commits into from
Feb 14, 2025

Conversation

cpsievert
Copy link
Collaborator

@cpsievert cpsievert commented Feb 10, 2025

This PR does 2 things:

  • Adds new get_latest_stream_result() method for an easy way to access the final result of the stream when it completes.
  • The .append_message_stream() method now returns the reactive.extended_task instance that it launches.
import asyncio

from shiny.express import render, ui

chat = ui.Chat("chat")

chat.ui()
chat.update_user_input(value="Press Enter to start the stream")


async def stream_generator():
    for i in range(10):
        await asyncio.sleep(0.25)
        yield f"Message {i} \n\n"


@chat.on_user_submit
async def _(message: str):
    await chat.append_message_stream(stream_generator())


@render.code
async def stream_result_ui():
    return chat.get_latest_stream_result()
chat-stream-result.mp4

TODO

  • Add return section to docstring?

@cpsievert cpsievert requested a review from gadenbuie February 10, 2025 21:50
@gadenbuie
Copy link
Collaborator

@chat.on_user_submit
async def _(message: str):
    stream = await chat.append_message_stream(stream_generator())
    current_stream.set(stream)


current_stream: reactive.value[ExtendedTask[[], str] | None] = reactive.value(None)

Could we wrap this bit up in a method, e.g. chat.get_last_stream() (naming is hard)? That might be technically difficult, but I think this pattern would be hard for users to "discover" on their own.

@cpsievert
Copy link
Collaborator Author

Could we wrap this bit up in a method

I went back and forth a bit on whether I wanted to add this since shinychat won't be able have an equivalent, but I think the end result (f625eba) is worth it.

Note that I also decided to get the result of the stream, not the stream itself since >95% of the time you just want the result.

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! I like the method interface 😄

@cpsievert cpsievert merged commit 1e9b868 into main Feb 14, 2025
41 checks passed
@cpsievert cpsievert deleted the chat-append-stream-return-task branch February 14, 2025 20:45
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