Chainlit / chainlit

Build Conversational AI in minutes ⚡️
https://docs.chainlit.io
Apache License 2.0
6.67k stars 860 forks source link

Use an action to send a prompt to LLM #459

Closed rtu-dataframe closed 11 months ago

rtu-dataframe commented 11 months ago

Hi, using this action button, how i can use the click event to send a prompt (like as the user write it in the prompt field) to the LLM?

I'm looking for a simple and effective way to automate the send of some simple prompts using buttons, avoiding the user to write manually the prompt.

import chainlit as cl

@cl.action_callback("action_button")
async def on_action(action):
    await cl.Message(content=f"Executed {action.name}").send()
    # Optionally remove the action button from the chatbot user interface
    await action.remove()

@cl.on_chat_start
async def start():
    # Sending an action button within a chatbot message
    actions = [
        cl.Action(name="action_button", value="example_value", description="Click me!")
    ]

    await cl.Message(content="Interact with this action button:", actions=actions).send()

Many thanks

willydouhard commented 11 months ago

Not sure I understand the question. If you want to send a specific prompt to your LLM and display the answer in the UI when a specific action is clicked you can write that logic in the cl.action_callback.

import chainlit as cl

@cl.action_callback("action_button")
async def on_action(action):
    response = # Call the LLM with a specific prompt
    await cl.Message(content=response).send()

@cl.on_chat_start
async def start():
    # Sending an action button within a chatbot message
    actions = [
        cl.Action(name="action_button", value="example_value", description="Click me!")
    ]

    await cl.Message(content="Interact with this action button:", actions=actions).send()