Skip to content

Commit

Permalink
Add custom callback exception callable
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuang11 committed Dec 17, 2024
1 parent ad154df commit 4863433
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 7 deletions.
47 changes: 47 additions & 0 deletions examples/reference/chat/ChatFeed.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"* **`header`** (Any): The header of the chat feed; commonly used for the title. Can be a string, pane, or widget.\n",
"* **`callback_user`** (str): The default user name to use for the message provided by the callback.\n",
"* **`callback_avatar`** (str, BytesIO, bytes, ImageBase): The default avatar to use for the entry provided by the callback. Takes precedence over `ChatMessage.default_avatars` if set; else, if None, defaults to the avatar set in `ChatMessage.default_avatars` if matching key exists. Otherwise defaults to the first character of the `callback_user`.\n",
"* **`callback_exception`** (str, Callable): How to handle exceptions raised by the callback. If \"raise\", the exception will be raised. If \"summary\", a summary will be sent to the chat feed. If \"verbose\" or \"traceback\", the full traceback will be sent to the chat feed. If \"ignore\", the exception will be ignored. If a callable is provided, the signature must contain the `exception` and `instance` arguments and it will be called with the exception.\n",
"* **`help_text`** (str): If provided, initializes a chat message in the chat log using the provided help text as the message object and `help` as the user. This is useful for providing instructions, and will not be included in the `serialize` method by default.\n",
"* **`placeholder_text`** (str): The text to display next to the placeholder icon.\n",
"* **`placeholder_params`** (dict) Defaults to `{\"user\": \" \", \"reaction_icons\": {}, \"show_copy_icon\": False, \"show_timestamp\": False}` Params to pass to the placeholder `ChatMessage`, like `reaction_icons`, `timestamp_format`, `show_avatar`, `show_user`, `show_timestamp`.\n",
Expand Down Expand Up @@ -433,6 +434,52 @@
"chat_feed.send(\"This will fail...\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Alternatively, you can provide a callable that accepts the exception and the instance as arguments to handle different exception scenarios."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"\n",
"def callback(content):\n",
" if random.random() < 0.5:\n",
" raise RuntimeError(\"This is an unhandled error\")\n",
" raise ValueError(\"This is a handled error\")\n",
"\n",
"\n",
"def callback_exception_handler(exception, instance):\n",
" if isinstance(exception, ValueError):\n",
" instance.stream(\"I can handle this\", user=\"System\")\n",
" return\n",
" instance.stream(\"Fatal error occurred\", user=\"System\")\n",
"\n",
" # you can raise a new exception here if desired\n",
" # raise RuntimeError(\"Fatal error occurred\") from exception\n",
"\n",
"\n",
"chat_feed = pn.chat.ChatFeed(\n",
" callback=callback, callback_exception=callback_exception_handler\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"chat_feed.send(\"This will sometimes fail...\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
23 changes: 23 additions & 0 deletions panel/chat/_param.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from param import Parameter


class CallbackException(Parameter):
"""
A Parameter type to validate callback_exception options. Supports
"raise", "summary", "verbose", "traceback", "ignore", or a callable.
"""

def __init__(self, default="summary", **params):
super().__init__(default=default, **params)
self._validate(default)

def _validate(self, val):
self._validate_value(val, self.allow_None)

def _validate_value(self, val, allow_None, valid=("raise", "summary", "verbose", "traceback", "ignore")):
if ((val is None and allow_None) or val in valid or callable(val)):
return
raise ValueError(
f"Callback exception mode {val} not recognized. "
f"Valid options are {valid} or a callable."
)
16 changes: 11 additions & 5 deletions panel/chat/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from ..viewable import Children
from ..widgets import Widget
from ..widgets.button import Button
from ._param import CallbackException
from .icon import ChatReactionIcons
from .message import ChatMessage
from .step import ChatStep
Expand Down Expand Up @@ -100,13 +101,16 @@ class ChatFeed(ListPanel):
the previous message value `contents`, the previous `user` name,
and the component `instance`.""")

callback_exception = param.Selector(
default="summary", objects=["raise", "summary", "verbose", "ignore"], doc="""
callback_exception = CallbackException(
default="summary", doc="""
How to handle exceptions raised by the callback.
If "raise", the exception will be raised.
If "summary", a summary will be sent to the chat feed.
If "verbose", the full traceback will be sent to the chat feed.
If "verbose" or "traceback", the full traceback will be sent to the chat feed.
If "ignore", the exception will be ignored.
If a callable is provided, the signature must contain the
`exception` and `instance` arguments and it
will be called with the exception.
""")

callback_user = param.String(default="Assistant", doc="""
Expand Down Expand Up @@ -574,13 +578,15 @@ async def _prepare_response(self, *_) -> None:
self._callback_state = CallbackState.STOPPED
except Exception as e:
send_kwargs: dict[str, Any] = dict(user="Exception", respond=False)
if self.callback_exception == "summary":
if callable(self.callback_exception):
self.callback_exception(e, self)
elif self.callback_exception == "summary":
self.send(
f"Encountered `{e!r}`. "
f"Set `callback_exception='verbose'` to see the full traceback.",
**send_kwargs
)
elif self.callback_exception == "verbose":
elif self.callback_exception in ("verbose", "traceback"):
self.send(f"```python\n{traceback.format_exc()}\n```", **send_kwargs)
elif self.callback_exception == "ignore":
return
Expand Down
22 changes: 20 additions & 2 deletions panel/tests/chat/test_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,12 +1083,13 @@ def callback(msg, user, instance):
assert "division by zero" in chat_feed.objects[-1].object
assert chat_feed.objects[-1].user == "Exception"

def test_callback_exception_traceback(self, chat_feed):
@pytest.mark.parametrize("callback_exception", ["traceback", "verbose"])
def test_callback_exception_traceback(self, chat_feed, callback_exception):
def callback(msg, user, instance):
return 1 / 0

chat_feed.callback = callback
chat_feed.callback_exception = "verbose"
chat_feed.callback_exception = callback_exception
chat_feed.send("Message", respond=True)
assert chat_feed.objects[-1].object.startswith(
"```python\nTraceback (most recent call last):"
Expand All @@ -1114,6 +1115,23 @@ def callback(msg, user, instance):
chat_feed.send("Message", respond=True)
wait_until(lambda: len(chat_feed.objects) == 1)

def test_callback_exception_callable(self, chat_feed):
def callback(msg, user, instance):
raise ValueError("Expected error")

def exception_callback(exception, instance):
instance.stream(f"The exception: {exception}")

chat_feed.callback = callback
chat_feed.callback_exception = exception_callback
chat_feed.send("Message", respond=True)
wait_until(lambda: len(chat_feed.objects) == 2)
assert chat_feed.objects[-1].object == "The exception: Expected error"

def test_callback_exception_invalid_option(self, chat_feed):
with pytest.raises(ValueError, match="Valid options are"):
chat_feed.callback_exception = "abc"

def test_callback_stop_generator(self, chat_feed):
def callback(msg, user, instance):
yield "A"
Expand Down

0 comments on commit 4863433

Please sign in to comment.