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
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
782a130
Start adding chat steps
ahuang11 Mar 30, 2024
ae589ca
Fix CSS
ahuang11 Apr 1, 2024
28a4eed
Add chat steps
ahuang11 Apr 2, 2024
a56872d
Address comments
ahuang11 Apr 2, 2024
d35538b
redesign layout
ahuang11 May 15, 2024
65af3fc
Tweak and add serialize
ahuang11 May 15, 2024
3f4255b
Refactor
ahuang11 May 20, 2024
f1a99b2
add tests
ahuang11 May 20, 2024
c428324
mention in chat feed
ahuang11 May 20, 2024
24d31d0
fix docs and remove chat steps from public
ahuang11 May 20, 2024
d2b8ae8
Update panel/chat/feed.py
ahuang11 May 20, 2024
1fbaac6
forgot to add this notebook
ahuang11 May 20, 2024
4be9ce9
fix tests
ahuang11 May 20, 2024
36ef00a
fix more tests
ahuang11 May 20, 2024
7364487
Add methods
ahuang11 May 23, 2024
3b9f276
renames and refactor
ahuang11 May 23, 2024
d4468bb
Update docs
ahuang11 May 23, 2024
c823b69
Merge branch 'main' into add_chat_steps
ahuang11 May 29, 2024
cb69745
Fix test
ahuang11 May 30, 2024
f8143df
fix the tests
ahuang11 May 30, 2024
d8db0b9
address minor comments
ahuang11 Jun 3, 2024
519a765
Rename to append_steps, compress into steps kwarg, docs
ahuang11 Jun 3, 2024
34955d6
Add test
ahuang11 Jun 3, 2024
00bedc0
remove unused test
ahuang11 Jun 3, 2024
be7d6c0
fix example
ahuang11 Jun 3, 2024
68fb65b
address comment
ahuang11 Jun 13, 2024
6daf74e
Merge branch 'main' into add_chat_steps
ahuang11 Jun 13, 2024
b0badd7
Merge branch 'main' into add_chat_steps
philippjfr Jun 25, 2024
3963a06
Merge branch 'main' into add_chat_steps
philippjfr Jun 25, 2024
a5ae22b
Remove ChatSteps
philippjfr Jun 26, 2024
add7b7a
Adjust append logic
philippjfr Jun 26, 2024
f1e0f73
Rename to add_step
philippjfr Jun 26, 2024
032c095
Update docstrings
philippjfr Jun 26, 2024
fe3f0c1
Add tests
philippjfr Jun 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions examples/reference/chat/ChatFeed.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,71 @@
"message = chat_feed.send(\"Hello bots!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Steps\n",
"\n",
"Intermediate steps, like chain of thoughts, can be provided through a series of [`ChatStep`](ChatStep.ipynb), which is conveniently accessible through `stream_steps` and `append_step`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"chat_feed = pn.chat.ChatFeed()\n",
"chat_feed"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"steps = chat_feed.stream_steps()\n",
"with steps.append_step(\"To answer the user's query, I need to first create a plan.\", title=\"Create a plan\", status=\"running\") as step:\n",
" step.stream(\"\\n\\n...Okay the plan is to demo this!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Multiple steps can be initialized."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"steps.append_step(title=\"The next thing is to come up with features to demo...\", status=\"pending\")\n",
"steps.append_step(title=\"TBD\", status=\"pending\")\n",
"steps.append_step(title=\"TBD\", status=\"pending\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then a context manager can be used to \"process\" them."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with steps[1] as step:\n",
" step.stream(\"\\n\\n...Okay, we can demo the context manager\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
8 changes: 6 additions & 2 deletions panel/chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,32 @@
For more detail see the Reference Gallery guide.
https://panel.holoviz.org/reference/chat/ChatInterface.html
"""

import importlib as _importlib

from .feed import ChatFeed # noqa
from .icon import ChatReactionIcons # noqa
from .input import ChatAreaInput # noqa
from .interface import ChatInterface # noqa
from .message import ChatMessage # noqa
from .step import ChatStep # noqa


def __getattr__(name):
"""
Lazily import langchain module when accessed.
"""
if name == 'langchain':
return _importlib.import_module('panel.chat.langchain')
if name == "langchain":
return _importlib.import_module("panel.chat.langchain")
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")


__all__ = (
"ChatAreaInput",
"ChatFeed",
"ChatInterface",
"ChatMessage",
"ChatReactionIcons",
"ChatStep",
"langchain",
)
18 changes: 18 additions & 0 deletions panel/chat/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from ..pane.image import SVG
from .icon import ChatReactionIcons
from .message import ChatMessage
from .step import ChatSteps

if TYPE_CHECKING:
from bokeh.document import Document
Expand Down Expand Up @@ -681,6 +682,23 @@ def stream(
self.param.trigger("_post_hook_trigger")
return message

def stream_steps(self, **params):
"""
Creates a new ChatSteps component and streams it.

