Skip to content

Commit 135052a

Browse files
authored
updating StructuredLlama, Messenger and WhatsApp for Llama 3 (meta-llama#468)
2 parents 163d856 + 3f6e474 commit 135052a

File tree

11 files changed

+144
-459
lines changed

11 files changed

+144
-459
lines changed

docs/images/messenger_llama_arch.jpg

-4.34 KB
Loading

docs/images/whatsapp_llama_arch.jpg

2.18 KB
Loading

recipes/use_cases/LiveData.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"\n",
1010
"## This demo app shows:\n",
1111
"* How to use LlamaIndex, an open source library to help you build custom data augmented LLM applications\n",
12-
"* How to ask Llama 3 questions about recent live data via the [Trvily](https://tavily.com) live search API"
12+
"* How to ask Llama 3 questions about recent live data via the [Tavily](https://tavily.com) live search API"
1313
]
1414
},
1515
{
@@ -86,7 +86,7 @@
8686
"id": "f8ff812b",
8787
"metadata": {},
8888
"source": [
89-
"Next you will use the [Trvily](https://tavily.com/) search engine to augment the Llama 3's responses. To create a free trial Trvily Search API, sign in with your Google or Github account [here](https://app.tavily.com/sign-in)."
89+
"Next you will use the [Tavily](https://tavily.com/) search engine to augment the Llama 3's responses. To create a free trial Tavily Search API, sign in with your Google or Github account [here](https://app.tavily.com/sign-in)."
9090
]
9191
},
9292
{

recipes/use_cases/README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
## VideoSummary: Ask Llama2 to Summarize a YouTube Video (using [Replicate](VideoSummary.ipynb) or [OctoAI](../llama_api_providers/OctoAI_API_examples/VideoSummary.ipynb))
2-
This demo app uses Llama2 to return a text summary of a YouTube video. It shows how to retrieve the caption of a YouTube video and how to ask Llama to summarize the content in four different ways, from the simplest naive way that works for short text to more advanced methods of using LangChain's map_reduce and refine to overcome the 4096 limit of Llama's max input token size.
1+
## [VideoSummary](VideoSummary.ipynb): Ask Llama 3 to Summarize a Long YouTube Video (using Replicate or [OctoAI](../llama_api_providers/OctoAI_API_examples/VideoSummary.ipynb))
2+
This demo app uses Llama 3 to return a text summary of a YouTube video. It shows how to retrieve the caption of a YouTube video and how to ask Llama to summarize the content in different ways, from the simplest naive way that works for short text to more advanced methods of using LangChain's map_reduce and refine to overcome the 8K context length limit of Llama 3.
33

4-
## [NBA2023-24](./text2sql/StructuredLlama.ipynb): Ask Llama2 about Structured Data
5-
This demo app shows how to use LangChain and Llama2 to let users ask questions about **structured** data stored in a SQL DB. As the 2023-24 NBA season is around the corner, we use the NBA roster info saved in a SQLite DB to show you how to ask Llama2 questions about your favorite teams or players.
4+
## [NBA2023-24](./text2sql/StructuredLlama.ipynb): Ask Llama 3 about Structured Data
5+
This demo app shows how to use LangChain and Llama 3 to let users ask questions about **structured** data stored in a SQL DB. As the 2023-24 NBA season is entering the playoff, we use the NBA roster info saved in a SQLite DB to show you how to ask Llama 3 questions about your favorite teams or players.
66

7-
## LiveData: Ask Llama2 about Live Data (using [Replicate](LiveData.ipynb) or [OctoAI](../llama_api_providers/OctoAI_API_examples/LiveData.ipynb))
8-
This demo app shows how to perform live data augmented generation tasks with Llama2 and [LlamaIndex](https://github.com/run-llama/llama_index), another leading open-source framework for building LLM apps: it uses the [You.com search API](https://documentation.you.com/quickstart) to get live search result and ask Llama2 about them.
7+
## [LiveData](LiveData.ipynb): Ask Llama 3 about Live Data (using Replicate or [OctoAI](../llama_api_providers/OctoAI_API_examples/LiveData.ipynb))
8+
This demo app shows how to perform live data augmented generation tasks with Llama 3, [LlamaIndex](https://github.com/run-llama/llama_index), another leading open-source framework for building LLM apps, and the [Tavily](https://tavily.com) live search API.
99

10-
## [WhatsApp Chatbot](./chatbots/whatsapp_llama/whatsapp_llama2.md): Building a Llama-enabled WhatsApp Chatbot
11-
This step-by-step tutorial shows how to use the [WhatsApp Business API](https://developers.facebook.com/docs/whatsapp/cloud-api/overview) to build a Llama-enabled WhatsApp chatbot.
10+
## [WhatsApp Chatbot](./chatbots/whatsapp_llama/whatsapp_llama3.md): Building a Llama 3 Enabled WhatsApp Chatbot
11+
This step-by-step tutorial shows how to use the [WhatsApp Business API](https://developers.facebook.com/docs/whatsapp/cloud-api/overview) to build a Llama 3 enabled WhatsApp chatbot.
1212

13-
## [Messenger Chatbot](./chatbots/messenger_llama/messenger_llama2.md): Building a Llama-enabled Messenger Chatbot
14-
This step-by-step tutorial shows how to use the [Messenger Platform](https://developers.facebook.com/docs/messenger-platform/overview) to build a Llama-enabled Messenger chatbot.
13+
## [Messenger Chatbot](./chatbots/messenger_llama/messenger_llama3.md): Building a Llama 3 Enabled Messenger Chatbot
14+
This step-by-step tutorial shows how to use the [Messenger Platform](https://developers.facebook.com/docs/messenger-platform/overview) to build a Llama 3 enabled Messenger chatbot.
1515

1616
### RAG Chatbot Example (running [locally](./chatbots/RAG_chatbot/RAG_Chatbot_Example.ipynb) or on [OctoAI](../llama_api_providers/OctoAI_API_examples/RAG_Chatbot_example/RAG_Chatbot_Example.ipynb))
17-
A complete example of how to build a Llama 2 chatbot hosted on your browser that can answer questions based on your own data using retrieval augmented generation (RAG). You can run Llama2 locally if you have a good enough GPU or on OctoAI if you follow the note [here](../README.md#octoai_note).
17+
A complete example of how to build a Llama 3 chatbot hosted on your browser that can answer questions based on your own data using retrieval augmented generation (RAG). You can run Llama2 locally if you have a good enough GPU or on OctoAI if you follow the note [here](../README.md#octoai_note).

recipes/use_cases/chatbots/messenger_llama/llama_messenger.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
2-
# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.
2+
# This software may be used and distributed according to the terms of the Llama 3 Community License Agreement.
33

44
import langchain
55
from langchain.llms import Replicate
@@ -11,11 +11,11 @@
1111
import json
1212

1313
os.environ["REPLICATE_API_TOKEN"] = "<your replicate api token>"
14-
llama2_13b_chat = "meta/llama-2-13b-chat:f4e2de70d66816a838a89eeeb621910adffb0dd0baba3976c96980970978018d"
14+
llama3_8b_chat = "meta/meta-llama-3-8b-instruct"
1515

1616
llm = Replicate(
17-
model=llama2_13b_chat,
18-
model_kwargs={"temperature": 0.01, "top_p": 1, "max_new_tokens":500}
17+
model=llama3_8b_chat,
18+
model_kwargs={"temperature": 0.0, "top_p": 1, "max_new_tokens":500}
1919
)
2020

2121
app = Flask(__name__)

recipes/use_cases/chatbots/messenger_llama/messenger_llama2.md renamed to recipes/use_cases/chatbots/messenger_llama/messenger_llama3.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
# Building a Llama-enabled Messenger Chatbot
1+
# Building a Llama 3 Enabled Messenger Chatbot
22

3-
This step-by-step tutorial shows the complete process of building a Llama-enabled Messenger chatbot. A demo video of using the iOS Messenger app to send a question to a Facebook business page and receive the Llama 2 generated answer is [here](https://drive.google.com/file/d/1B4ijFH4X3jEHZfkGdTPmdsgpUes_RNud/view).
3+
This step-by-step tutorial shows the complete process of building a Llama-enabled Messenger chatbot. A demo video of using the iOS Messenger app to send a question to a Facebook business page and receive the Llama 3 generated answer is [here](https://drive.google.com/file/d/1B4ijFH4X3jEHZfkGdTPmdsgpUes_RNud/view).
44

5-
If you're interested in a Llama-enabled WhatsApp chatbot, see [here](../whatsapp_llama/whatsapp_llama2.md) for a tutorial.
5+
If you're interested in a Llama 3 enabled WhatsApp chatbot, see [here](../whatsapp_llama/whatsapp_llama3.md) for a tutorial.
66

77
## Overview
88

9-
Messenger from Meta is a messaging service that allows a Facebook business page to respond to people who are interested in their business using [Messenger Platform](https://developers.facebook.com/docs/messenger-platform/overview). The benefits of an intelligent and knowledgable Llama 2 powered chatbot are obvious, including cost saving and better customer experience such as 24x7 availability. In this tutorial, we'll cover the details of integrating Llama 2 with the Messenger Platform to build a basic Llama 2 enabled chatbot - for more Llama 2 application development and deployment demos such as how to integrate your own data with Llama 2, see the recipes.
9+
Messenger from Meta is a messaging service that allows a Facebook business page to respond to people who are interested in their business using [Messenger Platform](https://developers.facebook.com/docs/messenger-platform/overview). The benefits of an intelligent and knowledgable Llama 3 powered chatbot are obvious, including cost saving and better customer experience such as 24x7 availability. In this tutorial, we'll cover the details of integrating Llama 3 with the Messenger Platform to build a basic Llama 3 enabled chatbot - for more Llama 3 application development and deployment demos such as how to integrate your own data with Llama 3, see the recipes.
1010

11-
The diagram below shows the components and overall data flow of the Llama 2 enabled Messenger chatbot demo we built, using an Amazon EC2 instance as an example for running the web server.
11+
The diagram below shows the components and overall data flow of the Llama 3 enabled Messenger chatbot demo we built, using an Amazon EC2 instance as an example for running the web server.
1212

1313
![](../../../../docs/images/messenger_llama_arch.jpg)
1414

1515
## Getting Started with Messenger Platform
1616

1717
1. A Facebook Page is required to send and receive messages using the Messenger Platform - see [here](https://www.facebook.com/business/help/461775097570076?id=939256796236247) for details about Facebook Pages and how to create a new page.
1818

19-
2. If you have followed the [Llama WhatsApp chatbot tutorial](../whatsapp_llama/whatsapp_llama2.md), or if you already have a Meta developer account and a business app, then you can skip this step. Otherwise, you need to first [create a Meta developer account](https://developers.facebook.com/) and then [create a business app](https://developers.facebook.com/docs/development/create-an-app/).
19+
2. If you have followed the [Llama WhatsApp chatbot tutorial](../whatsapp_llama/whatsapp_llama3.md), or if you already have a Meta developer account and a business app, then you can skip this step. Otherwise, you need to first [create a Meta developer account](https://developers.facebook.com/) and then [create a business app](https://developers.facebook.com/docs/development/create-an-app/).
2020

2121
3. Add the Messenger product to your business app by going to your business app's Dashboard, click "Add Product" and then select "Messenger".
2222

@@ -26,9 +26,9 @@ The diagram below shows the components and overall data flow of the Llama 2 enab
2626

2727
![](../../../../docs/images/messenger_api_settings.png)
2828

29-
## Writing Llama 2 Enabled Web App
29+
## Writing Llama 3 Enabled Web App
3030

31-
The Python-based web app we developed uses [LangChain](https://www.langchain.com/), an open source LLM development framework, and [Replicate](https://replicate.com/), a service provider hosting LLM models in the cloud, to receive the user query sent by the webhook, which will be covered in the next section, pass the query to Llama 2, and send the Llama 2 answer back to the webhook. For more information on how to use LangChain or LlamaIndex, another LLM app building framework, and other Llama cloud providers or on-premise deployment options to develop Llama 2 apps, see the recipes.
31+
The Python-based web app we developed uses [LangChain](https://www.langchain.com/), an open source LLM development framework, and [Replicate](https://replicate.com/), a service provider hosting LLM models in the cloud, to receive the user query sent by the webhook, which will be covered in the next section, pass the query to Llama 3, and send the Llama 3 answer back to the webhook. For more information on how to use LangChain or LlamaIndex, another LLM app building framework, and other Llama 3 cloud providers or on-premise deployment options to develop Llama 3 apps, see the recipes.
3232

3333
First, let's create a new conda (or you can use venv if you like) environment and install all the required packages:
3434

@@ -38,10 +38,10 @@ conda activate messenger-llama
3838
pip install langchain replicate flask requests uvicorn gunicorn
3939
```
4040

41-
Then, create a Python file named [llama_messenger.py](llama_messenger.py) that creates a Llama 2 instance and defines an HTTP method `msgrcvd_page` to:
41+
Then, create a Python file named [llama_messenger.py](llama_messenger.py) that creates a Llama 3 instance and defines an HTTP method `msgrcvd_page` to:
4242

4343
1. receive the user message forwarded by the webhook;
44-
2. ask Llama 2 for the answer;
44+
2. ask Llama 3 for the answer;
4545
3. send the answer back to the sender using the Facebook graph API.
4646

4747
```
@@ -55,11 +55,11 @@ import requests
5555
import json
5656
5757
os.environ["REPLICATE_API_TOKEN"] = "<your replicate api token"
58-
llama2_13b_chat = "meta/llama-2-13b-chat:f4e2de70d66816a838a89eeeb621910adffb0dd0baba3976c96980970978018d"
58+
llama3_8b_chat = "meta/meta-llama-3-8b-instruct"
5959
6060
llm = Replicate(
61-
model=llama2_13b_chat,
62-
model_kwargs={"temperature": 0.01, "top_p": 1, "max_new_tokens":500}
61+
model=llama3_8b_chat,
62+
model_kwargs={"temperature": 0.0, "top_p": 1, "max_new_tokens":500}
6363
)
6464
6565
app = Flask(__name__)
@@ -91,7 +91,7 @@ Replace <page_access_token> with the access token copied in step 5 "Open Messeng
9191

9292
## Modifying the Webhook
9393

94-
Open your glitch.com webhook URL created earlier, and change your `app.js` to simply forward the user message and the user and page ids sent by the Messenger Platform to the Llama 2 enabled web app `llama_messenger.py` described in the previous section:
94+
Open your glitch.com webhook URL created earlier, and change your `app.js` to simply forward the user message and the user and page ids sent by the Messenger Platform to the Llama 3 enabled web app `llama_messenger.py` described in the previous section:
9595

9696
```
9797
"use strict";
@@ -167,7 +167,7 @@ app.get("/webhook", (req, res) => {
167167
});
168168
169169
```
170-
Remember to change <web server public IP>, which needs to be publicly visible, to the IP of the server where your Llama 2 enabled web app in the previous section runs.
170+
Remember to change <web server public IP>, which needs to be publicly visible, to the IP of the server where your Llama 3 enabled web app in the previous section runs.
171171

172172
Note: It's possible and even recommended to implement a webhook in Python and call the Llama directly inside the webhook, instead of making an HTTP request, as the JavaScript code above does, to a Python app which calls Llama and sends the answer to Messenger.
173173

@@ -181,7 +181,7 @@ gunicorn -b 0.0.0.0:5000 llama_messenger:app
181181

182182
If you use Amazon EC2 as your web server, make sure you have port 5000 added to your EC2 instance's security group's inbound rules.
183183

184-
Now you can open your Messenger app, select the Facebook page you connected in Messenger's API Settings, enter a message and receive the Llama 2's answer shortly, as shown in the demo video in the beginning of this post.
184+
Now you can open your Messenger app, select the Facebook page you connected in Messenger's API Settings, enter a message and receive the Llama 3's answer shortly, as shown in the demo video in the beginning of this post.
185185

186186
To debug any possible issues, go to your glitch.com app log and copy the URL generated there when a Messenger message is sent, which looks like this:
187187

recipes/use_cases/chatbots/whatsapp_llama/llama_chatbot.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
2-
# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.
2+
# This software may be used and distributed according to the terms of the Llama 3 Community License Agreement.
33

44
import langchain
55
from langchain.llms import Replicate
@@ -39,26 +39,25 @@ def send_text_message(self,message, phone_number):
3939
return response.status_code
4040

4141
os.environ["REPLICATE_API_TOKEN"] = "<your replicate api token>"
42-
llama2_13b_chat = "meta/llama-2-13b-chat:f4e2de70d66816a838a89eeeb621910adffb0dd0baba3976c96980970978018d"
42+
llama3_8b_chat = "meta/meta-llama-3-8b-instruct"
4343

4444
llm = Replicate(
45-
model=llama2_13b_chat,
46-
model_kwargs={"temperature": 0.01, "top_p": 1, "max_new_tokens":500}
45+
model=llama3_8b_chat,
46+
model_kwargs={"temperature": 0.0, "top_p": 1, "max_new_tokens":500}
4747
)
4848
client = WhatsAppClient()
4949
app = Flask(__name__)
5050

5151
@app.route("/")
5252
def hello_llama():
53-
return "<p>Hello Llama 2</p>"
53+
return "<p>Hello Llama 3</p>"
5454

5555
@app.route('/msgrcvd', methods=['POST', 'GET'])
5656
def msgrcvd():
5757
message = request.args.get('message')
58-
#client.send_template_message("hello_world", "en_US", "14086745477")
5958
answer = llm(message)
6059
print(message)
6160
print(answer)
62-
client.send_text_message(llm(message), "14086745477")
61+
client.send_text_message(llm(message), "<your phone number>")
6362
return message + "<p/>" + answer
6463

0 commit comments

Comments
 (0)