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

Merged
merged 34 commits into from
Jun 26, 2024

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 98.07163% with 7 lines in your changes missing coverage. Please review.

Project coverage is 81.67%. Comparing base (3f28aa9) to head (fe3f0c1).
Report is 1 commits behind head on main.

Files Patch % Lines
panel/chat/step.py 96.70% 3 Missing ⚠️
panel/chat/__init__.py 33.33% 2 Missing ⚠️
panel/chat/feed.py 95.23% 1 Missing ⚠️
panel/chat/utils.py 98.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6617      +/-   ##
==========================================
+ Coverage   72.30%   81.67%   +9.37%     
==========================================
  Files         322      326       +4     
  Lines       47663    47944     +281     
==========================================
+ Hits        34463    39160    +4697     
+ Misses      13200     8784    -4416     

☔ 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
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
panel/chat/feed.py Outdated Show resolved Hide resolved
panel/chat/steps.py Outdated Show resolved Hide resolved
panel/chat/steps.py Outdated Show resolved Hide resolved
@philippjfr
Copy link
Member

Sorry for the delay in reviewing this. The functionality here looks great but now that I'm actually playing around with this I'm finding it pretty confusing. It's not helped by the fact that the examples in the original comment haven't been updated so I'm guessing at the proper way to use this. I'll have to spend some more time with it to form a better opinion on what the API should be but I'm fairly convinced that this isn't mergeable in the current state.

@philippjfr
Copy link
Member

I think the main thing I'm struggling with is that I don't quite understand the point of the ChatSteps component. It's a very light wrapper around a Column and since ChatFeed.append_step returns the step component, not the steps component it's not even really part of the API most people will use. So the only thing it really adds is the active parameter, which I also do not really understand. What I'd really have liked to see is a writeup that explains the reason for adding each of these components, as it is I'm going to work towards eliminating ChatSteps.

@philippjfr
Copy link
Member

Okay, I've made some changes here:

  1. ChatSteps no longer exists
  2. Renamed the append_step method to ChatFeed.add_step
  3. The logic for appending steps is largely implicit now, i.e. the first time you add_step it adds a Column that will collect the steps and subsequent add_step calls will be appended as long as the steps are considered active. To replace the logic around an "active" ChatSteps we now treat it as active only as long as the steps are the last item in the feed. A new set of steps can be appended by either specifying a different user OR explicitly setting append=False.

I'm quite happy with this approach, but may have missed something, e.g. maybe you should be able to append to an existing set of steps even if it's not the last message. If that's something we need to support we can revisit. For now I'll merge to unblock some of the other work going on.

@philippjfr philippjfr merged commit d06c1f2 into main Jun 26, 2024
15 checks passed
@philippjfr philippjfr deleted the add_chat_steps branch June 26, 2024 15:12
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