How fatwang2/dify2openai works
Project type and purpose
• This is a single‐service Node.js “API adapter” microservice. It exposes OpenAI‐compatible HTTP endpoints (v1/chat/completions, v1/completions, v1/agent, v1/workflow) and under the hood forwards requests to the Dify API, then remaps Dify’s JSON payload back into the OpenAI schema.
File structure
• Top‐level files:
• app.js – the Express.js HTTP server and router
• Dockerfile, docker-compose.yml / docker-compose-image.yml – container build and orchestration
• vercel.json – serverless deployment config
• .github/workflows/build.yaml – CI pipeline
• .env.template – environment variable template
• pictures/usage.png – usage example
• No frontend code, no database folders – purely a backend adapter.
Insights from README
• Core function: “Convert Dify API into an OpenAI API.”
• Supports streaming vs. blocking, Chat/Completion/Agent/Workflow bots.
• Deployable on Vercel (serverless, 10-sec limit), Zeabur, Docker.
• Key environment variables: DIFY_API_URL, BOT_TYPE, INPUT_VARIABLE, OUTPUT_VARIABLE, MODELS_NAME.
Main components
a. Clients
• Any OpenAI‐compatible client (ChatGPT plugin, curl, Postman, custom app)
b. API Adapter Service (Node.js/Express)
• Routes incoming v1/* requests
• Applies BOT_TYPE logic (Chat vs. Completion vs. Workflow)
• Supports streaming (Server‐Sent Events/WebSockets) and blocking
• Transforms request/response payloads between OpenAI and Dify formats
c. External Dify API
• The real LLM, knowledge base, tools, and workflows endpoint (DIFY_API_URL)
d. Deployment & Infrastructure
• CI/CD (GitHub Actions)
• Container orchestration (Docker Compose) or serverless (Vercel/Zeabur)
Relationships & interactions
• Clients → HTTP POST to /v1/* on the Adapter Service
• Adapter Service reads .env (DIFY_API_URL, BOT_TYPE, etc.)
• Adapter Service → HTTP(s) call to Dify API (with user’s Bearer token)
• Dify API → JSON response (stream or full) → Adapter Service
• Adapter Service → remapped JSON → Client
Architectural patterns & principles
• Single microservice, stateless
• Config‐driven behavior via environment variables
• HTTP proxy/adapter pattern
• Polyglot deployment flexibility (container or serverless)
Technologies, frameworks & libraries
• Node.js + Express.js (app.js)
• GitHub Actions (build workflow)
• Docker (+ Docker Compose)
• Vercel serverless functions
• Dify LLM platform (external API)
Details
• That the service is stateless and horizontally scalable.
• That environment variables tune the behavior at runtime.
• 10-second serverless timeout constraint on Vercel.