PageIndex/pageindex/__init__.py
Ray 15b0b18857 refactor: drop the __init__ lazy-export machinery, superseded by 41f4a0a
Once litellm/PyPDF2 moved inside the functions that use them, the
legacy modules became cheap to import — the PEP 562 __getattr__ table
in __init__ was guarding a door that no longer needs guarding. Restore
plain eager imports (~0.3s total, still zero heavy deps at import);
keep only the small submodule __getattr__ for pageindex.utils /
pageindex.page_index_md attribute access.
2026-07-24 03:16:30 +08:00

79 lines
2.3 KiB
Python

# pageindex/__init__.py
# Load .env first so env-based credentials (e.g. OPENAI_API_KEY) are set.
from dotenv import load_dotenv as _load_dotenv
_load_dotenv()
# Backward compatibility: honor CHATGPT_API_KEY as an alias for OPENAI_API_KEY.
import os as _os
_chatgpt_key = _os.getenv("CHATGPT_API_KEY")
if not _os.getenv("OPENAI_API_KEY") and _chatgpt_key:
_os.environ["OPENAI_API_KEY"] = _chatgpt_key
# Legacy (pre-SDK) exports. Cheap to import eagerly: litellm/PyPDF2 load
# inside the functions that use them, not at module import.
from .index.page_index import * # noqa: E402
from .index.page_index_md import md_to_tree
from .retrieve import get_document, get_document_structure, get_page_content
# SDK exports
from .client import PageIndexClient, LocalClient, CloudClient
from .config import IndexConfig, set_llm_params
from .collection import Collection
from .types import DocumentInfo, DocumentDetail, PageContent
from .parser.protocol import ContentNode, ParsedDocument, DocumentParser
from .storage.protocol import StorageEngine
from .events import QueryEvent
from .errors import (
PageIndexError,
PageIndexAPIError,
CollectionNotFoundError,
DocumentNotFoundError,
IndexingError,
CloudAPIError,
FileTypeError,
)
__all__ = [
"PageIndexClient",
"LocalClient",
"CloudClient",
"IndexConfig",
"set_llm_params",
"Collection",
"DocumentInfo",
"DocumentDetail",
"PageContent",
"ContentNode",
"ParsedDocument",
"DocumentParser",
"StorageEngine",
"QueryEvent",
"PageIndexError",
"PageIndexAPIError",
"CollectionNotFoundError",
"DocumentNotFoundError",
"IndexingError",
"CloudAPIError",
"FileTypeError",
# Legacy top-level exports (pre-SDK API), kept so `from pageindex import *`
# still binds them.
"page_index",
"page_index_main",
"tree_parser",
"ConfigLoader",
"llm_completion",
"llm_acompletion",
"md_to_tree",
"get_document",
"get_document_structure",
"get_page_content",
]
def __getattr__(name):
# `pageindex.utils` / `pageindex.page_index_md` attribute access without an
# explicit submodule import.
if name in ("utils", "page_index_md"):
import importlib
return importlib.import_module(f".{name}", __name__)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")