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

Trouble creating a chatbot in Python #8

Open
cpsievert opened this issue Nov 21, 2024 · 1 comment
Open

Trouble creating a chatbot in Python #8

cpsievert opened this issue Nov 21, 2024 · 1 comment

Comments

@cpsievert
Copy link
Contributor

The python prompt currently doesn't have any knowledge of ui.Chat() or chatlas. This was noticed by Ferit:

I've noticed the shinyassistant is having trouble creating an LLM chat application in Python. I am not sure what the problem is, but I am wondering if it doesn't know about the ui.Chat component. Here is my prompt:
Please create a basic application that provides a chat interface to talk to an LLM. Please use the ui.Chat component.
Here is the result: https://shinylive.io/py/editor/#code=NobwRAdghgtgpmAXGKAHVA6VBPMAaMAYwHsIAXOcpMAMwCdiYAC[…]IMgNsyMfD8OoGNeexjHWeprPLF1qUMdAxTEVA3CMUwOC5RkNGpMAHyVIA
Asking it to fix the error messages makes things increasingly worse. Replacing the ui.chat(...) with ui.Chat() makes it complain: RuntimeError: Chat() must be called from within an active Shiny session.

@Mauli-Jagtap
Copy link

Try -

from shiny import App, reactive, render, ui

app_ui = ui.page_fluid(
ui.card(
ui.card_header("Chat with AI Assistant"),
ui.output_ui("chat"),
ui.input_text("user_input", "", placeholder="Type your message here..."),
ui.input_action_button("send", "Send", class_="btn-primary"),
)
)

def server(input, output, session):
messages = reactive.Value([]) # Correct initialization

@reactive.Effect
@reactive.event(input.send)
def update_chat():
    if not input.user_input():
        return
    
    # Add user message
    current_messages = messages.get()
    current_messages.append({
        "content": input.user_input(),
        "avatar": "👤",
        "username": "User",
        "align": "right",
    })
    
    # Simulate AI response
    response = f"You said: '{input.user_input()}'. This is a simulated response."
    current_messages.append({
        "content": response,
        "avatar": "🤖",
        "username": "AI Assistant",
        "align": "left",
    })
    
    messages.set(current_messages)  # Update the reactive state
    ui.update_text("user_input", "")  # Clear input

@render.ui
def chat():
    return ui.chat_message_set(messages.get())

output.chat = chat

app = App(app_ui, server)

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

2 participants