Skip to content

Instantly share code, notes, and snippets.

@mattmccray
Created January 16, 2025 20:24
Show Gist options
  • Save mattmccray/bcaa28189daccefb567c4a835fb757bb to your computer and use it in GitHub Desktop.
Save mattmccray/bcaa28189daccefb567c4a835fb757bb to your computer and use it in GitHub Desktop.
Calculate a preferred context size for ollama calls (num_ctx).
const MIN_CONTEXT_SIZE = 4096
const MAX_CONTEXT_SIZE = MIN_CONTEXT_SIZE * 8
/**
* Calculates the context size for a given set of messages, generically
* in tokens.
*
* @param messages - The messages to calculate the context size for.
* @param maxContextSize - The maximum context size to return. Defaults to MAX_CONTEXT_SIZE.
* @returns The context size in tokens.
*/
export function calcContextSize(
messages: PromptMessage[],
maxContextSize: number = MAX_CONTEXT_SIZE
) {
const charLength = messages.reduce((acc, msg) => acc + msg.content.length, 0)
const tokenLength = (charLength / 4) * 2
const contextSize = Math.min(
Math.max(tokenLength, MIN_CONTEXT_SIZE),
maxContextSize
)
const preferredContextSize = Math.ceil(contextSize / 1024) * 1024
return preferredContextSize
}
@mattmccray
Copy link
Author

Where PromptMessage is:

type PromptMessage = {
  role: PromptRole
  content: string
  images?: Uint8Array[] | string[] // Base64 encoded images
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment