Fix imports, remove dead code, and make token handling model-aware in chat_paper and chat_arxiv#302
Draft
Fix imports, remove dead code, and make token handling model-aware in chat_paper and chat_arxiv#302
Conversation
…oding Co-authored-by: kaixindelele <28528386+kaixindelele@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Optimize project for better performance
Fix imports, remove dead code, and make token handling model-aware in chat_paper and chat_arxiv
Mar 2, 2026
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.
Several correctness and robustness issues in
chat_paper.pyandchat_arxiv.pycaused inaccurate token counting, silent text truncation bugs, and hardcoded context limits that broke for GPT-4 and 16k models.Changes
Fix duplicate
import os/ multi-module import (chat_paper.py):import fitz, io, osshadowed the earlierimport os; split into clean, separate stdlib and third-party import blocks.Remove unused
imagesvariable (both files):images = page.get_images()was assigned and immediately shadowed by thefor … in page.get_images()loop — dead assignment removed.Model-aware
max_token_num(both files): Replaced the hardcoded4096with a lookup dict so GPT-3.5-turbo-16k, GPT-4, GPT-4-32k, and GPT-4-turbo users get the correct context window. Unknown models fall back to 4096.Model-aware tiktoken encoding (both files):
tiktoken.get_encoding("gpt2")gives wrong token counts for GPT-3.5/4 (which usecl100k_base). Replaced withtiktoken.encoding_for_model(self.chatgpt_model)with acl100k_basefallback for unknown models.Bounds-check
clip_text_index(both files, all three chat methods): The index calculation could yield0or negative when prompt token budget ≥ text length, producing an empty or inverted slice. Wrapped withmax(1, …).Read model from config in
chat_arxiv.py: The model was hardcoded to"gpt-3.5-turbo"in all threeopenai.ChatCompletion.createcalls. Now readsCHATGPT_MODELfromapikey.iniviaself.chatgpt_model, consistent withchat_paper.py.💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.