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

Suggestion buttons under chat message #132

Open
ahuang11 opened this issue Feb 28, 2024 · 0 comments
Open

Suggestion buttons under chat message #132

ahuang11 opened this issue Feb 28, 2024 · 0 comments

Comments

@ahuang11
Copy link
Collaborator

"""
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant