ドキュメント

トークン化

トークン化

モデルのトークナイザーを使用してテキストをトークン化する

モデルは、トークナイザーを使用して、テキストをより簡単に扱える「トークン」に内部的に変換します。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 で入手できます。