18 min
Lesson 2 of 4
Building an AI Chat Application
From the course Bubble AI App Builder
Let's build a complete AI chat application in Bubble — one of the most requested features for modern web apps.
UI Design
Chat Interface Layout
- Header: App name, model selector, new chat button
- Sidebar: List of previous conversations
- Chat Area: Scrollable message list with auto-scroll
- Input Area: Text input with send button
Message Display
Create a repeating group for messages:
- Data source: Search for Messages where conversation = current conversation
- Sort by: created_date ascending
- Conditionally style user vs assistant messages
- Add a typing indicator for AI responses
Backend Workflow
Sending a Message
- User clicks Send or presses Enter
- Create a new Message (role: user, content: input text)
- Clear the input field
- Show typing indicator
- Trigger backend API workflow to call AI
- On response: Create new Message (role: assistant, content: response)
- Hide typing indicator
API Call Configuration
Configure the OpenAI API call:
- Endpoint: https://api.openai.com/v1/chat/completions
- Method: POST
- Headers: Authorization: Bearer [key], Content-Type: application/json
- Body: JSON with model, messages array, temperature
Building the Messages Array
The trickiest part is constructing the messages array:
- Start with the system prompt
- Add all previous messages in the conversation
- Include the latest user message
Use Bubble's ":format as text" to build the JSON array from your message database.
Advanced Features
- Model switching: Let users choose between GPT-4, Claude, Gemini
- System prompts: Allow users to set custom AI personalities
- Token counting: Track usage and set limits
- Export: Let users download conversation history
Key Takeaways
- •Build chat interfaces with repeating groups and conditional message styling
- •Use backend API workflows to call AI APIs and create assistant messages
- •Construct the messages array from conversation history for context
- •Add model switching, system prompts, and token tracking for production apps
0 of 4 lessons marked complete
Progress is kept in this browser only.