ドキュメント
エージェントフロー
インテグレーション
テキスト埋め込み
トークン化
モデル情報
エージェントフロー
インテグレーション
テキスト埋め込み
トークン化
モデル情報
基本
予測のキャンセル
lmstudio-jsで進行中の予測を停止
予測が完了する前に停止したい場合があります。例えば、ユーザーの気が変わったり、UIが離れたりした場合です。lmstudio-jsは、実行中の予測をキャンセルするための2つの簡単な方法を提供します。
.cancel()を呼び出すすべての予測メソッドはOngoingPredictionインスタンスを返します。.cancel()を呼び出すと、生成が停止し、最終的なstopReasonが"userStopped"になります。以下の例では、タイマーでキャンセルをスケジュールしています。
import { LMStudioClient } from "@lmstudio/sdk";
const client = new LMStudioClient();
const model = await client.llm.model("qwen2.5-7b-instruct");
const prediction = model.respond("What is the meaning of life?", {
maxTokens: 50,
});
setTimeout(() => prediction.cancel(), 1000); // cancel after 1 second
const result = await prediction.result();
console.info(result.stats.stopReason); // "userStopped"
AbortControllerを使用するアプリケーションがすでにキャンセルを伝播するためにAbortControllerを使用している場合、そのsignalを予測メソッドに渡すことができます。コントローラーを中止すると、同じstopReasonで予測が停止します。
import { LMStudioClient } from "@lmstudio/sdk";
const client = new LMStudioClient();
const model = await client.llm.model("qwen2.5-7b-instruct");
const controller = new AbortController();
const prediction = model.respond("What is the meaning of life?", {
maxTokens: 50,
signal: controller.signal,
});
setTimeout(() => controller.abort(), 1000); // cancel after 1 second
const result = await prediction.result();
console.info(result.stats.stopReason); // "userStopped"
どちらのアプローチでも、生成は直ちに停止され、返される統計情報はその予測がユーザーによって停止されたことを示します。
このページのソースはGitHubで利用可能です。