|
@@ -39,7 +39,7 @@ conda activate whatsapp-llama
|
|
|
pip install langchain replicate flask requests uvicorn gunicorn
|
|
|
```
|
|
|
|
|
|
-Then, create a Python file named llama-chatbot.py with the following code, which defines a class `WhatsAppClient` and a method `send_text_message` to post a message (the answer generated by Llama 2 on a user query) to the WhatsApp Cloud API, which then sends the answer back to the WhatsApp user. Remember to set `WHATSAPP_API_TOKEN` and `WHATSAPP_CLOUD_NUMBER_ID` to the values you saved in the previous section.
|
|
|
+Then, create a Python file named llama_chatbot.py with the following code, which defines a class `WhatsAppClient` and a method `send_text_message` to post a message (the answer generated by Llama 2 on a user query) to the WhatsApp Cloud API, which then sends the answer back to the WhatsApp user. Remember to set `WHATSAPP_API_TOKEN` and `WHATSAPP_CLOUD_NUMBER_ID` to the values you saved in the previous section.
|
|
|
|
|
|
```
|
|
|
import langchain
|
|
@@ -76,7 +76,7 @@ class WhatsAppClient:
|
|
|
return response.status_code
|
|
|
```
|
|
|
|
|
|
-Finally, add the code below to llama-chatbot.py, which creates a Llama 2 instance and defines an HTTP method `msgrcvd` to:
|
|
|
+Finally, add the code below to llama_chatbot.py, which creates a Llama 2 instance and defines an HTTP method `msgrcvd` to:
|
|
|
1. receive the user message forwarded by the webhook;
|
|
|
2. ask Llama 2 for the answer;
|
|
|
3. call the `WhatsAppClient`'s `send_text_message`` with a recipient's phone number.
|
|
@@ -104,11 +104,13 @@ def msgrcvd():
|
|
|
return message + "<p/>" + answer
|
|
|
```
|
|
|
|
|
|
+The complete script of llama_chatbot.py is [here](llama_chatbot.py).
|
|
|
+
|
|
|
Now it's time to modify the webhook to complete the whole app.
|
|
|
|
|
|
## Modifying the Webhook
|
|
|
|
|
|
-Open your glitch.com webhook URL created earlier, and after the code snippet:
|
|
|
+Open your glitch.com webhook URL created earlier, and after the code snippet in app.js:
|
|
|
|
|
|
```
|
|
|
// message received!
|
|
@@ -130,9 +132,16 @@ add the code below - remember to change <web server public IP>, which needs to b
|
|
|
});
|
|
|
```
|
|
|
|
|
|
-The code simply forwards the user message received by the WhatsApp Cloud Platform to the Llama 2 enabled web app described in the previous section.
|
|
|
+The code simply forwards the user message received by the WhatsApp Cloud Platform to the Llama 2 enabled web app llama_chatbot.py described in the previous section. Because the functionality of calling the WhatsApp Cloud API to send a message has been implemented in the `send_text_message` in Python above, you can comment out the whole following code snippet in the original app.js:
|
|
|
+
|
|
|
+```
|
|
|
+ '// info on WhatsApp text message payload: https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples#text-messages
|
|
|
+ if (req.body.object) {
|
|
|
+ ...
|
|
|
+ }
|
|
|
+```
|
|
|
|
|
|
-Note: It's possible 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.
|
|
|
+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 WhatsApp.
|
|
|
|
|
|
## Running the Chatbot
|
|
|
|