-
Notifications
You must be signed in to change notification settings - Fork 1
/
alternative_helper.py
230 lines (210 loc) · 8.36 KB
/
alternative_helper.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import os
import json
import logging
import requests
from openai import OpenAI
import azure.functions as func
from twilio.rest import Client
#------------------------------------#
# Load environment variables
#------------------------------------#
ACCOUNT_SID = os.environ["ACCOUNT_SID"]
AUTH_TOKEN = os.environ["AUTH_TOKEN"]
SEC_PIN = os.environ["SECURITY_PIN"]
#------------------------------------#
# OpenAI and Twilio Clients
#------------------------------------#
ai_client = OpenAI()
CLIENT = Client(ACCOUNT_SID, AUTH_TOKEN)
#------------------------------------#
# Security check
#------------------------------------#
def process_incoming_message(PIN, incoming_message, send_to, send_from):
"""
Generate a reply based on the incoming message.
Parameters
----------
PIN : str
Security PIN
incoming_message : str
Incoming message from Twilio
Returns
-------
message : str
Reply message
"""
if incoming_message.strip() == PIN:
return """Welcome to the new Luis AI reminder assistant.
- I can schedule calls and text reminders for you.
- I can also just answer questions.
- Text 'about' to see this message again"""
else:
messages = CLIENT.messages.list(from_=send_to, to=send_from)
sent_pin = False
for message in messages:
if message.body.strip() == PIN:
sent_pin = True
if sent_pin:
follow_up_reply = get_follow_up_text(send_to=send_to,
send_from=send_from,
incoming_message=incoming_message)
return follow_up_reply
else:
return f"Please provide security PIN to continue."
#------------------------------------#
# Current time
#------------------------------------#
def get_time():
"""
Robustly get the current time from an API.
Parameters
----------
None
Returns
-------
str
Current time
"""
max_retries = 3
attempts = 0
while attempts < max_retries:
try:
response = requests.get('http://worldtimeapi.org/api/timezone/America/Los_Angeles')
response.raise_for_status() # This will raise an exception for HTTP error codes
res = response.json()
datetime = res.get('datetime')
abbreviation = res.get('abbreviation')
day_of_week = res.get('day_of_week')
if datetime and abbreviation and day_of_week is not None:
return f"{datetime} {abbreviation} day of the week {day_of_week}"
else:
raise ValueError("Incomplete time data received")
except (requests.RequestException, ValueError) as e:
attempts += 1
if attempts == max_retries:
return "Failed to get time after several attempts."
#-----------------------------------------#
# Generate JSON body to schedule reminder
#-----------------------------------------#
def schedule_reminder(natural_language_request, number_from):
"""
Generate JSON body to schedule reminder
Parameters
----------
natural_language_request : str
Natural language request from user
Returns
-------
JSON body to schedule reminder
"""
sys_prompt = """Your job is to create the JSON body for an API call to schedule texts and calls. Then , you will schedule the text or call for the user will request based on pacific time (given in pacific time). If user asks for a reminder today at 6 pm that is 18:00 (24 hour notation). Set the 'to_number' to the users number. Always set twilio = True.
Example JSON:
{
"time": "18:20",
"day": "2023-11-27",
"message_body": "This is the reminder body!",
"call": "True",
"twilio": "True",
"to_number": "+15554443333"
}
Another example:
{
"time":"23:46",
"day":"2023-11-27",
"message_body":"text reminder to check email",
"to_number":"+15554443333",
"twilio":"True",
"call":"False"
}
"""
curr_time = get_time()
ai_client = OpenAI()
completion = ai_client.chat.completions.create(
model="gpt-3.5-turbo-1106",
messages=[
{"role": "system", "content": f"{sys_prompt}"},
{"role": "user", "content": f"{natural_language_request}. <User's number> {number_from} <Current Time>: {curr_time}"},
],
response_format={ "type": "json_object" },
)
return json.loads(completion.choices[0].message.content)
#------------------------------------#
# Follow up text
#------------------------------------#
def get_follow_up_text(send_to, send_from, incoming_message):
"""Send follow up text
Parameters
----------
send_to : str
Phone number to send text to
send_from : str
Phone number to send text from
incoming_message : str
Incoming message from Twilio
Returns
-------
message : str
Response from the AI to the user
"""
if incoming_message == 'about':
return """Welcome to the new Luis AI reminder assistant.
- I can schedule calls and text reminders for you.
- I can answer any questions, within reason.
- Text 'about' to see this message again"""
else:
tools = [
{
"type": "function",
"function": {
"name": "schedule_reminder",
"description": "Schedule a reminder using natural language",
"parameters": {
"type": "object",
"properties": {
"natural_language_request": {
"type": "string",
"description": "Requested reminder in natural language. Example: 'Remind me to call mom tomorrow at 6pm' or 'Send me a message with a Matrix quote on wednesday at 8am'",
}
},
"required": ["natural_language_request"],
},
}
}
]
#----------------------------------------------------#
# AI w/tools - reply or use tools to schedule reminder
#----------------------------------------------------#
completion = ai_client.chat.completions.create(
model="gpt-3.5-turbo-1106",
messages=[
{"role": "system", "content": f"You are an AI assistant that can schedule reminders (like calls and texts) if asked to do so. Be informative, funny, and helpful, and keep your messages clear and short. To schedule reminder just pass a natural language request to the function 'schedule_reminder'"},
{"role": "user", "content": f"{incoming_message}"}
],
tools=tools,
tool_choice="auto"
)
message = completion.choices[0].message.content
if message==None:
message = "Just a minute while I schedule your reminder."
else:
return message
#----------------------------------------------------#
# If tools are called, call the tools function
#----------------------------------------------------#
if completion.choices[0].message.tool_calls:
if completion.choices[0].message.tool_calls[0].function.name == 'schedule_reminder':
args = completion.choices[0].message.tool_calls[0].function.arguments
args_dict = json.loads(args)
try:
#--------------------------------#
# Schedule reminder
#--------------------------------#
json_body = schedule_reminder(**args_dict, number_from=send_to)
url_endpoint = os.environ["AMAZON_ENDPOINT"]
headers = {'Content-Type': 'application/json'}
response = requests.post(url_endpoint, headers=headers, data=json.dumps(json_body))
if response.status_code == 200:
return f"Your reminder has been scheduled to be sent to {send_to}"
except Exception as e:
logging.error(f"Error: {e}")
return f"Error scheduling reminder. {e}"