Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuang11 committed Jun 13, 2024
1 parent 9765df5 commit 28f8067
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 15 deletions.
77 changes: 65 additions & 12 deletions examples/reference/chat/ChatFeed.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
"source": [
"A `callback` can be attached for a much more interesting `ChatFeed`.\n",
"\n",
"The signature must include the latest available message value `contents`, the latest available `user` name, and the chat `instance`."
"The signature must include the latest available message value `contents`."
]
},
{
Expand All @@ -230,7 +230,7 @@
"metadata": {},
"outputs": [],
"source": [
"def echo_message(contents, user, instance):\n",
"def echo_message(contents):\n",
" return f\"Echoing... {contents}\"\n",
"\n",
"chat_feed = pn.chat.ChatFeed(callback=echo_message)\n",
Expand All @@ -246,6 +246,59 @@
"message = chat_feed.send(\"Hello!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In addition to `contents`, the signature can also include the latest available `user` name and the chat `instance`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def echo_message(contents, user, instance):\n",
" return f\"Echoing {user!r}... {contents}\\n\\n{instance!r}\"\n",
"\n",
"chat_feed = pn.chat.ChatFeed(callback=echo_message)\n",
"chat_feed"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"message = chat_feed.send(\"Hello!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However, not all three arguments need to be in the signature.\n",
"\n",
"- If there's only one, it will be `contents`.\n",
"- If there's two, it will be `contents` and `user`.\n",
"- If there's three, it will be `contents`, `user`, and `instance`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def echo_message(contents, user):\n",
" return f\"Echoing {user!r}... {contents}\"\n",
"\n",
"chat_feed = pn.chat.ChatFeed(callback=echo_message)\n",
"chat_feed"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -285,7 +338,7 @@
"metadata": {},
"outputs": [],
"source": [
"def parrot_message(contents, user, instance):\n",
"def parrot_message(contents):\n",
" return {\"value\": f\"No, {contents.lower()}\", \"user\": \"Parrot\", \"avatar\": \"🦜\"}\n",
"\n",
"chat_feed = pn.chat.ChatFeed(callback=parrot_message, callback_user=\"Echo Bot\")\n",
Expand Down Expand Up @@ -330,7 +383,7 @@
"metadata": {},
"outputs": [],
"source": [
"def bad_callback(contents, user, instance):\n",
"def bad_callback(contents):\n",
" return 1 / 0\n",
"\n",
"chat_feed = pn.chat.ChatFeed(callback=bad_callback, callback_exception=\"summary\")\n",
Expand Down Expand Up @@ -361,7 +414,7 @@
},
"outputs": [],
"source": [
"def bad_callback(contents, user, instance):\n",
"def bad_callback(contents):\n",
" return 1 / 0\n",
"\n",
"chat_feed = pn.chat.ChatFeed(callback=bad_callback, callback_exception=\"verbose\")\n",
Expand Down Expand Up @@ -405,7 +458,7 @@
"import asyncio\n",
"pn.extension()\n",
"\n",
"async def parrot_message(contents, user, instance):\n",
"async def parrot_message(contents):\n",
" await asyncio.sleep(2.8)\n",
" return {\"value\": f\"No, {contents.lower()}\", \"user\": \"Parrot\", \"avatar\": \"🦜\"}\n",
"\n",
Expand Down Expand Up @@ -439,7 +492,7 @@
"import time\n",
"pn.extension()\n",
"\n",
"async def parrot_message(contents, user, instance):\n",
"async def parrot_message(contents):\n",
" time.sleep(2.8)\n",
" return {\"value\": f\"No, {contents.lower()}\", \"user\": \"Parrot\", \"avatar\": \"🦜\"}\n",
"\n",
Expand Down Expand Up @@ -473,7 +526,7 @@
"metadata": {},
"outputs": [],
"source": [
"async def stream_message(contents, user, instance):\n",
"async def stream_message(contents):\n",
" message = \"\"\n",
" for character in contents:\n",
" message += character\n",
Expand Down Expand Up @@ -505,7 +558,7 @@
"metadata": {},
"outputs": [],
"source": [
"async def replace_message(contents, user, instance):\n",
"async def replace_message(contents):\n",
" for character in contents:\n",
" await asyncio.sleep(0.1)\n",
" yield character\n",
Expand Down Expand Up @@ -535,7 +588,7 @@
"\n",
"pn.extension()\n",
"\n",
"async def openai_callback(contents, user, instance):\n",
"async def openai_callback(contents):\n",
" response = await openai.ChatCompletion.acreate(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=[{\"role\": \"user\", \"content\": contents}],\n",
Expand Down Expand Up @@ -767,7 +820,7 @@
"metadata": {},
"outputs": [],
"source": [
"def say_hi(contents, user, instance):\n",
"def say_hi(contents, user):\n",
" return f\"Hi {user}!\"\n",
"\n",
"chat_feed = pn.chat.ChatFeed(help_text=\"This chat feed will respond by saying hi!\", callback=say_hi)\n",
Expand Down Expand Up @@ -1074,7 +1127,7 @@
"pn.extension()\n",
"\n",
"\n",
"async def get_response(contents, user, instance):\n",
"async def get_response(contents, user):\n",
" await asyncio.sleep(0.88)\n",
" return {\n",
" \"Marc\": \"It is 2\",\n",
Expand Down
6 changes: 3 additions & 3 deletions examples/reference/chat/ChatInterface.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"metadata": {},
"outputs": [],
"source": [
"def even_or_odd(contents, user, instance):\n",
"def even_or_odd(contents):\n",
" if len(contents) % 2 == 0:\n",
" return \"Even number of characters.\"\n",
" return \"Odd number of characters.\"\n",
Expand Down Expand Up @@ -153,7 +153,7 @@
"metadata": {},
"outputs": [],
"source": [
"def count_chars(contents, user, instance):\n",
"def count_chars(contents):\n",
" return f\"Found {len(contents)} characters.\"\n",
"\n",
"\n",
Expand All @@ -178,7 +178,7 @@
"metadata": {},
"outputs": [],
"source": [
"def get_num(contents, user, instance):\n",
"def get_num(contents):\n",
" if isinstance(contents, str):\n",
" num = len(contents)\n",
" else:\n",
Expand Down

0 comments on commit 28f8067

Please sign in to comment.