Arguments
---------
message_params : dict
Parameters to pass to the ChatSteps.

Returns
-------
The ChatSteps that was created.
"""
steps = ChatSteps(**params)
self.stream(steps)
return steps

def respond(self):
"""
Executes the callback with the latest message in the chat log.
Expand Down
158 changes: 15 additions & 143 deletions panel/chat/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
from __future__ import annotations

import datetime
import re

from contextlib import ExitStack
from dataclasses import dataclass
from functools import partial
from io import BytesIO
from tempfile import NamedTemporaryFile
from textwrap import indent
from typing import (
TYPE_CHECKING, Any, Callable, ClassVar, Iterable, Optional, Union,
TYPE_CHECKING, Any, Callable, ClassVar, Optional, Union,
)
from zoneinfo import ZoneInfo

Expand All @@ -35,6 +33,9 @@
from ..viewable import Viewable
from ..widgets.base import Widget
from .icon import ChatCopyIcon, ChatReactionIcons
from .utils import (
avatar_lookup, build_avatar_pane, serialize_recursively, stream_to,
)

if TYPE_CHECKING:
from bokeh.document import Document
Expand Down Expand Up @@ -335,70 +336,6 @@ def _build_layout(self):
viewable_params['stylesheets'] = self._stylesheets + self.param.stylesheets.rx()
self._composite = Row(left_col, right_col, **viewable_params)

def _get_obj_label(self, obj):
ahuang11 marked this conversation as resolved.
Show resolved Hide resolved
"""
Get the label for the object; defaults to specified object name;
if unspecified, defaults to the type name.
"""
label = obj.name
type_name = type(obj).__name__
# If the name is just type + ID, simply use type
# e.g. Column10241 -> Column
if label.startswith(type_name) or not label:
label = type_name
return label

def _serialize_recursively(
self,
obj: Any,
prefix_with_viewable_label: bool = True,
prefix_with_container_label: bool = True
) -> str:
"""
Recursively serialize the object to a string.
"""
if isinstance(obj, Iterable) and not isinstance(obj, str):
content = tuple(
self._serialize_recursively(
o,
prefix_with_viewable_label=prefix_with_viewable_label,
prefix_with_container_label=prefix_with_container_label
) for o in obj
)
if prefix_with_container_label:
if len(content) == 1:
return f"{self._get_obj_label(obj)}({content[0]})"
else:
indented_content = indent(",\n".join(content), prefix=" " * 4)
# outputs like:
# Row(
# 1,
# "str",
# )
return f"{self._get_obj_label(obj)}(\n{indented_content}\n)"
else:
# outputs like:
# (1, "str")
return f"({', '.join(content)})"

string = obj
if hasattr(obj, "value"):
string = obj.value
elif hasattr(obj, "object"):
string = obj.object

if hasattr(string, "decode") or isinstance(string, BytesIO):
self.param.warning(
f"Serializing byte-like objects are not supported yet; "
f"using the label of the object as a placeholder for {obj}"
)
return self._get_obj_label(obj)

if prefix_with_viewable_label and isinstance(obj, Viewable):
label = self._get_obj_label(obj)
string = f"{label}={string!r}"
return string

