simonsanvil / openai-whatsapp-chatbot

A chatbot app that uses OpenAI's GPT and DALL-E to reply to incoming messages from WhatsApp and generate images
MIT License
179 stars 50 forks source link
chatbot chatgpt codex dall-e gpt-3 openai-api twilio whatsapp

WhatApp OpenAI-API Chatbot

Build and deploy Python app to Azure Web App - openai-chatbot

Open In Colab

A chatbot that uses OpenAI's API to reply to incoming text and voice messages from WhatsApp with their GPT3-based language models (Davinci, Ada, Babbage, ...) and to generate images with DALL-E 2.

Requires a valid key to OpenAI's API.

Installation

git clone https://github.com/simonsanvil/openai-whatsapp-chatbot
pip install -r requirements.txt

Requirements

Setup and Configuration

You need to set the OpenAI API key as an environmental variables or add it to a .env file in the working directory where the app will be running:

export OPENAI_API_KEY=[YOUR OPENAI API ACCESS KEY]

To run the Whatsapp chatbot:

The image below shows which boxes you need to fill in when configuring your Twillio Sandbox for Whatsapp:

Additional config:

export MAX_TOKENS=[NUMBER OF MAX TOKENS IN EACH REPLY]
export CONVERSATION_EXPIRES_MINS=[N MINUTES UNTIL A CONVERSATION IS ERASED FROM MEMORY]
export ALLOWED_PHONE_NUMBERS=[+1234567890,+1987654321] # Default is any number
export START_TEMPLATE=[PATH TO A FILE WITH A TEMPLATE FOR THE START OF A CONVERSATION] #data/start_template.txt
export ASSEMBLYAI_API_KEY=[YOUR ASSEMBLY-AI API KEY]

The ASSEMBLYAI_API_KEY environmental variable is to use AssemblyAI's API to parse and transcribe the audio of incoming voice messages so that the agent can reply to them. If you don't need or want this, you can ignore that variable.

Running the app

Run from the command line:

# (Use --help to see all the options):
python3 -m app.whatsapp

Run with Docker:

Alternatively the docker container will automatically install all the requirements and run the whatsapp application.

# building the image
docker build -t openai-ws-chatbot .

#running the container
#It is expected that you have all the required environmental variables in a .env file
docker run -p 5000:5000 openai-ws-chatbot --env_file=.env

Usage

After following the instructions in the Twilio Sandbox for Whatsapp Tutorial you should be able to join your sandbox and start chatting with the agent inmediately