Build Whatsapp Chatbot With Flask and Open Source LLM - LLAMA3? - by Mayankchugh Jobathk - Medium
Build Whatsapp Chatbot With Flask and Open Source LLM - LLAMA3? - by Mayankchugh Jobathk - Medium
Open in app
38
Search
Get unlimited access to the best of Medium for less than $1/week. Become a member
Build a WhatsApp chatbot with Flask and open-source LLM (LLAMA3🤖). In this
tutorial, we will create an intelligent WhatsApp chatbot using Flask, OpenAI, and
open-source models from AnyScale, Twilio, and Ngrok. Follow our step-by-step
guide to implement these technologies and discover how they work together to
build an interactive and clear conversational experience, making technology
accessible and engaging.
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 1/23
2024/10/27 上午11:56 Build Whatsapp Chatbot with Flask and Open Source LLM — LLAMA3🤖 | by Mayankchugh Jobathk | Medium
We will use Ngrok and Flask as a server, Twilio as a WhatsApp agent, an open-source
model using Anyscale API to generate the answer, and Flask app as an API for the
request/response model. 🛠 #Ngrok #Flask #Twilio #AnyscaleAPI #OpenSource #API
If you would like to give us feedback, please contact me. What follows here is a
technical overview of how we made this prototype.
1. 🆓 Create free account in Anyscale for Open Source LLM, Twilio and Ngrok
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 2/23
2024/10/27 上午11:56 Build Whatsapp Chatbot with Flask and Open Source LLM — LLAMA3🤖 | by Mayankchugh Jobathk | Medium
Next, visit the Twilio website and sign up for a free account.
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 3/23
2024/10/27 上午11:56 Build Whatsapp Chatbot with Flask and Open Source LLM — LLAMA3🤖 | by Mayankchugh Jobathk | Medium
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 4/23
2024/10/27 上午11:56 Build Whatsapp Chatbot with Flask and Open Source LLM — LLAMA3🤖 | by Mayankchugh Jobathk | Medium
After creating your Twilio account, go to the Twilio Console. In the sidebar, locate
and click on. send a WhatsApp message . This connection enables Twilio to send and
receive messages on your behalf via the WhatsApp platform. 📱
Lastly, create a Ngrok account. Ngrok is a free tool that allows us to tunnel from a
public URL to our local application. ✨
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 5/23
2024/10/27 上午11:56 Build Whatsapp Chatbot with Flask and Open Source LLM — LLAMA3🤖 | by Mayankchugh Jobathk | Medium
This step ensures consistent package versions and simplifies environment setup for
collaborators.
🔐 Ngrok Authentication
Add your Ngrok authentication token to your local environment. Execute the
following command:
This command links your local Ngrok client to your Ngrok account, enabling
features like tunnelling your local server to a public URL.
Prioritize security by storing sensitive information, such as your AnuScale API key,
in a dedicated environment file ( .env ). Create a new file named .env in your
project directory and add the following line:
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 7/23
2024/10/27 上午11:56 Build Whatsapp Chatbot with Flask and Open Source LLM — LLAMA3🤖 | by Mayankchugh Jobathk | Medium
Add your_secrets_key secrets and actual API key obtained during your AnyScale
(https://www.anyscale.com/) account setup. Using a dedicated environment file
enhances security and maintains a separation between configuration details and
your codebase.
3. 🤖💬 Implementing the Flask App for a WhatsApp Chatbot with open source
model and Twilio
In this section, we’ll look at the implementation details of the Flask application
responsible for handling incoming WhatsApp messages, generating responses using
Open Source Model llama3 using AnyScale, and sending replies with Twilio.
import openai
from openai import OpenAI
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
from twilio.rest import Client
from dotenv import load_dotenv
import os
import sys
load_dotenv()
app = Flask(__name__)
The code imports necessary modules, including Flask for building the web
application, Twilio for handling messages, and Open Source LLM Model for utilizing
the llama3 model from AnyScale. The load_dotenv() function loads environment
variables such as Anyscale API Key, from a file named .env .
anyscale_api_key=os.getenv("ANY_SCALE_API_KEY")
anyscale_base_url = os.getenv("ANY_SCALE_API_BASE")
MODEL_NAME = 'meta-llama/Meta-Llama-3-8B-Instruct'
account_sid = os.getenv("TWILIO_ACCOUNT_SID")
auth_token = os.getenv("TWILIO_AUTH_TOKEN")
This line retrieves the AnyScale API key from environment variables. Storing
sensitive information like API keys in environment variables enhances security.
💭 📬 Defining the Flask Route for Incoming Messages and Generate Answers
with LLAMA3 using AnyScale
clientopenAi = OpenAI(
base_url="https://api.endpoints.anyscale.com/v1",
api_key=anyscale_api_key
)
try:
incoming_message = request.form['Body']
print(f"incoming_message:: {incoming_message}")
if incoming_message.lower() == 'restart':
print('Restarting app...')
# You can replace the command with the appropriate command to resta
os.execv(sys.executable, ['python'] + sys.argv)
else:
# Add the new message to the history list
history.append(f"user query: {incoming_message}")
# Remove the oldest message if the history list size is greater tha
if len(history) > 10:
history.pop(0)
# Pass the history list to LLM Model with message indicating it's p
prompt = incoming_message+"\nThese messages below are my previous m
print(prompt)
response = clientopenAi.chat.completions.create(
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 9/23
2024/10/27 上午11:56 Build Whatsapp Chatbot with Flask and Open Source LLM — LLAMA3🤖 | by Mayankchugh Jobathk | Medium
model=MODEL_NAME,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
]
).choices[0].message.content
print(response)
print("HISTORY:\n",history)
# Remove the oldest message if the history list size is greater tha
if len(history) > 10:
history.pop(0)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)
This section defines a route /sms that Twilio will use to forward incoming messages.
The function sms extracts the user's question, generates a response using the Open
Source Model llama3, creates a Twilio response, and sends it back.
python app.py
Executing this command initiates our Flask application, making it accessible locally
on port 5000.
🌐 Set Up Ngrok
Ngrok allows you to expose your local server to the internet. By running this
command, you create a secure tunnel that links Ngrok’s public URL to your Flask
app running on port 5000.
We will use this URL as the webhook for services like Twilio to forward incoming
messages to our local Flask server.
Lastly, Copy this public URL, and paste it with the /sms route into the Twilio
Console. Enter this URL into Twilio, and when you open WhatsApp, you can finally
start the conversation.
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 11/23
2024/10/27 上午11:56 Build Whatsapp Chatbot with Flask and Open Source LLM — LLAMA3🤖 | by Mayankchugh Jobathk | Medium
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 12/23
2024/10/27 上午11:56 Build Whatsapp Chatbot with Flask and Open Source LLM — LLAMA3🤖 | by Mayankchugh Jobathk | Medium
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 13/23
2024/10/27 上午11:56 Build Whatsapp Chatbot with Flask and Open Source LLM — LLAMA3🤖 | by Mayankchugh Jobathk | Medium
Once you’ve completed the integration of your Flask app with Twilio and set up the
necessary configurations, you can start a conversation on WhatsApp with your
Twilio-enabled Twilio Sandbox number.
As soon as you send a message, you’ll experience the seamless interaction with your
Flask app. Twilio will forward the message to your local server, where your chatbot
powered by AnyScale will generate responses.
💭 Notes 💬
lsof -i:5000
or
kill -9 :5000
GitHub - mayankchugh-learning/WhatsApp-Bot-using-GenAI:
Testing-Bot-using-GenAI - Flask==2.1.1…
Testing-Bot-using-GenAI - Flask==2.1.1 twilio==6.50.1
openai==0.28.0 python-dotenv …
github.com
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 14/23
2024/10/27 上午11:56 Build Whatsapp Chatbot with Flask and Open Source LLM — LLAMA3🤖 | by Mayankchugh Jobathk | Medium
https://www.linkedin.com/in/mchugh77
🎉 Conclusion
In this tutorial, we walked through the steps of building a WhatsApp chatbot using
Flask, Open Source model llama3 using AnyScale, Twilio, and Ngrok. Integrating
these powerful technologies creates a dynamic, intelligent conversational agent that
responds to real-time user queries. Following these steps, you’ve built a functional
WhatsApp chatbot and gained insights into integrating various technologies to
create an intelligent and responsive conversational system. 🚀🤖💬
Follow
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 15/23
2024/10/27 上午11:56 Build Whatsapp Chatbot with Flask and Open Source LLM — LLAMA3🤖 | by Mayankchugh Jobathk | Medium
Mayankchugh Jobathk
Jun 23 1
Mayankchugh Jobathk
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 16/23
2024/10/27 上午11:56 Build Whatsapp Chatbot with Flask and Open Source LLM — LLAMA3🤖 | by Mayankchugh Jobathk | Medium
Jul 9 1
Mayankchugh Jobathk
Jul 23 1
Mayankchugh Jobathk
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 17/23
2024/10/27 上午11:56 Build Whatsapp Chatbot with Flask and Open Source LLM — LLAMA3🤖 | by Mayankchugh Jobathk | Medium
Jul 18
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 18/23
2024/10/27 上午11:56 Build Whatsapp Chatbot with Flask and Open Source LLM — LLAMA3🤖 | by Mayankchugh Jobathk | Medium
Building Your Own Generative Search Engine for Local Files Using Open-
Source Models 🧐📂:Part-2
Hey fellow AI nerds! 🤓👋 Thanks for showing all the love on the first article — it was as
heartwarming as finding out your code works…
6d ago 423 2
Part 2 of the Series: Building a Smart WhatsApp Real Estate Chatbot: AI, Twilio, BERT, and
LangChain Integration
Sep 25
Lists
AI Regulation
6 stories · 597 saves
What is ChatGPT?
9 stories · 455 saves
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 20/23
2024/10/27 上午11:56 Build Whatsapp Chatbot with Flask and Open Source LLM — LLAMA3🤖 | by Mayankchugh Jobathk | Medium
Alisha Saboowala
Jul 25 1
C. C. Python Programming
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 21/23
2024/10/27 上午11:56 Build Whatsapp Chatbot with Flask and Open Source LLM — LLAMA3🤖 | by Mayankchugh Jobathk | Medium
4d ago 58 1
Devvrat Rana
Jun 2 66
Oct 14 904 9
https://medium.com/@mayankchugh.jobathk/build-whatsapp-chatbot-with-flask-and-open-source-llm-llama3-0284619a96d4 23/23