Ask the user to upload a file before continuing.
If the user does not answer in time (see timeout), a TimeoutError will be raised or None will be returned depending on raise_on_timeout.
If a project ID is configured, the messages will be uploaded to the cloud storage.
import chainlit as cl@cl.on_chat_startasync 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()
You can also pass a dict to the accept parameter to precise the file extension for each mime type:
Ask for a python file
Copy
import chainlit as clfile = await cl.AskFileMessage( content="Please upload a python file to begin!", accept={"text/plain": [".py"]} ).send()