Skip to content

zhangrr/image-route

Repository files navigation

ImgRouter

三合一图像生成 API 中转服务 - 一个接口,多渠道图像生成

Deno Docker License

快速开始

# 1. 复制配置文件模板
cp config.example.json config.json

# 2. 编辑配置文件,填入你的 API Keys
# 修改 config.json 中的以下字段:
# - server.url: 你的服务器访问地址(如 http://10.8.2.26:8080)
# - apiKeys.volcengine: 火山引擎 API Key
# - apiKeys.gitee: Gitee AI API Key
# - apiKeys.modelscope: ModelScope API Key

# 3. 启动服务
deno task start

# 4. 访问 Web 界面
浏览器打开配置文件中的 server.url 地址

统一端口

  • 🌐 8080 - Web 界面 + API 接口(统一端口)

详细启动指南请查看 QUICKSTART.md

特性

  • 智能路由 - 根据模型名称自动识别并分发到对应渠道
  • 三渠道支持 - 火山引擎、Gitee (模力方舟)、ModelScope (魔塔)
  • JSON 配置 - 使用 config.json 统一管理所有配置和 API Keys
  • OpenAI 兼容 - 完全兼容 /v1/chat/completions 接口格式
  • 流式响应 - 支持 SSE 流式输出
  • 图片参考 - 支持上传参考图片进行图生图
  • Docker 部署 - 开箱即用的容器化部署方案
  • 详细日志 - 完整的请求/响应日志记录

架构

┌─────────────────────────────────────────────────────────────┐
│                      客户端请求                             │
│              POST /v1/chat/completions                      │
│              Authorization: Bearer <ACCESS_TOKEN>           │
└─────────────────────┬───────────────────────────────────────┘
                       │
                       ▼
┌─────────────────────────────────────────────────────────────┐
│                   模型名称路由器                            │
│  ┌──────────────────┬──────────────────┬─────────────────┐  │
│  │ doubao-seedream  │ z-image-turbo    │ Tongyi-MAI/...  │  │
│  │ → VolcEngine     │ → Gitee/HF       │ → ModelScope    │  │
│  └──────────────────┴──────────────────┴─────────────────┘  │
└─────────────────────┬───────────────────────────────────────┘
                       │
           ┌───────────┼───────────┬───────────┐
           ▼           ▼           ▼           ▼
     ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐
     │VolcEngine│ │  Gitee   │ │ModelScope│ │HuggingFac│
     │ (火山)   │ │(模力方舟)│ │  (魔塔)  │ │ (抱脸)   │
     │ API Key  │ │ API Key  │ │ API Key  │ │ API Key  │
     └──────────┘ └──────────┘ └──────────┘ └──────────┘

快速开始

Docker Compose (推荐)

git clone https://github.com/lianwusuoai/img-router.git
cd img-router

# 配置环境变量
cat > .env <<EOF
ACCESS_TOKEN=your-secret-token-here
VOLCENGINE_API_KEY=your-volcengine-api-key
GITEE_API_KEY=your-gitee-api-key
MODELSCOPE_API_KEY=your-modelscope-api-key
EOF

docker-compose up -d

Docker 直接运行

docker build -t img-router .
docker run -d --name img-router -p 8080:8080 img-router

本地开发

# 安装 Deno
# Windows: irm https://deno.land/install.ps1 | iex
# macOS/Linux: curl -fsSL https://deno.land/install.sh | sh

# 配置环境变量
export ACCESS_TOKEN=your-secret-token-here
export VOLCENGINE_API_KEY=your-volcengine-api-key
export GITEE_API_KEY=your-gitee-api-key
export MODELSCOPE_API_KEY=your-modelscope-api-key

# 开发模式
deno task dev

# 生产模式
deno task start

使用方法

注意:客户端无需提供各渠道的真实 API Key。系统会根据 model 参数自动选择对应渠道。

基本请求

