$ show basketikun/chatgpt2api
This repository is a reverse-proxy that translates ChatGPT's proprietary web API into OpenAI-compatible REST endpoints, enabling standard OpenAI SDK clients to generate images, edit images, have chat conversations, and search using ChatGPT's backend. It manages a pool of ChatGPT accounts, handles token refresh, proxy rotation, Cloudflare clearance (FlareSolverr, WARP), and provides a Next.js web dashboard for account management, image generation, settings, and backup. The backend is built with FastAPI and uses curl_cffi for HTTP impersonation, while the frontend is a modern React application with shadcn/ui components. The architecture is modular: protocol adapters in `services/protocol/` convert between OpenAI/Anthropic formats and ChatGPT's internal API, storage backends (JSON, SQLite, PostgreSQL, Git) abstract persistence, and services handle account lifecycle, image processing, and proxy configuration. The system is designed for self-hosted deployment via Docker Compose and supports advanced features like content filtering, chat completion caching, and editable file generation (PPT/PSD). Despite its strong engineering and active maintenance, the codebase is heavily dependent on reverse-engineering of ChatGPT's private API, making it fragile to upstream changes. The large single-file `OpenAIBackendAPI` class and limited test coverage are areas of concern, but the separation of concerns in storage, protocol, and service layers offers good reusability.
The LLM identified 4 files worth highlighting for reuse:
Defines the `StorageBackend` abstract base class with a uniform interface for loading and saving account data, configuration, and other persisted objects. Concrete implementations for JSON files, SQLite/PostgreSQL (via `database_storage.py`), and Git-backed storage each conform to this interface. This pattern allows easy swapping of storage backends without affecting business logic, and is reusable in any project needing pluggable persistence.
Implements a thread-safe cache for chat completions with deduplication of in-flight requests (coalescing identical concurrent calls) and TTL-based response caching. Handles both streaming and non-streaming responses, normalizes messages to generate cache keys, and tracks cached tokens in usage statistics. This is a clean, reusable caching solution for any API that serves completion-style responses.
Provides a runtime proxy configuration system with support for multiple proxy profiles (WARP, FlareSolverr, custom HTTP/HTTPS/SOCKS5). Includes a Cloudflare clearance mechanism that caches clearance tokens (cookies, user-agent) with single-flight deduplication to avoid redundant FlareSolverr calls. The `ProxyRuntimeSettings` model is serializable and can be dynamically updated. This is a reusable pattern for any application that needs to route requests through configurable proxies with bypass capabilities.
Contains a collection of well-isolated utility functions for image validation (MIME type, size, base64/URL decoding), SSE streaming helpers (`sse_json_stream`, `anthropic_sse_stream`), and common data transformations (token anonymization, prompt extraction). Each function is self-contained, fully parameterized, and can be extracted into any Python project requiring image processing or SSE handling.