Skip to content

Suggestion buttons under chat message #132

Description

@ahuang11
"""
Demonstrates how to use the `ChatInterface` to create a chatbot using
OpenAI's with async/await.
"""

import panel as pn
from openai import AsyncOpenAI

pn.extension()


def custom_serializer(widget):
    if isinstance(widget, str):
        return widget
    elif hasattr(widget, "objects"):
        return widget.objects[0].object
    elif hasattr(widget, "object"):
        return widget.object
    elif hasattr(widget, "value"):
        return widget.value
    return widget


async def callback(contents: str, user: str, instance: pn.chat.ChatInterface):
    def _send(click, suggestion):
        instance.widgets[0].value = suggestion

    messages = instance.serialize(custom_serializer=custom_serializer)
    response = await aclient.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=messages,
        stream=True,
    )

    parts = ""
    async for chunk in response:
        part = chunk.choices[0].delta.content
        if part:
            parts += part
            yield parts

    suggestion_buttons = []
    for _ in range(2):
        suggestion = await aclient.chat.completions.create(
            model="gpt-3.5-turbo",
            temperature=1.25,
            messages=[
                *messages[-2:],
                {
                    "role": "user",
                    "content": "Output a follow up question to continue the conversation.",
                },
            ],
        )
        suggestion_button = pn.widgets.Button(
            name=suggestion.choices[0].message.content, styles={"color": "darkgrey"}
        )
        pn.bind(
            _send, suggestion_button, suggestion.choices[0].message.content, watch=True
        )
        suggestion_buttons.append(suggestion_button)
    yield pn.Column(parts, pn.layout.Divider(), pn.Row(*suggestion_buttons))


aclient = AsyncOpenAI()
chat_interface = pn.chat.ChatInterface(callback=callback, callback_user="ChatGPT")
chat_interface.send(
    "Send a message to get a reply from ChatGPT!", user="Help", respond=False
)
chat_interface.servable()

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions