ドキュメント
エージェントフロー
インテグレーション
テキスト埋め込み
トークン化
モデル情報
エージェントフロー
インテグレーション
テキスト埋め込み
トークン化
モデル情報
モデル情報
コンテキスト長を取得
モデルの最大コンテキスト長を取得するためのAPI。
LLMおよび埋め込みモデルは、その根本的なアーキテクチャにより、コンテキスト長、より具体的には最大コンテキスト長というプロパティを持っています。これは、モデルがテキストや埋め込みを生成する際に「メモリに保持できる」トークンの数です。この制限を超えると、モデルの動作が不安定になる可能性があります。
getContextLength()関数を使用するモデルのコンテキスト長を確認できることは、特にモデルに長文の入力を与える前に、追加のチェックとして役立ちます。
const contextLength = await model.getContextLength();
上記のコードスニペットのmodelは、llm.modelメソッドから取得したロード済みのモデルのインスタンスです。詳細については、メモリ内のモデルの管理を参照してください。
以下の手順で、指定された会話がモデルのコンテキストに収まるかどうかを確認できます。
import { Chat, type LLM, LMStudioClient } from "@lmstudio/sdk";
async function doesChatFitInContext(model: LLM, chat: Chat) {
// Convert the conversation to a string using the prompt template.
const formatted = await model.applyPromptTemplate(chat);
// Count the number of tokens in the string.
const tokenCount = await model.countTokens(formatted);
// Get the current loaded context length of the model
const contextLength = await model.getContextLength();
return tokenCount < contextLength;
}
const client = new LMStudioClient();
const model = await client.llm.model();
const chat = Chat.from([
{ role: "user", content: "What is the meaning of life." },
{ role: "assistant", content: "The meaning of life is..." },
// ... More messages
]);
console.info("Fits in context:", await doesChatFitInContext(model, chat));
このページのソースはGitHubで入手できます。