ドキュメント
エージェントフロー
インテグレーション
テキスト埋め込み
トークン化
モデル情報
エージェントフロー
インテグレーション
テキスト埋め込み
トークン化
モデル情報
トークン化
トークン化
モデルのトークナイザーを使用してテキストをトークン化する
モデルは、トークナイザーを使用して、テキストをより簡単に扱える「トークン」に内部的に変換します。LM Studio は、このトークナイザーをユーティリティとして公開します。
SDK を使用して、ロードされた LLM または埋め込みモデルで文字列をトークン化できます。以下の例では、llm を埋め込みモデルの emb に置き換えることができます。
import { LMStudioClient } from "@lmstudio/sdk";
const client = new LMStudioClient();
const model = await client.llm.model();
const tokens = await model.tokenize("Hello, world!");
console.info(tokens); // Array of token IDs.
トークンの数のみが必要な場合は、代わりに .countTokens メソッドを使用できます。
const tokenCount = await model.countTokens("Hello, world!");
console.info("Token count:", tokenCount);
以下の手順で、指定された会話がモデルのコンテキストに収まるかどうかを確認できます。
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 で入手できます。