在继续之前要求用户上传文件。如果用户未在规定时间内响应(见 timeout 参数),将根据 raise_on_timeout 参数的值引发 TimeoutError 错误或返回 None。如果配置了项目 ID,消息将被上传到云存储。

属性

content
str

上传按钮上方显示的文本。

accept
Union[List[str], Dict[str, List[str]]]

要接受的 MIME 类型列表,例如 ["text/csv", "application/pdf"],或者像 {"text/plain": [".txt", ".py"]} 这样的字典。更多信息请参见此处 https://react-dropzone.org/#!/Accepting%20specific%20file%20types

max_size_mb
int

文件最大大小,单位 MB。默认为 2。

max_files
int

允许上传的最大文件数量。默认为 1。最大值为 10。

timeout
int

在引发 TimeoutError 错误之前等待响应的秒数。

raise_on_timeout
bool

如果用户未在规定时间内响应,是否引发 socketio TimeoutError 错误。

返回值

response
List[AskFileResponse]
必需

用户上传的文件。

示例

请求上传一个文本文件
import chainlit as cl


@cl.on_chat_start
async def start():
    files = None

    # Wait for the user to upload a file
    while files == None:
        files = await cl.AskFileMessage(
            content="Please upload a text file to begin!", accept=["text/plain"]
        ).send()

    text_file = files[0]

    with open(text_file.path, "r", encoding="utf-8") as f:
        text = f.read()

    # Let the user know that the system is ready
    await cl.Message(
        content=f"`{text_file.name}` uploaded, it contains {len(text)} characters!"
    ).send()

您还可以将字典传递给 accept 参数,以指定每种 MIME 类型的文件扩展名

请求上传一个 Python 文件
import chainlit as cl

file = await cl.AskFileMessage(
        content="Please upload a python file to begin!", accept={"text/plain": [".py"]}
      ).send()