eval_w_subhypo_gen.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. import json
  2. import logging
  3. from openai import OpenAI
  4. from .lm_utils import run_chatgpt_query_multi_turn
  5. from .openai_helpers import get_response
  6. logging.basicConfig(
  7. format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
  8. datefmt='%m/%d/%Y %H:%M:%S',
  9. level=logging.INFO,
  10. )
  11. logger = logging.getLogger(__name__)
  12. def get_score_from_answer(type, answer):
  13. if type == 'context':
  14. answer = answer.replace('Answer:', '').strip()
  15. if answer.startswith('A)'):
  16. return 1.0
  17. elif answer.startswith('B)'):
  18. return 0.0
  19. return -1.0
  20. elif type == 'var':
  21. try:
  22. var_json = json.loads(answer)
  23. # print(f"var_json:{var_json}")
  24. p = 0.0
  25. r = 0.0
  26. f1 = 0.0
  27. if var_json['sizeB']:
  28. p = var_json['intersection'] / var_json['sizeB']
  29. if var_json['sizeA']:
  30. r = var_json['intersection'] / var_json['sizeA']
  31. if p > 0.0 and r > 0.0:
  32. f1 = (2 * p * r) / (p + r)
  33. else:
  34. f1 = 0.0
  35. eval_rec = {
  36. 'p': p,
  37. 'r': r,
  38. 'f1': f1,
  39. 'sizeA': var_json['sizeA'],
  40. 'sizeB': var_json['sizeB'],
  41. 'intersection': var_json['intersection'],
  42. 'explanation': var_json['explanation'],
  43. }
  44. print(f'var_eval: {eval_rec}')
  45. return eval_rec
  46. except Exception: # COMMENT: added Exception
  47. return {'p': -1.0, 'r': -1.0, 'f1': -1.0}
  48. elif type == 'rel':
  49. print(answer)
  50. rel_json = json.loads(answer)
  51. answer_str = rel_json['answer'].strip()
  52. if answer_str.startswith('A') or 'very similar' in answer_str:
  53. return 1.0
  54. elif (
  55. answer_str.startswith('B') or 'similar but general than HypoA' in answer_str
  56. ):
  57. return 0.5
  58. elif answer_str.startswith('C') or 'different' in answer_str:
  59. return 0.0
  60. return -1.0
  61. return -1.0
  62. def ask_dimension_question(
  63. query,
  64. gold_hypo,
  65. gold_workflow,
  66. gen_hypo,
  67. gen_workflow,
  68. dataset_meta,
  69. llm_used,
  70. dimension,
  71. dataset_type,
  72. use_column_metadata=True,
  73. ):
  74. dimension_question = ''
  75. answer = ''
  76. score = 0.0
  77. if dimension == 'var':
  78. score = {'p': -1.0, 'r': -1.0, 'f1': -1.0}
  79. num_tokens = 256
  80. num_retries = 1
  81. json_response = False
  82. messages = [
  83. {
  84. 'role': 'system',
  85. 'content': 'You are an AI assistant that helps evaluate a data-driven hypothesis. You are a helpful assistant who is not talkative. You only respond with the exact answer to a query without additional conversation.',
  86. },
  87. ]
  88. if dimension == 'context':
  89. dimension_question = """\
  90. Question: Is HypoB defined in the same context as HypoA?
  91. (Context refers to assumptions/stratification under which the hypotheses are defined.)
  92. Options: A) same B) different
  93. What is your answer?"""
  94. elif dimension == 'var':
  95. dimension_question = """\
  96. Question: For both HypoA and HypoB, what are the different variables found in the hypotheses? \
  97. Return your answer as a JSON object in the following format:
  98. ```json
  99. {{
  100. "sizeA": num of variables used in HypoA
  101. "sizeB": num of variables used in HypoB
  102. "intersection": num of variables common in HypoA and HypoB. Use *fuzzy matching* to determine intersection, accounting for paraphrases or slightly different surface forms
  103. "explanation": a short text explanation about the variables
  104. }}```
  105. Answer:"""
  106. num_tokens = 512
  107. num_retries = 1
  108. json_response = True
  109. elif dimension == 'rel':
  110. dimension_question = """\
  111. Question: Does HypoB exhibit the same relation as HypoA?
  112. Compare using following example hierarchy of relationships (based on specificity): \
  113. "there exists a relationship" > "positive relationship" > "positive AND (linear OR quadratic)" > "positive AND linear".
  114. Options: A) very similar B) similar but general than HypoA C) different
  115. Return your answer as a JSON object in the following format:
  116. ```json
  117. {{
  118. "answer": one of the options from A) very similar B) similar but general than HypoA C) different
  119. "explanation": a short text explanation about the relationship comparison
  120. }}```
  121. Answer:"""
  122. num_tokens = 512
  123. num_retries = 1
  124. json_response = True
  125. datasets_json = prepare_dataset_metadata_json(
  126. dataset_meta, dataset_type=dataset_type, use_column_metadata=use_column_metadata
  127. )
  128. dimension_question_str = f"""\
  129. You are going to compare two natural-language hypotheses HypoA and HypoB accompanied with optional workflows: WorkflowA for HypoA and WorkflowB for HypoB. \
  130. Both the hypotheses answer the natural language query "QUERY" over the dataset(s) described by dataset description(s) and column description(s) below. \
  131. Compare HypoA and HypoB in terms of three aspects: Contexts, Variables, and Relations. \
  132. E.g., for the hypothesis "From 1995 to 2009, the number of sandhill cranes around the tundra (Indigilka River) surged by an astounding ~10X":
  133. * Contexts refer to stratification of the data under which the given hypothesis is True. E.g., "For all women", "From 1995 to 2009".
  134. * Variables refer to the set of variables (either dependent or independent) that are mentioned in the hypothesis. E.g., number of sandhill cranes, location.
  135. * Relations refer to the form of relation between the variables. E.g., "surged by ~10x".
  136. Answer following questions for a given pair of hypotheses, HypoA and HypoB, along with an explanation grounded on the QUERY and the DATASET(S).
  137. Here is the metadata for the task:
  138. ```json
  139. {{
  140. "datasets": {datasets_json},
  141. "query": {query},
  142. "HypoA": {gold_hypo},
  143. "WorkflowA": {gold_workflow},
  144. "HypoB": {gen_hypo},
  145. "WorkflowB": {gen_workflow}
  146. }}
  147. ```
  148. {dimension_question}"""
  149. messages.append({'role': 'user', 'content': dimension_question_str})
  150. for retry in range(num_retries):
  151. response = run_chatgpt_query_multi_turn(
  152. messages=messages,
  153. model_name=llm_used,
  154. max_tokens=num_tokens,
  155. temperature=0, # 0 for greedy best decoding
  156. json_response=json_response,
  157. )
  158. if response is not None: # COMMENT: changed from != to is not
  159. break
  160. if response is not None: # COMMENT: changed from != to is not
  161. answer = response.choices[0].message.content.strip()
  162. score = get_score_from_answer(type=dimension, answer=answer)
  163. return dimension_question, answer, score
  164. def prepare_dataset_metadata_json(dataset_meta, dataset_type, use_column_metadata=True):
  165. if dataset_meta is None: # COMMENT: changed from == to is None
  166. return [
  167. {
  168. 'dataset_description': '',
  169. 'columns': [],
  170. }
  171. ]
  172. datasets_json = []
  173. if dataset_type == 'real':
  174. for d in dataset_meta['datasets']:
  175. datasets_json.append(
  176. {
  177. 'dataset_description': d['description'],
  178. 'columns': [
  179. {'name': col['name'], 'description': col['description']}
  180. for col in d['columns']['raw']
  181. ]
  182. if use_column_metadata
  183. else [],
  184. }
  185. )
  186. else:
  187. for d in dataset_meta['datasets']:
  188. datasets_json.append(
  189. {
  190. 'dataset_description': d['description'],
  191. 'columns': [
  192. {'name': col['name'], 'description': col['description']}
  193. for col in d['columns']
  194. ]
  195. if use_column_metadata
  196. else [],
  197. }
  198. )
  199. return datasets_json
  200. def get_sub_hypotheses(
  201. query,
  202. hypo,
  203. workflow,
  204. dataset_meta,
  205. llm_used,
  206. dataset_type,
  207. use_column_metadata=True,
  208. ):
  209. client = OpenAI()
  210. extraction_prompt = """\
  211. Given a set of dataset columns, a ground-truth hypothesis, and the analysis workflow used, your task is to extract three dimensions that define the hypothesis: Context, Variables, and Relations. \
  212. Here are the definitions for these dimensions:
  213. - Contexts: Boundary conditions that limit the scope of a hypothesis. E.g., “for men over \
  214. the age of 30”, “in Asia and Europe”. If the context applies to the full dataset, then extract the context from the dataset_descrption.
  215. - Variables: Known concepts that interact in a meaningful way under a given context to \
  216. produce the hypothesis. E.g., gender, age, income, or "None" if there is no interacting variable.
  217. - Relations: Interactions between a given set of variables under a given context to produce \
  218. the hypothesis. E.g., “quadratic relationship”, “inversely proportional”, piecewise conditionals, \
  219. or "None" if there is no interacting relationship.
  220. Make sure to only use the information present in the hypothesis and the workflow. Do not add any new information. \
  221. For each dimension, be specific, and do not omit any important details.
  222. Here is the metadata for the task:
  223. ```json
  224. {
  225. "datasets": %s,
  226. "hypothesis": "%s",
  227. "workflow": "%s"
  228. }
  229. ```
  230. Return your answer as a JSON object in the following format:
  231. ```json
  232. {
  233. "sub_hypo": [
  234. {
  235. "text": the hypothesis in natural language,
  236. "context": a short text description of the context of the hypothesis,
  237. "variables": a list of columns involved in the hypothesis,
  238. "relations": a short text description of the relationship between the variables of the hypothesis
  239. },
  240. ...
  241. ]
  242. }```
  243. """
  244. datasets_json = prepare_dataset_metadata_json(
  245. dataset_meta, dataset_type, use_column_metadata=use_column_metadata
  246. )
  247. _prompt = extraction_prompt % (datasets_json, hypo, workflow)
  248. sub_hypo_json = get_response(client, _prompt, model=llm_used, max_retry=1)
  249. if sub_hypo_json is not None: # COMMENT: changed from != to is not
  250. # print(f"full hypothesis: {hypo}")
  251. print(f'sub_hypo_json: {sub_hypo_json}')
  252. else:
  253. sub_hypo_json = {
  254. 'sub_hypo': [],
  255. }
  256. sub_hypo_json['full_hypo'] = hypo
  257. return sub_hypo_json
  258. def match_context_with_gpt(
  259. gold_hyp, gold_context, pred_hyp, pred_context, model='gpt-3.5-turbo'
  260. ):
  261. prompt = f"""\
  262. Given a gold hypothesis, a gold context, a predicted hypothesis, and a predicted context, your task is \
  263. to determine if the predicted context semantically matches the ground-truth context. \
  264. Here is the definition for Context: Boundary conditions that limit the scope of a sub-hypothesis. E.g., “for men over the age of 30”, “in Asia and Europe”. If the context applies to the full dataset, then the context is derived from the dataset_descrption. \
  265. Here is the definition for Context: Boundary conditions that limit the scope of a sub-hypothesis. E.g., “for men over the age of 30”, “in Asia and Europe”. If the context applies to the full dataset, then the context is derived from the dataset_descrption. \
  266. If the predicted context matches the gold context, return true, otherwise return false.
  267. If both gold and predicted hypotheses are defined over the context of the full dataset, then also return true.
  268. If both gold and predicted hypotheses are defined over the context of the full dataset, then also return true.
  269. Here is the metadata for the task:
  270. ```json
  271. {{
  272. "gold_hypothesis": "{gold_hyp}",
  273. "gold_context": "{gold_context}",
  274. "predicted_hypothesis": "{pred_hyp}",
  275. "predicted_context": "{pred_context}"
  276. }}
  277. ```
  278. Return your answer as a JSON object in the following format:
  279. ```json
  280. {{
  281. "match": true or false
  282. }}
  283. ```"""
  284. client = OpenAI()
  285. output = get_response(client, prompt, model=model)
  286. return output.get('match', False)
  287. def is_matching_context(gold_hyp, gold_context, pred_hyp, pred_context, llm_used):
  288. if gold_context == pred_context:
  289. return True
  290. if 'None' in [gold_context, pred_context]:
  291. return False
  292. return match_context_with_gpt(
  293. gold_hyp, gold_context, pred_hyp, pred_context, model=llm_used
  294. )
  295. def run_eval_gold_vs_gen_NL_subhypo(
  296. query,
  297. gold_hypo,
  298. gold_workflow,
  299. gen_hypo,
  300. gen_workflow,
  301. dataset_meta,
  302. llm_used,
  303. context_score,
  304. dataset_type,
  305. use_column_metadata=True,
  306. ):
  307. # GPT-4 based evaluation to evaluate generated hypothesis in terms of context, variables, relation
  308. eval_rec = {
  309. 'query': query,
  310. 'HypoA': gold_hypo,
  311. 'WorkflowA': gold_workflow,
  312. 'HypoB': gen_hypo,
  313. 'WorkflowB': gen_workflow,
  314. }
  315. for dimension in ['var', 'rel']:
  316. question, answer, score = ask_dimension_question(
  317. query,
  318. gold_hypo,
  319. gold_workflow,
  320. gen_hypo,
  321. gen_workflow,
  322. dataset_meta,
  323. llm_used,
  324. dimension=dimension,
  325. dataset_type=dataset_type,
  326. use_column_metadata=use_column_metadata,
  327. )
  328. eval_rec[dimension] = {'question': question, 'answer': answer, 'score': score}
  329. eval_rec['context'] = context_score
  330. eval_rec['accuracy_score'] = (
  331. 1.0
  332. * eval_rec['context']['score']
  333. * eval_rec['var']['score']['f1']
  334. * eval_rec['rel']['score']
  335. )
  336. return eval_rec
  337. def run_eval_gold_vs_gen_NL_hypo_workflow(
  338. query,
  339. gold_hypo,
  340. gold_workflow,
  341. gen_hypo,
  342. gen_workflow,
  343. dataset_meta,
  344. llm_used,
  345. dataset_type,
  346. use_column_metadata=True,
  347. ):
  348. # Input: Dataset Metadata, Query, Gold {Hg, Wg}, Predicted {Hp, Wp}
  349. # Output: eval_rec json includes final_score
  350. # Procedure:
  351. # Dataset Metadata, Query, Gold {Hg, Wg}, Pred {Hg, Wg}
  352. # Gold: [Hg1, Hg2] (compute on the fly) Hg1 is a NL form of subhypothesis
  353. # Predicted: [Hp1, Hp2] (compute on the fly)
  354. # Compute Intersection: [(Hg_i, Hp_j), …] # tuples of (gold,pred) that matched with context (do this w/o explicit extraction)
  355. # # filter so that a gold context and a predicted context are only attached to one tuple
  356. # Compute recall_context (programmatically)
  357. # r_v_list = []
  358. # For (Hg_i, Hp_j) in the intersection:
  359. # With Hg_i, Hp_j in NL, ask GPT4 → #variables and #intersection and a paragraph explanation and programmatically calculate f1_v
  360. # Hg_i, Hp_j in NL, ask GPT4 → matching score (0, 0.5 or 1) : A) very similar B) similar but general than HypoA C) different + explanation
  361. # r_v_list ← f1_v * score_r
  362. # accuracy_score = mean(r_v_list)
  363. # score = [ recall_context * mean over predicted context(context_score * var_score *rel_score )]
  364. # recall_context = 1.0 # COMMENT: never used
  365. eval_rec = {
  366. 'query': query,
  367. 'HypoA': gold_hypo,
  368. 'WorkflowA': gold_workflow,
  369. 'HypoB': gen_hypo,
  370. 'WorkflowB': gen_workflow,
  371. }
  372. gold_sub_hypo_json = get_sub_hypotheses(
  373. query=query,
  374. hypo=gold_hypo,
  375. workflow=gold_workflow,
  376. dataset_meta=dataset_meta,
  377. llm_used=llm_used,
  378. dataset_type=dataset_type,
  379. use_column_metadata=use_column_metadata,
  380. )
  381. if len(gold_sub_hypo_json['sub_hypo']) == 0:
  382. gold_sub_hypo_json['sub_hypo'] = [
  383. {
  384. 'text': gold_hypo,
  385. 'context': 'None',
  386. 'variables': [],
  387. 'relations': '',
  388. 'explanation': 'unable to segment',
  389. }
  390. ]
  391. print(f'gold_sub_hypo_json: {gold_sub_hypo_json}')
  392. gen_sub_hypo_json = get_sub_hypotheses(
  393. query=query,
  394. hypo=gen_hypo,
  395. workflow=gen_workflow,
  396. dataset_meta=dataset_meta,
  397. llm_used=llm_used,
  398. dataset_type=dataset_type,
  399. use_column_metadata=use_column_metadata,
  400. )
  401. if len(gen_sub_hypo_json['sub_hypo']) == 0:
  402. gen_sub_hypo_json['sub_hypo'] = [
  403. {
  404. 'text': gen_hypo,
  405. 'context': 'None',
  406. 'variables': [],
  407. 'relations': '',
  408. 'explanation': 'unable to segment',
  409. }
  410. ]
  411. print(f'gen_sub_hypo_json: {gen_sub_hypo_json}')
  412. eval_rec['gold_sub_hypo'] = gold_sub_hypo_json
  413. eval_rec['gen_sub_hypo'] = gen_sub_hypo_json
  414. gold_subh_covered = []
  415. gen_subh_to_gold_subh = dict()
  416. gen_gold_subh_to_context = dict()
  417. for p_id, gen_subh in enumerate(gen_sub_hypo_json['sub_hypo']):
  418. gen_subh_to_gold_subh[p_id] = -1
  419. for g_id, gold_subh in enumerate(gold_sub_hypo_json['sub_hypo']):
  420. if g_id in gold_subh_covered:
  421. continue
  422. # match context
  423. context_bool = is_matching_context(
  424. gold_subh['text'],
  425. gold_subh.get('context', ''),
  426. gen_subh['text'],
  427. gen_subh.get('context', ''),
  428. llm_used,
  429. )
  430. if context_bool:
  431. context_score = 1.0
  432. else:
  433. context_score = 0.0
  434. if context_score == 1.0: # match only when context_score = 1.0
  435. gen_subh_to_gold_subh[p_id] = g_id
  436. gold_subh_covered.append(g_id)
  437. gen_gold_subh_to_context[f'P{p_id}||G{g_id}'] = {
  438. 'question': f"""Comapring: GoldH: {gold_subh["text"]}, GoldC: {gold_subh['context']}\nGenH: {gen_subh['text']}, GenC: {gen_subh['context']}""",
  439. 'answer': context_bool,
  440. 'score': context_score,
  441. }
  442. break
  443. print(f'gen_subh_to_gold_subh: {gen_subh_to_gold_subh}')
  444. eval_rec['gen_subh_to_gold_subh'] = gen_subh_to_gold_subh
  445. eval_rec['gold_subh_covered'] = gold_subh_covered
  446. matched_gold_gen_subh_evals = dict()
  447. sum_accuracy_score = 0.0
  448. for p_id, g_id in gen_subh_to_gold_subh.items():
  449. if g_id >= 0:
  450. key = f'P{p_id}||G{g_id}'
  451. context_score = gen_gold_subh_to_context[key]
  452. subh_eval_rec = run_eval_gold_vs_gen_NL_subhypo(
  453. query,
  454. gold_hypo,
  455. gold_workflow,
  456. gen_hypo,
  457. gen_workflow,
  458. dataset_meta,
  459. llm_used,
  460. context_score,
  461. dataset_type=dataset_type,
  462. use_column_metadata=use_column_metadata,
  463. )
  464. sum_accuracy_score += subh_eval_rec['accuracy_score']
  465. matched_gold_gen_subh_evals[key] = subh_eval_rec
  466. eval_rec['matched_gold_gen_subh_evals'] = matched_gold_gen_subh_evals
  467. eval_rec['recall_context'] = (
  468. len(gold_subh_covered) / len(gold_sub_hypo_json['sub_hypo'])
  469. if len(gold_sub_hypo_json['sub_hypo'])
  470. else 0.0
  471. )
  472. mean_accuracy_score = (
  473. sum_accuracy_score / len(gen_subh_to_gold_subh)
  474. if len(gen_subh_to_gold_subh)
  475. else 0.0
  476. )
  477. eval_rec['mean_accuracy_score'] = mean_accuracy_score
  478. final_score = eval_rec['recall_context'] * mean_accuracy_score
  479. eval_rec['final_score'] = final_score
  480. print(f'eval_rec: {json.dumps(eval_rec, indent=2)}')
  481. return eval_rec