mirror of
https://github.com/VectifyAI/PageIndex.git
synced 2026-04-24 23:56:21 +02:00
fix params
This commit is contained in:
parent
480f7583f7
commit
6d1b505541
6 changed files with 126 additions and 42 deletions
|
|
@ -1,8 +1,8 @@
|
|||
model: "gpt-4o-2024-11-20"
|
||||
model: "gpt-4.1"
|
||||
toc_check_page_num: 20
|
||||
max_page_num_each_node: 10
|
||||
max_token_num_each_node: 20000
|
||||
if_add_node_id: "yes"
|
||||
if_add_node_summary: "no"
|
||||
if_add_doc_description: "yes"
|
||||
if_add_doc_description: "no"
|
||||
if_add_node_text: "no"
|
||||
|
|
@ -496,7 +496,7 @@ def remove_first_physical_index_section(text):
|
|||
return text
|
||||
|
||||
### add verify completeness
|
||||
def generate_toc_continue(toc_content, part, model="gpt-4o-2024-11-20"):
|
||||
def generate_toc_continue(toc_content, part, model="gpt-4.1"):
|
||||
print('start generate_toc_continue')
|
||||
prompt = """
|
||||
You are an expert in extracting hierarchical tree structure.
|
||||
|
|
@ -729,7 +729,7 @@ def check_toc(page_list, opt=None):
|
|||
|
||||
|
||||
################### fix incorrect toc #########################################################
|
||||
def single_toc_item_index_fixer(section_title, content, model="gpt-4o-2024-11-20"):
|
||||
def single_toc_item_index_fixer(section_title, content, model="gpt-4.1"):
|
||||
tob_extractor_prompt = """
|
||||
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.
|
||||
|
||||
|
|
@ -1084,7 +1084,9 @@ def page_index_main(doc, opt=None):
|
|||
if opt.if_add_node_text == 'no':
|
||||
remove_structure_text(structure)
|
||||
if opt.if_add_doc_description == 'yes':
|
||||
doc_description = generate_doc_description(structure, model=opt.model)
|
||||
# Create a clean structure without unnecessary fields for description generation
|
||||
clean_structure = create_clean_structure_for_description(structure)
|
||||
doc_description = generate_doc_description(clean_structure, model=opt.model)
|
||||
return {
|
||||
'doc_name': get_pdf_name(doc),
|
||||
'doc_description': doc_description,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import asyncio
|
||||
import json
|
||||
import re
|
||||
import os
|
||||
try:
|
||||
from .utils import *
|
||||
except:
|
||||
|
|
@ -239,7 +240,7 @@ def clean_tree_for_output(tree_nodes):
|
|||
return cleaned_nodes
|
||||
|
||||
|
||||
async def md_to_tree(md_path, if_thinning=True, min_token_threshold=None, if_summary=True, summary_token_threshold=None, model=None):
|
||||
async def md_to_tree(md_path, if_thinning=True, min_token_threshold=None, if_add_node_summary='no', summary_token_threshold=None, model=None, if_add_doc_description='yes', if_add_node_text='no', if_add_node_id='yes'):
|
||||
with open(md_path, 'r', encoding='utf-8') as f:
|
||||
markdown_content = f.read()
|
||||
|
||||
|
|
@ -257,14 +258,44 @@ async def md_to_tree(md_path, if_thinning=True, min_token_threshold=None, if_sum
|
|||
print(f"Building tree from nodes...")
|
||||
tree_structure = build_tree_from_nodes(nodes_with_content)
|
||||
|
||||
if if_summary:
|
||||
print(f"Generating summaries for each node...")
|
||||
tree_structure = await generate_summaries_for_structure_md(tree_structure,summary_token_threshold=summary_token_threshold, model=model)
|
||||
# Add node IDs if requested (matching PDF behavior)
|
||||
if if_add_node_id == 'yes':
|
||||
write_node_id(tree_structure)
|
||||
|
||||
print(f"Formatting tree structure...")
|
||||
tree_structure = format_structure(tree_structure, order = ['title', 'node_id', 'summary', 'prefix_summary', 'text', 'line_num', 'nodes'])
|
||||
|
||||
return tree_structure
|
||||
if if_add_node_summary == 'yes':
|
||||
# Always include text for summary generation
|
||||
tree_structure = format_structure(tree_structure, order = ['title', 'node_id', 'summary', 'prefix_summary', 'text', 'line_num', 'nodes'])
|
||||
|
||||
print(f"Generating summaries for each node...")
|
||||
tree_structure = await generate_summaries_for_structure_md(tree_structure, summary_token_threshold=summary_token_threshold, model=model)
|
||||
|
||||
if if_add_node_text == 'no':
|
||||
# Remove text after summary generation if not requested
|
||||
tree_structure = format_structure(tree_structure, order = ['title', 'node_id', 'summary', 'prefix_summary', 'line_num', 'nodes'])
|
||||
|
||||
if if_add_doc_description == 'yes':
|
||||
print(f"Generating document description...")
|
||||
# Create a clean structure without unnecessary fields for description generation
|
||||
clean_structure = create_clean_structure_for_description(tree_structure)
|
||||
doc_description = generate_doc_description(clean_structure, model=model)
|
||||
return {
|
||||
'doc_name': os.path.splitext(os.path.basename(md_path))[0],
|
||||
'doc_description': doc_description,
|
||||
'structure': tree_structure,
|
||||
}
|
||||
else:
|
||||
# No summaries needed, format based on text preference
|
||||
if if_add_node_text == 'yes':
|
||||
tree_structure = format_structure(tree_structure, order = ['title', 'node_id', 'summary', 'prefix_summary', 'text', 'line_num', 'nodes'])
|
||||
else:
|
||||
tree_structure = format_structure(tree_structure, order = ['title', 'node_id', 'summary', 'prefix_summary', 'line_num', 'nodes'])
|
||||
|
||||
return {
|
||||
'doc_name': os.path.splitext(os.path.basename(md_path))[0],
|
||||
'structure': tree_structure,
|
||||
}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
|
|
@ -410,7 +410,7 @@ def add_preface_if_needed(data):
|
|||
|
||||
|
||||
|
||||
def get_page_tokens(pdf_path, model="gpt-4o-2024-11-20", pdf_parser="PyPDF2"):
|
||||
def get_page_tokens(pdf_path, model="gpt-4.1", pdf_parser="PyPDF2"):
|
||||
enc = tiktoken.encoding_for_model(model)
|
||||
if pdf_parser == "PyPDF2":
|
||||
pdf_reader = PyPDF2.PdfReader(pdf_path)
|
||||
|
|
@ -623,6 +623,29 @@ async def generate_summaries_for_structure(structure, model=None):
|
|||
return structure
|
||||
|
||||
|
||||
def create_clean_structure_for_description(structure):
|
||||
"""
|
||||
Create a clean structure for document description generation,
|
||||
excluding unnecessary fields like 'text'.
|
||||
"""
|
||||
if isinstance(structure, dict):
|
||||
clean_node = {}
|
||||
# Only include essential fields for description
|
||||
for key in ['title', 'node_id', 'summary', 'prefix_summary']:
|
||||
if key in structure:
|
||||
clean_node[key] = structure[key]
|
||||
|
||||
# Recursively process child nodes
|
||||
if 'nodes' in structure and structure['nodes']:
|
||||
clean_node['nodes'] = create_clean_structure_for_description(structure['nodes'])
|
||||
|
||||
return clean_node
|
||||
elif isinstance(structure, list):
|
||||
return [create_clean_structure_for_description(item) for item in structure]
|
||||
else:
|
||||
return structure
|
||||
|
||||
|
||||
def generate_doc_description(structure, model=None):
|
||||
prompt = f"""Your are an expert in generating descriptions for a document.
|
||||
You are given a structure of a document. Your task is to generate a one-sentence description for the document, which makes it easy to distinguish the document from other documents.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue