qwen2api

repository·main·Indexed 20 days ago

https://github.com/rfym21/qwen2api

Qwen-Proxy is a proxy service that translates Qwen's proprietary web and CLI interfaces into OpenAI and Anthropic compatible APIs. It supports OpenAI tools and Anthropic Messages API, multi-account polling, multi-modal inputs, and specialized modes for search and deep thinking. The service includes a /cli endpoint for Qwen Code / Qwen Cli with 256K context and provides a Web UI for account and proxy configuration.

Tokens
18.3K
Snippets
56
Records
73
Agent score
69%

What's inside qwen2api

  1. What is Qwen-Proxy and its core capabilities

    main

    Qwen-Proxy is a proxy service that transforms https://chat.qwen.ai and Qwen Code / Qwen Cli into an OpenAI-compatible API. This allows you to use any OpenAI-compatible client (e.g., ChatGPT-Next-Web, LobeChat) to access Qwen models.

    Key Features:

    • OpenAI Compatibility: Seamless integration with existing AI clients.
    • Account Rotation: Supports rotating multiple accounts to increase availability.
    • Advanced Model Support: Supports streaming/non-streaming responses, multi-modality (image recognition/generation), intelligent search, and deep reasoning.
    • CLI Endpoint: Provides a /cli endpoint with 256K context and native tool calling support.
    • Management Dashboard: A web interface for configuration and monitoring.
    • Mass Account Management: Real-time progress display and configurable login concurrency.
  2. What is Qwen-Proxy?

    main

    Qwen-Proxy is a proxy service that converts https://chat.qwen.ai and Qwen Code / Qwen Cli into OpenAI-compatible APIs. It allows you to use any OpenAI-compatible client (such as ChatGPT-Next-Web or LobeChat) to call various Qwen models using a single account.

    Key Features:

    • OpenAI Compatibility: Seamlessly integrates with OpenAI-compatible clients.
    • Anthropic Compatibility: Supports Anthropic Messages API (/v1/messages) for clients like Claude Code and the Anthropic SDK.
    • Function Calling: Supports OpenAI tools and Anthropic tools, including streaming arguments incremental chunks and tool_choice=required retry logic.
    • Multi-Account Support: Supports multi-account polling to improve availability.
    • Advanced Capabilities: Supports streaming/non-streaming responses, multi-modal inputs (image/video recognition and generation), intelligent search, and deep thinking.
    • CLI Endpoint: Provides a /cli endpoint with 256K context and tool-calling capabilities via Qwen Code / Qwen Cli.
  3. Project Structure Overview

    main

    The project is divided into a backend (src/) and a frontend (public/).

    Backend (src/)

    • server.js: The main entry point for the server.
    • start.js: The smart start script for process management.
    • controllers/: Logic for chat, image/video generation, and models.
    • middlewares/: Handles authorization and chat-specific logic.
    • routes/: Defines API endpoints for accounts, chat, models, settings, and verification.
    • utils/: Core utilities including account-rotator.js, cookie-generator.js, proxy-helper.js, and fingerprint.js.

    Frontend (public/)

    • Built with Vue.js and Vite.
    • Contains views for auth, dashboard, and settings.
  4. Understand the Qwen2API Project Structure

    main

    The project is divided into a backend (src/) and a frontend (public/).

    Backend (src/) Key Components:

    • server.js: The main entry point for the server.
    • start.js: An intelligent startup script that automatically detects whether to run in single-process or multi-process mode.
    • config/: Contains configuration logic.
    • controllers/: Handles core logic for chat, image/video generation, CLI chat, and models.
    • routes/: Defines API endpoints for accounts, chat, CLI, models, settings, and verification.
    • middlewares/: Includes authorization and chat-specific middleware.
    • utils/: Contains critical utilities such as account-rotator.js (for account rotation), cookie-generator.js, fingerprint.js (for browser fingerprinting), and proxy-helper.js.
    • data/: Stores persistent data in data.json.

    Frontend (public/) Key Components:

    • src/: Vue.js source code including views for auth.vue, dashboard.vue, and settings.vue.
    • dist/: The directory containing compiled production assets.

    Infrastructure & Deployment:

    • docker/: Contains Dockerfile and docker-compose.yml for containerized deployment.
    • ecosystem.config.js: PM2 configuration file for process management.
  5. Configure per-account proxies

    main

    To prevent mass bans caused by IP correlation, you can assign a specific outgoing proxy to each individual account. The priority for proxy selection is: account.proxy > global PROXY_URL > no proxy.

    Supported protocols: HTTP, HTTPS, SOCKS5.

    Methods of Configuration:

    1. Via Dashboard (Recommended): Open the dashboard, fill in the "Proxy URL" field when adding an account, or click "Change proxy" on an existing account card.
    2. Via Environment Variables (when DATA_SAVE_MODE=none):
      • Old Format: ACCOUNTS=user1@mail.com:pass1,user2@mail.com:pass2 (no proxy specified).
      • New Format: Use | to separate the proxy URL. You can mix old and new formats.

    Example (New Format):

    ACCOUNTS=user1@mail.com:pass1|http://10.0.0.1:8080,user2@mail.com:pass2|socks5://10.0.0.2:1080

    Example (JSON data/data.json in file mode):

    {
      "accounts": [
        {
          "email": "user@mail.com",
          "password": "...",
          "token": "...",
          "expires": 1234567890,
          "proxy": "http://10.0.0.1:8080"
        }
      ]
    }

    ⚠️ Warning: Proxy URLs in API responses are not masked. This project is intended for use in trusted local/private networks with a single administrator.

    # New format (URL proxy via `|`, can be mixed with old format)
    ACCOUNTS=user1@mail.com:pass1|http://10.0.0.1:8080,user2@mail.com:pass2|socks5://10.0.0.2:1080
  6. Manage API Key Permissions

    main

    The API_KEY environment variable supports multiple comma-separated keys to implement different access levels.

    Key TypePermissionsDescription
    Administrator Key (First key)FullAccess Web UI, modify settings, call all APIs, manage other keys.
    Regular Key (Subsequent keys)API OnlyCall API endpoints only. Cannot access Web UI or modify settings.

    Example Configuration:

    # Single admin key
    API_KEY=sk-admin123
    
    # Admin key followed by regular user keys
    API_KEY=sk-admin123,sk-user456,sk-user789
    API_KEY=sk-admin123,sk-user456,sk-user789
  7. Use Smart Mode for process management

    main

    The project includes a 'Smart Mode' via npm start that automatically detects how it should be executed based on the PM2_INSTANCES environment variable:

    • If PM2_INSTANCES=1: Runs in single-process mode.
    • If PM2_INSTANCES>1: Runs in Node.js cluster mode.

    The number of processes is automatically capped by the number of available CPU cores.

  8. Use Function Calling (Tool Use)

    main

    The /v1/chat/completions endpoint supports the full OpenAI Function Calling protocol. Even if the upstream web interface lacks native tool capabilities, this service simulates them via prompt injection and streaming state machine parsing.

    Key Features:

    • Token Efficiency: Automatically compresses tools[] into TS-style signatures in the prompt, saving ~70% tokens.
    • Streaming Support: Follows OpenAI spec for chunking function.name and arguments.
    • Tool Choice: Supports "auto", "required", "none", and specific function selection.
    • Automatic Retry: If tool_choice="required" is set but no tool is triggered on the first attempt, the service automatically appends a constraint hint to retry.
    {
      "model": "qwen3-coder-plus",
      "stream": true,
      "messages": [
        {"role": "user", "content": "Check the weather in Beijing"}
      ],
      "tools": [
        {
          "type": "function",
          "function": {
            "name": "get_weather",
            "description": "Get city weather",
            "parameters": {
              "type": "object",
              "properties": { "city": { "type": "string" } },
              "required": ["city"]
            }
          }
        }
      ],
      "tool_choice": "required"
    }
  9. Use the Anthropic-compatible Messages API bridge

    main

    Qwen2API provides an Anthropic-compatible bridge at the /v1/messages endpoint. This allows tools like Claude Code, Anthropic SDK, and aider to work with Qwen models.

    Important Compatibility Notes: Qwen2API is a compatibility layer, not a native Anthropic backend. Some fields are implemented via 'soft' strategies (prompt injection or simulation) rather than native upstream semantics.

    • Supported: model, messages.text, messages.image, stream.
    • Partial Support: messages.tool_use, messages.tool_result, system (injected as prompt prefix), messages (multi-turn history compression), tools[] (via prompt/XML simulation), tool_choice (via prompt steering), thinking (legacy format support).
    • Ignored with Warning: max_tokens, stop_sequences, metadata, temperature, top_p, top_k, service_tier, container, output_config. These fields will not affect upstream behavior but will trigger warnings in response headers.
    • Not Supported: mcp_servers, context_management.

    If unsupported or partially supported fields are used, the API may return these headers:

    • X-Qwen2API-Anthropic-Compatibility
    • X-Qwen2API-Anthropic-Warnings
  10. Use advanced model modes (Search, Thinking, Combined)

    main

    You can enable specialized capabilities by appending suffixes to the model name:

    • Search Mode: Append -search (e.g., qwen-max-latest-search).
    • Reasoning Mode: Append -thinking (e.g., qwen-max-latest-thinking).
    • Combined Mode: Append -thinking-search (e.g., qwen-max-latest-thinking-search) to enable both reasoning and search capabilities simultaneously.
  11. Use Smart Start Mode

    main

    Running npm start triggers a smart startup script that automatically determines whether to run in single-process or multi-process (cluster) mode based on the PM2_INSTANCES environment variable:

    • If PM2_INSTANCES=1: Runs in single-process mode.
    • If PM2_INSTANCES > 1: Runs in Node.js cluster mode.
    • The script automatically limits the process count to the number of available CPU cores.
  12. Configure Multi-API_KEY Permissions

    main

    The API_KEY environment variable allows for tiered access control by separating administrative and usage permissions.

    Key TypePermission ScopeFunction Description
    Admin KeyFull PermissionsAccess frontend management, modify system settings, call all APIs, add/delete regular keys
    Regular KeyAPI Call PermissionsAPI interface calls only; cannot access frontend or modify settings

    Note: The first key provided in the API_KEY list is automatically designated as the Admin Key.