Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pageindex/page_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,15 @@ async def check_title_appearance_in_start_concurrent(structure, page_list, model

def toc_detector_single_page(content, model=None):
prompt = f"""
Your job is to detect if there is a table of content provided in the given text.
Your job is to detect if there is a table of content provided in the given single-page text.

Given text: {content}

Guidance for single-page documents:
- Text with actual document content, including policies, rules, regulations, memos, or numbered sections, is not a table of contents just because it is structured.
- A true table of contents primarily lists references to content elsewhere in the document, usually as navigation entries for later sections or pages.
- If the given page contains the content itself instead of references to other content, answer "no".

return the following JSON format:
{{
"thinking": <why do you think there is a table of content in the given text>
Expand Down Expand Up @@ -1151,4 +1156,4 @@ def validate_and_truncate_physical_indices(toc_with_page_number, page_list_lengt
if truncated_items:
print(f"Truncated {len(truncated_items)} TOC items that exceeded document length")

return toc_with_page_number
return toc_with_page_number
32 changes: 32 additions & 0 deletions tests/test_toc_detector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import importlib


page_index = importlib.import_module("pageindex.page_index")


def test_single_page_toc_prompt_distinguishes_content_from_toc(monkeypatch):
captured = {}

def fake_completion(model, prompt):
captured["model"] = model
captured["prompt"] = prompt
return '{"thinking": "structured content, not a toc", "toc_detected": "no"}'

monkeypatch.setattr(page_index, "llm_completion", fake_completion)

result = page_index.toc_detector_single_page(
"1. Scope\nThis policy applies to all staff.\n\n"
"2. Requirements\nUsers must rotate secrets.\n\n"
"3. Exceptions\nExceptions require approval.",
model="test-model",
)

assert result == "no"
assert captured["model"] == "test-model"

prompt = captured["prompt"].lower()
assert "single-page" in prompt
assert "actual document content" in prompt
assert "numbered sections" in prompt
assert "true table of contents" in prompt
assert "references to content elsewhere" in prompt