-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_labels.py
431 lines (337 loc) · 17.2 KB
/
fetch_labels.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import uvicorn
from fastapi import FastAPI, Form, Request, HTTPException
import httpx
import json
import logging
from fastapi import FastAPI, Form
from fastapi.responses import JSONResponse
from fuzzywuzzy import fuzz
from typing import List, Union
import uvicorn
from typing import List, Dict, Union
from database.connection import get_collection
app = FastAPI()
logging.basicConfig(level=logging.DEBUG)
def stored_input(tenant: str):
#logging.debug(f"Getting collection for tenant: {tenant}")
return get_collection(tenant, "openai_input")
def stored_response(tenant: str):
#logging.debug(f"Getting collection for storing scores for tenant: {tenant}")
return get_collection(tenant, "openai_output")
def convert_string_to_list(input_str: str) -> List[str]:
# Remove leading and trailing whitespaces, and split by ','
return [element.strip() for element in input_str.strip('[]').split(',')]
def compare_lists_with_fuzzy(l1, l2, threshold=50):
matching_elements_l1 = []
matching_elements_l2 = []
non_matching_elements_l1 = []
non_matching_elements_l2 = []
for element_l1 in l1:
max_similarity = 0
matching_element_l2 = ''
for element_l2 in l2:
el1 = str(element_l1).lower()
el2 = str(element_l2).lower()
similarity = fuzz.ratio(
el1, el2
)
#print(f'l1 {el1} ;;; l2 {el2} ;; similarity {similarity}') # Convert to lowercase for case-insensitive comparison
if similarity > max_similarity and similarity >= threshold:
max_similarity = similarity
matching_element_l2 = element_l2
if matching_element_l2:
matching_elements_l1.append(element_l1.strip("'"))
matching_elements_l2.append(matching_element_l2.strip("'"))
else:
non_matching_elements_l1.append(element_l1.strip("'"))
non_matching_elements_l2 = [
element_l2.strip("'")
for element_l2 in l2
if element_l2.strip("'") not in matching_elements_l2
]
similar_elements = []
for element_l1, element_l2 in zip(matching_elements_l1, matching_elements_l2):
similar_elements.append({"element_name_l1": element_l1, "element_name_l2": element_l2})
result = {"similar_elements": similar_elements}
return result
def generate_final_response(similar_elements: List[Dict[str, str]], response_data: List[Dict[str, str]]) -> List[Dict[str, Union[str, int]]]:
final_response = []
processed_labels = set()
# Create a dictionary for easy lookup of response_data based on labels
response_lookup = {data['label']: data for data in response_data}
for element in similar_elements:
# Find matching element in response_data based on label
matched_data = next((data for data in response_data if data['label'] == element['element_name_l1']), None)
if matched_data:
final_response.append({
'jsonPath': matched_data['jsonPath'],
'l2_matched': element['element_name_l2'],
'datatype': matched_data['datatype'],
'value': matched_data['value']
})
processed_labels.add(element['element_name_l1']) # Track processed labels
# Handle unmatched elements from l1
for data in response_data:
if data['label'] not in processed_labels:
final_response.append({
'jsonPath': data['jsonPath'],
'l2_matched': '', # No match from l2
'datatype': data['datatype'],
'value': data['value'] # Use value from response_data
})
return final_response
@app.post('/generativeaisrvc/send_and_get_sample')
async def process_data(request: Request, data: dict):
response_data = {}
try:
tenant = "generativeAI"
input_collection = stored_input(tenant)
output_collection = stored_response(tenant)
#input_collection.insert_one(data)
input_collection.update_one(
{"appId": data["appId"]},
{"$set": data},
upsert=True
)
logging.debug("Input respone saved successfully")
print("data :",data)
def extract_info(json_data):
appId = json_data.get("appId")
body = ""
params = None
headers = None
try:
body = json.loads(json_data["schema"]["nodes"][0]["data"]["body"])
except:
print("body not found")
try:
headers = {header["key"]: header["value"] for header in json_data["schema"]["nodes"][0]["data"]["headers"]}
except:
print("headers not found")
url = json_data["schema"]["nodes"][0]["data"]["url"]
request_method = json_data["schema"]["nodes"][0]["data"]["requestMethod"]
try:
params_list = json_data["schema"]["nodes"][0]["data"]["params"]
params = {param["key"]: param["value"] for param in params_list if param["included"]}
except:
print("params not found")
print("appId: ",appId)
print("body: ",body)
print("headers: ",headers)
#print("url: ",url)
print("request_method: ",request_method)
print("params: ",params)
return appId, body, headers, url, request_method, params
appId, body, headers, url, request_method, params = extract_info(data)
final_url = url
if params:
final_url += "?" + "&".join(f"{key}={value}" for key,value in params.items())
print("final_url: ",final_url)
# Fetch JSON data from the specified URL using httpx for asynchronous requests
async with httpx.AsyncClient() as client:
#url_from_request = data["url"]
#print("url_from_request: ",url)
requestMethod_from_request = request_method
#print("requestMethod_from_request: ",requestMethod_from_request)
match requestMethod_from_request:
case "GET":
# Call method for GET request
response = await client.get(url=final_url, headers=headers)
print("response: ",response)
# case "POST":
# # Call method for POST request
# ## if body is empty, skip the data field
# ## if body is not empty, use as below
# response = client.post(url=url_from_request)
# print("response: ",response)
# case "PUT":
# # Handle other request methods
# response = client.put(url=request.data.url, data=body_dict)
# case "PATCH":
# response = client.patch(url=request.data.url, data=body_dict)
# case "DELETE":
# response = client.delete(url=request.data.url)
if response.status_code >= 200 or response.status_code <= 204:
# Assuming the response contains JSON data, you can parse it
json_data = response.json()
print("json_data: ",json_data)
'''
users_array = json_data['users'][0]
users_json = json.dumps(users_array, indent=4)
print(users_json)
print("shreyas")
'''
json_data = json_data['users']
#print("json_data: ",json_data)
if isinstance(json_data, list) and len(json_data) > 0:
sample_record = json.dumps(json_data[0])
#sample_dict = {}
#sample_dict['primaryEmail'] = sample_record['primaryEmail']
print("sample_record: ",sample_record)
response_data = json_data[0]
except Exception as e:
print(e)
#return response_data
return JSONResponse(content=response_data, media_type="application/json")
@app.post('/generativeaisrvc/process_data')
async def process_data(request: Request, data: dict):
try:
tenant = "generativeAI"
input_collection = stored_input(tenant)
output_collection = stored_response(tenant)
#input_collection.insert_one(data)
input_collection.update_one(
{"appId": data["appId"]},
{"$set": data},
upsert=True
)
logging.debug("Input respone saved successfully")
print("data :",data)
def extract_info(json_data):
appId = json_data.get("appId")
body = ""
params = None
headers = None
try:
body = json.loads(json_data["schema"]["nodes"][0]["data"]["body"])
except:
print("body not found")
try:
headers = {header["key"]: header["value"] for header in json_data["schema"]["nodes"][0]["data"]["headers"]}
except:
print("headers not found")
url = json_data["schema"]["nodes"][0]["data"]["url"]
request_method = json_data["schema"]["nodes"][0]["data"]["requestMethod"]
try:
params_list = json_data["schema"]["nodes"][0]["data"]["params"]
params = {param["key"]: param["value"] for param in params_list if param["included"]}
except:
print("params not found")
print("appId: ",appId)
print("body: ",body)
print("headers: ",headers)
#print("url: ",url)
print("request_method: ",request_method)
print("params: ",params)
return appId, body, headers, url, request_method, params
appId, body, headers, url, request_method, params = extract_info(data)
final_url = url
if params:
final_url += "?" + "&".join(f"{key}={value}" for key,value in params.items())
print("final_url: ",final_url)
# Fetch JSON data from the specified URL using httpx for asynchronous requests
async with httpx.AsyncClient() as client:
#url_from_request = data["url"]
#print("url_from_request: ",url)
requestMethod_from_request = request_method
#print("requestMethod_from_request: ",requestMethod_from_request)
match requestMethod_from_request:
case "GET":
# Call method for GET request
response = await client.get(url=final_url, headers=headers)
print("response: ",response)
# case "POST":
# # Call method for POST request
# ## if body is empty, skip the data field
# ## if body is not empty, use as below
# response = client.post(url=url_from_request)
# print("response: ",response)
# case "PUT":
# # Handle other request methods
# response = client.put(url=request.data.url, data=body_dict)
# case "PATCH":
# response = client.patch(url=request.data.url, data=body_dict)
# case "DELETE":
# response = client.delete(url=request.data.url)
if response.status_code >= 200 or response.status_code <= 204:
# Assuming the response contains JSON data, you can parse it
json_data = response.json()
print("json_data: ",json_data)
'''
users_array = json_data['users'][0]
users_json = json.dumps(users_array, indent=4)
print(users_json)
print("shreyas")
'''
#json_data = json_data['users']
#print("json_data: ",json_data)
if isinstance(json_data, list) and len(json_data) > 0:
sample_record = json.dumps(json_data[0])
#sample_dict = {}
#sample_dict['primaryEmail'] = sample_record['primaryEmail']
print("sample_record: ",sample_record)
# Send the sample record to OpenAI for schema extraction
openai_url = 'https://cymetriopen.openai.azure.com/openai/deployments/instructionalmodel/completions?api-version=2023-09-15-preview'
openai_headers = {
'Content-Type': 'application/json',
'api-key': 'ebe64320148849aead404cc3aec9cc49'
}
openai_payload = {
"prompt": "Give me list of fields as jsonPath and labels and datatype and value in this json sample in json format only,keep all fields in lowercase only"+sample_record,
"max_tokens": 8000,
"temperature": 0.2,
"frequency_penalty": 0,
"presence_penalty": 0,
"top_p": 1,
"stop": None
}
try:
print("declaring async client")
async with httpx.AsyncClient(timeout=360) as openai_client:
print("start of openai request")
print(f'url is {openai_url}')
#print(f'headers are {openai_headers}')
#print(f'payload is {openai_payload}')
openai_response = ''
try:
openai_response = await openai_client.post(openai_url, headers=openai_headers, json=openai_payload,timeout=300)
except Exception as e:
print(f'openai response {openai_response}')
print(e)
finally:
print(f'openai response {openai_response}')
#openai_response.raise_for_status()
print(f'Respoinse freom openai is {openai_response}')
# Extract only the text part from the OpenAI response
response_text = openai_response.json()['choices'][0]['text']
print(f'response text is {response_text}')
#Parse the response_text into a list of dictionaries
response_data = json.loads(response_text)
print(f'response data is {response_data}')
logging.debug(f"Received response from OpenAI: {response_data}")
l1 = [item['label'] for item in response_data]
if isinstance(l1, str):
l1_list = convert_string_to_list(l1)
else:
l1_list = l1
l2 = ['Id', 'Displayname', 'Firstname', 'Lastname', 'Country', 'Mobile', 'Email', 'Status', 'Created', 'Updated', 'Created By', 'Updated By', 'Assignedgroups', 'Provisionedapps', 'Attributes', 'Rbacroles', 'Version', ' Class']
if isinstance(l2, str):
l2_list = convert_string_to_list(l2)
else:
l2_list = l2
threshold = 55
result = compare_lists_with_fuzzy(l1_list, l2_list, threshold)
final_response = generate_final_response(result['similar_elements'], response_data)
final_response_dict = {"final_response": final_response}
#output_collection.insert_one(final_response_dict)
final_response_dict['appId'] = appId
output_collection.update_one(
{"appId": appId},
{"$set": final_response_dict},
upsert=True
)
logging.debug("Final response saved successfully")
#print(f'final response is {final_response}')
return JSONResponse(content=final_response)
except Exception as errh:
print(f"HTTP Error {errh} is: {errh}")
#raise HTTPException(status_code=500, detail=f"HTTP Error: {errh}")
else:
return "No records found in the JSON data."
else:
raise HTTPException(status_code=response.status_code, detail=f"API call to fetch data failed with status code {response.status_code}")
except Exception as e:
#logging.error(f"An unexpected error occurred: {e}")
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=5000)