文档
管理内存中的模型
AI 模型体积庞大。将它们加载到内存中可能需要一些时间。LM Studio 的 SDK 允许您精确控制此过程。
最常见用法
.model()
获取任何当前加载的模型.model("model-key")
使用特定模型高级用法 (手动模型管理)
.load("model-key")
加载模型的新实例model.unload()
从内存中卸载模型.model()
获取当前模型如果您已经在 LM Studio 中加载了模型(通过 GUI 或 lms load
),您可以通过调用不带任何参数的 .model()
来使用它。
import { LMStudioClient } from "@lmstudio/sdk";
const client = new LMStudioClient();
const model = await client.llm.model();
.model("model-key")
获取特定模型如果您想使用特定模型,可以将模型键作为参数提供给 .model()
。
调用 .model("model-key")
将加载模型(如果尚未加载),否则将返回现有实例。
import { LMStudioClient } from "@lmstudio/sdk";
const client = new LMStudioClient();
const model = await client.llm.model("llama-3.2-1b-instruct");
.load()
加载模型的新实例使用 load()
加载模型的新实例,即使已存在一个实例。这允许您同时加载相同或不同模型的多个实例。
import { LMStudioClient } from "@lmstudio/sdk";
const client = new LMStudioClient();
const llama = await client.llm.load("llama-3.2-1b-instruct");
const another_llama = await client.llm.load("llama-3.2-1b-instruct", {
identifier: "second-llama"
});
如果您提供的实例标识符已存在,服务器将抛出错误。因此,如果您不介意,最好不要提供标识符,在这种情况下服务器会为您生成一个。您也可以随时在 LM Studio 的服务器选项卡中查看!
.unload()
从内存中卸载模型当您不再需要模型时,只需在其句柄上调用 unload()
即可将其卸载。
import { LMStudioClient } from "@lmstudio/sdk";
const client = new LMStudioClient();
const model = await client.llm.model();
await model.unload();
您还可以在加载模型时指定相同的加载时配置选项,例如上下文长度和 GPU 卸载。
更多信息请参见 加载时配置。
您可以为您加载的模型指定一个生存时间(time to live),即从上次请求到模型卸载之间的空闲时间(以秒为单位)。更多信息请参见 空闲 TTL。
import { LMStudioClient } from "@lmstudio/sdk";
const client = new LMStudioClient();
const model = await client.llm.load("llama-3.2-1b-instruct", {
ttl: 300, // 300 seconds
});
本页内容
使用 .model() 获取当前模型
使用 .model("model-key") 获取特定模型
- 如果已加载则获取,否则加载
使用 .load() 加载模型的新实例
关于实例标识符的注意事项
使用 .unload() 从内存中卸载模型
设置自定义加载配置参数
设置自动卸载计时器 (TTL)