curl -X POST http://localhost:10001/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "doubao-seedream-4-0-250828",
    "messages": [{"role": "user", "content": "一只可爱的猫咪"}],
    "size": "1024x1024"
  }'

带参考图片

curl -X POST http://localhost:10001/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "doubao-seedream-4-0-250828",
    "messages": [{
      "role": "user",
      "content": [
        {"type": "text", "text": "转换为水彩画风格"},
        {"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}
      ]
    }]
  }'

流式响应

curl -X POST http://localhost:10001/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "doubao-seedream-4-0-250828",
    "messages": [{"role": "user", "content": "美丽的风景"}],
    "stream": true
  }'

支持的模型

渠道 模型名称 说明
火山引擎 doubao-seedream-4-0-250828 豆包生图 4.0(默认)
火山引擎 doubao-seedream-4-5-251128 豆包生图 4.5
Gitee z-image-turbo Z-Image Turbo
ModelScope Tongyi-MAI/Z-Image-Turbo 通义万相

系统根据 model 参数自动路由到对应渠道。如果不指定 model 或指定了未知模型,默认使用Gitee引擎。

Web 测试界面

ImgRouter 内置了一个类 ChatGPT 风格的 Web 界面,启动服务后可直接访问。

访问 Web 界面

启动服务后,Web 界面会自动在 8080 端口启动:

# 启动服务
deno task start

# 访问 Web 界面
浏览器访问: http://localhost:8080

两个端口说明

  • 8080 端口 - Web 界面+API服务(供浏览器+程序访问)

界面功能

  • 🎨 模型选择 - 顶部下拉框切换不同渠道的模型
  • 💬 对话式交互 - 类似 ChatGPT 的聊天界面
  • 🖼️ 实时预览 - 生成的图片直接显示在对话中
  • ⚙️ 配置管理 - 可自定义 API 地址和访问令牌
  • 📱 响应式设计 - 支持移动端和桌面端

首次使用配置

  1. 访问 http://localhost:8080
  2. 选择模型并输入提示词开始生成

配置

环境变量

变量 说明 必需 默认值
VOLCENGINE_API_KEY 火山引擎 API Key -
GITEE_API_KEY Gitee API Key -
MODELSCOPE_API_KEY ModelScope API Key -
PORT 监听端口 8080
LOG_LEVEL 日志级别 (DEBUG/INFO/WARN/ERROR) INFO

说明

  • 各渠道 API Key 按需配置,只需配置你要使用的渠道
  • 如果某个渠道的 API Key 未配置,访问该渠道时会返回错误

默认模型

渠道 默认模型
火山引擎 doubao-seedream-4-0-250828
Gitee z-image-turbo
ModelScope Tongyi-MAI/Z-Image-Turbo

Docker 环境变量配置

docker-compose.yml 中添加环境变量:

services:
  img-router:
    image: img-router:latest
    ports:
      - "8080:8080"
    environment:
      - VOLCENGINE_API_KEY=your-volcengine-api-key
      - GITEE_API_KEY=your-gitee-api-key
      - MODELSCOPE_API_KEY=your-modelscope-api-key
      - LOG_LEVEL=INFO

或使用 .env 文件(推荐):

# .env
VOLCENGINE_API_KEY=your-volcengine-api-key
GITEE_API_KEY=your-gitee-api-key
MODELSCOPE_API_KEY=your-modelscope-api-key

响应格式

成功响应

{
  "id": "chatcmpl-xxx",
  "object": "chat.completion",
  "created": 1734323445,
  "model": "doubao-seedream-4-0-250828",
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "![Generated Image](https://example.com/image.jpg)"
    },
    "finish_reason": "stop"
  }]
}

错误响应

{
  "error": {
    "message": "API Error: ...",
    "type": "server_error",
    "provider": "VolcEngine"
  }
}

开发

deno fmt      # 格式化代码
deno lint     # 代码检查
deno check main.ts  # 类型检查

许可证

MIT License

贡献

欢迎提交 Issue 和 Pull Request!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors