← 返回首页

LM Studio本地AI Agent开发踩坑复盘:5个真实配置陷阱

Local AI ToolsLM StudioAgent开发本地LLM

我第一次用LM Studio的Agent功能时,用的是一个3B的小模型,想着反正只是做个测试。结果:

[LM Studio] Model does not support tool calling. Please use a model with tool-calling capability.

这个错误信息很清楚,但我在选模型时完全没注意这个限制。LM Studio的Hub上有很多模型,但不是每个都能用.act().respond()调用工具。

我的经验:最小选择Qwen2.5-7B-Instruct或同等规模(约7B参数以上)的模型,才能可靠执行Tool Use。我实际测试了Qwen2.5-7B-Instruct和llama3-8B,Qwen在工具调用上表现明显更好。官方文档明确建议:

下载模型的正确命令:

lms get qwen2.5-7b-instruct
# 或
lms get lmstudio-community/Qwen2.5-7B-Instruct-GGUF

查看已下载模型:

lms ls

查看正在运行的模型:

lms ps

GPU卸载比例:默认配置会让你OOM

第二个坑是GPU内存配置。我在一台有8GB显存的机器上,默认lms load qwen2.5-7b,结果直接OOM:

[LM Studio] Out of memory error: Requested to load 7B model with 8192MB, but available is 6144MB

问题在于默认的--gpu=auto在8GB显存的机器上不会给你留足够余量。正确做法:

# 保留2GB给系统,GPU卸载70%
lms load qwen2.5-7b-instruct --gpu=0.7

# 或者明确指定VRAM上限
lms load qwen2.5-7b-instruct --gpu=6.0

也可以用代码配置:

const model = await client.llm.model("qwen2.5-7b-instruct", {
  gpuOffload: 0.7, // 70% GPU offload
});

我实测:7B模型建议6-7GB显存(如果你有8GB),13B模型建议12GB以上。

Zod版本不匹配:v4会报错

这个问题让我卡了30分钟。我的项目里装的是zod@4,但LM Studio的SDK要求zod@3

The current code expects zod v3, but found zod v4

解决方案有两个:

方案1:降级Zod(推荐)

npm uninstall zod
npm install zod@3

方案2:在package.json中锁定版本

{
  "dependencies": {
    "@lmstudio/sdk": "latest",
    "zod": "^3.0.0"
  }
}

如果你已经装了zod v4,可以用npx zod@3临时降级,或者在Docker环境里隔离版本。

服务器没启动:lms server start是必须的

这是最蠢的一个坑:我写了完整的Agent代码,但忘了启动本地服务器。

报错:

[LM Studio] Connection refused: localhost:1234
Is the server running? Start it with 'lms server start'

代码里你写的是:

const client = new LMStudioClient(); // 默认连接 localhost:1234

但这个服务器不会自动启动。你需要两个终端:

终端1:启动服务器

lms server start
# 看到类似输出:
# Server is running at http://localhost:1234
# API docs available at http://localhost:1234/api-docs

终端2:运行你的代码

import { LMStudioClient } from "@lmstudio/sdk";

const client = new LMStudioClient();
const model = await client.llm.model("qwen2.5-7b-instruct");
const result = await model.respond("Hello");
console.log(result.content);

如果你想指定端口:

lms server start --port 8080

代码里对应:

const client = new LMStudioClient({ port: 8080 });

模型加载时机:先加载再调用

最后一个坑是模型的加载时机。我写了代码但模型还没下载到本地:

# 先检查模型是否存在
lms ls
# 如果没有,下载它
lms get qwen2.5-7b-instruct

正确的顺序是:

1. lms server start — 启动服务器

2. lms load qwen2.5-7b-instruct — 在另一个终端手动加载模型

3. 或者在代码里自动加载:

// 第一次调用时会自动加载模型
const model = await client.llm.model("qwen2.5-7b-instruct");
// 如果模型不存在,会自动下载

但自动下载有时会很慢,建议先用lms get手动下载。

完整的最小示例

这是我在90分钟踩坑后整理的最简可用代码,直接复制就能跑:

import { LMStudioClient, tool } from "@lmstudio/sdk";
import { z } from "zod"; // 注意:zod v3,不是 v4

const client = new LMStudioClient();

const multiplyTool = tool({
  name: "multiply",
  description: "给定两个数字a和b,返回它们的乘积",
  parameters: {
    a: z.number(),
    b: z.number()
  },
  implementation: ({ a, b }) => a * b,
});

async function main() {
  // 1. 确保服务器运行中:lms server start
  // 2. 确保模型已下载:lms get qwen2.5-7b-instruct
  // 3. 加载模型(如果没有在别处加载)
  await client.llm.load("qwen2.5-7b-instruct", { gpuOffload: 0.7 });

  const model = await client.llm.model("qwen2.5-7b-instruct");

  const result = await model.act(
    "计算 12345 × 67890 的结果",
    [multiplyTool],
    {
      onMessage: (msg) => console.log("AI:", msg.toString()),
    }
  );
}

main().catch(console.error);

运行:

node your-script.js

总结:5个坑的快速检查清单

错误表现解决方案
模型选错`Model does not support tool calling`选7B+模型,推荐Qwen2.5-7B
GPU OOM`Out of memory`加`--gpu=0.7`或调低到`0.6`
Zod版本`expects zod v3``npm install zod@3`
服务器未启动`Connection refused: localhost:1234`先运行`lms server start`
模型未加载`Model not found`先`lms get`再`lms load`

---

声明:本文为个人经验记录,LM Studio为第三方工具,无商业合作关系。本文可能包含联盟链接,读者点击购买可能产生少量佣金。

📌 本文由 AI 辅助生成并经人工审核发布 | TechPassive — AI 驱动的内容测试站点,专注于效率工具与 SaaS 真实评测

👉 想要更强大的AI能力?试试MiniMax API,新用户有免费额度:

立即参与:https://platform.minimaxi.com/subscribe/token-plan?code=E5yur9NOub&source=link

🔗 Related Tech Articles

Deep dive into related technical topics:

LM Studio本地AI Agent开发踩坑复盘:5个真实配置陷阱
技术标签: lm studio, agent开发
2026-05-08-jan-ai-vs-ollama-vs-lm-studio-the-2026-complete-lo-en.html
技术标签: lm studio, comparison
Jan AI vs Ollama vs LM Studio横评本地AI工具完整对比
技术标签: jan ai, ollama
🤖 Local AI Inference Hardware
查看推荐 →