-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbot.py
More file actions
43 lines (29 loc) · 1.14 KB
/
Copy pathbot.py
File metadata and controls
43 lines (29 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import requests
from flask import Flask, request, Response
from twilio.twiml.messaging_response import MessagingResponse
app = Flask(__name__)
def get_chatgpt_response(prompt):
try:
response = requests.get("http://localhost:5001/chat", params={"q": prompt})
except Exception as e:
response = None
if response and response.status_code == 200:
return response.text
return None
def get_email_prompt(text):
prompt_text = """Can you write a good email for "{context}"? The output should be formatted and should only contain email content."""
return prompt_text.format(context=text.strip())
@app.route("/bot", methods=["POST"])
def bot():
incoming_msg = request.values.get("Body", "").lower()
resp = MessagingResponse()
if "email" in incoming_msg.lower():
msg_stripped = incoming_msg.replace("email", "")
final_prompt = get_email_prompt(msg_stripped)
else:
final_prompt = incoming_msg
response = get_chatgpt_response(final_prompt)
resp.message(response)
return Response(str(resp), mimetype="application/xml")
if __name__ == "__main__":
app.run(port=4000)