ドキュメント

トークン化

トークン化

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

モデルは、テキストをより簡単に処理できる「トークン」に内部的に変換するためにトークナイザーを使用します。LM Studio は、このトークナイザーをユーティリティとして公開しています。

トークン化

SDK を使用して、ロードされた LLM または埋め込みモデルで文字列をトークン化できます。以下の例では、埋め込みモデル参照に置き換えても、他の変更は必要ありません。

import lmstudio as lms

model = lms.llm()

tokens = model.tokenize("Hello, world!")

print(tokens) # Array of token IDs.

トークン数を数える

トークン数のみを知りたい場合は、結果の配列の長さを確認してください。

token_count = len(model.tokenize("Hello, world!"))
print("Token count:", token_count)

例: コンテキストのカウント

以下の手順で、指定された会話がモデルのコンテキストに収まるかどうかを確認できます。

  • プロンプトテンプレートを使用して、会話を文字列に変換します。
  • 文字列のトークン数をカウントします。
  • トークン数をモデルのコンテキスト長と比較します。
import lmstudio as lms

def does_chat_fit_in_context(model: lms.LLM, chat: lms.Chat) → bool:
    # Convert the conversation to a string using the prompt template.
    formatted = model.apply_prompt_template(chat)
    # Count the number of tokens in the string.
    token_count = len(model.tokenize(formatted))
    # Get the current loaded context length of the model
    context_length = model.get_context_length()
    return token_count < context_length

model = lms.llm()

chat = lms.Chat.from_history({
    "messages": [
        { "role": "user", "content": "What is the meaning of life." },
        { "role": "assistant", "content": "The meaning of life is..." },
        # ... More messages
    ]
})

print("Fits in context:", does_chat_fit_in_context(model, chat))

このページのソースは GitHub で利用可能です