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 stopping respond callbacks midway #5962

Merged
merged 10 commits into from
Dec 10, 2023
Merged

Allow stopping respond callbacks midway #5962

merged 10 commits into from
Dec 10, 2023

Conversation

ahuang11
Copy link
Contributor

@ahuang11 ahuang11 commented Dec 1, 2023

Closes #5943

Upon sending a message, if the callback is async, the Send button gets hidden and the Stop button appears. Clicking it will cancel the callback midway.

Screen.Recording.2023-11-30.at.3.58.11.PM.mov

Makes it so that sync tasks are submitted to a separate thread so it's cancellable.

Screen.Recording.2023-12-04.at.10.58.51.PM.mov
import panel as pn
from openai import OpenAI

pn.extension()


def callback(contents: str, user: str, instance: pn.chat.ChatInterface):
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": contents}],
        stream=True,
    )
    message = None
    for chunk in response:
        part = chunk.choices[0].delta.content
        if part is not None:
            message = instance.stream(part, user="OpenAI", message=message)


client = OpenAI()
chat_interface = pn.chat.ChatInterface(callback=callback, callback_user="ChatGPT")
chat_interface.send(
    "Send a message to get a reply from ChatGPT!", user="System", respond=False
)
chat_interface.stream("Hello")
chat_interface.show()

Also, I think I made it more performant for sync too.

Copy link

codecov bot commented Dec 1, 2023

Codecov Report

Attention: 13 lines in your changes are missing coverage. Please review.

Comparison is base (7e3d985) 84.67% compared to head (ebba0ef) 84.67%.

Files Patch % Lines
panel/tests/util.py 71.42% 8 Missing ⚠️
panel/tests/chat/test_interface.py 95.00% 2 Missing ⚠️
panel/chat/feed.py 98.03% 1 Missing ⚠️
panel/chat/interface.py 95.00% 1 Missing ⚠️
panel/tests/chat/test_feed.py 98.03% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5962      +/-   ##
==========================================
- Coverage   84.67%   84.67%   -0.01%     
==========================================
  Files         291      291              
  Lines       43586    43722     +136     
==========================================
+ Hits        36907    37020     +113     
- Misses       6679     6702      +23     
Flag Coverage Δ
ui-tests 40.67% <16.93%> (-0.08%) ⬇️
unitexamples-tests 72.84% <93.15%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@ahuang11 ahuang11 marked this pull request as draft December 2, 2023 01:45
@ahuang11 ahuang11 marked this pull request as ready for review December 4, 2023 22:07
ahuang11 and others added 2 commits December 4, 2023 22:00
* Add sync interrupt

* Handle midstreams

* Simplify

* Fix tests

* Update batch_Watch
@ahuang11 ahuang11 changed the title Allow stopping async respond callbacks midway Allow stopping respond callbacks midway Dec 5, 2023
panel/chat/feed.py Outdated Show resolved Hide resolved
@philippjfr
Copy link
Member

Still seeing some errors in the tests.

@ahuang11
Copy link
Contributor Author

ahuang11 commented Dec 5, 2023

Okay fixed the issue: generators are "finished" prematurely from the POV of a future, so I had to introduce a _callback_state param that track whether it's a generator and stop it manually.

@philippjfr philippjfr merged commit 4b36d7f into main Dec 10, 2023
13 checks passed
@philippjfr philippjfr deleted the add_interrupt_async branch December 10, 2023 17:01
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.

Interrupt Button for ChatInterface
2 participants