拉取 vLLM Docker 镜像
docker pull vllm/vllm-openai:latest
使用 Docker 启动 vLLM + Qwen3
1、Qwen3-14B-AWQ 模型启动命令:
docker run --runtime nvidia --name qwen_vllm_server --rm --gpus all -v E:/models/Qwen3-14B-AWQ:/model -p 8000:8000 --ipc=host vllm/vllm-openai:latest --model /model --dtype auto --enforce-eager --port 8000
测试用例:
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:8000/v1", # vllm的OpenAI兼容端点
api_key="token-abc123" # 任意值,vllm不需要实际的API密钥
)
def generate_llm():
prompt = "介绍一下北京"
# 4. 调用推理函数
response = client.chat.completions.create(
model="/model",
messages=[
# {"role": "system", "content": prompt},
{"role": "user", "content": prompt}
],
temperature=0.7,
max_tokens=2000,
top_p=0.7,
stream=False # 为了获取首token时间,需要使用流式输出
)
result = response.choices[0].message.content
print(result)
if __name__ == '__main__':
generate_llm()
2、Qwen3-VL-8B-AWQ-INT4 模型启动命令:
docker run --runtime nvidia --name qwen_vllm_server --rm --gpus all -v E:/models/Qwen3-VL-8B-Instruct-AWQ-4bit:/Qwen3-VL-8B-Instruct-AWQ-4bit -p 8000:8000 --ipc=host vllm/vllm-openai:latest --model /Qwen3-VL-8B-Instruct-AWQ-4bit --dtype auto --port 8000 --tensor-parallel-size 1 --gpu-memory-utilization 0.9 --max-model-len 15000 --trust-remote-code
测试用例:
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:8000/v1", # vllm的OpenAI兼容端点
api_key="token-abc123", # 任意值,vllm不需要实际的API密钥
http_client=httpx.Client(trust_env=False),
)
def generate_llm(batch_images_b64):
# 和你原代码一样的用户内容结构
user_content = [
{
"type": "text",
"text": f"这是连续的 {len(batch_images_b64)} 帧视频画面。"
f"请分析其中的视觉内容和动态变化。"
f"输出内容要清晰简短,简明扼要。禁止输出如md格式,语言描述自然。"
}
]
# 逐帧追加图片
for b64 in batch_images_b64:
user_content.append({
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{b64}"}
})
# 4. 调用推理函数
response = client.chat.completions.create(
model="/Qwen3-VL-8B-Instruct-AWQ-4bit",
messages=[
# {"role": "system", "content": prompt},
{"role": "user", "content": prompt}
],
max_tokens=128,
temperature=0.7
)
# 取出模型回复文本
reply = response.choices[0].message.content
# 可选:取出 tokens 统计
usage = response.usage
if usage:
print(
f"Tokens 使用情况:"
f"prompt={getattr(usage, 'prompt_tokens', 0)}, "
f"completion={getattr(usage, 'completion_tokens', 0)}, "
f"total={getattr(usage, 'total_tokens', 0)}"
)
return reply
if __name__ == '__main__':
generate_llm()
3、参数解释:
-
--gpus all:让容器使用所有 GPU -
-p 8000:8000:把容器的 8000 端口映射到宿主机的 8000 -
--model:Hugging Face 仓库名或本地路径 -
--dtype auto:vLLM 自动选择合适精度(一般 FP16 或 BF16) -
--max-model-len:上下文长度,视显存可调整(4096/8192/16384 等) -
--tensor-parallel-size 1:使用 1 卡做张量并行 -
--gpu-memory-utilization 0.9:控制 vLLM 使用多少比例的显存(默认 0.9 左右) -
--max-num-batched-tokens、--max-num-seqs:控制并发请求的 token/序列数量,提升吞吐时可调大,显存不足时调小 -
--kv-cache-dtype fp8:KV Cache 量化
575

被折叠的 条评论
为什么被折叠?



