Toweel is an intelligent emotion analysis search engine demo project developed for the AI in Action Hackathon. This project adopts a frontend-backend separation architecture, and this repository contains the backend API service.
By combining Google Cloud Vertex AI's text embedding models with MongoDB vector database, Toweel can understand users' emotional expressions and return relevant emotional content, helping users better understand and express their emotional states.
- Intelligent Emotion Analysis: Based on Google Cloud Vertex AI's advanced text embedding models
- Vector Semantic Search: Precise semantic matching using MongoDB vector search
- Conversational Guidance: Progressive collection of user input for more accurate search results
- RAG Enhanced Analysis: Combined with Retrieval-Augmented Generation technology for in-depth emotion analysis reports
- Session Management: Multi-turn conversation support with context continuity
- Health Monitoring: Complete service health status monitoring
- FastAPI - High-performance async web framework
- Uvicorn - ASGI server
- Pydantic - Data validation and settings management
- Google Cloud Vertex AI - Text embedding model service
- Text-Embedding-005 - 256-dimensional text vectorization model
- Gemini-2.0-flash - Intelligent analysis, provide guidance, and RAG
- MongoDB - Document database and vector search
- Motor - Async MongoDB driver
- PyMongo - Sync MongoDB driver
- NumPy - Numerical computation and vector operations
- Pandas - Data processing and analysis
- Docker - Containerized deployment
- Docker Compose - Multi-container orchestration
backend/
βββ app/
β βββ api/
β β βββ endpoints/ # API route definitions
β β βββ search.py # Search and analysis endpoints
β β βββ history.py # History record endpoints
β β βββ debug.py # Debug endpoints
β βββ services/ # Business logic layer
β β βββ search_service.py # Search service
β β βββ rag_service.py # RAG analysis service
β β βββ session_service.py # Session management service
β β βββ conversation_guide_service.py # Conversation guidance service
β β βββ history_service.py # History record service
β βββ main.py # Application entry point
β βββ database.py # Database connection configuration
β βββ vector_index.py # Vector index management
βββ DataProcess/ # Data preprocessing (development phase only)
βββ requirements.txt # Python dependencies
βββ Dockerfile # Docker image build
βββ docker-compose.yml # Container orchestration configuration
Note: The
DataProcess/folder contains code and data for dataset cleaning and vectorization performed before the project started. The software does not use any code from this folder during actual runtime.
POST /search/- Process user text input, perform emotion analysis and conversational guidancePOST /search/execute- Execute search and return RAG analysis resultsGET /search/session- Create new search sessionGET /search/session-status- Get current session status
GET /health- Service health checkGET /debug/*- Debug and monitoring endpointsGET /history/*- History record management endpoints
- Python 3.12+
- MongoDB database and connection string
- Google Cloud Platform account and service keys
- Docker (optional, for containerized deployment)
If you want to try the backend of your own, please replace the environment variables to the variables of your own project.
# Clone repository
git clone https://github.com/Towe-el/backend.git
cd backend
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
# Install dependencies
pip install -r requirements.txtCreate a .env file or set the following environment variables:
# Google Cloud configuration
GOOGLE_CLOUD_PROJECT=project_id
GOOGLE_APPLICATION_CREDENTIALS=path_to_`toweel-cred.json`
# MongoDB configuration
MONGODB_URI=MongoDB_connection_string
MONGODB_DATABASE=GoEmotion
MONGODB_COLLECTION=vectorizedText
# Service configuration
PORT=8080uvicorn app.main:app --host 0.0.0.0 --port 8080 --reload# Build and start
docker-compose up --build
# Run in background
docker-compose up -dVisit http://localhost:8080/health to check service status:
{
"status": "healthy",
"timestamp": "2024-01-01T00:00:00.000Z",
"services": {
"database": "connected",
"vertex_ai": "connected"
}
}- Create project in Google Cloud Console
- Enable Vertex AI API
- Create service account and download key file
- Place key file as
toweel-cred.json
- Database:
GoEmotion - Collection:
vectorizedText - Requires vector search capability
Default allowed domains:
https://toweel-frontend.web.app(production environment)https://toweel-frontend.firebaseapp.com(Firebase hosting)
- Layered Architecture: API -> Services -> Database
- Async First: Use async/await for improved performance
- Error Handling: Comprehensive exception handling and retry mechanisms
- Type Annotations: Use Pydantic for data validation
# Run tests
pytest app/tests/
# Code coverage
pytest --cov=app app/tests/- Use
/debugendpoints for system debugging - Check log output for service status
- Use
/search/session-statusto monitor session state
- Use environment variables to manage sensitive information
- Configure MongoDB replica sets for high availability
- Set appropriate resource limits and health checks
- Enable HTTPS and security headers
- Configure log collection and monitoring
- Health check endpoint:
/health - Service status monitoring integration
- Structured log output
- Error tracking and reporting
This project is licensed under an open source license. See LICENSE file for details.
Note: This is a Hackathon demo project, intended only for demonstrating technical concepts and prototype validation.