OpenAI时隔6年再次推出开源模型gpt-oss系列,本次gpt-oss系列包含两个模型gpt-oss-120b与gpt-oss-20b。由于模型原生支持一种新的量化技术MXFP4,所以模型的部署所需的显存也显著的降低。
- openai/gpt-oss-20b 只需要大概16GB的显存
- openai/gpt-oss-120b 需要大概>=60GB的显存
这让本地部署成为可能,接下来跟着我一起看一下如何在本地使用vllm对gpt-oss-20b进行推理并且支持工具调用吧!
显卡信息
NVIDIA H20 96G VRAM
环境准备
安装uv
pip install uv
创建虚拟环境
uv venv --python 3.12 --seed
source .venv/bin/activate
安装依赖
uv pip install --pre vllm==0.10.1+gptoss \--extra-index-url https://wheels.vllm.ai/gpt-oss/ \--extra-index-url https://download.pytorch.org/whl/nightly/cu128 \--index-strategy unsafe-best-match
下载模型
安装modelscope
pip install modelscope
下载gpt-oss-20b
modelscope download --model 'openai-mirror/gpt-oss-20b' --exclude 'metal/*'
vllm服务
准备vllm 服务配置
model: /data/.cache/modelscope/models/openai-mirror/gpt-oss-20b
served_model_name: gpt-oss
host: 0.0.0.0
port: 8000
tensor-parallel-size: 1
gpu-memory-utilization: 0.9
api-key: yor-api-key
disable-fastapi-docs: true
注意model这一行需要找到你自己的modelscope模型下载到本机的地址,或者在下载模型时使用--local_dir 指定下载路径。
你可以使用以下命令找到你的模型地址
modelscope scan-cache
api-key改成你想要设置的api-key
将文件保存成 gpt-oss-config.yml
启动服务
vllm serve --config gpt-oss-config.yml
工具调用
工具调用可以使用openai python sdk调用
使用openai python sdk
pip install openai
import json
from openai import OpenAI
from openai.types.responses import ResponseFunctionToolCallclient = OpenAI(base_url="http://{你的vllm服务器地址}/v1",api_key="{vllm服务设置的api-key}",
)# 定义工具
tools = [{"type": "function","name": "get_weather","description": "Get current weather in a given city","parameters": {"type": "object","properties": {"city": {"type": "string"}},"required": ["city"],},}
]messages = [{"role": "user", "content": "上海现在天气怎么样?"}]def fetch_response(messages):response = client.responses.create(model="gpt-oss",input=messages,tools=tools,)return response# 工具函数
def get_weather(city: str):return f"The weather in {city} is sunny."response = fetch_response(messages)
for item in response.output:if isinstance(item, ResponseFunctionToolCall):print(f"正在调用工具: `{item.name}` 参数 {item.arguments}")messages.append(item.model_dump())tool_result = get_weather(**json.loads(item.arguments))messages.append({"type": "function_call_output","call_id": item.call_id,"output": tool_result,})response = fetch_response(messages)print(response.output_text)
将以上的python代码保存成tool_use.py文件并运行:
uv run tool_use.py