From 10797fd0f5741d8dfb35c91f8c74b8470e6a2f60 Mon Sep 17 00:00:00 2001 From: Carson Date: Tue, 17 Dec 2024 11:17:26 -0600 Subject: [PATCH 1/2] feat(Chat): The .append_message() method now automatically streams in generators --- shiny/ui/_chat.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/shiny/ui/_chat.py b/shiny/ui/_chat.py index b72995d94..b1cec61c7 100644 --- a/shiny/ui/_chat.py +++ b/shiny/ui/_chat.py @@ -515,13 +515,21 @@ async def append_message(self, message: Any) -> None: The message to append. A variety of message formats are supported including a string, a dictionary with `content` and `role` keys, or a relevant chat completion object from platforms like OpenAI, Anthropic, Ollama, and others. + When the message is a generator or async generator, it is automatically + treated as a stream of message chunks (i.e., uses + `.append_message_stream()`) Note ---- - Use `.append_message_stream()` instead of this method when `stream=True` (or - similar) is specified in model's completion method. + Although this method tries its best to handle various message formats, it's + not always possible to handle every message format. If you encounter an error + or no response when appending a message, try extracting the message content + as a string and passing it to this method. """ - await self._append_message(message) + if inspect.isasyncgen(message) or inspect.isgenerator(message): + await self.append_message_stream(message) + else: + await self._append_message(message) async def _append_message( self, message: Any, *, chunk: ChunkOption = False, stream_id: str | None = None From 96f87b37bd929d24ddc81da1d8875787932b1d45 Mon Sep 17 00:00:00 2001 From: Carson Date: Tue, 17 Dec 2024 11:24:15 -0600 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e46ae7f0d..21684afb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Added a new `.add_sass_layer_file()` method to `ui.Theme` that supports reading a Sass file with layer boundary comments, e.g. `/*-- scss:defaults --*/`. This format [is supported by Quarto](https://quarto.org/docs/output-formats/html-themes-more.html#bootstrap-bootswatch-layering) and makes it easier to store Sass rules and declarations that need to be woven into Shiny's Sass Bootstrap files. (#1790) +* `ui.Chat`'s `.append_message()` method now automatically streams generators and async generators. (#1800) + ### Bug fixes * `ui.Chat()` now correctly handles new `ollama.chat()` return value introduced in `ollama` v0.4. (#1787)