Created
January 16, 2025 20:24
-
-
Save mattmccray/bcaa28189daccefb567c4a835fb757bb to your computer and use it in GitHub Desktop.
Calculate a preferred context size for ollama calls (num_ctx).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where
PromptMessage
is: