modelrelay

repository·master·Indexed 43 days ago

https://github.com/ellipticmarketing/modelrelay

An OpenAI-compatible local router (v1.22.2) that benchmarks and forwards requests to the fastest and most capable free coding models across providers such as NVIDIA, Groq, and Google. It features QoS-based routing via the `auto-fastest` model, capability-based routing using tags (e.g., `tag:coding`), and minimum context window filtering. Supports integration with OpenCode and OpenClaw, and allows for custom OpenAI-compatible upstream endpoints.

Tokens
12.8K
Snippets
50
Records
69
Agent score
56%

What's inside modelrelay

  1. Understand QoS (Quality of Service) routing

    master

    Routing via auto-fastest, grouped-IDs, or tag:<name> uses a QoS score that balances model quality (intell percentile) and observed latency.

    Latency Scoring: Latency is scored using the formula: target / (target + avg).

    • An instant response scores 1.0.
    • A model matching the qosLatencyTargetMs scores 0.5.
    • The score decays continuously but never reaches zero, ensuring models remain as last-resort candidates.

    Configuration: The qosLatencyTargetMs (default: 3000ms) can be adjusted in the Web UI under Settings → QoS Latency Target (ms). Lowering this value weights speed more heavily; raising it allows quality to dominate.

  2. How model tags and minimum context work

    master

    Model tags allow you to route by capability rather than specific model names. Tags come from two sources:

    1. Curated tags: Fixed vocabulary including coding, reasoning, general, fast, and agentic.
    2. Custom tags: Freeform labels assigned via the Web UI under Custom Routing Tags.

    Filtering by Context Window

    To ensure a model can handle your prompt size, use the +min_ctx:<size> modifier.

    • tag:general+min_ctx:32000 (raw tokens)
    • tag:coding+min_ctx:128k (k suffix)
    • auto-fastest+min_ctx:1m (m suffix)

    Modelrelay uses provider-reported context or curated values from sources.js. If a provider's reported context is higher than its observed live rate-limit quota, modelrelay uses the smaller of the two for filtering.

  3. Configure OpenAI-compatible upstream endpoints

    master

    You can add custom OpenAI-compatible endpoints (like vLLM or llama.cpp) to modelrelay.

    Via Web UI:

    1. Click + Add Endpoint under the OpenAI-Compatible endpoints group.
    2. Provide a name, base URL, model ID, and optional API key.
    3. Discovery is enabled by default; modelrelay will probe /v1/models on the endpoint to expose its models.

    Via API:

    • POST /api/openai-compatible/endpoints (Body: {name, baseUrl, modelId, apiKey?})
    • DELETE /api/openai-compatible/endpoints/<id>

    Configuration Storage: Endpoints are stored in ~/.modelrelay.json under composite keys like openai-compatible:my-vllm.

    {
      "apiKeys": {
        "openai-compatible:my-vllm": "sk-…"
      },
      "providers": {
        "openai-compatible:my-vllm": {
          "enabled": true,
          "name": "Local vLLM",
          "baseUrl": "http://localhost:8000/v1",
          "modelId": "qwen-coder"
        }
      }
    }
  4. Install and run modelrelay via Docker

    master

    To run modelrelay using Docker Compose, create a directory, download the necessary files, and start the container:

    mkdir modelrelay
    
    cd modelrelay
    
    curl -fsSL -o Dockerfile https://raw.githubusercontent.com/ellipticmarketing/modelrelay/master/Dockerfile
    curl -fsSL -o docker-compose.yml https://raw.githubusercontent.com/ellipticmarketing/modelrelay/master/docker-compose.yml
    
    docker compose up -d --build

    Once running, the router is accessible at http://localhost:7352/.

  5. Transfer configuration between instances

    master

    To move your configuration (including API keys, provider toggles, and bans) to another machine:

    1. Using Web UI Tokens: In the Web UI, go to Settings -> Configuration Transfer to export/copy/import a token. Warning: Treat these tokens as secrets.
    2. Manual File Copy: Copy the ~/.modelrelay.json file directly to the target machine at the same path.
  6. Install and run modelrelay via NPM

    master

    To install modelrelay globally and start the router, use the following commands:

    npm install -g modelrelay
    
    # Start the router
    modelrelay

    Once running, the router is accessible at http://localhost:7352/.

    npm install -g modelrelay
    
    # Start it
    modelrelay
  7. Configure OpenClaw integration

    master

    You can automatically configure OpenClaw by running modelrelay onboard.

    For manual configuration, merge the following into ~/.openclaw/openclaw.json:

    {
      "models": {
        "providers": {
          "modelrelay": {
            "baseUrl": "http://127.0.0.1:7352/v1",
            "api": "openai-completions",
            "apiKey": "no-key",
            "models": [
              { "id": "auto-fastest", "name": "Auto Fastest" }
            ]
          }
        }
      },
      "agents": {
        "defaults": {
          "model": {
            "primary": "modelrelay/auto-fastest"
          },
          "models": {
            "modelrelay/auto-fastest": {}
          }
        }
      }
    }
  8. How model quality scoring works

    master

    Model quality in modelrelay is determined through a tiered scoring system that prioritizes empirical benchmarks over heuristics:

    1. Artificial Analysis Coding Index: If a model has a valid artificial_analysis.coding_index, it is used as the primary score (normalized to a 0-1 scale).
    2. Design Arena Elo (Estimated): If no coding index is available, the system uses a linear regression model to estimate a score based on the model's design_arena Elo rating.
    3. Metadata Heuristic: If neither benchmark is available, a score is calculated using metadataQuality, which considers:
      • Popularity: Rank within the catalog.
      • Recency: How recently the model was created.
      • Features: Support for reasoning, tools, and structured_outputs.
      • Context: The model's context_length (logarithmic scaling).
  9. Configure OpenCode integration

    master

    You can automatically configure OpenCode by running modelrelay onboard.

    For manual configuration, add the following to ~/.config/opencode/opencode.json:

    {
      "$schema": "https://opencode.ai/config.json",
      "provider": {
        "router": {
          "npm": "@ai-sdk/openai-compatible",
          "name": "modelrelay",
          "options": {
            "baseURL": "http://127.0.0.1:7352/v1",
            "apiKey": "dummy-key"
          },
          "models": {
            "auto-fastest": {
              "name": "Auto Fastest"
            }
          }
        }
      },
      "model": "router/auto-fastest"
    }
  10. Manage multiple OpenAI-compatible endpoints

    master

    modelrelay supports multiple independent OpenAI-compatible instances. Each instance is identified by a unique key following the pattern openai-compatible:<id>.

    Key Concepts

    • Instance ID: A unique identifier (e.g., my-local-server).
    • Instance Key: The internal key used in config, formatted as openai-compatible-my-local-server (sanitized to lowercase and hyphenated).
    • Default Instance: If you use legacy environment variables (OPENAI_COMPATIBLE_API_KEY, etc.) without a JSON entry, modelrelay treats them as the openai-compatible:default instance.

    Managing Endpoints via API

    • upsertOpenAICompatibleEndpoint(config, fields): Adds or updates an instance. Fields can include id, name, baseUrl, modelId, apiKey, enabled, and discoverModels.
    • removeOpenAICompatibleEndpoint(config, instanceKey): Deletes an instance and its associated API key.
    • listOpenAICompatibleEndpoints(config): Returns an array of all configured OpenAI-compatible endpoints with their current settings.
  11. Import and Export configuration tokens

    master

    You can move configurations between machines using encoded tokens. This is useful for syncing settings without manually copying JSON files.

    Exporting

    Use exportConfigToken(config) to generate a base64url encoded string prefixed with mrconf:v1:.

    Importing

    Use importConfigToken(token) to parse a token back into a normalized configuration object. The function supports:

    1. Tokens prefixed with mrconf:v1: (preferred).
    2. Raw JSON strings.
    3. Legacy base64 encoded payloads.
  12. Run modelrelay via Docker Compose

    master

    You can deploy modelrelay using Docker Compose. The service exposes port 7352 and uses a named volume modelrelay_config to persist configuration files at /app/config inside the container.

    Key environment variables:

    • NODE_ENV: Set to production by default.
    • HOME: Set to /app/config to ensure the application uses the persistent volume for its home directory/configuration.
    services:
      modelrelay:
        build: .
        container_name: modelrelay
        restart: unless-stopped
        ports:
          - "7352:7352"
        volumes:
          - modelrelay_config:/app/config
        environment:
          - NODE_ENV=production
          - HOME=/app/config
    
    volumes:
      modelrelay_config: