命令是一种以确定性方式捕获用户意图的好方法。

属性

id
str

命令的标识符,这将在用户界面中使用。

icon
str

命令的 lucide 图标名称。参见 https://lucide.dev/icons/

description
str

命令的描述。

button
boolean

是否在消息编辑器中将命令显示为按钮。

persistent
boolean

用户发送消息后是否保持命令激活。

设置可用命令

您可以使用 cl.context.emitter.set_commands 方法随时设置可用命令。

import chainlit as cl

commands = [
    {"id": "Picture", "icon": "image", "description": "Use DALL-E"},
    {"id": "Search", "icon": "globe", "description": "Find on the web"},
    {
        "id": "Canvas",
        "icon": "pen-line",
        "description": "Collaborate on writing and code",
    },
]

@cl.on_chat_start
async def start():
    await cl.context.emitter.set_commands(commands)

@cl.on_message
async def message(msg: cl.Message):
    if msg.command == "Picture":
        # User is using the Picture command
        pass
    pass

用户选择命令