llama_chatbot.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # 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.
  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. llama2_13b_chat = "meta/llama-2-13b-chat:f4e2de70d66816a838a89eeeb621910adffb0dd0baba3976c96980970978018d"
  36. llm = Replicate(
  37. model=llama2_13b_chat,
  38. model_kwargs={"temperature": 0.01, "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 2</p>"
  45. @app.route('/msgrcvd', methods=['POST', 'GET'])
  46. def msgrcvd():
  47. message = request.args.get('message')
  48. #client.send_template_message("hello_world", "en_US", "14086745477")
  49. answer = llm(message)
  50. print(message)
  51. print(answer)
  52. client.send_text_message(llm(message), "14086745477")
  53. return message + "<p/>" + answer