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

Allow custom tags for chatbot #10743

Merged
merged 20 commits into from
Mar 7, 2025
Merged

Allow custom tags for chatbot #10743

merged 20 commits into from
Mar 7, 2025

Conversation

dawoodkhan82
Copy link
Collaborator

@dawoodkhan82 dawoodkhan82 commented Mar 6, 2025

Description

(1) Allow developers to set allow_tags=True to let all custom tags (non-standard HTML tags) to come through
(2) Make this the default for chatbots loaded for gr.load()

import gradio as gr
from gradio import ChatMessage
import time

sleep_time = 0.5

def simulate_thinking_chat(message, history):
    start_time = time.time()
    response = ChatMessage(
        content="",
        metadata={"title": "_Thinking_ step-by-step", "id": 0, "status": "pending"}
    )
    yield response

    thoughts = [
        "<thinking>First, I need to understand the core aspects of the query...</thinking>",
        "<thinking>Now, considering the broader context and implications...</thinking>",
        "<thinking>Analyzing potential approaches to formulate a comprehensive answer...</thinking>",
        "<thinking>Finally, structuring the response for clarity and completeness...</thinking>"
    ]

    accumulated_thoughts = ""
    for thought in thoughts:
        time.sleep(sleep_time)
        accumulated_thoughts += f"- {thought}\n\n"
        response.content = accumulated_thoughts.strip()
        yield response

    response.metadata["status"] = "done"
    response.metadata["duration"] = time.time() - start_time
    yield response

    response = [
        response,
        ChatMessage(
            content="Based on my thoughts and analysis above, my response is: This dummy repro shows how thoughts of a thinking LLM can be progressively shown before providing its final answer."
        )
    ]
    yield response

chatbot = gr.Chatbot(allow_tags=True)
demo = gr.ChatInterface(
    simulate_thinking_chat,
    title="Thinking LLM Chat Interface 🤔",
    type="messages",
    chatbot=chatbot,
)

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

Closes: #10684

🎯 PRs Should Target Issues

Before your create a PR, please check to see if there is an existing issue for this change. If not, please create an issue before you create this PR, unless the fix is very small.

Not adhering to this guideline will result in the PR being closed.

Testing and Formatting Your Code

  1. PRs will only be merged if tests pass on CI. We recommend at least running the backend tests locally, please set up your Gradio environment locally and run the backed tests: bash scripts/run_backend_tests.sh

  2. Please run these bash scripts to automatically format your code: bash scripts/format_backend.sh, and (if you made any changes to non-Python files) bash scripts/format_frontend.sh

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Mar 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/fbbec81f682bb20c05237fc66159e1bbd15a4d29/gradio-5.20.1-py3-none-any.whl

Install Gradio Python Client from this PR

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

Install Gradio JS Client from this PR

npm install https://gradio-npm-previews.s3.amazonaws.com/fbbec81f682bb20c05237fc66159e1bbd15a4d29/gradio-client-1.13.1.tgz

Use Lite from this PR

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

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Mar 6, 2025

🦄 change detected

This Pull Request includes changes to the following packages.

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

With the following changelog entry.

Allow custom tags for chatbot

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.

@dawoodkhan82
Copy link
Collaborator Author

When allowing all tags, the message content is wrapped in <p> tags, should we filter these out?
Also, when loading chatbots, I guess it makes more sense to only allow ["think", "thinking"] rather than allow all tags?

@abidlabs
Copy link
Member

abidlabs commented Mar 7, 2025

One issue I noticed with allow_tags=True is that new lines are converted to <br>. Take a look at this example:

import gradio as gr

value = """
<b>abc</b>
<thinking>adsf</thinking>
"""

with gr.Blocks() as demo:
    gr.Chatbot([(value, value)], allow_tags=True)

demo.launch()

whereas this works fine:

import gradio as gr

value = """
<b>abc</b>
<thinking>adsf</thinking>
"""

with gr.Blocks() as demo:
    gr.Chatbot([(value, value)], allow_tags=["thinking", "b"])

demo.launch()

Copy link
Member

@abidlabs abidlabs left a comment

Choose a reason for hiding this comment

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

see above

@dawoodkhan82 dawoodkhan82 requested a review from abidlabs March 7, 2025 21:12
@dawoodkhan82 dawoodkhan82 changed the title Allow all tags for chatbot Allow custom tags for chatbot Mar 7, 2025
Copy link
Member

@abidlabs abidlabs left a comment

Choose a reason for hiding this comment

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

LGTM @dawoodkhan82 nice! cc @hysts for visibility as well

@abidlabs
Copy link
Member

abidlabs commented Mar 7, 2025

Just pushed a fix for the failing pytest, and I think we should also make allow_tags=True the default in gr.Chatbot in Gradio 6, so added here: #10471

@dawoodkhan82 dawoodkhan82 enabled auto-merge (squash) March 7, 2025 21:39
@dawoodkhan82 dawoodkhan82 merged commit 3086343 into main Mar 7, 2025
22 of 23 checks passed
@dawoodkhan82 dawoodkhan82 deleted the allow-tags branch March 7, 2025 21: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.

Allow all thinking tags to pass through in gr.Chatbot at least for models that are loaded with gr.load()
3 participants