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

Implement ChatSteps and ChatStep to show/hide intermediate steps #6617

Open
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

ahuang11
Copy link
Contributor

@ahuang11 ahuang11 commented Mar 30, 2024

Edit: see notebook for updated usage.

Closes #6598

The relationship:
ChatInterface > ChatFeed > Steps or Step
Steps > Step

Easily available to use by:
instance.step(**kwargs) -> instance.stream(ChatStep(**kwargs))
instance.steps(**kwargs) -> instance.stream(ChatSteps(**kwargs))
steps.step(**kwargs) -> steps.append(step)

ChatStep(s) methods mimic ChatFeed methods:
step.stream()
step.stream_title()

import time
import panel as pn

pn.extension()


def callback(contents: str, user: str, instance: pn.chat.ChatInterface):
    with instance.steps() as steps:
        for i in range(0, 4):
            with steps.step() as step:
                message = None
                for c in contents[i:]:
                    time.sleep(0.3)
                    message = step.stream(c, message=message)
                    step.title = f"Processing: {c}"

chat_interface = pn.chat.ChatInterface(callback=callback, callback_exception="verbose")
chat_interface.show()

These context managers are optional and the equivalent of:

import time
import panel as pn

pn.extension()


def callback(contents: str, user: str, instance: pn.chat.ChatInterface):
    steps = pn.chat.ChatSteps(title="Running...", status="running")
    instance.stream(steps)
    
    for i in range(0, 4):
        step = pn.chat.ChatStep(margin=0)
        step.title = "Running..."
        step.status = "running"
        steps.append(step)
        message = None
        for c in contents[i:]:
            time.sleep(0.3)
            message = step.stream(c, message=message)
            step.title = f"Processing: {c}"
        step.status = "completed"
    steps.title = step.objects[-1].object
    steps.status = "completed"

chat_interface = pn.chat.ChatInterface(callback=callback, callback_exception="verbose")
chat_interface.show()
Screen.Recording.2024-04-01.at.6.00.35.PM.mov
  • docs
  • tests
  • serialize

@MarcSkovMadsen
Copy link
Collaborator

MarcSkovMadsen commented Apr 2, 2024

I'm overall very much for making progress reporting easy.

The first feedback I have is

  • Is this functionality really chat specific. I believe many other processes/ flows would like to report back feedback like this. For example Streamlit made a status component. Not a chat_status component. The feature will be more valuable if its general.
  • I like the context manager a lot. But if we introduce it here would it not be a good idea to analyze which components/ flows could benefit from a context manager approach and then make a general design specification?
  • Rember to check look and feel with sizing_mode="stretch_width" and in a template (like FastListTemplate). I would also recommend finding some beautiful icons instead of colorful emojis that will not work great with specific accent colors and templates.

@ahuang11
Copy link
Contributor Author

ahuang11 commented Apr 2, 2024

I like the idea of a Status component that inherits from Card.

I like the context manager a lot. But if we introduce it here would it not be a good idea to analyze which components/ flows could benefit from a context manager approach and then make a general design specification?

I'm not 100% sure that's neceessary since it's a matter of implementing __enter__ & __exit__ in the corresponding classes if needed--no special method names required.

@MarcSkovMadsen
Copy link
Collaborator

The reason i think its important with a strategy for Context managers is that its much easier to use a framework if it follows some general principløs. It's much harder if suddenly one component can be used as a Context manager and the rest of the components cannot.

@ahuang11
Copy link
Contributor Author

ahuang11 commented Apr 2, 2024

Can you think of other components that context managers could be useful for?

I imagine indicators, boolean, Card/Accordion.

Also, maybe disabled/loading?

@ahuang11
Copy link
Contributor Author

Cleaner layout:

Screen.Recording.2024-05-14.at.7.23.52.PM.mov

Copy link

codecov bot commented May 15, 2024

Codecov Report

Attention: Patch coverage is 97.61905% with 9 lines in your changes missing coverage. Please review.

Project coverage is 81.63%. Comparing base (97c2918) to head (be7d6c0).
Report is 12 commits behind head on main.

Files Patch % Lines
panel/chat/feed.py 85.00% 3 Missing ⚠️
panel/chat/step.py 96.70% 3 Missing ⚠️
panel/chat/__init__.py 50.00% 2 Missing ⚠️
panel/chat/utils.py 98.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6617      +/-   ##
==========================================
+ Coverage   81.52%   81.63%   +0.11%     
==========================================
  Files         318      324       +6     
  Lines       46776    47220     +444     
==========================================
+ Hits        38133    38547     +414     
- Misses       8643     8673      +30     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ahuang11 ahuang11 requested a review from philippjfr May 20, 2024 20:40
@ahuang11
Copy link
Contributor Author

ahuang11 commented May 20, 2024

Okay this is ready for review! See https://github.com/holoviz/panel/blob/1fbaac662d8e33887eebac7e215fa441e04c091b/examples/reference/chat/ChatStep.ipynb for usage.

I'm not sure if ChatSteps requires a separate gallery notebook?

image

@ahuang11 ahuang11 marked this pull request as ready for review May 20, 2024 20:41
panel/chat/feed.py Outdated Show resolved Hide resolved
@ahuang11 ahuang11 changed the title Start adding chat steps Implement ChatSteps and ChatStep to show/hide intermediate steps May 20, 2024
@ahuang11 ahuang11 marked this pull request as draft May 23, 2024 06:04
@ahuang11
Copy link
Contributor Author

Added some new methods while implementing holoviz/lumen#560. Need to change the docs, and also add tests, which I realized I didn't commit it and accidentally removed it locally :(

Screen.Recording.2024-05-22.at.11.01.27.PM.mov

@ahuang11 ahuang11 marked this pull request as ready for review May 23, 2024 21:18
@ahuang11
Copy link
Contributor Author

ready for review now

Screen.Recording.2024-05-23.at.2.23.24.PM.mov

panel/chat/step.py Outdated Show resolved Hide resolved
panel/chat/step.py Outdated Show resolved Hide resolved
panel/chat/step.py Outdated Show resolved Hide resolved
panel/chat/step.py Outdated Show resolved Hide resolved
panel/chat/feed.py Outdated Show resolved Hide resolved
self.stream(steps, user=user, avatar=avatar)
return steps

def attach_step(self, objects: str | list[str] | None = None, **step_params) -> ChatStep:
Copy link
Member

Choose a reason for hiding this comment

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

What happens right now if you .stream() when the last message contains ChatSteps could that not simply be used to attach a new step?

Copy link
Member

Choose a reason for hiding this comment

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

If this can't be removed I'd suggest renaming maybe, maybe add_step or stream_step?

Copy link
Member

Choose a reason for hiding this comment

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

append_step?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I decided against adding the functionality to stream since it can get quite complex due to the kwargs value accepting ChatMessage types and also message.

Instead, I compressed the two functions into append_step, where it accepts a steps kwarg which can be new, append, or a ChatSteps type.

panel/chat/steps.py Outdated Show resolved Hide resolved
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.

ChatAccordion and ChatCard
3 participants