Skip to content

xuerzong/mas

Repository files navigation

MAS (Model Auto Switch)

English | 中文

OpenAI-compatible API proxy service that intelligently routes requests to multiple upstream providers based on content conditions.

What is this?

MAS is a lightweight FastAPI server that receives OpenAI-compatible chat completion requests, automatically selects the best model based on configured conditions, and proxies the request to the upstream API.

Key Features:

  • Smart Model Selection: Intelligently select the most suitable model based on message content
  • OpenAI Compatible: Drop-in replacement for OpenAI's /chat/completions endpoint
  • Service Management CLI: mas start/stop/status/restart/config commands
  • Streaming Support: Support both streaming and non-streaming responses

How to Use

Installation

./install.sh

Start the Service

mas start          # Start service (default port 8000)
mas status         # Check service status
mas stop           # Stop service
mas restart        # Restart service

Call the API

Health Check:

curl http://localhost:8000/health

Chat Completion (auto model selection):

curl -X POST http://localhost:8000/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [{"role": "user", "content": "Write a quicksort function"}],
    "stream": false
  }'

Streaming:

curl -X POST http://localhost:8000/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [{"role": "user", "content": "Hello"}],
    "stream": true
  }'

Configuration

Edit ~/.mas/config.json (auto-created on first run):

{
  "baseURL": "https://api.aiproxy.shop/v1",
  "apiKey": "your-api-key-here",
  "models": [
    {
      "condition": "programming, code examples, debugging",
      "model": "gpt-4"
    },
    {
      "condition": "casual chat, greetings, conversation",
      "model": "gpt-3.5-turbo"
    }
  ]
}

Configuration Fields:

  • baseURL: Upstream API endpoint URL
  • apiKey: Upstream API authentication key
  • models: List of model configurations
    • condition: Natural language description of when to use this model
    • model: Model identifier

Use Custom Config File:

export MAS_CONFIG_PATH=/path/to/config.json
mas start

View Current Configuration:

mas config

License

MIT

About

OpenAI-compatible API proxy service that intelligently routes requests to multiple upstream providers based on content conditions.

Resources

Stars

Watchers

Forks

Contributors