# Copyright (c) Meta Platforms, Inc. and affiliates.
# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.

import langchain
from langchain.llms import Replicate

from flask import Flask
from flask import request
import os
import requests
import json

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}
)

app = Flask(__name__)

@app.route('/msgrcvd_pager', methods=['POST', 'GET'])
def msgrcvd_pager():    
    message = request.args.get('message')
    sender = request.args.get('sender')
    recipient = request.args.get('recipient')

    answer = llm(message)
    print(message)
    print(answer)

    url = f"https://graph.facebook.com/v18.0/{recipient}/messages"
    params = {
        'recipient': '{"id": ' + sender + '}',
        'message': json.dumps({'text': answer}),
        'messaging_type': 'RESPONSE',
        'access_token': "<your page access token>"
    }
    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.post(url, params=params, headers=headers)
    print(response.status_code)
    print(response.text)

    return message + "<p/>" + answer


langchain.llms.Replicate
[((488, 595), 'langchain.llms.Replicate', 'Replicate', ([], {'model': 'llama2_13b_chat', 'model_kwargs': "{'temperature': 0.01, 'top_p': 1, 'max_new_tokens': 500}"}), "(model=llama2_13b_chat, model_kwargs={'temperature': 0.01, 'top_p':\n    1, 'max_new_tokens': 500})\n", (497, 595), False, 'from langchain.llms import Replicate\n'), ((608, 623), 'flask.Flask', 'Flask', (['__name__'], {}), '(__name__)\n', (613, 623), False, 'from flask import Flask\n'), ((718, 745), 'flask.request.args.get', 'request.args.get', (['"""message"""'], {}), "('message')\n", (734, 745), False, 'from flask import request\n'), ((759, 785), 'flask.request.args.get', 'request.args.get', (['"""sender"""'], {}), "('sender')\n", (775, 785), False, 'from flask import request\n'), ((802, 831), 'flask.request.args.get', 'request.args.get', (['"""recipient"""'], {}), "('recipient')\n", (818, 831), False, 'from flask import request\n'), ((1250, 1300), 'requests.post', 'requests.post', (['url'], {'params': 'params', 'headers': 'headers'}), '(url, params=params, headers=headers)\n', (1263, 1300), False, 'import requests\n'), ((1045, 1073), 'json.dumps', 'json.dumps', (["{'text': answer}"], {}), "({'text': answer})\n", (1055, 1073), False, 'import json\n')]