The AI-powered support and assistance service for the PS3Stack Voting System. This service provides intelligent chatbot functionality, help desk operations, and user assistance across all system components.
The Support Service provides:
- AI Chatbot - Intelligent conversational assistance using OpenAI
- User Help - Context-aware support for all system users
- Issue Resolution - Automated problem-solving and guidance
- Documentation Access - Dynamic help content and FAQs
- Multilingual Support - Multi-language assistance capabilities
Client → API Gateway → Support Service (Port 8083)
↓
OpenAI Integration
↓
Context Processing
↓
Response Generation
↓
User Assistance
- Ballerina Swan Lake Update 8 or later
- OpenAI API Key (Required for chatbot functionality)
- Network access for OpenAI API calls
- Authentication service integration
The Support Service requires an OpenAI API key to function properly. Without this key, the chatbot will not work.
- Visit OpenAI Platform: Go to https://platform.openai.com
- Create Account: Sign up for an OpenAI account if you don't have one
- Access API Keys: Navigate to the API Keys section in your dashboard
- Generate Key: Create a new API key for your project
- Secure Storage: Store the key securely and never commit it to version control
-
Copy Configuration Template:
cp Config.toml.example Config.toml
-
Add Your OpenAI API Key:
HTTP_PORT = 8083 # OpenAI Configuration (REQUIRED) OPENAI_API_KEY = "sk-your-actual-openai-api-key-here" OPENAI_MODEL = "gpt-3.5-turbo" OPENAI_MAX_TOKENS = 500 OPENAI_TEMPERATURE = 0.7
-
Alternative: Environment Variable:
export OPENAI_API_KEY="sk-your-actual-openai-api-key-here"
# Ensure your OpenAI API key is configured
# Start the Support Service
bal run
# Check service health
curl http://localhost:8083/healthPOST /support/chat
Authorization: Bearer <jwt_token>
Content-Type: application/json
{
"message": "How do I check in a voter at the polling station?",
"context": {
"user_role": "polling_staff",
"current_page": "voter_checkin",
"election_id": 1
}
}Response:
{
"reply": "To check in a voter at the polling station:\n\n1. Verify the voter's identity using their ID\n2. Look up the voter in the system using their voter ID\n3. Confirm they are registered for this election\n4. Click 'Check In Voter' and scan their ID or enter voter ID manually\n5. Issue them a ballot once check-in is confirmed\n6. The system will prevent double voting automatically\n\nIs there a specific issue you're encountering with voter check-in?",
"context": {
"response_type": "instruction",
"related_topics": ["voter_management", "polling_operations"],
"confidence": 0.95
},
"timestamp": "2024-12-17T10:30:00Z"
}GET /support/chat/history
Authorization: Bearer <jwt_token>Response:
{
"conversations": [
{
"id": "conv-123",
"started_at": "2024-12-17T10:00:00Z",
"messages": [
{
"type": "user",
"message": "How do I add a new candidate?",
"timestamp": "2024-12-17T10:00:00Z"
},
{
"type": "assistant",
"message": "To add a new candidate, navigate to the Elections page...",
"timestamp": "2024-12-17T10:00:15Z"
}
]
}
]
}GET /support/help/{topic}
Authorization: Bearer <jwt_token>Examples:
/support/help/voting- Voting process help/support/help/candidates- Candidate management help/support/help/elections- Election administration help
Response:
{
"topic": "voting",
"title": "Voting Process Guide",
"content": "Step-by-step guide for the voting process...",
"related_topics": ["voter_checkin", "ballot_creation"],
"last_updated": "2024-12-17T08:00:00Z"
}GET /support/search?q=voter+registration
Authorization: Bearer <jwt_token>GET /healthResponse:
{
"service": "PS3Stack Support Service",
"version": "1.0.0",
"status": "healthy",
"timestamp": 1693123456,
"openai_status": "connected",
"api_key_configured": true,
"chat_sessions_active": 5
}The chatbot uses OpenAI's GPT models to provide intelligent assistance:
- Model: GPT-3.5-turbo (configurable)
- Context Awareness: Understands user roles and current system context
- Response Quality: High-quality, relevant responses
- Multilingual: Supports multiple languages
The chatbot understands:
- User Role - Tailors responses based on Admin, Observer, Field Staff, or Polling Staff roles
- Current Page - Provides relevant help based on what the user is viewing
- Election Context - Considers current election information
- System State - Aware of system status and current operations
- Election creation and management
- User role administration
- System configuration
- Data management and reports
- Security settings
- Candidate management
- Election monitoring
- Voter oversight
- Result observation
- Reporting processes
- Voter registration
- Field operations
- Equipment setup
- Data collection
- Area management
- Voter check-in procedures
- Ballot issuance
- Polling station operations
- Incident reporting
- Vote processing
# Server Configuration
HTTP_PORT = 8083
# OpenAI Configuration (REQUIRED)
OPENAI_API_KEY = "sk-your-openai-api-key-here"
OPENAI_MODEL = "gpt-3.5-turbo"
OPENAI_MAX_TOKENS = 500
OPENAI_TEMPERATURE = 0.7
OPENAI_TIMEOUT = 30
# Chat Configuration
MAX_CHAT_HISTORY = 50
CONVERSATION_TIMEOUT_MINUTES = 30
ENABLE_CHAT_LOGGING = true
# Content Configuration
HELP_CONTENT_CACHE_TTL = 3600
ENABLE_SEARCH_INDEXING = true
# Security Configuration
RATE_LIMIT_REQUESTS_PER_MINUTE = 60
ENABLE_CONTENT_FILTERING = true# Essential configuration
export OPENAI_API_KEY="sk-your-openai-api-key-here"
export HTTP_PORT=8083
# Optional configuration
export OPENAI_MODEL="gpt-3.5-turbo"
export OPENAI_MAX_TOKENS=500
export OPENAI_TEMPERATURE=0.7# Test Basic Chat
curl -X POST http://localhost:8083/support/chat \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"message": "How do I create a new election?",
"context": {
"user_role": "admin",
"current_page": "elections"
}
}'
# Test Role-Specific Help
curl -X POST http://localhost:8083/support/chat \
-H "Authorization: Bearer YOUR_POLLING_STAFF_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"message": "What should I do if a voter lost their ID?",
"context": {
"user_role": "polling_staff",
"current_page": "voter_checkin"
}
}'
# Test Help Content
curl -X GET http://localhost:8083/support/help/voting \
-H "Authorization: Bearer YOUR_TOKEN"
# Search Help
curl -X GET "http://localhost:8083/support/search?q=candidate+registration" \
-H "Authorization: Bearer YOUR_TOKEN"
# Health Check
curl http://localhost:8083/health# Test OpenAI connectivity
curl -X POST http://localhost:8083/support/debug/openai-test \
-H "Authorization: Bearer YOUR_ADMIN_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"test_message": "Hello, this is a test message."
}'The service tracks:
- Total chat sessions - Number of conversations
- Messages per session - Average interaction length
- Response time - AI response performance
- User satisfaction - Based on interaction patterns
- Popular topics - Most frequently asked questions
- OpenAI API latency - Response time monitoring
- Token usage - API cost tracking
- Error rates - Failed request monitoring
- Rate limiting - Request throttling statistics
- Environment Variables - Secure key storage
- No Logging - API keys never logged
- Encryption - Encrypted key transmission
- Rotation Support - Easy key rotation
- Input Validation - Malicious input prevention
- Response Filtering - Inappropriate content blocking
- Rate Limiting - Abuse prevention
- Context Validation - Proper context enforcement
- No Personal Data - No PII sent to OpenAI
- Anonymized Logs - Privacy-preserving logging
- Session Isolation - Secure conversation boundaries
- Data Retention - Configurable chat history retention
-
OpenAI API Key Not Configured (500)
Error: OpenAI API key is not configuredSolution: Add your OpenAI API key to Config.toml or environment variables
-
OpenAI API Call Failed (503)
Error: Failed to connect to OpenAI APISolutions:
- Verify API key is valid and active
- Check internet connectivity
- Ensure OpenAI service is available
- Verify API quota/billing status
-
Rate Limit Exceeded (429)
Error: Too many requests to OpenAI APISolutions:
- Wait for rate limit reset
- Upgrade OpenAI plan for higher limits
- Implement request throttling
-
Invalid Chat Context (400)
Error: Invalid user context providedSolutions:
- Ensure user_role is valid
- Check current_page context
- Verify authentication token
# Check OpenAI configuration
curl -X GET http://localhost:8083/support/debug/config \
-H "Authorization: Bearer YOUR_ADMIN_TOKEN"
# Test OpenAI connectivity
curl -X POST http://localhost:8083/support/debug/openai-ping \
-H "Authorization: Bearer YOUR_ADMIN_TOKEN"
# View service metrics
curl -X GET http://localhost:8083/support/metrics \
-H "Authorization: Bearer YOUR_ADMIN_TOKEN"support_service/
├── Ballerina.toml # Project configuration
├── Config.toml.example # Configuration template
├── Dependencies.toml # Dependencies
├── main.bal # Main service implementation
└── modules/
├── support/ # Support service logic
├── chat/ # Chat processing
├── openai/ # OpenAI integration
├── content/ # Help content management
└── analytics/ # Usage analytics
- Define Chat Flows - Create conversation patterns
- Implement Context Handlers - Add context processing
- Create Help Content - Add documentation content
- Add API Endpoints - Define new service endpoints
- Update Documentation - Document new capabilities
// Example: Custom prompt for specific context
public function generateContextPrompt(string userRole, string currentPage) returns string {
string basePrompt = "You are a helpful assistant for the PS3Stack Voting System.";
if (userRole == "admin") {
basePrompt += " You are helping a system administrator.";
} else if (userRole == "polling_staff") {
basePrompt += " You are helping polling station staff.";
}
if (currentPage == "voter_checkin") {
basePrompt += " The user is currently on the voter check-in page.";
}
return basePrompt + " Provide clear, actionable guidance.";
}FROM ballerina/ballerina:swan-lake-latest
COPY . /home/ballerina/
WORKDIR /home/ballerina/
# Build application
RUN bal build
# Expose port
EXPOSE 8083
# Set environment variable for API key
ENV OPENAI_API_KEY=""
CMD ["bal", "run", "target/bin/support_service.jar"]# Production Config.toml
HTTP_PORT = 8083
# OpenAI Configuration - Use environment variables
OPENAI_API_KEY = "${OPENAI_API_KEY}"
OPENAI_MODEL = "gpt-3.5-turbo"
OPENAI_MAX_TOKENS = 300 # Lower for cost control
OPENAI_TEMPERATURE = 0.5 # More deterministic responses
# Rate limiting for production
RATE_LIMIT_REQUESTS_PER_MINUTE = 30
MAX_CHAT_HISTORY = 20
# Security
ENABLE_CONTENT_FILTERING = true
ENABLE_AUDIT_LOGGING = true# Production environment variables
export OPENAI_API_KEY="your-production-api-key"
export HTTP_PORT=8083
export OPENAI_MODEL="gpt-3.5-turbo"
export RATE_LIMIT_REQUESTS_PER_MINUTE=30- Token Limits - Configure max tokens per request
- Response Caching - Cache common responses
- Request Batching - Optimize API calls
- Model Selection - Use appropriate model for needs
- Track token usage per conversation
- Monitor API costs in OpenAI dashboard
- Set up usage alerts and limits
- Regular cost analysis and optimization
This project is licensed under the MIT License - see the LICENSE file for details.