A comprehensive therapeutic chatbot system implementing Structured Agentic Therapy (SAT) using Large Language Models (LLMs). This project includes both frontend and backend components for delivering therapeutic dialogue, along with data analysis tools for user study evaluation.
This repository contains the complete implementation of an LLM-based therapeutic chatbot that features:
- Multi-Agent Architecture: Structured therapeutic dialogue with specialized agents
- Single-Agent Mode: Simplified therapeutic interaction
- Placebo/Control Mode: For comparative user studies
- Data Analysis Tools: Statistical analysis of user study results
The system is designed to provide evidence-based therapeutic interventions through natural language conversations, incorporating RAG (Retrieval-Augmented Generation) for exercise recommendations and multi-stage dialogue management.
SAT-chatbot/
βββ frontend/ # React-based web application
β βββ src/
β β βββ Chatbot.js # Multi-agent chatbot implementation
β β βββ ChatbotSimple.js # Single-agent chatbot
β β βββ ChatbotPlacebo.js # Placebo/control chatbot
β β βββ Auth/ # Authentication components
β β βββ assets/ # Images and media files
β β βββ utils/ # Utility functions
β βββ package.json
β
βββ backend/ # Django REST API
β βββ api/
β β βββ models.py # Database models
β β βββ views.py # API endpoints
β β βββ serializers.py # Data serialization
β β βββ bot/
β β βββ gpt.py # Main chatbot logic
β β βββ simple_bot.py # Single-agent implementation
β β βββ placebo_bot.py # Placebo implementation
β β βββ Memory/ # Conversation memory management
β β βββ Prompts/ # LLM prompt templates
β β βββ RAG/ # Exercise recommendation system
β βββ backend/ # Django settings
β βββ manage.py
β βββ requirements.txt
β
βββ data-analysis/ # User study analysis scripts
βββ analyze_sat_user_study.py # Statistical analysis (ANOVA, permutation tests)
- Node.js (v14 or higher) and npm
- Python (3.8 or higher)
- OpenAI API Key (for LLM integration)
- Navigate to the frontend directory:
cd frontend- Install dependencies:
npm install-
Configure environment variables:
- Create a
.envfile in the frontend directory (or copy from.env.example) - Add the following:
REACT_APP_BASE_URL=http://localhost:8000 - Update the URL if your backend is running on a different host or port
- Create a
-
Start the development server:
npm startThe frontend will be available at http://localhost:3000 (React's default port).
- Navigate to the backend directory:
cd backend- Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt-
Configure environment variables:
- Create a
.envfile in the backend directory (or copy from.env.example) - Add your OpenAI API key:
OPENAI_API_KEY=your_api_key_here - The
.env.examplefile shows all available configuration options
- Create a
-
Run database migrations:
python manage.py migrate- Create a superuser (optional, for admin access):
python manage.py createsuperuser- Start the development server:
python manage.py runserverThe backend API will be available at http://localhost:8000.
Note: The data analysis scripts require CSV files with user study data. These files are not included in the repository.
- Navigate to the data-analysis directory:
cd data-analysis- Install required Python packages:
pip install -r requirements.txt-
Prepare your data files:
SAT User Study (Responses) - Form Responses 1.csv- Survey responsesemail_username_group_flag.csv- Email to group mapping
-
Run analysis scripts:
# For user study statistical analysis
python analyze_sat_user_study.py-
Multiple Chatbot Modes:
- Multi-agent structured therapy (Chatbot.js)
- Single-agent simplified therapy (ChatbotSimple.js)
- Placebo/control mode (ChatbotPlacebo.js)
-
User Authentication: Login and registration system
-
Rich UI: Built with React, Ant Design, and Tailwind CSS
-
Markdown Support: Display formatted responses from the chatbot
-
Audio Recording: Support for voice input (AudioRecorder.js)
-
Exercise Visualization: Interactive exercise recommendations with images
-
Multi-Stage Dialogue Management:
- Greeting and name collection
- Emotion identification and validation
- Event exploration
- Exercise recommendation
- Exercise execution and guidance
-
RAG System:
- 26+ therapeutic exercises with detailed descriptions
- Semantic search for exercise matching
- Context-aware recommendations
-
Memory Management: Maintains conversation context across interactions
-
API Endpoints:
- User registration and authentication (JWT)
- Message handling and chat history
- Group assignment (control/intervention/placebo)
-
Statistical Analysis:
- One-way ANOVA with permutation testing
- Effect size calculation (eta-squared)
- Automated visualization (boxplots)
-
Metrics:
- Group comparison across multiple survey questions
- Demographic analysis
- User engagement metrics
Create frontend/.env file:
REACT_APP_BASE_URL=http://localhost:8000
Edit backend/.env:
OPENAI_API_KEY=your_openai_api_key
SECRET_KEY=your_django_secret_key
DEBUG=True
The backend uses Django REST Framework with JWT authentication. Key settings are in backend/backend/settings.py:
- CORS settings for frontend communication
- Database configuration (SQLite by default)
- OpenAI integration settings
POST /api/register/- User registrationPOST /api/login/- User authenticationPOST /api/message/- Send message to chatbot (multi-agent mode)POST /api/simple-chat/- Send message to simple chatbotPOST /api/placebo-chat/- Send message to placebo chatbotGET /api/chat-history/- Retrieve chat history (requires authentication)GET /api/user-sessions/- Get all session IDs for authenticated userPOST /api/reset-state/- Reset state machine for authenticated userPOST /api/end-session/- End current sessionPOST /api/send-audio/- Send audio file for transcriptionPOST /api/process-buffered/- Process buffered messages for authenticated user
The API uses JWT (JSON Web Tokens) for authentication. Include the token in the Authorization header:
Authorization: Bearer <your_jwt_token>
cd frontend
npm testcd backend
python manage.py testThis system was developed as part of research on therapeutic dialogue design using LLMs. Key aspects include:
- Comparison of multi-agent vs. single-agent architectures
- Evaluation of structured vs. unguided conversations
- User study with control, intervention, and placebo groups
For more details, refer to the research paper included in this project.
- Never commit
.envfiles or API keys to version control - Use environment variables for all sensitive configuration
- Implement proper rate limiting in production
- Enable HTTPS for production deployments
This is a research project. If you'd like to contribute or adapt this code:
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is intended for research and educational purposes. Please cite the associated research paper if you use this code in your work.
- OpenAI for GPT API
- Django and React communities
- All user study participants
Note: This is a research prototype. For production use, additional security hardening, scalability improvements, and comprehensive testing are recommended.