def __str__(self) -> str:
"""
Serialize the message object to a string.
Expand All @@ -423,31 +360,6 @@ def _get_model(
self._models[ref] = (model, parent)
return model

@staticmethod
def _to_alpha_numeric(user: str) -> str:
"""
Convert the user name to an alpha numeric string,
removing all non-alphanumeric characters.
"""
return re.sub(r"\W+", "", user).lower()

def _avatar_lookup(self, user: str) -> Avatar:
"""
Lookup the avatar for the user.
"""
alpha_numeric_key = self._to_alpha_numeric(user)
# always use the default first
updated_avatars = DEFAULT_AVATARS.copy()
# update with the user input
updated_avatars.update(self.default_avatars)
# correct the keys to be alpha numeric
updated_avatars = {
self._to_alpha_numeric(key): value for key, value in updated_avatars.items()
}

# now lookup the avatar
return updated_avatars.get(alpha_numeric_key, self.avatar).format(dist_path=CDN_DIST)

def _select_renderer(
self,
contents: Any,
Expand Down Expand Up @@ -593,26 +505,9 @@ def _render_avatar(self) -> HTML | Image:
if not avatar and self.user:
avatar = self.user[0]

avatar_params = {'css_classes': ["avatar"]}
if isinstance(avatar, ImageBase):
avatar_pane = avatar
avatar_params['css_classes'] = (
avatar_params.get('css_classes', []) +
avatar_pane.css_classes
)
avatar_params.update(width=35, height=35)
avatar_pane.param.update(avatar_params)
elif not isinstance(avatar, (BytesIO, bytes)) and len(avatar) == 1:
# single character
avatar_pane = HTML(avatar, **avatar_params)
else:
try:
avatar_pane = Image(
avatar, width=35, height=35, **avatar_params
)
except ValueError:
# likely an emoji
avatar_pane = HTML(avatar, **avatar_params)
avatar_pane = build_avatar_pane(
avatar, css_classes=["avatar"], height=35, width=35
)
return avatar_pane

def _update_avatar_pane(self, event=None):
Expand Down Expand Up @@ -666,7 +561,12 @@ def _update_avatar(self):
if self.avatar_lookup:
self.avatar = self.avatar_lookup(self.user)
else:
self.avatar = self._avatar_lookup(self.user)
self.avatar = avatar_lookup(
self.user,
self.avatar,
self.default_avatars,
DEFAULT_AVATARS,
)

def _update_chat_copy_icon(self):
object_panel = self._object_panel
Expand Down Expand Up @@ -703,33 +603,7 @@ def stream(self, token: str, replace: bool = False):
replace: bool (default=False)
Whether to replace the existing text.
"""
i = -1
parent_panel = None
object_panel = self
attr = "object"
obj = self.object
if obj is None:
obj = ""

while not isinstance(obj, str) or isinstance(object_panel, ImageBase):
object_panel = obj
if hasattr(obj, "objects"):
parent_panel = obj
attr = "objects"
obj = obj.objects[i]
i = -1
elif hasattr(obj, "object"):
attr = "object"
obj = obj.object
elif hasattr(obj, "value"):
attr = "value"
obj = obj.value
elif parent_panel is not None:
obj = parent_panel
parent_panel = None
i -= 1
contents = token if replace else obj + token
setattr(object_panel, attr, contents)
stream_to(obj=self.object, token=token, replace=replace)

def update(
self,
Expand Down Expand Up @@ -782,8 +656,6 @@ def serialize(

Arguments
---------
obj : Any
ahuang11 marked this conversation as resolved.
Show resolved Hide resolved
The object to format.
prefix_with_viewable_label : bool
Whether to include the name of the Viewable, or type
of the viewable if no name is specified.
Expand All @@ -796,7 +668,7 @@ def serialize(
str
The serialized string.
"""
return self._serialize_recursively(
return serialize_recursively(
self.object,
prefix_with_viewable_label=prefix_with_viewable_label,
prefix_with_container_label=prefix_with_container_label,
Expand Down
Loading