-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
318 lines (273 loc) · 11.2 KB
/
utils.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import time
import json
from openai import OpenAI
import requests
from mailjet_rest import Client
from secrets2 import get_keys
from PIL import Image
import io
import os
import pybase64
from mailjet_rest import Client
import streamlit as st
openai_secret_key, MAILJET_API_KEY, MAILJET_API_SECRET, db_pwd = get_keys()
client = OpenAI(api_key=openai_secret_key)
mailjet = Client(auth=(MAILJET_API_KEY, MAILJET_API_SECRET))
tools = [{
"type":"function",
"function":{
"name": "send_sql_query",
"description": "Send a sql query to get the required data from a table with the following schema - \n \n create table mood_freq_table(\n sno int not null,\n name varchar(20),\n mood varchar(15),\n frequency int,\n primary key (sno)\n );",
"parameters": {
"type": "object",
"properties": {
"sql_query": {
"type": "string",
"description": "The sql query alone as a string"
}
},
"required": [
"sql_query"
]
}
}
},
{
"type":"function",
"function":{
"name": "send_email",
"description": "Send an email to the specified email with the subject, content and attached files",
"parameters": {
"type": "object",
"properties": {
"FromEmail": {
"type": "string",
"description": "The email address, eg., [email protected]"
},
"FromName": {
"type": "string",
"description": "The name of the sender, eg., Aloo"
},
"Subject": {
"type": "string",
"description": "Subject of the email"
},
"Text-part": {
"type": "string",
"description": "The content of the email"
},
"Recipients": {
"type": "string",
"description": "The recipients' email addresses"
},
"Attachments": {
"type": "object",
"description": "A list containing dictionaries for each image. Each dictionary has 'Content-type', 'Filename' and 'content' as the keys. 'Content-type' takes the value 'image/png', 'Filename' takes the name of the file and 'content' takes the Base64 encoded form of the image. "
}
},
"required": [
"FromEmail",
"FromName",
"Subject",
"Text-part",
"Recipients",
"Attachments"
]
}
}
},
{"type":"retrieval"},
{"type":"code_interpreter"}]
def send_sql_query(sql_query):
json_data = {"message": sql_query}
try:
response = requests.post(
"http://localhost:5000/get_sql_response",
json=json_data,
)
print(response.text)
return response.text
except Exception as e:
print("Unable to generate response")
print(f"Exception: {e}")
return e
def send_email(prompt):
print(prompt.keys())
res = mailjet.send.create(data=prompt)
return('Email has been sent')
def create_assistant():
assistant_id_file = 'assistant_id.txt'
if os.path.exists(assistant_id_file):
with open(assistant_id_file, 'r') as file:
assistant_id = file.read().strip()
else:
assistant = client.beta.assistants.create(
name="Data Plotter",
instructions="You are a data visualizer. Your role is to call functions to generate sql queries for the given schema - '\n \n create table mood_freq_table(\n sno int not null,\n name varchar(20),\n mood varchar(15),\n frequency int,\n primary key (sno)\n ); ' and to retrieve data from it and plot graphs and charts and save the images as png files. You can send an email containing the images only if requested by the user.",
model='gpt-3.5-turbo-1106',
tools=tools
)
assistant_id = assistant.id
with open(assistant_id_file, 'w') as file:
file.write(assistant_id)
assistant = client.beta.assistants.retrieve(assistant_id=assistant_id)
print(f'Assistant with id {assistant_id} retrieved.')
return assistant
def create_thread():
print("Creating a thread for new convo")
thread = client.beta.threads.create()
print(f'Thread created. ID - {thread.id}')
return thread
def send_message_and_run_assistant(thread, assistant, user_message):
print('User message added to thread: '+user_message)
message = client.beta.threads.messages.create(
thread_id=thread.id,
role='user',
content=user_message
)
print('Running the assistant')
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id
)
return run
def poll_run_status(thread, run):
while True:
run = client.beta.threads.runs.retrieve(run_id=run.id, thread_id=thread.id)
if run.status in ['completed','failed','cancelled']:
break
elif run.status == 'requires_action':
prompt, if_send_email = handle_required_actions(thread, run)
else:
print('waiting')
time.sleep(3)
return run, prompt, if_send_email
def handle_required_actions(thread, run):
print('Assistant req function calls')
required_actions = run.required_action.submit_tool_outputs
with open('jsons/required_actions.json','w') as f:
required_actions_json = required_actions.model_dump()
json.dump(required_actions_json,f,indent=4)
tool_outputs = []
prompt = ""
if_send_email = False
for action in required_actions.tool_calls:
func_name = action.function.name
arguments = json.loads(action.function.arguments)
if func_name=="send_sql_query":
output = send_sql_query(arguments['sql_query'])
elif func_name=="send_email":
prompt = arguments
if_send_email = True
output = 'Email sent'
else:
raise ValueError(f'Unkown function {func_name}')
print(f'{func_name} has been called with arguments: {arguments}')
tool_outputs.append({
'tool_call_id':action.id,
'run_id':run.id,
'output':output
})
print('Submitting fn call back to Assistant')
client.beta.threads.runs.submit_tool_outputs(
thread_id=thread.id,
run_id=run.id,
tool_outputs=tool_outputs
)
return prompt, if_send_email
def load_imgs(path):
files = os.listdir(path)
file_list = {}
for i in files:
with open(f"charts/{i}", "rb") as img_file:
file_list[i] = pybase64.b64encode(img_file.read()).decode('utf-8')
return file_list
def display_final_response(thread,run,prompt,if_send_email):
messages = client.beta.threads.messages.list(thread_id=thread.id)
run_steps = client.beta.threads.runs.steps.list(thread_id=thread.id,run_id=run.id)
with open('jsons/run_steps.json','w') as f:
run_steps_json = run_steps.model_dump()
json.dump(run_steps_json,f,indent=4)
with open('jsons/messages.json','w') as f:
messages_json = messages.model_dump()
json.dump(messages_json,f,indent=4)
op_string = ""
for msg in messages.data:
for content in msg.content:
if hasattr(content, 'image_file'):
continue
if hasattr(content, 'text'):
op_string = f"{msg.role.capitalize()}: {content.text.value}"
print(op_string)
updated_messages = []
#images
image_counter = 0
for message in messages.data:
if message.content:
citations = []
for content_part in message.content:
if content_part.type == 'text':
annotations = content_part.text.annotations
text_value = content_part.text.value
op_string = text_value
if annotations!=[]:
for index, annotation in enumerate(annotations):
text_value = text_value.replace(annotation.text, f' [{index}]')
if (file_citation := getattr(annotation, 'file_citation',None)):
cited_file = client.files.retrieve(file_citation.file_id)
citations.append(f'[{index}] {file_citation.quote} from {cited_file.filename}')
elif (file_path := getattr(annotation, 'file_path',None)):
cited_file = client.files.retrieve(file_path.file_id)
image_file_id = cited_file.id
image_data : bytes = client.files.with_raw_response.content(image_file_id).content
image = Image.open(io.BytesIO(image_data))
image.save(f'charts/chart{image_counter}.png')
image_counter += 1
elif content_part.type == 'image_file':
image_file_id = content_part.image_file.file_id
image_data : bytes = client.files.with_raw_response.content(image_file_id).content
image = Image.open(io.BytesIO(image_data))
image.save(f'charts/chart{image_counter}.png')
image_counter += 1
updated_messages.append(message)
op_string = 'The graph'
if if_send_email:
imgs = load_imgs('charts')
prompt['Attachments'] = []
prompt['Recipients'] = [{"Email":prompt['Recipients']}]
for i in imgs.keys():
img_dict = {
'Content_type':'image/png',
'Filename':i,
'content':imgs[i]
}
prompt['Attachments'].append(img_dict)
# print(send_email(prompt))
op_string = send_email(prompt)
image_counter = -1
return op_string, image_counter
if 'messages' not in st.session_state:
st.session_state.messages=[]
for message in st.session_state.messages:
with st.chat_message(message['role']):
st.markdown(message['content'])
if message['role']=='assistant':
if message['image']!=-1:
st.image(message['image'],'')
thread = create_thread()
assistant = create_assistant()
if user_message := st.chat_input('Type in the data required'):
with st.chat_message('user'):
st.markdown(user_message)
st.session_state.messages.append({'role':'user','content':user_message})
img = -1
with st.chat_message('assistant'):
message_placeholder = st.empty()
run = send_message_and_run_assistant(thread,assistant,user_message)
run, prompt, if_send_email = poll_run_status(thread,run)
resp, img = display_final_response(thread,run,prompt,if_send_email)
message_placeholder.markdown(resp)
st.session_state.messages.append({'role':'assistant',
'content':resp,
'image':f'charts/chart{img-1}.png' if img!=-1 else ''})
# Plot the graphs for the total frequencies of all the moods and save the image