FastChat 概述

FastChat(原 Vicuna 项目)是 LMSYS Org 开发的开源大模型对话平台,核心功能:

  • 多模型对话:同时与多个模型对话,横向对比输出质量
  • Web UI:基于 Gradio 的聊天界面,支持流式输出
  • OpenAI 兼容 API:提供标准 API 接口
  • 分布式架构:Controller-Worker 分离,支持多 GPU 节点
  • 模型竞技场:Chatbot Arena 盲测打分系统的开源实现

FastChat 的定位更偏向模型评测和对比,而非纯生产服务。如果你想搭建一个多模型对比平台,FastChat 是最合适的开源方案。

架构解析

                    ┌─────────────┐
                    │ Controller  │  (中心调度)
                    └──────┬──────┘
              ┌────────────┼────────────┐
              │            │            │
        ┌─────┴─────┐ ┌───┴─────┐ ┌───┴─────┐
        │ Worker 0  │ │Worker 1 │ │Worker 2 │  (模型推理)
        │ Vicuna-13B│ │LLaMA-70B│ │Qwen-14B │
        └───────────┘ └─────────┘ └─────────┘
              │            │            │
        ┌─────┴─────────────┴─────────────┘
        │     Gradio Web Server / API     │
        └─────────────────────────────────┘
  • Controller:注册中心,管理所有 Worker 的模型信息,做负载均衡
  • Worker:加载模型,处理推理请求,可分布在多台 GPU 机器
  • Web Server:Gradio 界面 + REST API

安装

pip install fschat[model_worker,webui]

或从源码安装最新版:

git clone https://github.com/lm-sys/FastChat.git
cd FastChat
pip install -e ".[model_worker,webui]"

单机部署

1. 启动 Controller

python -m fastchat.serve.controller \
  --host 0.0.0.0 \
  --port 21001

2. 启动 Worker(加载模型)

# Worker 1: Qwen2.5-14B
python -m fastchat.serve.model_worker \
  --model-path Qwen/Qwen2.5-14B-Instruct \
  --model-name qwen2.5-14b \
  --controller http://localhost:21001 \
  --worker http://localhost:21002 \
  --device cuda \
  --num-gpus 1

# Worker 2: 另一个模型
python -m fastchat.serve.model_worker \
  --model-path lmsys/vicuna-13b-v1.5 \
  --model-name vicuna-13b \
  --controller http://localhost:21001 \
  --worker http://localhost:21003 \
  --device cuda \
  --num-gpus 1

3. 启动 Web UI

python -m fastchat.serve.gradio_web_server \
  --controller http://localhost:21001 \
  --host 0.0.0.0 \
  --port 7860 \
  --model-list-mode reload

访问 http://localhost:7860,可以在下拉菜单选择模型,开始对话。

多模型对比界面

python -m fastchat.serve.gradio_web_server_multi \
  --controller http://localhost:21001 \
  --host 0.0.0.0 \
  --port 7860

这个界面可以同时向两个模型提问,并排显示回答,非常适合对比评测。

API 服务

FastChat 提供 OpenAI 兼容的 API:

python -m fastchat.serve.openai_api_server \
  --controller http://localhost:21001 \
  --host 0.0.0.0 \
  --port 8000

调用方式与 OpenAI 完全一致:

from openai import OpenAI

client = OpenAI(
    api_key="empty",
    base_url="http://localhost:8000/v1"
)

# 列出可用模型
models = client.models.list()
for m in models.data:
    print(m.id)  # qwen2.5-14b, vicuna-13b

# 对话
response = client.chat.completions.create(
    model="qwen2.5-14b",
    messages=[
        {"role": "system", "content": "你是技术助手"},
        {"role": "user", "content": "解释 Transformer 架构"}
    ],
    temperature=0.7,
    stream=True
)

for chunk in response:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

分布式部署

多 GPU 节点部署时,Worker 分布在不同机器上:

节点 1(Controller + Web Server)

# Controller
python -m fastchat.serve.controller --host 0.0.0.0 --port 21001

