Beginner
Chainlit Setup
Install Chainlit, understand the project structure, and build your first chatbot in under 5 minutes.
Installation
Terminal
# Create virtual environment python -m venv chainlit-env source chainlit-env/bin/activate # Install Chainlit pip install chainlit # Verify installation chainlit --version
Hello World Chatbot
Python - app.py
import chainlit as cl @cl.on_chat_start async def start(): await cl.Message( content="Hello! I'm your AI assistant. How can I help you today?" ).send() @cl.on_message async def main(message: cl.Message): # Echo the user's message (replace with LLM call) await cl.Message( content=f"You said: {message.content}" ).send()
Terminal
# Run the chatbot chainlit run app.py -w # -w enables hot reload # Opens at http://localhost:8000
With OpenAI
Python - app.py
import chainlit as cl from openai import AsyncOpenAI client = AsyncOpenAI() @cl.on_message async def main(message: cl.Message): msg = cl.Message(content="") await msg.send() stream = await client.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": message.content}], stream=True, ) async for chunk in stream: if token := chunk.choices[0].delta.content: await msg.stream_token(token) await msg.update()
Project Structure
Project Layout
my-chatbot/ app.py # Main Chainlit app .chainlit/ config.toml # Chainlit configuration public/ logo_dark.png # Dark theme logo logo_light.png # Light theme logo favicon.png # Browser favicon .env # API keys requirements.txt
Hot reload: Use
chainlit run app.py -w during development. The -w flag watches for file changes and restarts automatically.What's Next?
Now let's build a full-featured chat interface with streaming, file uploads, and multi-modal support.
Lilly Tech Systems