LM StudioLM Studio

获取上下文长度

用于获取模型最大上下文长度的 API。

由于其基本架构,LLM(大语言模型)和嵌入模型都具有名为 上下文长度 (context length) 的属性,更具体地说是 最大 上下文长度。通俗地说,这是模型在生成文本或嵌入时可以在“内存”中保留的 token 数量。超过此限制将导致模型行为异常。

在模型对象上使用 get_context_length() 函数

能够检查模型的上下文长度非常有用,尤其是在向模型提供潜在的长输入之前作为一项额外检查。

example.py
context_length = model.get_context_length()

上述代码片段中的 model 是通过 llm.model 方法获取的已加载模型实例。有关详细信息,请参阅 管理内存中的模型

示例:检查输入是否符合模型的上下文窗口

您可以通过以下方式确定给定的对话是否符合模型的上下文:

  1. 使用提示模板将对话转换为字符串。
  2. 计算字符串中的 token 数量。
  3. 将 token 数量与模型的上下文长度进行比较。
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))
© . This site is unofficial and not affiliated with Element Labs, Inc.