// Get the element where you want to display the outputconst outputElement = document.getElementById("output");
forawait (const text of prediction) {
outputElement.textContent += text;
}
デフォルト以外の LM Studio サーバーポートの使用
この例では、異なるポート (例: 8080) で実行されている LM Studio に接続する方法を示します。
デフォルトでは、クライアントが LM Studio から切断されると、そのクライアントによって読み込まれたすべてのモデルがアンロードされます。noHup オプションをtrueに設定することで、これを回避できます。
await client.llm.load("lmstudio-community/Meta-Llama-3-8B-Instruct-GGUF",
config: { gpuOffload: "max" },
noHup: true,
});
// The model stays loaded even after the client disconnects
読み込まれたモデルに分かりやすい名前を付ける
モデルを読み込む際に、識別子を設定できます。この識別子は、後でモデルを参照するために使用できます。
await client.llm.load("lmstudio-community/Meta-Llama-3-8B-Instruct-GGUF", {
config: { gpuOffload: "max" },
identifier: "my-model",
});
// You can refer to the model later using the identifierconst myModel = await client.llm.get("my-model");
// myModel.complete(...);
カスタム設定を使用してモデルを読み込む
デフォルトでは、モデルの読み込み設定は、モデルに関連付けられたプリセットから取得されます(LM Studio の「マイモデル」ページで変更できます)。
const llama3 = await client.llm.load("lmstudio-community/Meta-Llama-3-8B-Instruct-GGUF", {
config: {
gpuOffload: "max",
contextLength: 1024,
gpuOffload: 0.5, // Offloads 50% of the computation to the GPU
},
});
// llama3.complete(...);
特定のプリセットを使用してモデルを読み込む
プリセットは、モデルのデフォルトの読み込み設定とデフォルトの推論設定を決定します。デフォルトでは、モデルに関連付けられたプリセットが使用されます(LM Studio の「マイモデル」ページで変更できます)。preset オプションを指定することで、使用するプリセットを変更できます。
const llama3 = await client.llm.load("lmstudio-community/Meta-Llama-3-8B-Instruct-GGUF", {
config: { gpuOffload: "max" }, // Overrides the presetpreset: "My ChatML",
});
// Matches any quantizationconst llama3 = await client.llm.get({ path: "lmstudio-community/Meta-Llama-3-8B-Instruct-GGUF" });
// Or if a specific quantization is desired:const llama3 = await client.llm.get({
path: "lmstudio-community/Meta-Llama-3-8B-Instruct-GGUF/Meta-Llama-3-8B-Instruct-Q4_K_M.gguf",
});
// llama3.complete(...);
const prediction = anyModel.complete("Meaning of life is", {
contextOverflowPolicy: "stopAtLimit",
maxPredictedTokens: 100,
prePrompt: "Some pre-prompt",
stopStrings: ["\n"],
temperature: 0.7,
});
// ...Do stuff with the prediction...
チャット補完
会話を実行するには、respondメソッドを使用します。
const prediction = anyModel.respond([
{ role: "system", content: "Answer the following questions." },
{ role: "user", content: "What is the meaning of life?" },
]);
forawait (const text of prediction) {
process.stdout.write(text);
}
const prediction = model.complete("The meaning of life is");
forawait (const text of prediction) {
process.stdout.write(text);
}
const { stats } = await prediction;
console.log(stats);
const prediction = model.complete("The meaning of life is");
const result = await prediction;
const content = result.content;
const stats = result.stats;
// Or just:const { content, stats } = await model.complete("The meaning of life is");
const prediction = model.complete("Here is a joke in JSON:", {
maxPredictedTokens: 100,
structured: { type: "json" },
});
const result = await prediction;
try {
// Although the LLM is guaranteed to only produce valid JSON, when it is interrupted, the// partial result might not be. Always check for errors. (See below)const parsed = JSON.parse(result.content);
console.info(parsed);
} catch (e) {
console.error(e);
}
forawait (const text of prediction) {
process.stdout.write(text);
}
const { stats } = await prediction;
if (stats.stopReason === "userStopped") {
console.log("Prediction was canceled by the user");
}