文档
分词
分词
使用模型的分词器对文本进行分词
模型使用分词器将文本内部转换为它们可以更轻松处理的“标记”。LM Studio 暴露了此分词器以供实用。
您可以使用 SDK 对加载的 LLM 或嵌入模型中的字符串进行分词。在以下示例中,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 上获取。