ContextWindowStrategy

Represents a strategy for computing the context window length for OllamaClient. Different implementations define specific approaches to computing the context window length. Based on the context window length computed by this strategy, Ollama will truncate the context window accordingly.

To decide the context window length, Ollama proceeds as follows:

  • If a num_ctx parameter is specified in the chat request, the context window length is set to that value.

  • If the model definition contains a num_ctx parameter, the context window length is set to that value.

  • If an OLLAMA_CONTEXT_LENGTH environment variable is set, the context window length is set to that value.

  • Otherwise, the context window length is set to the default value of 2048.

Effectively, this strategy allows you to specify what num_ctx value will be set in chat requests sent to Ollama, for a given prompt and model.

Important: You will want to have a context window length that does not change often for a specific model. Indeed, Ollama will reload the model every time the context window length changes.

Example implementations:

Inheritors

Types

Link copied to clipboard
object Companion

Provides companion object-related strategies for determining the context window length. It contains multiple strategies that are implemented as subtypes of ContextWindowStrategy.

Functions

Link copied to clipboard
abstract fun computeContextLength(prompt: Prompt, model: LLModel): Long?

Computes the context length for a given prompt and language model. This may involve calculating the number of tokens used in the prompt and determining if it fits within the model's context length constraints.