From 8c3c674e75cbd52e2868e22fdb5e263b485ae6bb Mon Sep 17 00:00:00 2001 From: Katon Minhas Date: Mon, 6 Jan 2025 21:08:56 +0000 Subject: [PATCH] Merged in feature/daip2-33-optimize-exhibit-start-prompt (pull request #343) Feature/daip2 33 optimize exhibit start prompt * Updated Exhibit Check prompt * Updated prompt, reorg * Switch to claude 3 haiku for new exhibit check prompt Approved-by: Alex Galarce --- fieldExtraction/src/one_to_n_funcs.py | 12 --------- fieldExtraction/src/preprocess.py | 2 +- fieldExtraction/src/preprocessing_funcs.py | 13 +++++++++ fieldExtraction/src/prompts.py | 31 +++++++++++++--------- 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/fieldExtraction/src/one_to_n_funcs.py b/fieldExtraction/src/one_to_n_funcs.py index 7c42cf0..d125763 100644 --- a/fieldExtraction/src/one_to_n_funcs.py +++ b/fieldExtraction/src/one_to_n_funcs.py @@ -369,15 +369,3 @@ def run_top_down(filename, text_dict, bu_results, exhibit_pages): final_dicts = add_medically_necessary(final_dicts, medically_necessary_dict) return final_dicts - - -def get_exhibit_pages(text_dict, filename): - exhibit_pages = [] - for page_num, page in text_dict.items(): - prompt = prompts.TOP_DOWN_EXHIBIT_CHECK(page[0:100]) - answer = claude_funcs.invoke_claude( - prompt, config.MODEL_ID_CLAUDE2, filename, max_tokens=10 - ) - if "Y" in answer: - exhibit_pages.append(page_num) - return exhibit_pages \ No newline at end of file diff --git a/fieldExtraction/src/preprocess.py b/fieldExtraction/src/preprocess.py index a0fbdf2..26c1da8 100644 --- a/fieldExtraction/src/preprocess.py +++ b/fieldExtraction/src/preprocess.py @@ -21,7 +21,7 @@ def preprocess(contract_text, filename, fields=config.FIELDS): text_dict, top_sheet_dict = preprocessing_funcs.filter_quick_review(text_dict) if fields == "b": - exhibit_pages = one_to_n_funcs.get_exhibit_pages( + exhibit_pages = preprocessing_funcs.get_exhibit_pages( text_dict, filename ) # All pages with exhibit headers text_dict = table_funcs.align_and_format_tables(text_dict, filename) diff --git a/fieldExtraction/src/preprocessing_funcs.py b/fieldExtraction/src/preprocessing_funcs.py index 0f43adc..e475366 100644 --- a/fieldExtraction/src/preprocessing_funcs.py +++ b/fieldExtraction/src/preprocessing_funcs.py @@ -10,6 +10,7 @@ import smart_chunking_funcs import string_funcs import valid from valid import DERIVED_INDICATOR_FIELDS +from regex_patterns import PIPE_PATTERN def clean_newlines(contract_text: str) -> str: @@ -299,3 +300,15 @@ def filter_quick_review(text_dict): or "COVER SHEET" in page_text.upper() } +def get_exhibit_pages(text_dict, filename): + exhibit_pages = [] + for page_num, page in text_dict.items(): + prompt = prompts.EXHIBIT_CHECK(page[0:100]) + claude_answer_raw = claude_funcs.invoke_claude( + prompt, config.MODEL_ID_CLAUDE3_HAIKU, filename, max_tokens=10 + ) + claude_answer_extracted = re.findall(pattern=PIPE_PATTERN, string=claude_answer_raw)[-1] + if "Y" in claude_answer_extracted: + exhibit_pages.append(page_num) + return exhibit_pages + diff --git a/fieldExtraction/src/prompts.py b/fieldExtraction/src/prompts.py index 9d67b34..487daf3 100644 --- a/fieldExtraction/src/prompts.py +++ b/fieldExtraction/src/prompts.py @@ -211,18 +211,6 @@ TD_PRIMARY_FIELDS = [ ] -def TOP_DOWN_EXHIBIT_CHECK(page_start): - return f"""Text: {page_start} - -The above text is the start of a page. -Return 'Y' ONLY if the text contains the header of an Exhibit, Article, Amendment, Schedule, Attachment, or Addendum. - -'Additional Provisions' is not an Exhibit name. - -Otherwise, return 'N'. -""" - - def TOP_DOWN_EXCLUSIONS(page): return f"""### PAGE START ### {page} ### PAGE END @@ -363,6 +351,25 @@ Return only the Line of Business answer, without using complete sentences. ##################################################################################### ############################# PRE- AND POST-PROCESSING ############################## ##################################################################################### +def EXHIBIT_CHECK(page_start): + return f""" +Analyze the following contract excerpt and determine if it indicates the start of a section. Follow the following instructions: +1. Look specifically for the words 'Exhibit', 'Article', 'Amendment', 'Schedule', 'Attachment', or 'Addendum'. These words indicate the start of a new section. +2. If the above words are found in the context of a longer sentence or statement, this does not by itself indicate the start of a section. Section starts will be formatted like a title or header. +3. The phrase 'Additional Provisions' does not indicate the start of a section. +4. Return Y if the excerpt indicates the start of a section, N if not. Enclose your final answer in |pipes| + +Here is the text to analyze: + +## START CONTRACT TEXT ## +{page_start} +## END CONTRACT TEXT ## + +State your answer according to the instructions above. +""" + + + def FIX_JSON(d): return f"""dict: {d}