Repository navigation
Expand file tree
/
Copy pathsecond_lambda.py
More file actions
357 lines (318 loc) · 13 KB
/
Copy pathsecond_lambda.py
File metadata and controls
357 lines (318 loc) · 13 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import json
import boto3
import sys
import ast
# Bedrock client used to interact with APIs around models
bedrock = boto3.client(
service_name='bedrock',
region_name='us-west-2'
)
# Bedrock Runtime client used to invoke and question the models
bedrock_runtime = boto3.client(
service_name='bedrock-runtime',
region_name='us-west-2'
)
#modelId = "anthropic.claude-3-opus-20240229-v1:0"
#modelId = "anthropic.claude-3-5-sonnet-20240620-v1:0"
modelId = "anthropic.claude-3-haiku-20240307-v1:0"
def getRankedList(ust,list_of_recordings):
prompt = f"""
Given the following User Search Term (UST):
"{ust}"
And the following list of recording labels with their Serial IDs and Redocrding IDs:
{list_of_recordings}
Please rank all the labels from best to worst in terms of answering the UST. Consider the semantic meaning and relevance of each label to the UST.
Provide your answer in the following format:
[
{{
"rank": 1,
"selected_index": <serial id of the recording>,
"selected_recording_id": <recording id of the recording>,
"selected_label": "<the selected label>",
}},
{{
"rank": 2,
...
}},
...
]
Select only one label per recording id. Ie if there are 3 labels for a recording id, select only the best one.
Include all labels in the ranked list. Only provide the JSON array as your response, without any additional text.
"""
body = json.dumps({
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 4096,
"messages": [
{
"role": "user",
"content": prompt
}
],
"temperature": 0.2,
"top_p": 1
})
response = bedrock_runtime.invoke_model(
body=body,
modelId=modelId,
accept='application/json',
contentType='application/json'
)
response_body = json.loads(response.get('body').read())
answer = response_body.get("content")[0].get("text")
ranked_list = json.loads(answer)
return ranked_list
def getInputValues(inputs, ust):
prompt = f"""
Given the following user query:
"{ust}"
Please extract the following variables:
{', '.join(inputs)}
For each variable, provide the extracted value if found in the query. If the information is not present, set "found" to "False" and "InputValue" to an empty string.
Format your response as a JSON array of objects, like this:
[
{{
"Input": "input_name",
"found": "True",
"InputValue": "extracted_value"
}},
{{
"Input": "another_input",
"found": "False",
"InputValue": ""
}},
...
]
Only provide the JSON array as your response, without any additional explanation.
"""
# The payload provided to Bedrock
body = json.dumps({
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 2049,
"messages": [
{
"role": "user",
"content": prompt
}
],
"temperature": 0.5,
"top_p": 1
})
# The call made to the model
response = bedrock_runtime.invoke_model(
body=body,
modelId=modelId,
accept='application/json',
contentType='application/json'
)
response_body = json.loads(response.get('body').read())
# The response from the model
#answer = response_body.get("content")[0].get("text")
answer = response_body.get("content")[0].get("text")
answer = json.loads(answer)
print("Here is the answer:")
print (answer)
return answer
#return json.dumps(result, indent=2)
def getStitchedResponse(ust, matched_recordings):
formatted_json = {
"UST": ust,
"matched_recordings": []
}
for recording in matched_recordings:
input_values = recording["inputValues"]
#input_values = ast.literal_eval(recording["inputValues"])
formatted_recording = {
"recording_id": recording["recording_id"],
"matched_recording_label": recording["matched_recording_label"],
"inputValues": input_values
#"inputValues": recording["inputValues"]
#"inputValues": "[{name:abhi, rool: 1}, {name:def, rool: 2}, {name:ghi, rool: 3}]"
}
formatted_json["matched_recordings"].append(formatted_recording)
return json.dumps(formatted_json)
def simplifyJson(original_json_string):
parsed_json = json.loads(original_json_string)
# Parse the nested inputValues string
for recording in parsed_json['matched_recordings']:
recording['inputValues'] = json.loads(recording['inputValues'])
# Create a simplified JSON string
simplified_json = json.dumps(parsed_json, indent=2)
return simplified_json
def lambda_handler(event, context):
try:
# Check if the body is in the event
if 'body' in event:
print ("Body found in event")
# If the body is a string, parse it as JSON
if isinstance(event['body'], str):
post_data = json.loads(event['body'])
else:
post_data = event['body']
else:
if isinstance(event, str):
post_data = json.loads(event)
else:
post_data = event
print("Extracting UST")
ust = post_data["UST"]
print (ust)
labels_with_ids = []
print ("Extracting recordings...")
ii = 0
for recording in post_data["Recordings"]:
for label in recording["Recording_Labels"]:
labels_with_ids.append({
"serial_id": ii,
"recording_id": recording["Recording_Id"],
"label": label
})
ii+=1
list_of_recordings = json.dumps(labels_with_ids, indent=2)
#print ("list of recordings")
#print (list_of_recordings)
print ("Getting ranked list of labels...")
ranked_list = getRankedList(ust,list_of_recordings)
#print("Ranked list of recordings:")
#print(json.dumps(ranked_list, indent=2))
except json.JSONDecodeError as e:
return {
'statusCode': 400,
'body': json.dumps({'error': f'Invalid JSON in request body: {str(e)}'})
}
except Exception as e:
return {
'statusCode': 400,
'body': json.dumps({'error': f'Error processing request: {str(e)} : {event}'})
}
matched_recordings = []
seen_recording_ids = set()
for rank, item in enumerate(ranked_list, 1):
selected_index = item['selected_index']
selected_recording_id = item['selected_recording_id']
selected_label = item['selected_label']
if selected_recording_id in seen_recording_ids:
continue
seen_recording_ids.add(selected_recording_id)
a_recording = post_data["Recordings"][int(selected_index)]
inputs = [input_data["Input"] for input_data in a_recording["Expected_User_Input"]]
print("getting input values...")
inputValues = getInputValues(inputs, ust)
matched_recordings.append({
"recording_id": selected_recording_id,
"matched_recording_label": selected_label,
"inputValues": inputValues
})
'''print(f"Rank {rank}:")
print(f" Selected Index: {selected_index}")
print(f" Recording ID: {selected_recording_id}")
print(f" Label: {selected_label}")
print(f" Inputs: {inputs}")
print(f" Input Values: {inputValues}")
print()
'''
#print(f"Matched Recordings: {matched_recordings}")
response = getStitchedResponse(ust, matched_recordings)
print ("Stitched response")
print (response)
print ("After Stringification")
print (json.dumps(response))
#response = simplifyJson(response)
#print(f"Stitched Response: {response}")
return {
'statusCode': 200,
'body': json.dumps(response)
}
def main():
dummy_event = {
"UST": "Show me Spent time on Digital Assistant by Yuresh",
"Recordings": [
{
"Recording_Id": 4531,
"Recording_Labels": [
"See Spent time on Udan by Abhishek"
],
"Expected_User_Input": [
{
"Input": "ProjectName",
"input_metadata": "This is the name of the Project"
},
{
"Input": "ProjectNaviation",
"input_metadata": "This is the Action to take"
},
{
"Input": "UserCondition",
"input_metadata": "This is used for selecting the user condition",
"html_element_type": "dropdown",
"html_content": "<select id=\"operators_assigned_to_id\" name=\"op[assigned_to_id]\"><option value=\"=\">is</option><option value=\"!\">is not</option><option value=\"!\">none</option><option value=\"\">any</option></select>"
},
{
"Input":"UserName",
"input_metadata": "This is the user name dropdown",
"html_element_type": "dropdown",
"html_content": "<select class=\"value\" id=\"values_assigned_to_id_1\" name=\"v[assigned_to_id][]\"><option value=\"me\"><< me >></option><optgroup label=\"active\"><option value=\"86\">Aakash Entab</option><option value=\"99\">Abhishek Mathur</option><option value=\"8\">ajay k</option><option value=\"84\">Bhushan Entab</option><option value=\"87\">Ganesh Chandu</option><option value=\"12\">Haritha C</option><option value=\"83\">Jitendar Kumar</option><option value=\"82\">Jitendar Sharma</option><option value=\"64\">Lakshman Veti</option><option value=\"45\">Moen Ediga</option><option value=\"22\">Nagamunemma T</option><option value=\"88\">Navya Nimmagadda</option><option value=\"81\">Raju Kamireddy</option><option value=\"5\">Ramakrishna Krishnamsetty</option><option value=\"85\">Sandhya Entab</option><option value=\"65\">TBD TBD</option><option value=\"75\">Tej Reddy</option><option value=\"36\">Yureshwar Ravuri</option></optgroup><optgroup label=\"locked\"><option value=\"76\">Amith Bachuwala</option><option value=\"71\">ashwini indukande</option><option value=\"66\">Atul Arora</option><option value=\"9\">Dharani Reddy</option><option value=\"68\">Praveen Dodda</option><option value=\"54\">Ragavan K</option><option value=\"70\">Rama Krishna Mundru</option><option value=\"69\">Sunil Gutta</option><option value=\"102\">Udan Public</option></optgroup><option value=\"36\">Yureshwar Ravuri</option></select>"
}
]
}
]
}
#dummy_event = {"UST":"How much time did abhi spend on Udan","Recordings":[{"Recording_Id":4531,"Recording_Labels":["How much time did a user spent on project","Time spent by a user on project","user spent time on a project"],"Expected_User_Input":[{"Input":"project_name","input_metadata":""},{"Input":"action","input_metadata":""},{"Input":"user_name","input_metadata":""}]}]}
dummy_event1 = {
"UST": "How much time did ajay spent on udan",
"Recordings": [
{
"Recording_Id": 4531,
"Recording_Labels": [
"How much time did a user spent on project",
"Time spent by a user on project",
"user spent time on a project"
],
"Expected_User_Input": [
{
"Input": "project_name",
"input_metadata": ""
},
{
"Input": "action",
"input_metadata": ""
},
{
"Input": "user_name",
"input_metadata": ""
}
]
},
{
"Recording_Id": 4530,
"Recording_Labels": [
"Time spent by user in a project",
"Time spent by Ajay in Digital Assistant",
"Get time spent by ajay in digital assistant"
],
"Expected_User_Input": [
{
"Input": "project_name",
"input_metadata": ""
},
{
"Input": "action",
"input_metadata": ""
},
{
"Input": "user_filter_selector",
"input_metadata": ""
},
{
"Input": "user_name",
"input_metadata": ""
}
]
}
]
}
# Call lambda_handler with dummy event
result = lambda_handler(dummy_event, None)
print("Lambda handler result:")
print(result)
if __name__ == "__main__":
main()