# xqdoo00o/chatgpt-to-api architecture

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

- Interactive diagram: https://gitdiagram.com/xqdoo00o/chatgpt-to-api
- Repository: https://github.com/xqdoo00o/chatgpt-to-api

## Overview

##### Project Type and Purpose
- This is a server-side Go application acting as a fake API gateway for ChatGPT. Its purpose is to receive API calls on an endpoint (http://127.0.0.1:8080/v1/chat/completions), perform authentication and token cycling, interact with the underlying ChatGPT (and even Bard) implementations, and then output responses consistent with the official API.
- The README emphasizes authentication (tokens, HAR files, cookies) and conversion (request/response) processes, so this project can be thought of as a middleware/adapter layer between client API calls and the ChatGPT website interactions.

##### Analyzing the File Structure
- Top-level files: main.go (entry point), auth.go, handlers.go, and middleware.go indicate core application logic, API handling, and middleware layers.
- Directories like internal, conversion, and typings suggest a modular architecture:
- internal/: Contains packages to interact with external services such as ChatGPT and Bard (see internal/chatgpt and internal/bard), and token management (internal/tokens).
- conversion/: Contains logic to process and convert requests and responses for different modules (i.e., chatgpt conversions).
- cases/: Likely contains test cases, examples, or configuration (as seen with Sider in cases/sider).
- docs/: Provides documentation for different users/languages.
- GitHub workflows and Docker configuration files indicate CI/CD and containerization support.

##### Key Architectural Components
a. Client/API Layer:
- Elements for handling API keys (api_keys.txt) and optional proxy configurations.

b. Authentication and Token Management:
- An “Authentication Module” that reads from accounts.txt and accesses/updates access_tokens.json, cookies.json, and (optionally) api_keys.txt.
- Details on the use of OpenAIAuth automation and HAR file pool handling.
- Environment variable-based configuration (SERVER_HOST, SERVER_PORT, ENABLE_HISTORY).

c. Request Conversion and Routing:
- A “Conversion Module” which houses the conversion/requests and conversion/response directories. This layer converts incoming API calls into the format expected by internal modules and vice versa.
- That this module also interfaces with the internal “typings” and conversion layers.

d. Internal Service Adapters:
- “Internal ChatGPT Module” (internal/chatgpt) handles request formatting, communication (with the ChatGPT website), and any special processing (including token renewal using turnstile).
- “Internal Bard Module” (internal/bard) similarly handles Bard-specific functionality.
- “Internal Token Management” (internal/tokens) ensures that the system tracks, cycles, and renews tokens.

e. Middleware and Routing:
- A “Middleware Layer” to process incoming requests, enforce authentication, and possibly log/detect errors.
- “Handlers” (handlers.go) that define endpoints and delegate processing to conversion modules and internal services.

f. External Dependencies and CI/CD:
- A component or note for Continuous Integration (from .github/workflows) and container orchestration (Dockerfile, docker-compose.yml) as supporting infrastructure.

##### Relationships and Data Flow
- From the endpoint, flow should pass through the Middleware Layer (authentication & request validation).
- Next, direct the flow to the Conversion Module where requests are adjusted based on defined typings.
- Then, split the internal flow into the appropriate service: either “Internal ChatGPT Module” or “Internal Bard Module” based on the logic.
- A return path from these modules via the Conversion Module to the API endpoint, sending back the converted response.
- Optionally, show a side path for token management operations interacting with access_tokens.json and cookies.json.

##### Architectural Patterns and Design Principles
- The project employs a clear separation of concerns: API routing, middleware-driven authentication, conversion logic, and internal services.
- It uses modularization to isolate various functionalities (e.g., ChatGPT and Bard adapters).
- Utilize dependency inversion by letting the conversion modules abstract details of the underlying services.
- Make clear note of the configuration layer (environment variables, Docker setups) that supports deployment and scaling.

##### Final Composition
- Keep the design modular, reflecting the file structure’s separation (for example, highlighting modules based on directories such as internal, conversion, and typings).

## Diagram (Mermaid)

```mermaid
flowchart TD
    A(("Client")):::external
    B(("API Endpoint (http://127.0.0.1:8080/v1/chat/completions)")):::external
    C["Middleware & Authentication"]:::middleware
    D["Request Conversion Module"]:::conversion
    E["Internal ChatGPT Adapter"]:::internal
    F["Internal Bard Adapter"]:::internal
    G["Internal Token Management"]:::internal
    H["Typings & Data Schemas"]:::typings
    I["External Config & CI/CD"]:::config

    A -->|"calls"| B
    B -->|"passes"| C
    C -->|"forwards"| D
    D -->|"toChatGPT"| E
    D -->|"toBard"| F
    E -->|"response"| D
    F -->|"response"| D
    D -->|"returns"| B
    B -->|"reply"| A

    E -->|"tokenRenewal"| G
    G -->|"tokenUpdate"| E

    D -->|"uses"| H
    H -->|"schemaValidate"| D

    I -->|"envConfig"| C
    I -->|"CI/CD"| E
    I -->|"CI/CD"| F

    click B "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/main.go"
    click B "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/handlers.go"

    click C "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/middleware.go"
    click C "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/auth.go"

    click D "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/conversion/requests/chatgpt/convert.go"
    click D "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/conversion/response/chatgpt/convert.go"

    click E "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/internal/chatgpt/reqtype.go"
    click E "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/internal/chatgpt/request.go"
    click E "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/internal/chatgpt/turnstile.go"

    click F "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/internal/bard/lib.go"
    click F "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/internal/bard/request.go"
    click F "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/internal/bard/utilities.go"

    click G "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/internal/tokens/tokens.go"

    click H "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/typings/chatgpt/response.go"
    click H "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/typings/official/request.go"
    click H "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/typings/official/response.go"
    click H "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/typings/typings.go"

    click I "https://github.com/xqdoo00o/chatgpt-to-api/tree/master/.github/workflows"
    click I "https://github.com/xqdoo00o/chatgpt-to-api/tree/master/Dockerfile"
    click I "https://github.com/xqdoo00o/chatgpt-to-api/blob/master/docker-compose.yml"

    classDef external fill:#FFEEAD,stroke:#333,stroke-width:2px;
    classDef middleware fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef conversion fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef internal fill:#FFA500,stroke:#000,stroke-width:2px;
    classDef typings fill:#D8BFD8,stroke:#000,stroke-width:2px;
    classDef config fill:#D3D3D3,stroke:#000,stroke-width:2px;
```
