# jun-howie/erniebot-openai-api architecture

> Architecture diagram of the GitHub repository jun-howie/erniebot-openai-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jun-howie/erniebot-openai-api
- Repository: https://github.com/jun-howie/erniebot-openai-api

## Overview

##### Project Type and Purpose
- Type: A lightweight Python microservice that exposes an OpenAI-compatible HTTP API and proxies requests to the Baidu ERNIE Bot platform.
- Purpose:
– Accept incoming POST requests on `/v1/chat/completions`.
– Translate them into ERNIE Bot SDK calls (streaming or non-streaming).
– Return ERNIE Bot responses in OpenAI-style JSON.
– Run standalone (via `python erniebot-openai-api.py`) or as a Docker container.

##### File Structure
- Dockerfile – builds a containerized version of the Python service.
- erniebot-openai-api.py – main script: loads env vars, spins up HTTP server (Flask/FastAPI/uvicorn), defines endpoint logic.
- requirements.txt – lists runtime dependencies (web framework, ERNIE Bot agent SDK, etc.).
- README.md – usage instructions, environment variables (`EB_AGENT_ACCESS_TOKEN`, `EB_AGENT_LOGGING_LEVEL`), curl examples, Docker run commands.

##### Key Components
a. Client(s)
– Examples: `curl`, front-end app, or any OpenAI-compatible client library.
b. API Proxy Service
– Container/Host running `erniebot-openai-api.py`.
- HTTP Server (FastAPI/Flask + Uvicorn/Gunicorn)
- Request Handler (endpoint logic)
- ERNIE Bot Agent SDK client module
- Configuration loader (reads `EB_AGENT_ACCESS_TOKEN`, logging level, port)
c. External Service
– Baidu ERNIE Bot endpoint (e.g., `https://api.ernie-bot.baidu.com`).
– Show as an external “AI Inference” service.
d. Docker / Deployment
– Illustrate the Docker container as the deployment unit around the API Proxy Service.
– Indicate port mapping (`8000:8000`).

##### Relationships & Data Flows
– HTTP POST `/v1/chat/completions` (JSON payload).
– Function call / HTTP request to ERNIE Bot (with streaming or not).
– Response stream or JSON.
– HTTP response (OpenAI-style JSON or streaming chunks).

##### Architectural Patterns & Principles
- Single‐responsibility microservice: only handles HTTP translation and proxying.
- Configuration via environment variables for 12-factor compliance.
- Containerization for consistent deployment.
- Optional streaming data flow: highlight chunked response path.

##### Technologies & Frameworks
- Python 3.10
- FastAPI or Flask (whichever `erniebot-openai-api.py` uses)
- ERNIE Bot Agent SDK (from `requirements.txt`)
- Uvicorn/Gunicorn (WSGI/ASGI server)
- Docker

## Diagram (Mermaid)

```mermaid
flowchart TB
    classDef client fill:#cdefff,stroke:#036,stroke-width:1px
    classDef internal fill:#cfeccc,stroke:#060,stroke-width:1px
    classDef external fill:#ffe0b3,stroke:#a85,stroke-width:1px
    classDef docs fill:#f3f3f3,stroke:#999,stroke-width:1px

    User["User Agent\n(curl, client lib)"]:::client
    subgraph "Docker Container (port 8000:8000)"
        direction TB
        subgraph "API Proxy Service"
            direction TB
            HTTPServer["HTTP Server & Request Handler"]:::internal
            SDKClient["ERNIE Bot Agent SDK Client"]:::internal
            ConfigLoader["Configuration Loader\n(Env Vars)"]:::internal
        end
    end
    ExtService["Baidu ERNIE Bot\nExternal AI Inference"]:::external
    DockerfileNode["Dockerfile"]:::docs
    Requirements["requirements.txt"]:::docs
    README["README.md"]:::docs

    User -->|"HTTP POST /v1/chat/completions"| HTTPServer
    HTTPServer -->|"invoke SDK client"| SDKClient
    SDKClient -->|"HTTP/SDK request"| ExtService
    ExtService -->|"response / stream"| SDKClient
    SDKClient -->|"SDK response"| HTTPServer
    HTTPServer -->|"HTTP 200 JSON / streaming chunks"| User

    ConfigLoader -.-> HTTPServer
    ConfigLoader -.-> SDKClient

    DockerfileNode -.-> API_Proxy["API Proxy Service"]
    Requirements -.-> HTTPServer
    README -.-> ConfigLoader

    click HTTPServer "https://github.com/jun-howie/erniebot-openai-api/blob/main/erniebot-openai-api.py"
    click SDKClient "https://github.com/jun-howie/erniebot-openai-api/blob/main/erniebot-openai-api.py"
    click ConfigLoader "https://github.com/jun-howie/erniebot-openai-api/blob/main/erniebot-openai-api.py"
    click DockerfileNode "https://github.com/jun-howie/erniebot-openai-api/tree/main/Dockerfile"
    click Requirements "https://github.com/jun-howie/erniebot-openai-api/blob/main/requirements.txt"
    click README "https://github.com/jun-howie/erniebot-openai-api/blob/main/README.md"

    class User client
    class HTTPServer,SDKClient,ConfigLoader internal
    class ExtService external
    class DockerfileNode,Requirements,README docs
```
