One API

repository·main·Indexed 13 days ago

https://github.com/songquanpeng/one-api

A unified gateway that standardizes access to various large language models (LLMs) using the OpenAI API format. It provides multi-model support for providers like OpenAI, Anthropic, Google Gemini, Mistral, DeepSeek, and Ollama, featuring load balancing, token and user management, and integration with Cloudflare AI Gateway.

Tokens
30.2K
Snippets
137
Records
162
Agent score
98%

What's inside One API

  1. Overview of One API features

    main

    One API provides a unified interface to access multiple Large Language Models (LLMs) using the standard OpenAI API format. It acts as a proxy/aggregator that simplifies integration by encapsulating various model providers into a single, consistent API structure.

    Key Capabilities:

    • Multi-Model Support: Supports OpenAI (including Azure OpenAI), Anthropic Claude, Google PaLM2/Gemini, Baidu Wenxin Yiyuan, Alibaba Tongyi Qianwen, and Zhipu ChatGLM.
    • Load Balancing: Distributes requests across multiple channels.
    • Stream Mode: Supports streaming responses for typewriter-style effects.
    • Token & Voucher Management: Manage token expiration, usage limits, and bulk generation/export of vouchers for balance replenishment.
    • User & Channel Management: Supports user groups, channel groups, rate limiting per group, and bulk channel creation.
    • Customization: Customize system names, logos, footers, and home/about pages using HTML, Markdown, or iframe embeds.
    • Authentication & Access: Supports system access tokens for management APIs, Cloudflare Turnstile, and multiple login methods (Email, GitHub OAuth, and WeChat via wechat-server).
  2. Overview of One API

    main

    One API is a tool that allows you to access various large language models (LLMs) through a standardized OpenAI API format. It acts as a unified gateway, enabling out-of-the-box compatibility with many different model providers.

    Key capabilities include:

    • Multi-Model Support: Integrates with OpenAI (including Azure), Anthropic (including AWS), Google Gemini, Mistral, DeepSeek, Ollama, and many others.
    • Load Balancing: Access multiple channels via load balancing.
    • Token & User Management: Manage tokens with expiration, quotas, IP restrictions, and model access. Supports user groups and channel groups with custom multipliers.
    • Advanced Features: Supports streaming (typewriter effect), model mapping, automatic retries, image generation interfaces, and Cloudflare AI Gateway integration.
    • Customization: Highly customizable UI (system name, logo, footer, home/about pages) and theme support via the THEME environment variable.
  3. Available One API Web themes

    main

    The One API frontend supports multiple themes. Note that not all themes are guaranteed to be synchronized with all features immediately; the default theme is prioritized for updates.

    • default: The primary theme developed by [JustSong].
    • berry: A theme developed by [MartialBE].
    • air: A theme developed by [Calon].

    For specific development instructions regarding the berry theme, refer to web/berry/README.md.

  4. Configure Multi-Machine Deployment

    main

    For high availability and scaling across multiple servers, follow these requirements:

    1. Shared Session: Set the same SESSION_SECRET on all servers.
    2. Shared Database: Use MySQL instead of SQLite via SQL_DSN. All nodes must connect to the same database.
    3. Node Roles: Set NODE_TYPE=slave on all nodes except the master.
    4. Configuration Sync: Set SYNC_FREQUENCY (in seconds) to periodically sync configurations from the database.
    5. Frontend Redirection: On slave nodes, optionally set FRONTEND_BASE_URL to redirect page requests to the master server.
    6. Caching: Install Redis on slave nodes and configure REDIS_CONN_STRING to achieve zero-latency access to cached data.
    7. Master Caching: If the master node experiences high database latency, enable Redis and set SYNC_FREQUENCY.
  5. How quota is calculated in One API

    main

    One API calculates quota based on a multiplier system to account for different model costs.

    Formula: Quota = Group multiplier * Model multiplier * (number of prompt tokens + number of completion tokens * completion multiplier)

    Key Details:

    • Completion Multiplier: Fixed at 1.33 for GPT-3.5 and 2 for GPT-4, matching official definitions.
    • Token Counts: For non-stream modes, the official API returns total tokens. Note that prompt and completion multipliers are applied separately.
  6. Route requests to a specific channel using a token

    main

    By default, One API uses load balancing to distribute requests across available channels. To force a request to use a specific channel, append the CHANNEL_ID to your token using a hyphen.

    Requirement: The token must be created by an administrator to support channel ID specification.

    Format: Authorization: Bearer ONE_API_KEY-CHANNEL_ID

    Authorization: Bearer ONE_API_KEY-CHANNEL_ID
  7. How multi-machine deployment works

    main

    For high availability or distributed setups, you can deploy multiple One API instances. To ensure they work together, follow these requirements:

    1. Shared Secret: All servers must have the same SESSION_SECRET.
    2. Shared Database: You must use MySQL (not SQLite) and point all servers to the same database using SQL_DSN.
    3. Master/Slave Roles:
      • The primary server is the default.
      • All secondary servers must have NODE_TYPE set to slave.
    4. Configuration Sync: Set SYNC_FREQUENCY to allow servers to periodically sync configurations from the database. It is recommended to enable Redis for this.
    5. Redirection: Slave servers can set FRONTEND_BASE_URL to redirect page requests back to the master server.
    6. Caching: For reduced latency, install Redis on each slave server and set REDIS_CONN_STRING. This allows slaves to serve cached data without hitting the database.
  8. Understand Quota and Token Calculation

    main

    One API calculates quotas using a specific formula involving group multipliers, model multipliers, and token counts. Note that completion tokens may have a different multiplier than prompt tokens.

    Quota Formula: Quota = Group Multiplier * Model Multiplier * (Prompt Tokens + Completion Tokens * Completion Multiplier)

    Fixed Completion Multipliers:

    • GPT-3.5: 1.33
    • GPT-4: 2.0

    Note: In non-streaming mode, the official API returns the total tokens consumed, but you must account for the different multipliers for prompt and completion tokens.

  9. Deploy One API using Docker Compose

    main

    If you prefer Docker Compose, you can use the following setup. Note that the parameter settings remain the same as the standard Docker deployment.

    # Start the service (supports MySQL startup)
    docker-compose up -d
    
    # Check deployment status
    docker-compose ps
    docker-compose up -d
  10. Deploy One API manually from source

    main

    To deploy manually, you must build both the frontend and the backend.

    1. Clone the repository:

      git clone https://github.com/songquanpeng/one-api.git
    2. Build the Frontend:

      cd one-api/web/default
      npm install
      npm run build
    3. Build the Backend:

      cd ../..
      go mod download
      go build -ldflags "-s -w" -o one-api
    4. Run the application:

      chmod u+x one-api
      ./one-api --port 3000 --log-dir ./logs

    Default Credentials:

    • Username: root
    • Password: 123456
    chmod u+x one-api
    ./one-api --port 3000 --log-dir ./logs