simple-one-api

repository·main·Indexed 25 days ago

https://github.com/fruitbars/simple-one-api

A lightweight proxy layer that unifies domestic and international LLM providers into a single, standardized OpenAI-compatible API. It supports load balancing, model aliasing, automatic model selection, and rate limiting. It is designed for easy access to free model tiers from providers such as iFlytek Spark, Baidu Qianfan, Tencent Hunyuan, Cloudflare Workers AI, Coze.cn, Groq, Google Gemini, and SiliconFlow, making them compatible with clients like Next Chat, Lobe Chat, and gpt_academic.

Tokens
16.2K
Snippets
37
Records
67
Agent score
79%

What's inside simple-one-api

  1. Overview of simple-one-api

    main
    simple-one-api is a proxy service designed to unify various domestic (Chinese) and international large language models (LLMs) into a single, standardized OpenAI-compatible API format. It aims to simplify integration by handling the subtle differences in API implementations across different providers, making them easy to use with any OpenAI-compatible client (like Next Chat, Lobe Chat, or gpt_academic) without needing to manage complex billing or traffic tracking for each individual provider.
  2. Configure load balancing for credentials and models

    main

    The load_balancing configuration key controls how the backend selects between multiple available options. Setting load_balancing to random allows the system to automatically choose between multiple credential sets for the same model or between different services/models that are marked as enabled: true.

    Load balancing multiple credentials

    To rotate through different API keys for the same model, define multiple service entries with the same model name but different credentials:

    {
      "server_port": ":9099",
      "load_balancing": "random",
      "services": {
        "openai": [
          {
            "models": ["@cf/meta/llama-2-7b-chat-int8"],
            "enabled": true,
            "credentials": { "api_key": "xxx" },
            "server_url": "https://api.cloudflare.com/..."
          },
          {
            "models": ["@cf/meta/llama-2-7b-chat-int8"],
            "enabled": true,
            "credentials": { "api_key": "yyy" },
            "server_url": "https://api.cloudflare.com/..."
          }
        ]
      }
    }

    Load balancing between different models/services

    To let the backend randomly select a model from all enabled services, set load_balancing to random and ensure the desired models have enabled: true:

    {
      "server_port": ":9099",
      "load_balancing": "random",
      "services": {
        "openai": [
          {
            "models": ["@cf/meta/llama-2-7b-chat-int8"],
            "enabled": true,
            "credentials": { "api_key": "xxx" },
            "server_url": "https://api.cloudflare.com/..."
          }
        ],
        "ollama": [
          {
            "models": ["llama2"],
            "enabled": true,
            "server_url": "http://127.0.0.1:11434/api/chat"
          }
        ]
      }
    }
  3. Supported features of simple-one-api

    main

    The service provides several advanced management features for LLM orchestration:

    • Load Balancing: Supports configuring multiple models with random load balancing, and multiple api_keys per model with random load balancing.
    • Unified API: Provides a single external api_key and supports both /v1 and /v1/chat/completions paths.
    • Model Aliasing: Supports model name redirection (aliasing) and global redirection (using all mode to redirect all requests).
    • Automatic Model Selection: Supports a random model type that automatically finds an available configured model.
    • Prompt Compatibility: For models that do not support a system role, simple-one-api automatically prepends the system prompt to the first user prompt to ensure compatibility with clients like Immersive Translate.
    • Rate Limiting: Supports setting QPS (Queries Per Second), QPM (Queries Per Minute), or concurrency limits per service.
    • Endpoint Management: Allows setting specific service addresses for each model type.
    • Standard Endpoints: Supports /v1/models and /v1/models/:model for model discovery.
  4. Set up load balancing strategies

    main

    You can control how the API selects between multiple enabled services or multiple API keys for the same model using the load_balancing field. Supported values are:

    • first: Always picks the first enabled configuration.
    • random: Randomly selects an enabled configuration.

    To provide multiple API keys for a single model, add multiple objects to the service's array in services.

    {
        "api_key":"123456",
        "load_balancing": "random",
        "services": {
           "xinghuo": [
             {
               "models": ["spark-lite"],
               "enabled": true,
               "credentials": {
                 "appid": "xxx",
                 "api_key": "xxx",
                 "api_secret": "xxx"
               }
             },
             {
               "models": ["spark-lite"],
               "enabled": true,
               "credentials": {
                 "appid": "yyy",
                 "api_key": "yyy",
                 "api_secret": "yyy"
               }
             }
           ]
       }
    }
  5. Enable automatic load balancing in simple-one-api

    main

    To avoid concurrency limits and automatically distribute requests across multiple models or multiple accounts for the same model, use the load_balancing configuration key.

    Using random Load Balancing

    Setting load_balancing to random allows the server to automatically select a model from the pool of available services where enabled is set to true.

    Scenario 1: Mixing multiple different models

    If you want to rotate between different providers (e.g., Spark, Qianfan, and Hunyuan), include them in your services configuration and set load_balancing: "random".

    Scenario 2: Load balancing multiple accounts for a single model

    If you have multiple sets of credentials (e.g., multiple appids for spark-lite), you can define multiple entries for the same model within a service array. When load_balancing is set to random, simple-one-api will randomly select one of the enabled credential sets for each request.

    {
      "api_key":"sk-123456",
      "server_port":":9090",
      "load_balancing": "random",
      "services": {
        "qianfan": [
          {
            "models": ["yi_34b_chat", "ERNIE-Speed-8K"],
            "enabled": true,
            "credentials": {
              "api_key": "xxx",
              "secret_key": "xxx"
            }
          }
        ],
        "xinghuo": [
          {
            "models": ["spark-lite"],
            "enabled": true,
            "credentials": {
              "appid": "xxx",
              "api_key": "xxx",
              "api_secret": "xxx"
            }
          }
        ]
      }
    }
  6. Supported Model Providers and Features

    main

    simple-one-api acts as a unified proxy for various providers, normalizing them to the OpenAI API format.

    Supported Providers

    • OpenAI ChatGPT series (OpenAI, Azure OpenAI, Cloudflare Workers AI, Groq)
    • ByteDance Coze (Coze.com)
    • Ollama (Local LLM runner)

    Key Capabilities

    • Load Balancing: Supports random load balancing across multiple models or multiple api_keys for a single model.
    • Global Settings: Ability to set a global api_key or a global proxy mode.
    • Model Aliasing: Support for setting aliases for models.
    • Automatic Model Selection: Using the random model name automatically finds an enabled model.
    • Protocol Normalization: Supports both /v1 and /v1/chat/completions paths. For models that do not natively support a system role, simple-one-api automatically injects the system prompt into the first user prompt to ensure compatibility with clients like immersive translation.
  7. Apply for Tencent Hunyuan (hunyuan-lite) API access

    main

    To use the hunyuan-lite model via simple-one-api, you must first obtain an API key from the Tencent Cloud console.

    1. Visit the Tencent Hunyuan access page: https://console.cloud.tencent.com/hunyuan/start.
    2. Locate the option to create keys (创建密钥).
    3. On the new page, select 'New Key' (新建密钥) to generate your credentials.
    4. You can use the Tencent Cloud debugging interface to test your keys before integrating them into simple-one-api.
  8. Obtain API access for coze.cn

    main

    To use coze.cn models via simple-one-api, you must first create and publish a Bot on the coze.cn platform with API capabilities enabled. Follow these steps:

    1. Create a Bot: Navigate to the coze.cn platform and initiate the Bot creation process.
    2. Configure the Bot: Set up your Bot's configuration and select your desired model.
    3. Publish the Bot:
      • Click publish.
      • Select Skip and publish directly (跳过并直接发布).
      • Crucially, check the Bot as API option before clicking publish.
    4. Retrieve Credentials:
      • Personal Access Token: Generate this at https://www.coze.cn/open/api.
      • Bot ID: This is the numeric ID found in the URL of your Bot's development page (e.g., in .../bot/73428668*****, the ID is 73428668*****).
  9. Apply for iFlytek Spark-Lite (spark-lite) model credentials

    main

    To use the spark-lite model via simple-one-api, you must obtain credentials from the iFlytek Spark API platform.

    1. Visit the Spark API introduction page: https://xinghuo.xfyun.cn/sparkapi?scr=true.
    2. Access the iFlytek Console at https://console.xfyun.cn/services/cbm to retrieve your specific credentials.
    3. Locate the following three required pieces of information in the console:
      • appid
      • apikey
      • apisecret

    You can also use the iFlytek Debugging Center to test the model before integrating it.

  10. Configure Gemini in simple-one-api

    main

    To use Google Gemini models, create a new service entry with the type gemini in your configuration. You must provide your API key obtained from the Google AI Studio.

    Because the Gemini free tier has strict rate limits, it is highly recommended to configure the limit object within the service definition to prevent API errors.

    Gemini Free Tier Limits:

    • RPM (Requests Per Minute): 15
    • TPM (Tokens Per Minute): 1,000,000
    • RPD (Requests Per Day): 1,500
    {
      "server_port": ":9099",
      "log_level": "prodj",
      "load_balancing": "random",
      "services": {
        "gemini": [
          {
            "models": ["gemini-1.5-flash"],
            "enabled": true,
            "credentials": {
              "api_key": "xxx"
            },
            "limit": {
              "rpm": 15,
              "timeout": 120
            }
          }
        ]
      }
    }