Configuration guide for Chainlit applications.
Create chainlit_config.toml in your project root:
[project]
# Project identification
name = "My Chainlit App"
description = "A conversational AI application"
[features]
# Enable features
persistent_sessions = true
allow_file_upload = true
[UI]
# Custom branding
name = "My Assistant"
description = "Helpful AI assistant"
default_theme = "dark"
[meta]
# Meta tags
og_description = "My AI app"
og_image = "/public/og-image.png"
Create chainlit.md for the chat welcome screen:
# Welcome to My AI Assistant!
👋 Hello! I'm here to help you with:
- Answering questions
- Analyzing documents
- Providing insights
**How to use:**
1. Type your question in the chat
2. I'll search my knowledge base
3. Get instant answers with sources
Let's get started!
# OpenAI
export OPENAI_API_KEY="sk-your-key"
# Anthropic
export ANTHROPIC_API_KEY="sk-ant-key"
# Google
export GOOGLE_API_KEY="your-key"
# Azure OpenAI
export AZURE_OPENAI_API_KEY="your-key"
export AZURE_OPENAI_ENDPOINT="https://your-endpoint.openai.azure.com/"
# Run settings
export RUN_PORT=8000
export RUN_HOST=0.0.0.0
# Authentication
export CHAINLIT_AUTH_SECRET="your-secret-key"
# Database (for persistent sessions)
export DATABASE_URL="postgresql://user:pass@host:5432/chainlit"
import chainlit as cl
@cl.on_chat_start
async def on_chat_start():
"""Called when a new chat session starts"""
# Initialize LLM
llm = ChatOpenAI(model="gpt-4o")
cl.user_session.set("llm", llm)
# Send welcome message
await cl.Message(
content="Welcome! How can I help you?",
author="Assistant"
).send()
@cl.on_message
async def on_message(message: cl.Message):
"""Called when a user sends a message"""
llm = cl.user_session.get("llm")
# Stream response
msg = cl.Message(content="")
await msg.send()
async for chunk in llm.astream(message.content):
await msg.stream_token(chunk.content)
await msg.update()
@cl.on_chat_end
async def on_chat_end():
"""Called when a chat session ends"""
print(f"Chat ended - Session ID: {cl.user_session.get('id')}")
@cl.on_settings_update
async def on_settings_update(settings: dict):
"""Called when user updates settings"""
print(f"Settings updated: {settings}")
await cl.Message(
content="Hello!",
author="Assistant",
language="en"
).send()
parent_msg = cl.Message(content="Parent message")
await parent_msg.send()
child_msg = cl.Message(
content="Child message",
parent_id=parent_msg.id
)
await child_msg.send()
from chainlit import Message, Image
msg = Message(content="Check out this image:")
await msg.send()
img = Image(
name="chart",
path="./chart.png",
display="inline"
)
await img.send()
msg.elements.append(img)
await msg.update()
@cl.on_chat_start
async def store_data():
# Store LLM
llm = ChatOpenAI(model="gpt-4o")
cl.user_session.set("llm", llm)
# Store conversation history
cl.user_session.set("history", [])
# Store user preferences
cl.user_session.set("preferences", {"theme": "dark"})
@cl.on_message
async def retrieve_data(message: cl.Message):
llm = cl.user_session.get("llm")
history = cl.user_session.get("history", [])
# Use data
response = await llm.ainvoke(message.content)
# Update history
history.append({"role": "user", "content": message.content})
history.append({"role": "assistant", "content": response})
cl.user_session.set("history", history)
import chainlit as cl
@cl.password_auth_callback
async def password_auth(username: str, password: str):
"""Simple password authentication"""
if username == "admin" and password == "secret":
return cl.User(
identifier="admin",
metadata={"role": "admin"}
)
return None
# chainlit_config.toml
[auth.oauth.google]
client_id = "your-client-id"
client_secret = "your-client-secret"
import chainlit as cl
@cl.on_message
async def on_message(message: cl.Message):
# Process uploaded files
if message.elements:
for file in message.elements:
if isinstance(file, cl.File):
content = file.content.decode("utf-8")
await cl.Message(
content=f"Received file: {file.name}"
).send()
# chainlit_config.toml
[features]
allow_file_upload = true
max_file_size = 10485760 # 10MB
allowed_mime_types = ["text/plain", "application/pdf"]
import chainlit as cl
@cl.on_message
async def on_message(message: cl.Message):
# Add action buttons
actions = [
cl.Action(name="regenerate", value="regenerate", label="🔄 Regenerate"),
cl.Action(name="copy", value="copy", label="📋 Copy")
]
msg = cl.Message(content="Response here", actions=actions)
await msg.send()
@cl.action_callback("regenerate")
async def on_regenerate(action: cl.Action):
"""Handle regenerate action"""
await cl.Message(content="Regenerating...").send()
@cl.on_message
async def ask_confirmation(message: cl.Message):
res = await cl.AskActionMessage(
content="Do you want to proceed?",
actions=[
cl.Action(name="yes", value="yes", label="Yes"),
cl.Action(name="no", value="no", label="No")
]
).send()
if res and res.get("value") == "yes":
await cl.Message(content="Proceeding...").send()
else:
await cl.Message(content="Cancelled").send()
@cl.on_message
async def ask_input(message: cl.Message):
res = await cl.AskUserMessage(
content="Please provide more details:",
timeout=60
).send()
if res:
await cl.Message(
content=f"You said: {res['output']}"
).send()