How iidamie/deepseek2api works
Project Type and Purpose
• DeepSeek2API is an open-source backend service that exposes an OpenAI-compatible chat and model-listing API (/v1/chat/completions and /v1/models) by reverse-engineering DeepSeek’s undocumented API.
• It’s implemented as a single Python application (app.py) using a web framework (likely FastAPI/UVicorn) and provides multiple deployment options: Docker, docker-compose, Vercel, or native Python.
High-Level Architecture
• Monolithic API service with clear separation of concerns:
a. HTTP/API layer
b. Account & key management
c. Tokenization & hashing utilities
d. External DeepSeek integration
e. Static content (welcome page)
Main Components
a. Client (REST or OpenAI-compatible SDK)
b. HTTP Server (Uvicorn/FastAPI)
• Serves JSON endpoints (GET /v1/models, POST /v1/chat/completions)
• Serves a welcome HTML template (templates/welcome.html)
c. Configuration Loader
• Loads config.json (API keys + DeepSeek account pool) at startup
d. Account Manager
• Manages a rotating pool of DeepSeek accounts and API keys to distribute requests and mitigate rate limits
e. Tokenizer & Hashing Utilities
• Loads tokenizer.json and tokenizer_config.json for token counting (usage statistics)
• Uses sha3_wasm_bg.*.wasm for cryptographic hashing or signing requests
f. DeepSeek Proxy Service
• Forwards client requests to DeepSeek official endpoints
• Handles streaming responses (chunked transfer) when stream=true
• Injects OpenAI-compatible response schema (choices, usage, etc.)
g. Deployment & Infrastructure
• Dockerfile + docker-compose.yml define containerized environment
• Optional Nginx reverse proxy for buffering optimizations
• Vercel configuration (vercel.json) for serverless deployment
h. CI/CD
• GitHub Actions workflows (.github/workflows/*.yml) for building and pushing Docker images
Component Interactions / Data Flow
• Client → HTTP Server / Uvicorn
• HTTP Server
• reads config & initializes Account Manager
• validates incoming request and headers (Authorization: Bearer)
• (optional) invokes Tokenizer for usage logging
• selects an account/API key via Account Manager
• constructs DeepSeek request (may include hashed signature via WASM module)
• opens HTTP/stream connection to DeepSeek
• streams chunks back to Client, re-wrapping into OpenAI schema
• (If deployed behind Nginx) Nginx sits between Client and Uvicorn, handling low-level TCP and buffering settings
Architectural Patterns & Principles
• Layered architecture: Controllers (API) → Services (Account & Proxy) → Utilities (Tokenizer & WASM)
• Configuration-driven: all credentials & settings in config.json
• Single‐responsibility: each module (account manager, proxy, tokenizer) handles a distinct concern
• Streaming support: chunked transfer encoding for low-latency partial outputs
• Containerization: Docker best practices for isolation and reproducibility