关于 sglang
SGLang 是用于部署大模型和视觉语言模型的框架,支持结构化生成,并通过 RadixAttention 缓存共享提示词前缀。它适用于构建 Agent 工作流、多轮应用和模型推理服务的团队。
主要功能
- 01RadixAttention 前缀缓存
- 02JSON、正则表达式和语法约束生成
- 03函数调用工作流
- 04多模态视觉模型推理
- 05兼容 OpenAI 的 API
适用场景
- 01为重复使用系统提示词的 Agent 提供服务
- 02生成符合架构约束的输出
- 03运行共享上下文的多轮对话
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ orchestra-research/ ai-research-skills --skill sglang安装后在 Claude Code 中输入/sglang确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/deployment.mdradix-attention.mdstructured-generation.md详细介绍
SGLang
面向大模型和 VLM 的高性能服务框架,借助 RadixAttention 实现自动前缀缓存。
何时使用 SGLang
在以下情况下使用 SGLang:
- 需要结构化输出(JSON、正则、语法)
- 构建带有重复前缀(系统提示词、工具)的 Agent
- 带函数调用的 Agentic 工作流
- 共享上下文的多轮对话
- 需要更快的 JSON 解码(相比标准方式快 3×)
在以下情况下改用 vLLM:
- 无需结构的简单文本生成
- 不需要前缀缓存
- 想要成熟、经过广泛测试的生产系统
在以下情况下改用 TensorRT-LLM:
- 追求单请求延迟最低(无需批处理)
- 仅限 NVIDIA 部署
- 需要在 H100 上进行 FP8/INT4 量化
快速开始
安装
# pip install (recommended)
pip install "sglang[all]"
# With FlashInfer (faster, CUDA 11.8/12.1)
pip install sglang[all] flashinfer -i https://flashinfer.ai/whl/cu121/torch2.4/
# From source
git clone https://github.com/sgl-project/sglang.git
cd sglang
pip install -e "python[all]"
启动服务
# Basic server (Llama 3-8B)
python -m sglang.launch_server \
--model-path meta-llama/Meta-Llama-3-8B-Instruct \
--port 30000
# With RadixAttention (automatic prefix caching)
python -m sglang.launch_server \
--model-path meta-llama/Meta-Llama-3-8B-Instruct \
--port 30000 \
--enable-radix-cache # Default: enabled
# Multi-GPU (tensor parallelism)
python -m sglang.launch_server \
--model-path meta-llama/Meta-Llama-3-70B-Instruct \
--tp 4 \
--port 30000
基础推理
import sglang as sgl
# Set backend
sgl.set_default_backend(sgl.OpenAI("http://localhost:30000/v1"))
# Simple generation
@sgl.function
def simple_gen(s, question):
s += "Q: " + question + "\n"
s += "A:" + sgl.gen("answer", max_tokens=100)
# Run
state = simple_gen.run(question="What is the capital of France?")
print(state["answer"])
# Output: "The capital of France is Paris."
结构化 JSON 输出
import sglang as sgl
@sgl.function
def extract_person(s, text):
s += f"Extract person information from: {text}\n"
s += "Output JSON:\n"
# Constrained JSON generation
s += sgl.gen(
"json_output",
max_tokens=200,
regex=r'\{"name": "[^"]+", "age": \d+, "occupation": "[^"]+"\}'
)
# Run
state = extract_person.run(
text="John Smith is a 35-year-old software engineer."
)
print(state["json_output"])
# Output: {"name": "John Smith", "age": 35, "occupation": "software engineer"}
RadixAttention(核心创新)
它做什么:自动缓存并复用跨请求的公共前缀。
性能:
- 对于共享系统提示词的 Agentic 工作负载,快 5×
- 对于重复示例的少样本提示,快 10×
- 零配置 - 自动生效
工作原理:
- 为所有已处理的 token 构建基数树
- 自动检测共享前缀
- 为匹配的前缀复用 KV 缓存
- 只计算新的 token
示例(带系统提示词的 Agent):
Request 1: [SYSTEM_PROMPT] + "What's the weather?"
→ Computes full prompt (1000 tokens)
Request 2: [SAME_SYSTEM_PROMPT] + "Book a flight"
→ Reuses system prompt KV cache (998 tokens)
→ Only computes 2 new tokens
→ 5× faster!
结构化生成模式
带 schema 的 JSON
@sgl.function
def structured_extraction(s, article):
s += f"Article: {article}\n\n"
s += "Extract key information as JSON:\n"
# JSON schema constraint
schema = {
"type": "object",
"properties": {
"title": {"type": "string"},
"author": {"type": "string"},
"summary": {"type": "string"},
"sentiment": {"type": "string", "enum": ["positive", "negative", "neutral"]}
},
"required": ["title", "author", "summary", "sentiment"]
}
s += sgl.gen("info", max_tokens=300, json_schema=schema)
state = structured_extraction.run(article="...")
print(state["info"])
# Output: Valid JSON matching schema
正则约束生成
@sgl.function
def extract_email(s, text):
s += f"Extract email from: {text}\n"
s += "Email: "
# Email regex pattern
s += sgl.gen(
"email",
max_tokens=50,
regex=r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
)
state = extract_email.run(text="Contact [email protected] for details")
print(state["email"])
# Output: "[email protected]"
基于语法的生成
@sgl.function
def generate_code(s, description):
s += f"Generate Python code for: {description}\n"
s += "```python\n"
# EBNF grammar for Python
python_grammar = """
?start: function_def
function_def: "def" NAME "(" [parameters] "):" suite
parameters: parameter ("," parameter)*
parameter: NAME
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
"""
s += sgl.gen("code", max_tokens=200, grammar=python_grammar)
s += "\n```"
带函数调用的 Agent 工作流
import sglang as sgl
# Define tools
tools = [
{
"name": "get_weather",
"description": "Get weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"}
}
}
},
{
"name": "book_flight",
"description": "Book a flight",
"parameters": {
"type": "object",
"properties": {
"from": {"type": "string"},
"to": {"type": "string"},
"date": {"type": "string"}
}
}
}
]
@sgl.function
def agent_workflow(s, user_query, tools):
# System prompt (cached with RadixAttention)
s += "You are a helpful assistant with access to tools.\n"
s += f"Available tools: {tools}\n\n"
# User query
s += f"User: {user_query}\n"
s += "Assistant: "
# Generate with function calling
s += sgl.gen(
"response",
max_tokens=200,
tools=tools, # SGLang handles tool call format
stop=["User:", "\n\n"]
)
# Multiple queries reuse system prompt
state1 = agent_workflow.run(
user_query="What's the weather in NYC?",
tools=tools
)
# First call: Computes full system prompt
state2 = agent_workflow.run(
user_query="Book a flight to LA",
tools=tools
)
# Second call: Reuses system prompt (5× faster)
性能基准
RadixAttention 加速
少样本提示(提示中包含 10 个示例):
- vLLM:2.5 秒/请求
- SGLang:0.25 秒/请求(快 10×)
- 吞吐量:高 4×
Agent 工作流(1000 token 系统提示词):
- vLLM:1.8 秒/请求
- SGLang:0.35 秒/请求(快 5×)
JSON 解码:
- 标准:45 tok/s
- SGLang:135 tok/s(快 3×)
吞吐量(Llama 3-8B,A100)
| 工作负载 | vLLM | SGLang | 加速 |
|---|---|---|---|
| 简单生成 | 2500 tok/s | 2800 tok/s | 1.12× |
| 少样本(10 个示例) | 500 tok/s | 5000 tok/s | 10× |
| Agent(工具调用) | 800 tok/s | 4000 tok/s | 5× |
| JSON 输出 | 600 tok/s | 2400 tok/s | 4× |
多轮对话
@sgl.function
def multi_turn_chat(s, history, new_message):
# System prompt (always cached)
s += "You are a helpful AI assistant.\n\n"
# Conversation history (cached as it grows)
for msg in history:
s += f"{msg['role']}: {msg['content']}\n"
# New user message (only new part)
s += f"User: {new_message}\n"
s += "Assistant: "
s += sgl.gen("response", max_tokens=200)
# Turn 1
history = []
state = multi_turn_chat.run(history=history, new_message="Hi there!")
history.append({"role": "User", "content": "Hi there!"})
history.append({"role": "Assistant", "content": state["response"]})
# Turn 2 (reuses Turn 1 KV cache)
state = multi_turn_chat.run(history=history, new_message="What's 2+2?")
# Only computes new message (much faster!)
# Turn 3 (reuses Turn 1 + Turn 2 KV cache)
state = multi_turn_chat.run(history=history, new_message="Tell me a joke")
# Progressively faster as history grows
高级功能
投机解码
# Launch with draft model (2-3× faster)
python -m sglang.launch_server \
--model-path meta-llama/Meta-Llama-3-70B-Instruct \
--speculative-model meta-llama/Meta-Llama-3-8B-Instruct \
--speculative-num-steps 5
多模态(视觉模型)
@sgl.function
def describe_image(s, image_path):
s += sgl.image(image_path)
s += "Describe this image in detail: "
s += sgl.gen("description", max_tokens=200)
state = describe_image.run(image_path="photo.jpg")
print(state["description"])
批处理与并行请求
# Automatic batching (continuous batching)
states = sgl.run_batch(
[
simple_gen.bind(question="What is AI?"),
simple_gen.bind(question="What is ML?"),
simple_gen.bind(question="What is DL?"),
]
)
# All 3 processed in single batch (efficient)
OpenAI 兼容 API
# Start server with OpenAI API
python -m sglang.launch_server \
--model-path meta-llama/Meta-Llama-3-8B-Instruct \
--port 30000
# Use with OpenAI client
curl http://localhost:30000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "default",
"messages": [
{"role": "system", "content": "You are helpful"},
{"role": "user", "content": "Hello"}
],
"temperature": 0.7,
"max_tokens": 100
}'
# Works with OpenAI Python SDK
from openai import OpenAI
client = OpenAI(base_url="http://localhost:30000/v1", api_key="EMPTY")
response = client.chat.completions.create(
model="default",
messages=[{"role": "user", "content": "Hello"}]
)
支持的模型
文本模型:
- Llama 2、Llama 3、Llama 3.1、Llama 3.2
- Mistral、Mixtral
- Qwen、Qwen2、QwQ
- DeepSeek-V2、DeepSeek-V3
- Gemma、Phi-3
视觉模型:
- LLaVA、LLaVA-OneVision
- Phi-3-Vision
- Qwen2-VL
来自 HuggingFace 的 100+ 模型
硬件支持
NVIDIA:A100、H100、L4、T4(CUDA 11.8+) AMD:MI300、MI250(ROCm 6.0+) Intel:带 GPU 的 Xeon(即将推出) Apple:通过 MPS 支持 M1/M2/M3(实验性)
参考资料
- 结构化生成指南 - JSON schema、正则、语法、验证
- RadixAttention 深入解析 - 工作原理、优化、基准
- 生产部署 - 多 GPU、监控、自动扩缩容
资源
- GitHub:https://github.com/sgl-project/sglang
- 文档:https://sgl-project.github.io/
- 论文:RadixAttention(arXiv:2312.07104)
- Discord:https://discord.gg/sglang
评论
支持 Markdown