如何在本地使用 LM Studio 运行 gpt-oss
LM Studio 是一个高性能、用户友好的桌面应用程序,用于在本地硬件上运行大型语言模型(LLM)。本指南将引导您完成使用 LM Studio 设置和运行 gpt-oss-20b 或 gpt-oss-120b 模型的过程,包括如何与它们聊天、使用 MCP 服务器或通过 LM Studio 的本地开发 API 与模型进行交互。
请注意,本指南适用于消费级硬件,例如在 PC 或 Mac 上运行 gpt-oss。对于配备专用 GPU(如 NVIDIA H100s)的服务器应用程序,请参阅我们的 vLLM 指南。
选择您的模型
LM Studio 支持 gpt-oss 的两种模型尺寸:
openai/gpt-oss-20b
- 较小的模型
- 仅需要至少 16GB 的 VRAM
- 非常适合高端消费级 GPU 或 Apple Silicon Mac
openai/gpt-oss-120b
- 我们更大尺寸的完整模型
- 最适合 ≥60GB VRAM
- 非常适合多 GPU 或强大的工作站设置
LM Studio 同时提供 llama.cpp 推理引擎(运行 GGUF 格式的模型)以及适用于 Apple Silicon Mac 的 Apple MLX 引擎。
快速设置
-
安装 LM Studio LM Studio 支持 Windows、macOS 和 Linux。请在此处获取。
-
下载 gpt-oss 模型 →
# 对于 20B
lms get openai/gpt-oss-20b
# 或对于 120B
lms get openai/gpt-oss-120b
- 在 LM Studio 中加载模型 → 打开 LM Studio 并使用模型加载界面加载您下载的 gpt-oss 模型。或者,您也可以使用命令行:
# 对于 20B
lms load openai/gpt-oss-20b
# 或对于 120B
lms load openai/gpt-oss-120b
- 使用模型 → 加载后,您可以直接在 LM Studio 的聊天界面或通过 API 与模型进行交互。
与 gpt-oss 聊天
使用 LM Studio 的聊天界面开始与 gpt-oss 对话,或在终端中使用 chat
命令:
lms chat openai/gpt-oss-20b
关于提示格式的说明:LM Studio 使用 OpenAI 的 Harmony 库来构建 gpt-oss 模型的输入,无论是通过 llama.cpp 还是 MLX 运行。
将 gpt-oss 与本地 /v1/chat/completions 端点结合使用
LM Studio 提供了一个兼容聊天补全的 API,因此您可以修改很少的代码即可使用 OpenAI SDK。这是一个 Python 示例:
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:1234/v1",
api_key="not-needed" # LM Studio 不需要 API 密钥
)
result = client.chat.completions.create(
model="openai/gpt-oss-20b",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain what MXFP4 quantization is."}
]
)
print(result.choices[0].message.content)
如果您以前使用过 OpenAI SDK,这会感觉非常熟悉,并且通过更改基础 URL,您现有的代码应该可以正常工作。
如何在聊天 UI 中使用 MCP
LM Studio 是一个 MCP 客户端,这意味着您可以连接 MCP 服务器到它。这允许您为 gpt-oss 模型提供外部工具。
LM Studio 的 mcp.json 文件位于:
~/.lmstudio/mcp.json
在 Python 或 TypeScript 中使用 gpt-oss 进行本地工具使用
LM Studio 的 SDK 同时支持 Python 和 TypeScript。您可以使用该 SDK 来实现工具调用和本地函数执行与 gpt-oss 的结合。
实现这一目标的方法是通过 .act()
调用,它允许您向 gpt-oss 模型提供工具,并让它在调用工具和进行推理之间切换,直到完成您的任务。
下面的示例展示了如何向模型提供一个能够创建本地文件系统的文件的工具。您可以将此示例作为起点,并用更多工具进行扩展。有关工具定义的文档,请参阅 Python 和 TypeScript。
uv pip install lmstudio
import readline # 启用输入行编辑
from pathlib import Path
import lmstudio as lms
# 定义一个可以被模型调用的函数,并将它们作为工具提供给模型。
# 工具只是普通的 Python 函数。它们可以是任何东西。
def create_file(name: str, content: str):
"""使用给定的名称和内容创建文件。"""
dest_path = Path(name)
if dest_path.exists():
return "Error: File already exists."
try:
dest_path.write_text(content, encoding="utf-8")
except Exception as exc:
return "Error: {exc!r}"
return "File created."
def print_fragment(fragment, round_index=0):
# .act() 将回合索引作为第二个参数提供
# 设置默认值意味着回调也兼容 .complete() 和 .respond()。
print(fragment.content, end="", flush=True)
model = lms.llm("openai/gpt-oss-20b")
chat = lms.Chat("You are a helpful assistant running on the user's computer.")
while True:
try:
user_input = input("User (leave blank to exit): ")
except EOFError:
print()
break
if not user_input:
break
chat.add_user_message(user_input)
print("Assistant: ", end="", flush=True)
model.act(
chat,
[create_file],
on_message=chat.append,
on_prediction_fragment=print_fragment,
)
print()
对于希望在本地使用 gpt-oss 的 TypeScript 开发人员,这里有一个使用 lmstudio-js
的类似示例:
npm install @lmstudio/sdk
import { Chat, LMStudioClient, tool } from "@lmstudio/sdk";
import { existsSync } from "fs";
import { writeFile } from "fs/promises";
import { createInterface } from "readline/promises";
import { z } from "zod";
const rl = createInterface({ input: process.stdin, output: process.stdout });
const client = new LMStudioClient();
const model = await client.llm.model("openai/gpt-oss-20b");
const chat = Chat.empty();
const createFileTool = tool({
name: "createFile",
description: "Create a file with the given name and content.",
parameters: { name: z.string(), content: z.string() },
implementation: async ({ name, content }) => {
if (existsSync(name)) {
return "Error: File already exists.";
}
await writeFile(name, content, "utf-8");
return "File created.";
},
});
while (true) {
const input = await rl.question("User: ");
// Append the user input to the chat
chat.append("user", input);
process.stdout.write("Assistant: ");
await model.act(chat, [createFileTool], {
// When the model finish the entire message, push it to the chat
onMessage: (message) => chat.append(message),
onPredictionFragment: ({ content }) => {
process.stdout.write(content);
},
});
process.stdout.write("\n");
}