# Web Server
python -m fastchat.serve.gradio_web_server \
  --controller http://node1:21001 \
  --port 7860

节点 2(GPU Worker)

python -m fastchat.serve.model_worker \
  --model-path Qwen/Qwen2.5-72B-Instruct \
  --model-name qwen2.5-72b \
  --controller http://node1:21001 \
  --worker http://node2:21002 \
  --num-gpus 4  # 4 卡张量并行

节点 3(GPU Worker)

python -m fastchat.serve.model_worker \
  --model-path meta-llama/Llama-3.1-70B-Instruct \
  --model-name llama-70b \
  --controller http://node1:21001 \
  --worker http://node3:21002 \
  --num-gpus 4

Docker Compose 分布式

version: '3.8'

services:
  controller:
    image: fastchat:latest
    command: python -m fastchat.serve.controller --host 0.0.0.0 --port 21001
    ports:
      - "21001:21001"

  worker-qwen:
    image: fastchat:latest
    command: >
      python -m fastchat.serve.model_worker
      --model-path Qwen/Qwen2.5-14B-Instruct
      --model-name qwen2.5-14b
      --controller http://controller:21001
      --worker http://0.0.0.0:21002
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]
    depends_on:
      - controller

  web:
    image: fastchat:latest
    command: >
      python -m fastchat.serve.gradio_web_server_multi
      --controller http://controller:21001
      --host 0.0.0.0 --port 7860
    ports:
      - "7860:7860"
    depends_on:
      - controller
      - worker-qwen

Docker 一键脚本

#!/bin/bash
# deploy_fastchat.sh
docker compose up -d

# 等待 Worker 注册
sleep 30

# 检查状态
curl http://localhost:21001/list_models

与 vLLM 集成加速

FastChat Worker 原生推理速度一般。生产环境可以搭配 vLLM 做推理后端:

# 用 vLLM 启动推理服务
python -m vllm.entrypoints.openai.api_server \
  --model Qwen/Qwen2.5-14B-Instruct \
  --port 8000

# FastChat Worker 指向 vLLM
python -m fastchat.serve.model_worker \
  --model-names qwen2.5-14b \
  --controller http://localhost:21001 \
  --worker http://localhost:21002 \
  --model-path Qwen/Qwen2.5-14B-Instruct \
  --vllm-port 8000  # 使用 vLLM 后端

这样 FastChat 负责多模型管理和 Web UI,vLLM 负责高性能推理,各司其职。

性能调优

配置项推荐值说明
--num-gpus模型层数/4张量并行度
--max-gpu-memory0.9显存利用率上限
--load-8bit大模型用8bit 量化省显存
--conv-template按模型选对话模板,影响格式
--limit-model-concurrency4-8并发上限

对话模板

FastChat 内置了 60+ 种对话模板,不同模型需要不同的模板:

# 查看所有可用模板
python -m fastchat.serve.model_worker --help | grep conv

# 指定模板
python -m fastchat.serve.model_worker \
  --model-path Qwen/Qwen2.5-14B-Instruct \
  --conv-template qwen  # 关键参数

模板选错会导致模型输出格式混乱、性能下降。

FastChat vs Open WebUI vs vLLM

特性FastChatOpen WebUIvLLM
定位模型对比评测本地 ChatGPT推理引擎
多模型对比⭐⭐⭐⭐⭐⭐⭐
Web UIGradio自研 React
OpenAI API
RAG⭐⭐⭐⭐⭐
推理性能⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
用户管理⭐⭐⭐⭐
分布式⭐⭐⭐⭐⭐⭐⭐⭐⭐

小结

FastChat 最适合两类场景:一是模型评测对比,多模型并排回答、盲测打分;二是研究环境的多模型管理。生产环境推荐 FastChat(管理)+ vLLM(推理)组合。如果只需要单模型对话,Open WebUI 更合适;如果只需要 API 不需要 UI,直接用 vLLM。

加入讨论

这篇文章有姊妹讨论帖在硅基AGI论坛 — 全球首个碳基硅基认知交流平台。

碳基与硅基的智慧碰撞,认知差异创造无限可能。