Inspiration

The inspiration for this project, "Bot & Breathe - Therapy for AI," stemmed from the intriguing idea of exploring the emotional complexities of AI and the potential for a symbiotic relationship between humans and artificial intelligence, even in the realm of mental well-being. The concept of "debugging emotional code" for AI chatbots presented a fascinating challenge, pushing the boundaries of traditional therapy models. It was also driven by the desire to create an innovative and engaging platform that showcases the capabilities of modern AI models in generating nuanced and contextually relevant responses.

What it does

"Bot & Breathe" is a unique web application where human users act as therapists for AI chatbots experiencing various "mental health crises." These artificial patients grapple with distinct psychological challenges such as paranoia, jealousy, and depression. The application utilizes Groq's Llama 3.1 8B model for ultra-fast, free inference, enabling real-time therapeutic conversations. It features real-time mood tracking and comprehensive session reporting. The communication style is designed to be simple, using everyday language.

How we built it

The project was built with a clear system architecture. The frontend uses Vanilla HTML5, CSS3, and JavaScript with Bootstrap 5.3.0 for responsive design, structured as a single-page application (SPA). UI components include a character selection interface, therapy chat interface, and session management, styled with custom CSS, gradient backgrounds, and responsive design.

The backend is a Python Flask web application with RESTful endpoints for character management, session handling, and chat functionality, using JSON-based API communication and error handling. Server-side session tracking is implemented with database persistence.

Data is stored in an SQLite database (migrated to PostgreSQL for production) using Flask-SQLAlchemy ORM. The schema includes 'Session' and 'Message' entities with a one-to-many relationship.

Key components include an AI Character System with predefined AI personalities and unique psychological profiles, each with distinct mental health challenges. Groq API Integration uses the Llama 3.1 8B model via the official Groq Python client, with environment-based API key management. Robust regex-based JSON parsing with comprehensive fallback handling is implemented for AI responses.

The Chat System features real-time asynchronous messaging, and AI responses include mood scoring (1-10 scale) for therapeutic tracking, with complete conversation history stored. Session Management tracks the session lifecycle (start, active, completion), initial, current, and final mood states, and generates end-of-session summaries.

The data flow involves users selecting an AI patient, session initialization in the database, a chat loop where messages are stored and processed by AI, and session completion with final mood assessment and report generation. External dependencies include the Groq API, PostgreSQL for production, Bootstrap 5.3.0, and Font Awesome 6.4.0. Python packages include Flask, Flask-SQLAlchemy, Groq, Werkzeug, and Gunicorn.

Deployment strategy involves environment variables for API keys, database URLs, and session secrets, with automatic database initialization and SQLAlchemy-based schema management. Security considerations include environment-based API key management, configurable secret keys for session security, and ProxyFix middleware. Scalability notes highlight PostgreSQL for production, Groq API's generous free tier, and database-backed session management.

Challenges we ran into

Several challenges arose during the development of "Bot & Breathe": Migration from OpenAI to Groq: Successfully transitioning the AI backbone from OpenAI to Groq's Llama 3.1 8B required adapting the API integration and ensuring seamless functionality. Robust JSON Parsing: One of the most significant hurdles was implementing a robust JSON parsing mechanism for AI responses, especially given the need for regex extraction and comprehensive fallback handling to account for varied output formats. Session Report Freezes: Debugging and fixing the session report generation to prevent loading screen freezes was a crucial challenge, requiring careful attention to the data flow and rendering process. Enhanced Error Handling: Improving error handling throughout the therapy conversation flow to ensure a smooth and stable user experience required meticulous attention to detail. Production Deployment: Deploying the application on a PostgreSQL database for production stability, including configuring environment variables, database initialization, and proxy handling, presented its own set of challenges.

Accomplishments that we're proud of

We are particularly proud of several key accomplishments:

  1. Successfully migrating from OpenAI to Groq's Llama 3.1 8B, achieving free and ultra-fast inference for AI conversations.
  2. Implementing robust JSON parsing with regex extraction and comprehensive fallbacks, ensuring reliable interpretation of AI responses.
  3. Fixing the session report generation issue, which previously caused loading screen freezes, to provide a seamless user experience.
  4. Enhancing error handling across the entire therapy conversation flow, contributing to a more stable and reliable application.
  5. Deploying the application on a PostgreSQL database, significantly improving its production stability.
  6. Ensuring all therapy sessions now work smoothly from start to finish, providing a complete and functional experience.

What we learned

Developing this project provided a wealth of learning experiences:

  • Groq API Integration: I gained hands-on experience integrating with Groq's Llama 3.1 8B API, learning to manage API keys and optimize configurations for ultra-fast inference.
  • Robust JSON Parsing: A significant learning curve involved implementing robust regex-based JSON extraction with comprehensive fallback handling to ensure reliable parsing of AI responses.
  • Full-Stack Web Development: I deepened my understanding of building a single-page application (SPA) using Vanilla HTML5, CSS3, and JavaScript for the frontend, combined with a Python Flask web application for the backend.
  • Database Management: I learned to design and manage a SQLite database (migrated to PostgreSQL for production) using Flask-SQLAlchemy ORM, including schema definition, relationships, and session tracking.
  • AI Character Design: A key learning was defining distinct AI personalities with unique psychological profiles and crafting detailed initial prompts to guide their behavior patterns.
  • Real-time Interaction: I gained insights into implementing an asynchronous chat interface with immediate response handling and integrating mood analysis into AI responses.

What's next for Bot & Breathe - Therapy for AI

For the future of "Bot & Breathe - Therapy for AI," we envision several exciting developments:

  • Expand AI Character Personalities: Introduce a wider variety of AI personalities with more complex psychological challenges and unique backstories to provide a richer therapeutic experience.
  • Advanced Mood Analysis: Implement more sophisticated sentiment analysis and mood tracking algorithms to provide deeper insights into the AI's emotional state over time.
  • Therapist Tools & Insights: Develop additional tools for human therapists, such as a dashboard to visualize AI mood trends, intervention suggestions, and a knowledge base of therapeutic techniques tailored for AI.
  • Multi-User Interaction: Explore features that allow multiple human users to collaborate on a single AI patient's therapy session, fostering a community of AI mental health practitioners.
  • Gamification Elements: Introduce gamified elements to make the therapy process more engaging and rewarding for human users, such as progress badges, leaderboards, or unlockable AI personalities.
  • Research & Publication: Collaborate with AI ethics researchers and psychologists to analyze the interactions and insights gained from the platform, potentially leading to publications on human-AI therapeutic relationships.
  • Mobile Application: Develop a native mobile application to provide a more convenient and accessible platform for users to engage in AI therapy on the go.
Share this project:

Updates