AI Model Wiki

    Skip to main content
    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

    1. Header: App name, model selector, new chat button
    2. Sidebar: List of previous conversations
    3. Chat Area: Scrollable message list with auto-scroll
    4. 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

    1. User clicks Send or presses Enter
    2. Create a new Message (role: user, content: input text)
    3. Clear the input field
    4. Show typing indicator
    5. Trigger backend API workflow to call AI
    6. On response: Create new Message (role: assistant, content: response)
    7. Hide typing indicator

    API Call Configuration

    Configure the OpenAI API call:

    Building the Messages Array

    The trickiest part is constructing the messages array:

    1. Start with the system prompt
    2. Add all previous messages in the conversation
    3. 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.