Skip to content

Refactor: Generalize API endpoints to support OpenAI-compatible models via .env#157

Open
3985618547-debug wants to merge 8 commits intoVectifyAI:mainfrom
3985618547-debug:main
Open

Refactor: Generalize API endpoints to support OpenAI-compatible models via .env#157
3985618547-debug wants to merge 8 commits intoVectifyAI:mainfrom
3985618547-debug:main

Conversation

@3985618547-debug
Copy link

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)

  • Renamed CHATGPT_API_KEY to OPENAI_API_KEY.
  • Added support for OPENAI_MODEL (defaults to gpt-4o-2024-11-20) and OPENAI_BASE_URL (defaults to https://api.openai.com/v1).

2. Core Utilities (pageindex/utils.py)

  • Renamed API wrapper functions for broader accuracy:
    • ChatGPT_API_with_finish_reasonOpenAI_API_with_finish_reason
    • ChatGPT_APIOpenAI_API
    • ChatGPT_API_asyncOpenAI_API_async
  • Updated the OpenAI client initialization to explicitly use the base_url parameter fetched from the .env file.
  • Synchronized all documentation generation and node summary generation calls with the new function names.

3. PDF Main Workflow (page_index.py)

  • Replaced all instances of ChatGPT_API* calls with OpenAI_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.).
  • The parameter signatures remain strictly unchanged to ensure stability.

4. Markdown Workflow & Entry Scripts (run_pageindex.py, pageindex/page_index_md.py)

  • Added load_dotenv() at the top of the entry scripts to ensure environment variables are correctly loaded.
  • The --model argument default value now correctly falls back to os.getenv('OPENAI_MODEL').

5. Documentation (README.md)

  • Updated references from "ChatGPT-style" to "OpenAI-style".
  • Updated .env setup examples to reflect the new variable names.

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 .env file:

OPENAI_API_KEY=your_api_key_here
OPENAI_MODEL=your_model_name
OPENAI_BASE_URL=[https://api.deepseek.com](https://api.deepseek.com)

从环境变量读取 OPENAI_MODEL 和 OPENAI_BASE_URL,并设置了默认值
更新了三个 API 调用函数(ChatGPT_API_with_finish_reason、ChatGPT_API、ChatGPT_API_async),让它们使用自定义的 base_url
添加了 dotenv 加载支持
将 --model 参数的默认值改为从环境变量的 OPENAI_MODEL 读取
@dingxihao1991
Copy link

Haven't you merged it into the main branch yet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants