Overview
The WhatsApp RAG Bot uses OpenAI’s API for:- Chat Completion: GPT models for conversational responses
- Text Embeddings: Vector embeddings for semantic search (RAG)
- Audio Transcription: Whisper model for voice message processing
Prerequisites
You need an OpenAI API key with access to:- GPT models (gpt-3.5-turbo or gpt-4)
- Text embedding models
- Whisper audio transcription
Configuration Options
Option 1: Database Configuration (Recommended)
Configure through the admin dashboard:src/Services/CredentialService.php
Credentials stored in the database are automatically encrypted using AES-256-CBC encryption.
Option 2: Environment Variables
Add these to your.env file:
.env
Configuration File
config/config.php
Available Models
Chat Models
GPT-3.5 Turbo
Model:
gpt-3.5-turboFast and cost-effective for most use cases. Good balance of performance and price.- Context: 16k tokens
- Speed: Very fast
- Cost: Low
GPT-4
Model:
gpt-4More capable and accurate responses. Better for complex reasoning.- Context: 8k tokens
- Speed: Slower
- Cost: Higher
GPT-4 Turbo
Model:
gpt-4-turbo-previewLatest GPT-4 with improved performance and larger context window.- Context: 128k tokens
- Speed: Fast
- Cost: Moderate
GPT-4o
Model:
gpt-4oOptimized version with best balance of speed and capability.- Context: 128k tokens
- Speed: Very fast
- Cost: Moderate
Embedding Models
- Ada-002 (Legacy)
- v3-small (Recommended)
- v3-large
Model:
text-embedding-ada-002The previous generation embedding model.- Dimensions: 1536
- Cost: $0.0001 per 1K tokens
- Good for most RAG use cases
Model Parameters
Temperature
Controls randomness in responses (0.0 to 2.0):- 0.0-0.3: Focused and deterministic (good for factual responses)
- 0.4-0.7: Balanced creativity and consistency (recommended)
- 0.8-1.0: More creative and varied responses
- 1.1-2.0: Very creative (may be less coherent)
Max Tokens
Maximum length of generated responses:1 token ≈ 0.75 words in English. Adjust based on your needs:
- Short answers: 150-300 tokens
- Medium answers: 300-500 tokens
- Long answers: 500-1000 tokens
Usage in Code
webhook.php
OpenAI Service Features
Chat Completion
Generate responses with context and conversation history:Text Embeddings
Generate vector embeddings for semantic search:Audio Transcription
Transcribe voice messages using Whisper:src/Services/OpenAIService.php
Audio transcription is only available in AI mode. In classic mode, users are prompted to send text messages instead.
RAG Configuration
The bot uses embeddings for semantic search in the RAG system:config/config.php
How RAG Works
Document Processing
Documents are split into chunks and converted to embeddings using your configured embedding model.
Error Handling
Insufficient Funds
The system automatically detects when your OpenAI account has insufficient credits:webhook.php
Rate Limiting
OpenAI has rate limits based on your account tier:- Free tier: Limited requests per minute
- Pay-as-you-go: Higher limits based on usage history
- Enterprise: Custom rate limits
Cost Optimization
- Model Selection
- Token Management
- Caching
Choose models based on your budget:Most Cost-Effective:Balanced:Best Quality:
Testing Your Configuration
Troubleshooting
Invalid API key error
Invalid API key error
- Verify your API key is correct
- Check that the key hasn’t been revoked
- Ensure there are no extra spaces or newlines
- Generate a new key at platform.openai.com/api-keys
Model not found error
Model not found error
- Verify your account has access to the specified model
- Check for typos in the model name
- Some models require special access (e.g., GPT-4)
- Try using
gpt-3.5-turboas a fallback
Rate limit exceeded
Rate limit exceeded
- Reduce the number of concurrent requests
- Implement request queuing
- Upgrade your OpenAI account tier
- Add retry logic with exponential backoff
Embedding dimension mismatch
Embedding dimension mismatch
If you change embedding models, you must:
- Clear the
vectorstable - Clear the
query_embedding_cachetable - Re-process all documents
text-embedding-ada-002: 1536text-embedding-3-small: 1536text-embedding-3-large: 3072
Next Steps
System Settings
Configure system prompts and bot behavior
Google Calendar
Set up appointment scheduling