OpenAI Integration
OpenAI provides powerful language models including GPT-4, GPT-3.5, and assistants with advanced capabilities. You can integrate OpenAI with Evolution API to deploy AI-powered conversational experiences on WhatsApp.What is OpenAI?
OpenAI offers:- GPT-4 and GPT-3.5-turbo models for chat completions
- Assistants API with code interpreter and retrieval
- Function calling capabilities
- Vision and image understanding
- Text-to-speech and speech-to-text
- Fine-tuning for custom models
Enable OpenAI Integration
Add this environment variable to your.env file:
OpenAI Credentials Management
Before creating bots, you need to register your OpenAI API credentials.Create OpenAI Credentials
Get OpenAI API Key
Visit OpenAI Platform and create an API key.
Friendly name to identify this credential set
Your OpenAI API key (starts with “sk-”)
Fetch OpenAI Credentials
Retrieve all registered OpenAI credentials:Delete OpenAI Credentials
Configuration Settings
Create Default Settings
Configure default behavior settings for your instance.ID of the OpenAI credentials to use by default
Enable automatic speech-to-text for voice messages. Default: false
Fallback OpenAI bot ID to use when primary bot fails
OpenAI Bot Types
Evolution API supports two OpenAI bot types:Chat Completion
Use GPT models directly with custom prompts:- GPT-4, GPT-4-turbo
- GPT-3.5-turbo
- Custom system messages
- Conversation history
- Function calling
Assistant
Use OpenAI Assistants API:- Pre-configured assistants
- Code interpreter
- Knowledge retrieval
- File support
- Advanced tools
Create Chat Completion Bot
Chat Completion Parameters
Must be “chatCompletion” for this bot type
ID of the OpenAI credentials to use
OpenAI model: “gpt-4”, “gpt-4-turbo-preview”, “gpt-3.5-turbo”, etc.
Maximum tokens for the response (controls response length and cost)
System prompts that define the AI’s behavior and personality
Example assistant responses for few-shot learning
Example user messages paired with assistant messages
Webhook URL for function calling responses
Create Assistant Bot
Assistant Parameters
Must be “assistant” for this bot type
OpenAI Assistant ID (starts with “asst_”). Create assistants in OpenAI Platform.
Available Models
Retrieve all available OpenAI models for your credentials:Trigger Types
All Messages
Respond to every message:Keyword Trigger
Trigger based on specific keywords:Advanced Trigger
Use regex patterns:Update OpenAI Bot
Delete OpenAI Bot
Fetch OpenAI Bots
Session Management
Change Session Status
Fetch Sessions
Speech to Text
Enable automatic transcription of voice messages:- Transcribed using OpenAI Whisper
- Processed as text by the bot
- Responded to normally
Use Cases
24/7 Customer Support
24/7 Customer Support
Deploy AI support agents:
- Answer common questions instantly
- Provide product information
- Troubleshoot issues step-by-step
- Escalate to humans when needed
Sales and Lead Qualification
Sales and Lead Qualification
Automate sales conversations:
- Qualify leads with intelligent questions
- Provide product recommendations
- Schedule demos and calls
- Capture contact information
Content Creation Assistant
Content Creation Assistant
Help users create content:
- Generate social media posts
- Write email responses
- Create marketing copy
- Brainstorm ideas
Personal Assistant
Personal Assistant
Daily helper bot:
- Answer general questions
- Provide recommendations
- Set reminders and tasks
- Get quick information
Best Practices
Use GPT-3.5-turbo for cost-effective, fast responses. Reserve GPT-4 for complex reasoning tasks.
- Craft clear system messages: Define personality, tone, and constraints
- Keep responses concise: WhatsApp users prefer short messages
- Use few-shot examples: Provide example conversations to guide behavior
- Set appropriate token limits: Balance quality with cost
- Handle voice messages: Enable speech-to-text for better UX
- Monitor conversations: Review bot interactions regularly
- Implement safety: Use OpenAI’s moderation API for sensitive use cases
Troubleshooting
Bot not responding
Bot not responding
- Verify
OPENAI_ENABLED=truein environment - Check bot is enabled and credentials are valid
- Ensure OpenAI API key has sufficient credits
- Verify trigger conditions match messages
- Check OpenAI API status
Slow responses
Slow responses
- Use GPT-3.5-turbo instead of GPT-4 for faster responses
- Reduce
maxTokensfor quicker completions - Check your internet connection to OpenAI
- Monitor OpenAI API latency
High costs
High costs
- Reduce
maxTokensto limit response length - Use GPT-3.5-turbo instead of GPT-4
- Implement stricter triggers to reduce activations
- Set shorter session expiration times
- Monitor usage in OpenAI dashboard
Context not maintained
Context not maintained
- Increase
expiretime for longer sessions - Set
keepOpen: truefor persistent conversations - Verify conversation history is being sent
- Check maxTokens isn’t too low for context