Spaces:
Sleeping
Sleeping
File size: 14,971 Bytes
02b5c87 |
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 |
import ast
import re
from collections import OrderedDict
available_tools = [
"works_list",
"summarize_objects",
"prioritize_objects",
"add_work_items_to_sprint",
"get_sprint_id",
"get_similar_work_items",
"search_object_by_name",
"create_actionable_tasks_from_text",
"who_am_i",
"is_empty",
"count",
"works-create",
"works-delete",
"works-update",
"rev-orgs-create",
"rev-orgs-delete",
"rev-orgs-update",
"get_works_id",
"get_current_date"
]
available_arguments = [
"works_list/applies_to_part",
"works_list/created_by",
"works_list/issue.priority",
"works_list/issue.rev_orgs",
"works_list/limit",
"works_list/owned_by",
"works_list/stage.name",
"works_list/ticket.needs_response",
"works_list/ticket.rev_org",
"works_list/ticket.severity",
"works_list/ticket.source_channel",
"works_list/type",
"works_list/date_of_creation",
"works_list/last_modified",
"works_list/target_close_date",
"works_list/sprint",
"summarize_objects/objects",
"prioritize_objects/objects",
"add_work_items_to_sprint/work_ids",
"add_work_items_to_sprint/sprint_id",
"get_sprint_id/",
"get_similar_work_items/work_id",
"search_object_by_name/query",
"create_actionable_tasks_from_text/text",
"who_am_i/",
"is_empty/list_to_check",
"count/objects",
"works-create/applies_to_part",
"works-create/created_by",
"works-create/issue.priority",
"works-create/developed_with",
"works-create/owned_by",
"works-create/stage.name",
"works-create/sprint",
"works-create/type",
"works-create/target_close_date",
"works-create/title",
"works-delete/id",
"works-update/id",
"works-update/applies_to_part",
"works-update/created_by",
"works-update/owned_by",
"works-update/stage.name",
"works-update/type",
"works-update/target_close_date",
"works-update/title",
"works-update/priority",
"rev-orgs-create/description",
"rev-orgs-create/display_name",
"rev-orgs-create/environment",
"rev-orgs-delete/id",
"rev-orgs-update/description",
"rev-orgs-update/display_name",
"rev-orgs-update/environment",
"rev-orgs-update/id",
"get_works_id/objects",
"get_current_date/"
]
arg_allowed_values_dict = {'works-update/priority': ['p0', 'p1', 'p2', 'p3'],
'works-update/type': ['issue', 'task', 'ticket'],
'works_list/issue.priority': ['p0', 'p1', 'p2', 'p3'],
'works_list/ticket.needs_response': ['true', 'false'],
'works_list/ticket.severity': ['blocker', 'low', 'medium', 'high'],
'works_list/type': ['issue', 'task', 'ticket'],
'works-create/issue.priority': ['p0', 'p1', 'p2', 'p3'],
'works-create/type': ['issue', 'task', 'ticket'],
'works-create/title': ['issue', 'ticket']}
args_in_list_dict = {
'works_list/applies_to_part': 1,
'works_list/created_by': 1,
'works_list/issue.priority': 1,
'works_list/issue.rev_orgs': 1,
'works_list/limit': 0,
'works_list/owned_by': 1,
'works_list/stage.name': 1,
'works_list/ticket.needs_response': 0,
'works_list/ticket.rev_org': 1,
'works_list/ticket.severity': 1,
'works_list/ticket.source_channel': 1,
'works_list/type': 1,
'works_list/date_of_creation': 0,
'works_list/last_modified': 0,
'works_list/target_close_date': 0,
'works_list/sprint': 1,
'summarize_objects/objects': 1,
'prioritize_objects/objects': 1,
'add_work_items_to_sprint/work_ids': 1,
'add_work_items_to_sprint/sprint_id': 0,
'get_similar_work_items/work_id': 0,
'search_object_by_name/query': 0,
'create_actionable_tasks_from_text/text': 0,
'is_empty/list_to_check': 0,
'count/objects': 1,
'works-create/applies_to_part': 1,
'works-create/created_by': 1,
'works-create/issue.priority': 1,
'works-create/developed_with': 1,
'works-create/owned_by': 1,
'works-create/stage.name': 1,
'works-create/sprint': 1,
'works-create/type': 1,
'works-create/target_close_date': 0,
'works-create/title': 0,
'works-delete/id': 1,
'works-update/id': 1,
'works-update/applies_to_part': 1,
'works-update/created_by': 1,
'works-update/owned_by': 1,
'works-update/stage.name': 1,
'works-update/type': 1,
'works-update/target_close_date': 0,
'works-update/title': 0,
'works-update/priority': 1,
'rev-orgs-create/description': 0,
'rev-orgs-create/display_name': 0,
'rev-orgs-create/environment': 0,
'rev-orgs-delete/id': 0,
'rev-orgs-update/description': 0,
'rev-orgs-update/display_name': 0,
'rev-orgs-update/environment': 0,
'rev-orgs-update/id': 0,
'get_works_id/objects': 1,
'get_current_date/': 0
}
def find_hallucinations(json_response, arg_allowed_values_dict, available_tools, available_arguments, args_in_list_dict):
# check errors in names of tools and arguments
for i, item in enumerate(json_response):
print(item)
if 'tool_name' not in item:
if type(item) is dict:
first_key = list(item.keys())[0]
first_value = item.pop(first_key)
# Create a new dictionary with 'tool_name' and the rest of the key-value pairs
new_item = {'tool_name': first_value, **item}
json_response[i] = new_item
else:
item = 'tool_name'
tool_names = [item['tool_name'] for item in json_response]
print(f"tool_names:{tool_names}")
print(f"available_tools:{available_tools}")
print(f"available_arguments:{available_arguments}")
valid_tools = [tool_name for tool_name in tool_names if tool_name in available_tools]
print(f"valid_tools:{valid_tools}")
hallucinated_tools = [tool_name for tool_name in tool_names if tool_name not in available_tools]
print(f"hallucinated_tools:{hallucinated_tools}")
argument_names = []
for item in json_response:
for key in item:
if item[key] in valid_tools:
# try:
arguments = item.get("arguments", [])
for argument in arguments:
argument_name = argument.get("argument_name")
if argument_name:
argument_names.append(item["tool_name"]+"/"+argument_name)
# except AttributeError:
# pass
# valid_args = [arg_name for arg_name in argument_names if arg_name in merged_arguments]
hallucinated_args = [arg_name for arg_name in argument_names if arg_name not in available_arguments]
# check the validity of argument values using allowed_arg_values_dict
json_args_dict = {}
for item in json_response:
for key in item:
if item[key] in available_tools:
# try:
arguments = item.get("arguments", [])
for argument in arguments:
argument_name = argument.get("argument_name")
if argument_name:
json_args_dict[item["tool_name"]+"/"+argument_name] = argument["argument_value"]
concat_arg = item["tool_name"] + "/" + argument_name
argument_names.append(concat_arg)
# try:
if args_in_list_dict[concat_arg] == 1: ## fixes the arguments that are supposed to be in a list format but are not
arg_val = argument.get("argument_value")
if type(arg_val) is not list:
l = []
l.append(arg_val)
argument["argument_value"] = l
# except KeyError:
# pass
# except AttributeError:
# pass
hallucinated_args_values_prev = []
for idx, item in enumerate(json_response):
for key in item:
if item[key] in available_tools:
# try:
arguments = item.get("arguments", [])
for argument in arguments:
argument_name = argument.get("argument_name")
if argument_name:
json_args_dict[item["tool_name"]+"/"+argument_name] = argument["argument_value"]
# print("argument value ", argument['argument_value'])
# try:
print("argument value", argument['argument_value'])
arg_list = ast.literal_eval(argument['argument_value'])
for arg in arg_list:
# print("arument 2, ", arg)
if ("$$PREV" in arg and int(arg.split("[")[1].split("]")[0])):
hallucinated_args_values_prev.append((item["tool_name"] + "/" + argument_name, arg))
# except:
# pass
# except AttributeError:
# pass
hallucinated_args_values = []
for arg_name, arg_value in json_args_dict.items():
if arg_name in arg_allowed_values_dict:
if type(arg_value) is not list:
if arg_value not in arg_allowed_values_dict[arg_name]:
hallucinated_args_values.append((arg_name, arg_value))
else:
for i in arg_value:
if i not in arg_allowed_values_dict[arg_name]:
hallucinated_args_values.append((arg_name, i))
return hallucinated_args, hallucinated_tools, hallucinated_args_values, hallucinated_args_values_prev
def correction(hallucinated_args, hallucinated_args_values, hallucinated_tools, hallucinated_args_values_prev, json_response):
Correction_prompt = ''
Correction_prompt += f'These are the following errors in your previous json response \n {json_response} \n'
for i in hallucinated_args:
Correction_prompt += f"The argument {i} is used but is not present in the provided API list. \n"
for i in hallucinated_tools:
Correction_prompt += f"The tool {i} is used but is not present in the provided API list. \n"
for i, j in hallucinated_args_values:
Correction_prompt += f"Argument_value '{j}' for argument '{i}' is not valid according to the specified API list. \n"
for i, j in hallucinated_args_values_prev:
Correction_prompt += f"Argument_value '{j}' for argument '{i}' is not valid since it refers to an item in the API list that does not come before it.\n"
Correction_prompt += "You have to give the corrected solution to the product manager's query by modifying the provided JSON. You have to remove hallucinations and only output the corrected JSON. \n"
return Correction_prompt
def correction_if_wrong_schema(exception, json_response):
Correction_prompt = 'The json you extracted in your response does not follow the appropriate json schema' + '\n'
Correction_prompt += f'Upon extracting your json response : \n {json_response} \n'
Correction_prompt += f'The following error is encountered : {exception}' + '\n'
Correction_prompt += "You have to give the corrected solution to the product manager's query by modifying the provided JSON. You have to remove the error and only output the corrected JSON."
return Correction_prompt
def placeholder_check(json_response):
argument_names = []
if type(json_response) is dict:
l = []
l.append(json_response)
json_response = l
for item in json_response:
# try:
arguments = item.get("arguments", [])
for argument in arguments:
# try:
argument_name = argument.get("argument_name", [])
argument_value = argument["argument_value"]
x = re.search("<.*>", str(argument_value))
if x:
return 1
# except KeyError:
# return 1
# except AttributeError:
# return 0
return 0
def unsolvable_check(json_response):
for item in json_response:
for key in item:
if type(item[key]) is list:
return 0
arg = item[key]
x = re.search(".*cannot.*", arg)
if x:
return 1
return 0
def complexity(output):
num_tools = 0
num_args = 0
tool_wt = 2
args_wt = 0.5
for tool in output:
num_tools += 1
arguments = tool.get("arguments", [])
for argument in arguments:
num_args += 1
return num_args*args_wt + num_tools*tool_wt
def structure_check(json_response):
if type(json_response) is dict:
l = []
l.append(json_response)
json_response = l
for i, item in enumerate(json_response): #fixing tool_name issue
if 'tool_name' not in item:
if type(item) is dict:
first_key = list(item.keys())[0]
first_value = item.pop(first_key)
# Create a new dictionary with 'tool_name' and the rest of the key-value pairs
new_item = {'tool_name': first_value, **item}
json_response[i] = new_item
else:
item = 'tool_name'
restructured_json = []
for item in json_response:
d = {}
d['tool_name'] = item['tool_name']
x=[]
try:
for args in item['arguments']:
d_ = {}
d_['argument_name'] = args['argument_name']
d_['argument_value'] = args['argument_value']
x.append(d_)
d['arguments'] = x
restructured_json.append(d)
except (KeyError, TypeError) as e:
if 'arguments' in item:
if type(item['arguments']) is not list:
x = []
x.append(item['arguments'])
item['arguments'] = x
# print(item['arguments'])]
for args in item['arguments']:
# print(args)
x_ = []
n = {}
keys = list(args.keys())
# print(keys)
for j in keys:
d_ = {}
d_['argument_name'] = j
d_['argument_value'] = item['arguments'][0][j]
# print(d_)
x_.append(d_)
n['tool_name'] = item['tool_name']
n['arguments'] = x_
restructured_json.append(n)
else:
pass
print(restructured_json)
return restructured_json
|