llama_chatbot.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (c) Meta Platforms, Inc. and affiliates.
  2. # This software may be used and distributed according to the terms of the Llama 3 Community License Agreement.
  3. import langchain
  4. from langchain.llms import Replicate
  5. from flask import Flask
  6. from flask import request
  7. import os
  8. import requests
  9. import json
  10. class WhatsAppClient:
  11. API_URL = "https://graph.facebook.com/v17.0/"
  12. WHATSAPP_API_TOKEN = "<Temporary access token from your WhatsApp API Setup>"
  13. WHATSAPP_CLOUD_NUMBER_ID = "<Phone number ID from your WhatsApp API Setup>"
  14. def __init__(self):
  15. self.headers = {
  16. "Authorization": f"Bearer {self.WHATSAPP_API_TOKEN}",
  17. "Content-Type": "application/json",
  18. }
  19. self.API_URL = self.API_URL + self.WHATSAPP_CLOUD_NUMBER_ID
  20. def send_text_message(self,message, phone_number):
  21. payload = {
  22. "messaging_product": 'whatsapp',
  23. "to": phone_number,
  24. "type": "text",
  25. "text": {
  26. "preview_url": False,
  27. "body": message
  28. }
  29. }
  30. response = requests.post(f"{self.API_URL}/messages", json=payload,headers=self.headers)
  31. print(response.status_code)
  32. assert response.status_code == 200, "Error sending message"
  33. return response.status_code
  34. os.environ["REPLICATE_API_TOKEN"] = "<your replicate api token>"
  35. llama3_8b_chat = "meta/meta-llama-3-8b-instruct"
  36. llm = Replicate(
  37. model=llama3_8b_chat,
  38. model_kwargs={"temperature": 0.0, "top_p": 1, "max_new_tokens":500}
  39. )
  40. client = WhatsAppClient()
  41. app = Flask(__name__)
  42. @app.route("/")
  43. def hello_llama():
  44. return "<p>Hello Llama 3</p>"
  45. @app.route('/msgrcvd', methods=['POST', 'GET'])
  46. def msgrcvd():
  47. message = request.args.get('message')
  48. answer = llm(message)
  49. print(message)
  50. print(answer)
  51. client.send_text_message(llm(message), "<your phone number>")
  52. return message + "<p/>" + answer