How easychen/openai-api-proxy works
Project type and purpose
• From the file tree and README, this is a lightweight, server-side OpenAI API proxy written in Node.js.
• It’s meant to be deployed in Docker or as a cloud function. It exposes OpenAI-compatible endpoints, adds optional Tencent Cloud text moderation, and supports Server-Sent Events (SSE) streaming.
File structure
• Top level:
• app.js – main Node.js entry point
• package.json – Node dependencies and scripts
• FUNC.md – Tencent Cloud Function deployment guide
• docker/ – a self-contained Docker build context (Dockerfile, its own app.js, fetchsse.js, package.json)
• README.md, README.CN.md – usage docs
• The separation of docker/ shows two deployment contexts: “bare-metal” Node.js or containerized.
README for additional insights
• Features: SSE streaming, text moderation via Tencent Cloud, PROXY_KEY auth.
• Environment variables configure port, proxy key, timeouts, Tencent credentials, region.
• API usage: acts as an HTTP GET/POST proxy, replacing the OpenAI base URL.
• Client integration: works with chatgpt-API library by overriding apiBaseUrl.
• Client applications (e.g., chatgpt library, custom frontends)
• OpenAI API Proxy (Node.js service)
• HTTP server (app.js)
• SSE handler (fetchsse.js)
• Moderation module (invokes Tencent Cloud Moderation)
• Auth middleware (PROXY_KEY validation)
• External services:
• OpenAI REST/SSE API endpoints
• Tencent Cloud Content Moderation API
• Deployment targets:
• Docker container
• Cloud Function runtime
b. Relationships & data flow:
1. Client sends request (HTTP GET/POST with optional stream parameter) →
2. Proxy’s auth middleware checks PROXY_KEY →
3. If moderation=true, Proxy calls Tencent Cloud Moderation API →
– If verdict = Block/High, abort or modify request
4. Proxy forwards sanitized request to OpenAI API (fetch-SSE for stream) →
5. OpenAI responds (streaming tokens or JSON payload) →
6. Proxy relays response to client via SSE or HTTP response
c. Architectural patterns & principles:
• Single-process event-driven server (Node.js)
• Middleware pipeline (auth → moderation → proxy)
• Separation of concerns: moderation logic isolated from streaming logic
• Config-driven deployment (env vars for keys, timeouts, ports)
d. Key technologies & libraries:
• Node.js 14+
• Express-style HTTP server (built into app.js)
• fetch-sse for streaming support
• Tencent Cloud SDK or direct HTTP calls for moderation
• Docker for containerization
• Optional: Cloud Function platform
Important details to capture
• Optional SSE path vs. standard HTTP path.
• Error handling: timeouts, moderation failures, streaming interruptions.
• How client must modify apiBaseUrl and key format (sk…:<PROXY_KEY>).