Chainlit
Chainlit is an open-source Python framework designed to simplify the creation of interactive user interfaces for Large Language Model (LLM) applications. It enables developers to build ChatGPT-like UIs with minimal frontend code, offering features such as chat lifecycle hooks, UI actions, real-time message streaming, and integrations with popular LLM libraries like LangChain and LlamaIndex. The library is actively maintained with frequent releases, currently at version 2.11.0.
Warnings
- breaking Chainlit v2.9.4 introduced a breaking change requiring a database migration for users employing persistence. You must run `ALTER TABLE steps ADD COLUMN IF NOT EXISTS modes JSONB;` to migrate your database.
- gotcha Chainlit is an async-first framework. All operations that interact with the UI, such as `cl.Message().send()` or other `cl` methods that involve sending data, must be `await`ed. Failing to do so will result in a coroutine object being returned instead of the expected action, potentially leading to silent failures or unexpected behavior.
- gotcha When authentication is enabled (e.g., via `CHAINLIT_AUTH_SECRET` or OAuth), 'Invalid authentication token' errors can occur, especially when rendering images. This often indicates a missing or incorrect `CHAINLIT_AUTH_SECRET` environment variable.
- gotcha Configuration defined in `chainlit.toml` (e.g., project name, UI settings) may occasionally be ignored, particularly in certain older versions (e.g., v2.7.2). This can lead to default settings being applied instead of your custom configurations.
Install
-
pip install chainlit
Imports
- cl
import chainlit as cl
- cl.on_chat_start
@cl.on_chat_start
- cl.on_message
@cl.on_message
- cl.Message
await cl.Message(content='Hello!').send()
Quickstart
import chainlit as cl
import os
# Optional: Set CHAINLIT_AUTH_SECRET if authentication is enabled for your app
# os.environ['CHAINLIT_AUTH_SECRET'] = os.environ.get('CHAINLIT_AUTH_SECRET', 'your_secret_key_here_for_testing')
@cl.on_chat_start
async def start():
await cl.Message(
content="Welcome! I am a simple Chainlit bot. Type anything to get a response."
).send()
@cl.on_message
async def main(message: cl.Message):
# Simulate a tool's response
await cl.Message(author="Tool", content=f"Processing: {message.content}", indent=1).send()
# Send back the final answer
await cl.Message(content=f"You said: {message.content}").send()
# To run this:
# 1. Save the code as `app.py`
# 2. Run `chainlit run app.py -w` in your terminal