holoviz / panel

Panel: The powerful data exploration & web app framework for Python
https://panel.holoviz.org
BSD 3-Clause "New" or "Revised" License
4.42k stars 484 forks source link

ChatFeed documentation wrong in regards of ChatFeed/ChatInterface 'callback_avatar' and ChatMessage 'value' #6954

Open awesomebytes opened 3 days ago

awesomebytes commented 3 days ago

First, let me thank you for your wonderful work. I am testing your whole ecosystem for the first time and I am quite pleased and impressed with it. As I was trying to build a toy project with your Chat interfaces I found discrepancies between your latest available docs and the code.

ALL software version info

I cloned https://github.com/holoviz-topics/panel-chat-examples and used devcontainer environment. So all versions as specified in: https://github.com/holoviz-topics/panel-chat-examples/blob/main/pyproject.toml

panel's version is: panel==1.5.0a8

My output of pip freeze in the devcontainer is:

aiohttp==3.9.5 aiosignal==1.3.1 annotated-types==0.7.0 anyio==4.4.0 attrs==23.2.0 beautifulsoup4==4.12.3 bleach==6.1.0 bokeh==3.5.0rc1 certifi==2024.7.4 charset-normalizer==3.3.2 click==8.1.7 contourpy==1.2.1 dataclasses-json==0.6.7 Deprecated==1.2.14 dirtyjson==1.0.8 diskcache==5.6.3 distro==1.9.0 frozenlist==1.4.1 fsspec==2024.6.1 gitdb==4.0.11 GitPython==3.1.41 greenlet==3.0.3 h11==0.14.0 httpcore==1.0.5 httpx==0.27.0 idna==3.7 Jinja2==3.1.4 joblib==1.4.2 jsonpatch==1.33 jsonpointer==3.0.0 langchain==0.2.6 langchain-community==0.2.6 langchain-core==0.2.11 langchain-text-splitters==0.2.2 langsmith==0.1.83 linkify-it-py==2.0.3 llama-cloud==0.0.6 llama-index==0.10.52 llama-index-agent-openai==0.2.7 llama-index-cli==0.1.12 llama-index-core==0.10.52.post1 llama-index-embeddings-openai==0.1.10 llama-index-indices-managed-llama-cloud==0.2.3 llama-index-legacy==0.9.48 llama-index-llms-openai==0.1.25 llama-index-multi-modal-llms-openai==0.1.6 llama-index-program-openai==0.1.6 llama-index-question-gen-openai==0.1.3 llama-index-readers-file==0.1.27 llama-index-readers-llama-parse==0.1.6 llama-parse==0.4.5 llama_cpp_python==0.2.81 Markdown==3.6 markdown-it-py==3.0.0 MarkupSafe==2.1.5 marshmallow==3.21.3 mdit-py-plugins==0.4.1 mdurl==0.1.2 mistralai==0.4.2 multidict==6.0.5 mypy-extensions==1.0.0 nest-asyncio==1.6.0 networkx==3.3 nltk==3.8.1 numpy==1.26.4 openai==1.35.10 orjson==3.10.6 packaging==24.1 pandas==2.2.2 panel==1.5.0a8 -e git+https://github.com/holoviz-topics/panel-chat-examples@b18c0f24315d3926fcc5169a7736cd92b82db09f#egg=panel_chat_examples param==2.1.1 pillow==10.4.0 pydantic==2.8.2 pydantic_core==2.20.1 pypdf==4.2.0 python-dateutil==2.9.0.post0 pytz==2024.1 pyviz_comms==3.0.2 PyYAML==6.0.1 regex==2024.5.15 requests==2.32.3 six==1.16.0 smmap==5.0.1 sniffio==1.3.1 soupsieve==2.5 SQLAlchemy==2.0.31 striprtf==0.0.26 tenacity==8.4.2 tiktoken==0.7.0 tornado==6.4.1 tqdm==4.66.4 typing-inspect==0.9.0 typing_extensions==4.12.2 tzdata==2024.1 uc-micro-py==1.0.3 urllib3==2.2.2 webencodings==0.5.1 wrapt==1.16.0 xyzservices==2024.6.0 yarl==1.9.4

Description of expected behavior and the observed behavior

The ChatFeed documentation states that you can:

callback_avatar (str | bytes | BytesIO | pn.pane.ImageBase): The avatar to use for the user. Can be a single character text, an emoji, or anything supported by pn.pane.Image. If not set, uses the first character of the name.

However, trying to use the argument callback_avatar gets me:

        self.chat_interface = pn.chat.ChatInterface(
            callback=self.chat_callback,
            callback_user="Assistant",
            callback_avatar="A",
            widgets=[pn.chat.ChatAreaInput(name="Ask", placeholder="Ask something")]
        )
# Error on the browser:
TypeError: ChatInterface.init() got an unexpected keyword argument 'callback_avatar'

As I saw that it didn't work (and checked the source code and there isn't a callback_avatar declared) I went to try the next thing from the async callbacks documentation where it says:

async def parrot_message(contents, user, instance):
    await asyncio.sleep(2.8)
    return {"value": f"No, {contents.lower()}", "user": "Parrot", "avatar": "🦜"}

However if I try that, I get in the browser chat:

Encountered ValueError("'value' is not a parameter of ChatMessage"). Set callback_exception='verbose' to see the full traceback.

So, checking the code, I see that value is not the correct key, the correct one is object.

Complete, minimal, self-contained example code that reproduces the issue

This code reproduces both issues:

"""
Demonstrates how to use the `ChatInterface` and a `callback` function to
stream back responses.

The chatbot Assistant echoes back the message entered by the User in an
*async streaming* fashion.

Highlights:

- The function is defined as `async` and uses `yield` to stream back responses.
- Initialize `message` first to gather the characters and then `yield` it;
    without it, only one letter would be displayed at a time.
"""

from asyncio import sleep

import panel as pn

pn.extension()

async def callback(contents: str, user: str, instance: pn.chat.ChatInterface):
    await sleep(1)
    message = ""
    for char in "Echoing User: " + contents:
        await sleep(0.05)
        message += char
        yield {"value": message}

chat_interface = pn.chat.ChatInterface(callback=callback,
                                       callback_avatar="A"
                                       )
chat_interface.send(
    "Enter a message below and receive an echo!",
    user="System",
    respond=False,
)
chat_interface.servable()

Screenshots or screencasts of the bug in action

Screenshot 2024-07-04 165743 Screenshot 2024-07-04 165800

awesomebytes commented 3 days ago

I think this is related to: https://github.com/holoviz/panel/issues/5717