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

Reset flagged values when switching conversations in chat history #10292

Merged
merged 23 commits into from
Jan 6, 2025

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Jan 6, 2025

When both flagging (#10272) and chat history (#10191) are enabled, flagged messages are shared between conversations. E.g. if you flag the first message in one conversation, the first message appears to be flagged in all conversations.

This PR fixes that -- now the flagged values are saved as part of the chat history for each conversation separately reset. This PR also makes a few small tweaks to improve the UI/UX saving history and flagging:


For an example with only gr.Chatbot, try something like this:

import gradio as gr

with gr.Blocks() as demo:
    c = gr.Chatbot(
        value=[{"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hi"}, {"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Go away"}],
        type="messages",
        feedback_options=["Like", "Spam", "Inappropriate", "Other"],
        feedback_value=["Like", "Other"],
    )
    c.like(lambda x: x, None, None)
    b = gr.Button("Clear flags")
    b.click(lambda: gr.Chatbot(feedback_value=None), None, c)

if __name__ == "__main__":
    demo.launch()

Or test the whole experience of switching between conversations using the demo/chatinterface_streaming_echo/run.py demo.

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Jan 6, 2025

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-pypi-previews.s3.amazonaws.com/1485016253b16a502cd334c01d3e6aee37ba0aa2/gradio-5.9.1-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@1485016253b16a502cd334c01d3e6aee37ba0aa2#subdirectory=client/python"

Install Gradio JS Client from this PR

npm install https://gradio-npm-previews.s3.amazonaws.com/1485016253b16a502cd334c01d3e6aee37ba0aa2/gradio-client-1.8.0.tgz

Use Lite from this PR

<script type="module" src="https://gradio-lite-previews.s3.amazonaws.com/1485016253b16a502cd334c01d3e6aee37ba0aa2/dist/lite.js""></script>

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Jan 6, 2025

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/atoms minor
@gradio/chatbot minor
gradio minor
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Reset flagged values when switching conversations in chat history

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

while len(feedback_value) <= assistant_index:
feedback_value.append(None)
feedback_value[assistant_index] = value
feedback_values[str(conversation_id)] = feedback_value
Copy link
Member Author

Choose a reason for hiding this comment

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

orjson requires dictionaries to have str keys, otherwise serialization fails and the gradio app crashes.

@@ -249,7 +249,10 @@ def __init__(

with self:
self.saved_conversations = BrowserState(
[], storage_key="_saved_conversations"
[], storage_key=f"_saved_conversations_{self._id}"
Copy link
Member Author

Choose a reason for hiding this comment

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

Adds a self._id in case your Gradio app has multiple gr.ChatInterface-s

Suggested change
[], storage_key=f"_saved_conversations_{self._id}"
[], storage_key=f"_saved_conversations_{self._id}"

inputs=[self.saved_conversations],
outputs=[self.chat_history_dataset],
show_api=False,
queue=False,
)
def load_chat_history(conversations):
Copy link
Member Author

Choose a reason for hiding this comment

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

Decided to avoid using decorator syntax for consistency with the rest of the file

@abidlabs abidlabs marked this pull request as ready for review January 6, 2025 18:59
@aliabid94
Copy link
Collaborator

Screenshot 2025-01-06 at 12 07 05 PM icon should be dark gray/black. orange loooks weird

Copy link
Collaborator

@dawoodkhan82 dawoodkhan82 left a comment

Choose a reason for hiding this comment

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

Tested and fixes the flagging issues.

@abidlabs
Copy link
Member Author

abidlabs commented Jan 6, 2025

@aliabid94 thoughts on changing to a primary button? like this:

image image

@aliabid94
Copy link
Collaborator

Concern about storing flagged info in two different places (in csv, and in browser state) is that they may diverge. For example, a user may delete the csv to clear the flagging history, but it will still show up in the browser.

@abidlabs
Copy link
Member Author

abidlabs commented Jan 6, 2025

Synced with @aliabid94 about this:

Concern about storing flagged info in two different places (in csv, and in browser state) is that they may diverge. For example, a user may delete the csv to clear the flagging history, but it will still show up in the browser.

and decided to change this approach to reset the flagging values instead of preserving them in browser state. Simplifies the PR considerably.

@abidlabs abidlabs changed the title Store flagged values as part of the saved chat history Reset flagged values when switching conversations in chat history Jan 6, 2025
Copy link
Collaborator

@aliabid94 aliabid94 left a comment

Choose a reason for hiding this comment

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

LGTM!

@abidlabs
Copy link
Member Author

abidlabs commented Jan 6, 2025

Thanks @dawoodkhan82 @aliabid94 for the reviews!

@abidlabs abidlabs merged commit f2bd72f into main Jan 6, 2025
22 of 23 checks passed
@abidlabs abidlabs deleted the history-flag branch January 6, 2025 21:59
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.

Allow canceling the like/dislike status in gr.Chatbot
4 participants