Refactor: Generalize API endpoints to support OpenAI-compatible models via .env(Resubmission of #157[Wrong account previously used].)#158
Open
funnamer wants to merge 1 commit intoVectifyAI:mainfrom
Conversation
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.
Overview
This PR refactors the API calling logic to be model-agnostic. By generalizing the ChatGPT-specific naming conventions to a standard OpenAI format and moving configurations to
.env, the project can now seamlessly support any OpenAI-compatible API services (such as DeepSeek).Detailed Changes
1. Configuration (
.env)CHATGPT_API_KEYtoOPENAI_API_KEY.OPENAI_MODEL(defaults togpt-4o-2024-11-20) andOPENAI_BASE_URL(defaults tohttps://api.openai.com/v1).2. Core Utilities (
pageindex/utils.py)ChatGPT_API_with_finish_reason→OpenAI_API_with_finish_reasonChatGPT_API→OpenAI_APIChatGPT_API_async→OpenAI_API_asyncbase_urlparameter fetched from the.envfile.count_tokenslogic: Changedtiktoken.encoding_for_model(model)to explicitly usetiktoken.get_encoding("cl100k_base"). This prevents token-counting crashes when using third-party models (like DeepSeek) that are not natively recognized by thetiktokenlibrary, ensuring robust cross-provider compatibility.3. PDF Main Workflow (
page_index.py)ChatGPT_API*calls withOpenAI_API*across the entire PDF parsing and TOC generation workflow (e.g.,check_title_appearance,toc_detector_single_page,extract_toc_content,toc_transformer, etc.).4. Markdown Workflow & Entry Scripts (
run_pageindex.py,pageindex/page_index_md.py)load_dotenv()at the top of the entry scripts to ensure environment variables are correctly loaded.--modelargument default value now correctly falls back toos.getenv('OPENAI_MODEL').Compatibility & Migration Guide
Behavioral Change: Keys, models, and base URLs are no longer hardcoded.
Migration: To use a third-party compatible API (like DeepSeek), simply update the
.envfile: