fix: enforce max-tokens-per-node for TOC prompts and harden llm returns#353
Open
fooSynaptic wants to merge 1 commit into
Open
fix: enforce max-tokens-per-node for TOC prompts and harden llm returns#353fooSynaptic wants to merge 1 commit into
fooSynaptic wants to merge 1 commit into
Conversation
Wire --max-tokens-per-node into TOC page grouping and truncate oversized document parts before LLM calls so small context models stop hitting tokens_limit_reached. Ensure llm_completion always returns a clean 2-tuple when return_finish_reason=True to avoid unpack crashes (VectifyAI#15).
Author
|
Hi @KylinMountain — could you take a look at this when you have a moment? It fixes #15 by wiring |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #15.
--max-tokens-per-nodewas only used when recursively splitting large tree nodes. TOC construction still grouped pages with a hard-coded 20k budget and sent the full group text into the LLM prompt, so small-context models (e.g.gpt-4o-minion a multi-page PDF) hit providertokens_limit_reachederrors and could crash on unexpected return shapes when unpackingresponse, finish_reason = ....Root cause
process_no_toc/process_toc_no_page_numberscalledpage_list_to_group_text(...)without passingopt.max_token_num_each_node, so the CLI flag had no effect on TOC prompt size.llm_completion(..., return_finish_reason=True)always returns a 2-tuple; provider/Nonecontent edge cases could produce unpack failures.Fix
max_tokens=opt.max_token_num_each_nodeinto TOC page grouping.truncate_to_token_limitand apply it to document parts ingenerate_toc_init/generate_toc_continue/add_page_number_to_tocbefore building the LLM prompt.llm_completionalways return(str, finish_reason)whenreturn_finish_reason=True(includingNonecontent →"").No behavior change when the document already fits under the configured budget.
What changed
pageindex/utils.pytruncate_to_token_limit; hardenllm_completion2-tuple returnspageindex/page_index.pymax_token_num_each_nodeinto TOC grouping; truncate parts before TOC LLM callstests/test_issue_15.pyTest plan
python -m pytest tests/test_issue_15.py -v(11 passed; no live API key required)