Skip to content

Commit

Permalink
Make ChatMessage a pane and ChatFeed a ListPanel (#5659)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Oct 18, 2023
1 parent 1fc7f2d commit 9df8f1e
Show file tree
Hide file tree
Showing 16 changed files with 601 additions and 652 deletions.
61 changes: 44 additions & 17 deletions examples/reference/chat/ChatFeed.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The `ChatFeed` is a mid-level widget, that lets you manage a list of [`ChatMessage`](ChatMessage.ipynb) items.\n",
"The `ChatFeed` is a mid-level layout, that lets you manage a list of [`ChatMessage`](ChatMessage.ipynb) items.\n",
"\n",
"This widget provides backend methods to:\n",
"This layout provides backend methods to:\n",
"- Send (append) messages to the chat log.\n",
"- Stream tokens to the latest `ChatMessage` in the chat log.\n",
"- Execute callbacks when a user sends a message.\n",
Expand All @@ -35,7 +35,7 @@
"\n",
"##### Core\n",
"\n",
"* **`value`** (`List[ChatMessage]`): The entries added to the chat feed.\n",
"* **`objects`** (`List[ChatMessage]`): The messages added to the chat feed.\n",
"* **`renderers`** (List[Callable]): A callable or list of callables that accept the value and return a Panel object to render the value. If a list is provided, will attempt to use the first renderer that does not raise an exception. If None, will attempt to infer the renderer from the value.\n",
"* **`callback`** (callable): Callback to execute when a user sends a message or when `respond` is called. The signature must include the previous message value `contents`, the previous `user` name, and the component `instance`.\n",
"\n",
Expand Down Expand Up @@ -64,9 +64,9 @@
"\n",
"##### Other\n",
"\n",
"* **`clear`**: Clears the chat log and returns the entries that were cleared.\n",
"* **`clear`**: Clears the chat log and returns the messages that were cleared.\n",
"* **`respond`**: Executes the callback with the latest message in the chat log.\n",
"* **`undo`**: Removes the last `count` of entries from the chat log and returns them. Default `count` is 1.\n",
"* **`undo`**: Removes the last `count` of messages from the chat log and returns them. Default `count` is 1.\n",
"\n",
"___"
]
Expand All @@ -92,7 +92,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"You can send chat entries with the `send` method."
"You can send chat messages with the `send` method."
]
},
{
Expand All @@ -108,7 +108,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The `send` method returns a [`ChatEntry`](ChatEntry.ipynb), which can display any object that Panel can display. You can **interact with chat entries** like any other Panel component. You can find examples in the [`ChatEntry` Reference Notebook](ChatEntry.ipynb)."
"The `send` method returns a [`ChatEntry`](ChatEntry.ipynb), which can display any object that Panel can display. You can **interact with chat messages** like any other Panel component. You can find examples in the [`ChatEntry` Reference Notebook](ChatEntry.ipynb)."
]
},
{
Expand All @@ -133,7 +133,7 @@
"metadata": {},
"outputs": [],
"source": [
"message = chat_feed.send({\"value\": \"Welcome!\", \"user\": \"Bot\", \"avatar\": \"B\"})"
"message = chat_feed.send({\"object\": \"Welcome!\", \"user\": \"Bot\", \"avatar\": \"B\"})"
]
},
{
Expand All @@ -149,10 +149,10 @@
"metadata": {},
"outputs": [],
"source": [
"pn.chat.ChatFeed(value=[\n",
" pn.chat.ChatMessage(value=\"I'm an emoji!\", avatar=\"🤖\"),\n",
" pn.chat.ChatMessage(value=\"I'm an image!\", avatar=\"https://upload.wikimedia.org/wikipedia/commons/6/63/Yumi_UBports.png\"),\n",
"])"
"pn.chat.ChatFeed(\n",
" pn.chat.ChatMessage(\"I'm an emoji!\", avatar=\"🤖\"),\n",
" pn.chat.ChatMessage(\"I'm an image!\", avatar=\"https://upload.wikimedia.org/wikipedia/commons/6/63/Yumi_UBports.png\"),\n",
")"
]
},
{
Expand All @@ -168,7 +168,7 @@
"metadata": {},
"outputs": [],
"source": [
"message = chat_feed.send({\"value\": \"Overtaken!\", \"user\": \"Bot\"}, user=\"MegaBot\")"
"message = chat_feed.send({\"object\": \"Overtaken!\", \"user\": \"Bot\"}, user=\"MegaBot\")"
]
},
{
Expand Down Expand Up @@ -277,8 +277,35 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"You can surface exceptions by setting `callback_exception` to `\"summary\"`.\n",
"You can surface exceptions by setting `callback_exception` to `\"summary\"`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def bad_callback(contents, user, instance):\n",
" return 1 / 0\n",
"\n",
"chat_feed = pn.chat.ChatFeed(callback=bad_callback, callback_exception=\"summary\")\n",
"chat_feed"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"chat_feed.send(\"This will fail...\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To see the entire traceback, you can set it to `\"verbose\"`."
]
},
Expand Down Expand Up @@ -639,7 +666,7 @@
")\n",
"\n",
"chat_feed = ChatFeed(\n",
" value=[ChatMessage(user=\"Assistant\", value=\"Hi There!\", avatar=ASSISTANT_AVATAR)],\n",
" ChatMessage(\"Hi There!\", user=\"Assistant\", avatar=ASSISTANT_AVATAR),\n",
" callback=get_response,\n",
" height=500,\n",
" message_params=dict(\n",
Expand All @@ -650,15 +677,15 @@
"marc_button = pn.widgets.Button(\n",
" name=\"Marc\",\n",
" on_click=lambda event: chat_feed.send(\n",
" user=\"Marc\", value=\"What is the square root of 4?\", avatar=\"🚴\"\n",
" \"What is the square root of 4?\", user=\"Marc\", avatar=\"🚴\"\n",
" ),\n",
" align=\"center\",\n",
" disabled=chat_feed.param.disabled,\n",
")\n",
"andrew_button = pn.widgets.Button(\n",
" name=\"Andrew\",\n",
" on_click=lambda event: chat_feed.send(\n",
" user=\"Andrew\", value=\"What is the square root of 4 squared?\", avatar=\"🏊\"\n",
" \"What is the square root of 4 squared?\", user=\"Andrew\", avatar=\"🏊\"\n",
" ),\n",
" align=\"center\",\n",
" disabled=chat_feed.param.disabled,\n",
Expand Down
11 changes: 7 additions & 4 deletions examples/reference/chat/ChatInterface.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The `ChatInterface` is a high-level widget, providing a user-friendly front-end interface for inputting different kinds of messages: text, images, PDFs, etc.\n",
"The `ChatInterface` is a high-level layout, providing a user-friendly front-end interface for inputting different kinds of messages: text, images, PDFs, etc.\n",
"\n",
"This widget provides front-end methods to:\n",
"This layout provides front-end methods to:\n",
"\n",
"- Input (append) messages to the chat log.\n",
"- Re-run (resend) the most recent `user` input [`ChatMessage`](ChatMessage.ipynb).\n",
"- Remove entries until the previous `user` input [`ChatMessage`](ChatMessage.ipynb).\n",
"- Remove messages until the previous `user` input [`ChatMessage`](ChatMessage.ipynb).\n",
"- Clear the chat log, erasing all [`ChatMessage`](ChatMessage.ipynb) objects.\n",
"\n",
"Since `ChatInterface` inherits from [`ChatFeed`](ChatFeed.ipynb), it features all the capabilities of [`ChatFeed`](ChatFeed.ipynb); please see [ChatFeed.ipynb](ChatFeed.ipynb) for its backend capabilities.\n",
Expand Down Expand Up @@ -123,9 +123,12 @@
"def count_chars(contents, user, instance):\n",
" return f\"Found {len(contents)} characters.\"\n",
"\n",
"\n",
"ChatInterface(\n",
" callback=count_chars,\n",
" widgets=pn.widgets.TextAreaInput(placeholder=\"Enter some text to get a count!\"),\n",
" widgets=pn.widgets.TextAreaInput(\n",
" placeholder=\"Enter some text to get a count!\", auto_grow=True, max_rows=3\n",
" ),\n",
")"
]
},
Expand Down
Loading

0 comments on commit 9df8f1e

Please sign in to comment.