@@ -36,7 +36,7 @@ async def check_title_appearance(item, page_list, start_index=1, model=None):
3636 }}
3737 Directly return the final JSON structure. Do not output anything else."""
3838
39- response = await ChatGPT_API_async (model = model , prompt = prompt )
39+ response = await llm_acompletion (model = model , prompt = prompt )
4040 response = extract_json (response )
4141 if 'answer' in response :
4242 answer = response ['answer' ]
@@ -64,7 +64,7 @@ async def check_title_appearance_in_start(title, page_text, model=None, logger=N
6464 }}
6565 Directly return the final JSON structure. Do not output anything else."""
6666
67- response = await ChatGPT_API_async (model = model , prompt = prompt )
67+ response = await llm_acompletion (model = model , prompt = prompt )
6868 response = extract_json (response )
6969 if logger :
7070 logger .info (f"Response: { response } " )
@@ -116,7 +116,7 @@ def toc_detector_single_page(content, model=None):
116116 Directly return the final JSON structure. Do not output anything else.
117117 Please note: abstract,summary, notation list, figure list, table list, etc. are not table of contents."""
118118
119- response = ChatGPT_API (model = model , prompt = prompt )
119+ response = llm_completion (model = model , prompt = prompt )
120120 # print('response', response)
121121 json_content = extract_json (response )
122122 return json_content ['toc_detected' ]
@@ -135,7 +135,7 @@ def check_if_toc_extraction_is_complete(content, toc, model=None):
135135 Directly return the final JSON structure. Do not output anything else."""
136136
137137 prompt = prompt + '\n Document:\n ' + content + '\n Table of contents:\n ' + toc
138- response = ChatGPT_API (model = model , prompt = prompt )
138+ response = llm_completion (model = model , prompt = prompt )
139139 json_content = extract_json (response )
140140 return json_content ['completed' ]
141141
@@ -153,7 +153,7 @@ def check_if_toc_transformation_is_complete(content, toc, model=None):
153153 Directly return the final JSON structure. Do not output anything else."""
154154
155155 prompt = prompt + '\n Raw Table of contents:\n ' + content + '\n Cleaned Table of contents:\n ' + toc
156- response = ChatGPT_API (model = model , prompt = prompt )
156+ response = llm_completion (model = model , prompt = prompt )
157157 json_content = extract_json (response )
158158 return json_content ['completed' ]
159159
@@ -165,7 +165,7 @@ def extract_toc_content(content, model=None):
165165
166166 Directly return the full table of contents content. Do not output anything else."""
167167
168- response , finish_reason = ChatGPT_API_with_finish_reason (model = model , prompt = prompt )
168+ response , finish_reason = llm_completion (model = model , prompt = prompt , return_finish_reason = True )
169169
170170 if_complete = check_if_toc_transformation_is_complete (content , response , model )
171171 if if_complete == "yes" and finish_reason == "finished" :
@@ -176,7 +176,7 @@ def extract_toc_content(content, model=None):
176176 {"role" : "assistant" , "content" : response },
177177 ]
178178 prompt = f"""please continue the generation of table of contents , directly output the remaining part of the structure"""
179- new_response , finish_reason = ChatGPT_API_with_finish_reason (model = model , prompt = prompt , chat_history = chat_history )
179+ new_response , finish_reason = llm_completion (model = model , prompt = prompt , chat_history = chat_history , return_finish_reason = True )
180180 response = response + new_response
181181 if_complete = check_if_toc_transformation_is_complete (content , response , model )
182182
@@ -193,7 +193,7 @@ def extract_toc_content(content, model=None):
193193 {"role" : "assistant" , "content" : response },
194194 ]
195195 prompt = f"""please continue the generation of table of contents , directly output the remaining part of the structure"""
196- new_response , finish_reason = ChatGPT_API_with_finish_reason (model = model , prompt = prompt , chat_history = chat_history )
196+ new_response , finish_reason = llm_completion (model = model , prompt = prompt , chat_history = chat_history , return_finish_reason = True )
197197 response = response + new_response
198198 if_complete = check_if_toc_transformation_is_complete (content , response , model )
199199
@@ -215,7 +215,7 @@ def detect_page_index(toc_content, model=None):
215215 }}
216216 Directly return the final JSON structure. Do not output anything else."""
217217
218- response = ChatGPT_API (model = model , prompt = prompt )
218+ response = llm_completion (model = model , prompt = prompt )
219219 json_content = extract_json (response )
220220 return json_content ['page_index_given_in_toc' ]
221221
@@ -264,7 +264,7 @@ def toc_index_extractor(toc, content, model=None):
264264 Directly return the final JSON structure. Do not output anything else."""
265265
266266 prompt = toc_extractor_prompt + '\n Table of contents:\n ' + str (toc ) + '\n Document pages:\n ' + content
267- response = ChatGPT_API (model = model , prompt = prompt )
267+ response = llm_completion (model = model , prompt = prompt )
268268 json_content = extract_json (response )
269269 return json_content
270270
@@ -292,15 +292,20 @@ def toc_transformer(toc_content, model=None):
292292 Directly return the final JSON structure, do not output anything else. """
293293
294294 prompt = init_prompt + '\n Given table of contents\n :' + toc_content
295- last_complete , finish_reason = ChatGPT_API_with_finish_reason (model = model , prompt = prompt )
295+ last_complete , finish_reason = llm_completion (model = model , prompt = prompt , return_finish_reason = True )
296296 if_complete = check_if_toc_transformation_is_complete (toc_content , last_complete , model )
297297 if if_complete == "yes" and finish_reason == "finished" :
298298 last_complete = extract_json (last_complete )
299299 cleaned_response = convert_page_to_int (last_complete ['table_of_contents' ])
300300 return cleaned_response
301301
302302 last_complete = get_json_content (last_complete )
303+ attempt = 0
304+ max_attempts = 5
303305 while not (if_complete == "yes" and finish_reason == "finished" ):
306+ attempt += 1
307+ if attempt > max_attempts :
308+ raise Exception ('Failed to complete toc transformation after maximum retries' )
304309 position = last_complete .rfind ('}' )
305310 if position != - 1 :
306311 last_complete = last_complete [:position + 2 ]
@@ -316,7 +321,7 @@ def toc_transformer(toc_content, model=None):
316321
317322 Please continue the json structure, directly output the remaining part of the json structure."""
318323
319- new_complete , finish_reason = ChatGPT_API_with_finish_reason (model = model , prompt = prompt )
324+ new_complete , finish_reason = llm_completion (model = model , prompt = prompt , return_finish_reason = True )
320325
321326 if new_complete .startswith ('```json' ):
322327 new_complete = get_json_content (new_complete )
@@ -477,7 +482,7 @@ def add_page_number_to_toc(part, structure, model=None):
477482 Directly return the final JSON structure. Do not output anything else."""
478483
479484 prompt = fill_prompt_seq + f"\n \n Current Partial Document:\n { part } \n \n Given Structure\n { json .dumps (structure , indent = 2 )} \n "
480- current_json_raw = ChatGPT_API (model = model , prompt = prompt )
485+ current_json_raw = llm_completion (model = model , prompt = prompt )
481486 json_result = extract_json (current_json_raw )
482487
483488 for item in json_result :
@@ -499,7 +504,7 @@ def remove_first_physical_index_section(text):
499504 return text
500505
501506### add verify completeness
502- def generate_toc_continue (toc_content , part , model = "gpt-4o-2024-11-20" ):
507+ def generate_toc_continue (toc_content , part , model = None ):
503508 print ('start generate_toc_continue' )
504509 prompt = """
505510 You are an expert in extracting hierarchical tree structure.
@@ -527,7 +532,7 @@ def generate_toc_continue(toc_content, part, model="gpt-4o-2024-11-20"):
527532 Directly return the additional part of the final JSON structure. Do not output anything else."""
528533
529534 prompt = prompt + '\n Given text\n :' + part + '\n Previous tree structure\n :' + json .dumps (toc_content , indent = 2 )
530- response , finish_reason = ChatGPT_API_with_finish_reason (model = model , prompt = prompt )
535+ response , finish_reason = llm_completion (model = model , prompt = prompt , return_finish_reason = True )
531536 if finish_reason == 'finished' :
532537 return extract_json (response )
533538 else :
@@ -561,7 +566,7 @@ def generate_toc_init(part, model=None):
561566 Directly return the final JSON structure. Do not output anything else."""
562567
563568 prompt = prompt + '\n Given text\n :' + part
564- response , finish_reason = ChatGPT_API_with_finish_reason (model = model , prompt = prompt )
569+ response , finish_reason = llm_completion (model = model , prompt = prompt , return_finish_reason = True )
565570
566571 if finish_reason == 'finished' :
567572 return extract_json (response )
@@ -732,7 +737,7 @@ def check_toc(page_list, opt=None):
732737
733738
734739################### fix incorrect toc #########################################################
735- def single_toc_item_index_fixer (section_title , content , model = "gpt-4o-2024-11-20" ):
740+ async def single_toc_item_index_fixer (section_title , content , model = None ):
736741 toc_extractor_prompt = """
737742 You are given a section title and several pages of a document, your job is to find the physical index of the start page of the section in the partial document.
738743
@@ -746,7 +751,7 @@ def single_toc_item_index_fixer(section_title, content, model="gpt-4o-2024-11-20
746751 Directly return the final JSON structure. Do not output anything else."""
747752
748753 prompt = toc_extractor_prompt + '\n Section Title:\n ' + str (section_title ) + '\n Document pages:\n ' + content
749- response = ChatGPT_API (model = model , prompt = prompt )
754+ response = await llm_acompletion (model = model , prompt = prompt )
750755 json_content = extract_json (response )
751756 return convert_physical_index_to_int (json_content ['physical_index' ])
752757
@@ -815,7 +820,7 @@ async def process_and_check_item(incorrect_item):
815820 continue
816821 content_range = '' .join (page_contents )
817822
818- physical_index_int = single_toc_item_index_fixer (incorrect_item ['title' ], content_range , model )
823+ physical_index_int = await single_toc_item_index_fixer (incorrect_item ['title' ], content_range , model )
819824
820825 # Check if the result is correct
821826 check_item = incorrect_item .copy ()
@@ -1069,7 +1074,7 @@ def page_index_main(doc, opt=None):
10691074 raise ValueError ("Unsupported input type. Expected a PDF file path or BytesIO object." )
10701075
10711076 print ('Parsing PDF...' )
1072- page_list = get_page_tokens (doc )
1077+ page_list = get_page_tokens (doc , model = opt . model )
10731078
10741079 logger .info ({'total_page_number' : len (page_list )})
10751080 logger .info ({'total_token' : sum ([page [1 ] for page in page_list ])})
0 commit comments