All of us agree to some extent that by now we not solely have heard about ChatGPT but additionally received a hands-on expertise with it and we Cherished it! For individuals who haven’t received an opportunity to get in contact with ChatGPT here’s a temporary introduction: ChatGPT is a big language mannequin skilled by OpenAI, and it was designed to help customers by producing human-like textual content based mostly on the given immediate. It might assist with a variety of duties, similar to answering questions, offering info, and interesting in conversations on quite a lot of subjects.
On prime of that ChatGPT has left us dumbstruck by producing unimaginable solutions to virtually something, you throw a query and it’ll give you the proper reply that we have been discovering onerous to write down.
At Pragnakalp we consider in making issues user-friendly, since ChatGPT is one thing that can ease our life by leaps and bounds, we considered making it accessible at our fingertip. So we brainstormed and got here up with the thought of utilizing ChatGPT on WhatsApp!!
Sure, you learn it proper “ChatGPT on WhatsApp!” or what if we are saying ChatGPT on any of your preferable platforms?
This weblog describes how one can combine the WhatsApp Enterprise API, hosted by Meta, and create a python utility based mostly on the Flask net framework that may obtain consumer WhatsApp messages and make the most of ChatGPT to answer these messages intimately.
Step 1: Combine WhatsApp Enterprise API
To automate the messages with the Flask utility, we should combine the WhatsApp Enterprise API. For that comply with our weblog WhatsApp Enterprise API Setup to ship and obtain messages utilizing a check telephone quantity. Please guarantee that you’ve adopted the weblog’s directions earlier than continuing.
Step 2: ChatGPT API
We’re going to make use of ChatGPT API to answer customers’ messages. Detailed directions for organising and utilizing ChatGPT are supplied on this part.
The ChatGPT API affords a straightforward strategy to embrace technologically superior language understanding in your net providers. Since there are at present no official API endpoints obtainable, the ChatGPT neighborhood has developed quite a lot of easy options that you should use.
Stipulations
Earlier than we start using the unofficial ChatGPT API, please comply with the steps listed beneath to configure the code.
1. Clone this Git repository.
git clone https://github.com/mmabrouk/chatgpt-wrapper
2. To make use of this API, please just remember to have put in setuptools
pip set up setuptools
3. Set up dependencies by working the beneath command.
pip set up git+https://github.com/mmabrouk/chatgpt-wrapper
4. Putting in a browser in Playwright is required for launching the applying. By default, the script will use Firefox.
playwright set up firefox
After working the above command it’ll give a message as proven within the beneath picture, if you’re putting in playwright for the primary time, it’ll ask you to run playwright set up command one time solely.
5. After set up is completed, you possibly can run this system in Set up mode by working beneath command
chatgpt set up
It’ll open a brand new browser window as proven within the beneath picture, and ask for login or join chat.openai.com.
Log in and cease the working program and restart it.
Now you should use it with the shell command chatgpt <your immediate> with out “Set up” as proven within the beneath picture
Word: Nonetheless after a while, if you’re not actively utilizing ChatGPT it’ll expire your session mechanically and it’s essential to log in once more with chatgpt set up
Create an occasion of the category, use the ask technique to ship a message to OpenAI, after which use the response to work together with ChatGPT through the ChatGPT class as an API.
from chatgpt_wrapper import ChatGPTimport time
immediate = “what’s coronavirus? clarify me in 2 3 strains”bot = ChatGPT()response = bot.ask(immediate)print(“Immediate: “,immediate)print(“Response: “,response)
That is the response we received from chatGPT API.
Step 3: Combine ChatGPT API with Flask Software
After our ChatGPT API received put in efficiently, it’s time to combine it with the flask utility.
Now we have to modify the flask app that we’ve created in Step 1. Substitute your current code with the beneath code to get the consumer message’s response from ChatGPT.0][‘value’][‘contacts’][0][‘wa_id’] send_msg(response,receiver_number) besides: go return ‘200 OK HTTPS.’ if __name__ == “__main__”: app.run(debug=True)
from flask import Flask, requestimport requestsfrom chatgpt_wrapper import ChatGPT
app = Flask(__name__)
def send_msg(msg,receiver_number):
headers = {‘Authorization’: ‘Bearer VERIFICATION_TOKEN’,}json_data = {‘messaging_product’: ‘whatsapp’,’to’: receiver_number,’sort’: ‘textual content’,”textual content”: {“physique”: msg}}response = requests.publish(‘ headers=headers, json=json_data)print(response.textual content)
@app.route(‘/receive_msg’, strategies=[‘POST’,’GET’])def webhook():res = request.get_json()print(res)attempt:if res[‘entry’][0][‘changes’][0][‘value’][‘messages’][0][‘id’]:chat_gpt_input=res[‘entry’][0][‘changes’][0][‘value’][‘messages’][0][‘text’][‘body’]bot = ChatGPT()response = bot.ask(chat_gpt_input) print(“ChatGPT Response=>”,response)receiver_number=res[‘entry’][0][‘changes’][0][‘value’][‘contacts’][0][‘wa_id’]send_msg(response,receiver_number)besides:passreturn ‘200 OK HTTPS.’
if __name__ == “__main__”:app.run(debug=True)
Word:
Run the flask utility within the terminal: python SCRIPT_NAME.pyRun the ngrok on terminal: ngrok http 5000
Step 4: Check Chatbot
Now come again to the “Getting Began” web page as proven within the beneath picture and click on on the “Ship Message” button.
For all occasions, together with message ship, message supply, and message learn, you’ll obtain a response in your Flask app on the receive_msg endpoint. The ChatGPT response could be checked on the server terminal as nicely.
Right here is the ChatGPT response on our server.
You may also examine the conversion with ChatGPT on WhatsApp
We hope that you’ve efficiently built-in the ChatGPT in WhatsApp and having enjoyable in utilizing it.