-
Notifications
You must be signed in to change notification settings - Fork 114
Allow @chat.transform_assistant_response
function to return None
#1641
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
97af111
Allow transform_assistant_response function to return None
wch 85c6d17
Render chat message on message_end
wch e1186ae
Build JS
wch 7369efc
Reduce duplication in transform logic; update docstring
cpsievert ace45a4
Update changelog
cpsievert 1fcde6f
Merge branch 'main' into chat-content-none
cpsievert 5db1373
Add a test
cpsievert 92e4dc0
Fix test expectation
cpsievert e43757b
Fix type
cpsievert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
tests/playwright/shiny/components/chat/transform_assistant_stream/app.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from shiny import render | ||
from shiny.express import ui | ||
|
||
chat = ui.Chat(id="chat") | ||
chat.ui() | ||
|
||
|
||
@chat.transform_assistant_response | ||
def transform(content: str, chunk: str, done: bool): | ||
if done: | ||
return content + "...DONE!" | ||
else: | ||
return content | ||
|
||
|
||
@chat.on_user_submit | ||
async def _(): | ||
await chat.append_message_stream(("Simple ", "response")) | ||
|
||
|
||
"Message state:" | ||
|
||
|
||
@render.code | ||
def message_state(): | ||
return str(chat.messages()) |
31 changes: 31 additions & 0 deletions
31
.../shiny/components/chat/transform_assistant_stream/test_chat_transform_assistant_stream.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from playwright.sync_api import Page, expect | ||
from utils.deploy_utils import skip_on_webkit | ||
|
||
from shiny.playwright import controller | ||
from shiny.run import ShinyAppProc | ||
|
||
|
||
@skip_on_webkit | ||
def test_validate_chat_transform_assistant(page: Page, local_app: ShinyAppProc) -> None: | ||
page.goto(local_app.url) | ||
|
||
chat = controller.Chat(page, "chat") | ||
message_state = controller.OutputCode(page, "message_state") | ||
|
||
# Wait for app to load | ||
message_state.expect_value("()", timeout=30 * 1000) | ||
|
||
expect(chat.loc).to_be_visible(timeout=30 * 1000) | ||
expect(chat.loc_input_button).to_be_disabled() | ||
|
||
chat.set_user_input("foo") | ||
chat.send_user_input() | ||
chat.expect_latest_message("Simple response...DONE!", timeout=30 * 1000) | ||
|
||
message_state_expected = tuple( | ||
[ | ||
{"content": "foo", "role": "user"}, | ||
{"content": "Simple response", "role": "assistant"}, | ||
] | ||
) | ||
message_state.expect_value(str(message_state_expected)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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: