文档
分词
分词
使用模型的 tokenizer 对文本进行分词
模型使用 tokenizer 将文本内部转换为更易处理的“token”。LM Studio 提供此 tokenizer 以供使用。
您可以使用 SDK 对加载的 LLM 或 embedding 模型中的字符串进行分词。在下面的示例中,llm
可以替换为 embedding 模型 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.
如果您只关心 token 的数量,可以使用 .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 上获取