Browse Source

updated tutorial and added llama_chatbot.py

Jeff Tang 1 năm trước cách đây
mục cha
commit
7298487dbc
3 tập tin đã thay đổi với 76 bổ sung6 xóa
  1. 1 1
      README.md
  2. 61 0
      demo_apps/llama_chatbot.py
  3. 14 5
      demo_apps/whatsapp_llama2.md

+ 1 - 1
README.md

@@ -1,6 +1,6 @@
 # Llama 2 Fine-tuning / Inference Recipes, Examples and Demo Apps
 
-**[Update Nov. 7, 2023] We recently released a series of Llama 2 demo apps [here](./demo_apps). These apps show how to run Llama 2 locally, in the cloud, on-prem or with WhatsApp, and how to ask Llama 2 questions in general and about custom data (PDF, DB, or live).**
+**[Update Nov. 13, 2023] We recently released a series of Llama 2 demo apps [here](./demo_apps). These apps show how to run Llama 2 locally, in the cloud, on-prem or with WhatsApp, and how to ask Llama 2 questions in general and about custom data (PDF, DB, or live).**
 
 The 'llama-recipes' repository is a companion to the [Llama 2 model](https://github.com/facebookresearch/llama). The goal of this repository is to provide examples to quickly get started with fine-tuning for domain adaptation and how to run inference for the fine-tuned models. For ease of use, the examples use Hugging Face converted versions of the models. See steps for conversion of the model [here](#model-conversion-to-hugging-face).
 

+ 61 - 0
demo_apps/llama_chatbot.py

@@ -0,0 +1,61 @@
+import langchain
+from langchain.llms import Replicate
+
+from flask import Flask
+from flask import request
+import os
+import requests
+import json
+
+class WhatsAppClient:
+
+    API_URL = "https://graph.facebook.com/v17.0/"
+    WHATSAPP_API_TOKEN = "<Temporary access token from your WhatsApp API Setup>"
+    WHATSAPP_CLOUD_NUMBER_ID = "<Phone number ID from your WhatsApp API Setup>"
+
+    def __init__(self):
+        self.headers = {
+            "Authorization": f"Bearer {self.WHATSAPP_API_TOKEN}",
+            "Content-Type": "application/json",
+        }
+        self.API_URL = self.API_URL + self.WHATSAPP_CLOUD_NUMBER_ID
+
+    def send_text_message(self,message, phone_number):
+        payload = {
+            "messaging_product": 'whatsapp',
+            "to": phone_number,
+            "type": "text",
+            "text": {
+                "preview_url": False,
+                "body": message
+            }
+        }
+        response = requests.post(f"{self.API_URL}/messages", json=payload,headers=self.headers)
+        print(response.status_code)
+        assert response.status_code == 200, "Error sending message"
+        return response.status_code
+
+os.environ["REPLICATE_API_TOKEN"] = "<your replicate api token>"    
+llama2_13b_chat = "meta/llama-2-13b-chat:f4e2de70d66816a838a89eeeb621910adffb0dd0baba3976c96980970978018d"
+
+llm = Replicate(
+    model=llama2_13b_chat,
+    model_kwargs={"temperature": 0.01, "top_p": 1, "max_new_tokens":500}
+)
+client = WhatsAppClient()
+app = Flask(__name__)
+
+@app.route("/")
+def hello_llama():
+    return "<p>Hello Llama 2</p>"
+
+@app.route('/msgrcvd', methods=['POST', 'GET'])
+def msgrcvd():    
+    message = request.args.get('message')
+    #client.send_template_message("hello_world", "en_US", "14086745477")
+    answer = llm(message)
+    print(message)
+    print(answer)
+    client.send_text_message(llm(message), "14086745477")
+    return message + "<p/>" + answer
+

+ 14 - 5
demo_apps/whatsapp_llama2.md

@@ -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