Skip to main content

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

1

Get OpenAI API Key

Visit OpenAI Platform and create an API key.
2

Register credentials in Evolution API

Store your API key securely in Evolution API.
3

Use credentials when creating bots

Reference the credential ID when configuring OpenAI bots.
string
required
Friendly name to identify this credential set
string
required
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.
string
required
ID of the OpenAI credentials to use by default
boolean
Enable automatic speech-to-text for voice messages. Default: false
string
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

string
required
Must be “chatCompletion” for this bot type
string
required
ID of the OpenAI credentials to use
string
required
OpenAI model: “gpt-4”, “gpt-4-turbo-preview”, “gpt-3.5-turbo”, etc.
number
required
Maximum tokens for the response (controls response length and cost)
array
System prompts that define the AI’s behavior and personality
array
Example assistant responses for few-shot learning
array
Example user messages paired with assistant messages
string
Webhook URL for function calling responses

Create Assistant Bot

Assistant Parameters

string
required
Must be “assistant” for this bot type
string
required
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:
Operators: equals, contains, startsWith, endsWith, regex

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:
When enabled, voice messages sent to the bot are automatically:
  1. Transcribed using OpenAI Whisper
  2. Processed as text by the bot
  3. Responded to normally

Use Cases

Deploy AI support agents:
  • Answer common questions instantly
  • Provide product information
  • Troubleshoot issues step-by-step
  • Escalate to humans when needed
Automate sales conversations:
  • Qualify leads with intelligent questions
  • Provide product recommendations
  • Schedule demos and calls
  • Capture contact information
Help users create content:
  • Generate social media posts
  • Write email responses
  • Create marketing copy
  • Brainstorm ideas
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.
Monitor your OpenAI API usage carefully. Set reasonable maxTokens limits to control costs.
  • 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

  • Verify OPENAI_ENABLED=true in 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
  • Use GPT-3.5-turbo instead of GPT-4 for faster responses
  • Reduce maxTokens for quicker completions
  • Check your internet connection to OpenAI
  • Monitor OpenAI API latency
  • Reduce maxTokens to 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
  • Increase expire time for longer sessions
  • Set keepOpen: true for persistent conversations
  • Verify conversation history is being sent
  • Check maxTokens isn’t too low for context
  • Dify - LLM orchestration platform
  • Flowise - Visual LLM workflow builder
  • EvoAI - Evolution AI platform