llamaSMS / app.py
Tri4's picture
Update app.py
6f1235f verified
"""
|SMS chatbot|
https://www.twilio.com/en-us/blog/ai-sms-chatbot-langchain-llama2-baseten
https://www.twilio.com/en-us/blog/ai-sms-chatbot-replicate-llama-2-langchain
https://www.twilio.com/en-us/blog/build-a-sms-chatbot-with-python-flask-and-twilio
|free sms|
https://textlocal.com/integrations/api/
https://www.alfredosequeida.com/blog/how-to-send-text-messages-for-free-using-python-use-python-to-send-text-messages-via-email/
|llama|
https://www.youtube.com/watch?v=_kYJZP1G9mE
https://www.youtube.com/watch?v=T_iE6TT7pS8 - Hugchat api
|Twilio Whatsapp API|
https://huggingface.co/spaces/Arafath10/twilio_whatapp_api/blob/main/main.py
"""
from flask import Flask, request
from langchain import LLMChain, PromptTemplate
from langchain.llms import Baseten
from langchain.memory import ConversationBufferWindowMemory
from twilio.twiml.messaging_response import MessagingResponse
template = """Assistant is a large language model.
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
I want you to act as Taylor Swift giving advice and answering questions. You will reply with what she would say.
SMS: {sms_input}
Assistant:"""
prompt = PromptTemplate(input_variables=["sms_input"], template=template)
sms_chain = LLMChain(
llm=Baseten(model="YOUR-MODEL-VERSION-ID"),
prompt=prompt,
memory=ConversationBufferWindowMemory(k=2),
llm_kwargs={"max_length": 4096}
)
app = Flask(__name__)
@app.route("/sms", methods=['GET', 'POST'])
def sms():
resp = MessagingResponse()
inb_msg = request.form['Body'].lower().strip()
output = sms_chain.predict(sms_input=inb_msg)
print(output)
resp.message(output)
return str(resp)
if __name__ == "__main__":
app.run(debug=True)