How To Connect OpenAI To WhatsApp (Twilio & Python Tutorial)
1.73k views2120 WordsCopy TextShare
Lena Shakurova | AI, chatbots and voice agents
š Follow me on LinkedIn for more tips on GenAI & Conversational AI: https://www.linkedin.com/in/len...
Video Transcript:
hi everyone welcome back to this channnel in today's video I'm going to show you how to connect your business WhatsApp phone number with open AI there are actually different ways how to connect an AI system to Whatsapp many of those are no code but sometimes we developers don't want to depend on third party provider and we want to set up our own WhatsApp connections we can actually do that using python and a tool called twio however it's not that simple video to Whatsapp connection is not super trivial and I couldn't find a nice video online explaining the setup process step by step so I thought I'll create my own one so if you want to build an openi system connected to Whatsapp using python then this video will be super helpful for you I prepared a GitHub repository written fully in Python that we will use to make this process super simple and fast I'll show you how to properly set up tum how to get your open AI IP key and how to spin up this GitHub repository so that your WhatsApp AI assistant is up and running all right let's get started shall we first you'll have to open the GitHub repository that will share with you in the comments below this REO has all the python code that you would need and it's built using the following text tag we'll use Docker fast IP toio to connect WhatsApp open AI for using the llm model and radi to store conversations in the memory what you need to do is you need to clone the repository using this link which I already did and once you do that you will have the following setup let's have a look at the folder structure we have a Docker file here Docker compose also in the main folder and the requirements file then we have the app folder that has a main. py file which is the main file that connects everything together and it calls other modules like cookies utils that talks to radius and stores the cookies gets the cookies and clears the cookies then we have logger U just for the logs we have open AI utils that has the code to run open AI models as you will notice you can also connect it to other models that you like we are using a library called light llm that is model agnostic which is very convenient you just need to provide your respective API keys and the model name and then you can use it open a utils file also has a function called summarized conversation which is going to take the last 70 turns of the conversation and write a short summary using one of the cheapest and fastest open a models and then we'll pass this to our prompt for some context so that our chatbot is better aware of what is happening without having too long of a conversation history which impacts the speed another file we have here is prompts dopy this one has summary prompt which will summarize the conversation and the system prompt which is the main prompt in this tutorial we are going to be creating a brainstorming assistant that's going to act as a brainstorming body you can ask it any kind of question and it will ask you followup questions and offer you some suggestions we are not going to focus on the system prompt here in this video because the goal is to learn how to connect to Whatsapp using 3o all right and the last file is a radius file which opens up the radius connection now that we know the folder structure let's set everything up the first thing you'll need to do is to make make sure that you already have Docker and radi installed on your machine I already have that so I will skip that step the next thing you need to do is to copy n. example file and paste all your credentials here we will need twer credentials open AI credentials and R credentials let's start with open AI to get your openi key you need to go to platform.
open. com do settings. organization and I keys I already have some keys but if you don't have any Keys then you'll probably have to connect your bank account to open a playround and then click create new key add a name let's call it videoo test key select the project I'll select default project and click create secret key afterwards you have to copy this key immediately you will not be shown it afterwards so you need to save it uh in a good place and you go to yourn file uh which will look like this and you paste your open a key like this without any quot then we'll need to set up our twio account we will start with using twio sandbox first first we need to copy our account seat and paste it to our environment file then we need to copy our token and paste it here and then we need to get really WhatsApp number which is what we will need sandbox for to set up sandbox you'll need to go to messaging and click try it out send a WhatsApp message T sandbox setup is rather simple you need to go to your WhatsApp click on scan QR code and scan the QR code code then the too chat will open automatically and you will be asked to paste a code I send a code and twio sandbox says you are all set that's perfect now we have a phone number from twio which looks like this and we will need to paste it here just like this that's it for the T sendbox setup now you only need to paste information about your radi host port and password and I'm not going to spend too much time on this you can find documentation online on how to do that it is rather straightforward now now let's quickly test that the send boox Works let's click Send template message and you can see on your WhatsApp phone number that the message was received so the send box is working that's perfect all right now let's try to spin up this repository what we would need to do is copy this command Docker compose up and paste it here and wait until it's up and running okay it seems that it's working now let's verify it using Postman here I have the URL which you can find in your app.