export const normalizeModel = (model: string) => {
    let currentModel = model.split(/[\\\/]/).at(-1);
    currentModel = currentModel.split('::').at(0).toLowerCase();
    let normalizedModel: string;

    do {
        normalizedModel = currentModel;

        currentModel = currentModel
            .replace(/[ ._-]\d+(k$|-context)/i, '') // remove context length, i.e. -32k
            .replace(/[ ._-](gptq|awq|exl2?|imat|i\d|h\d)/i, '') // remove quant name
            .replace(/([ ._-]?gg(uf|ml)[ ._-]?(v[ ._-]?\d)?)/i, '') // remove gguf-v3/ggml/etc
            .replace(/[ ._-]i?q([ ._-]?\d[ ._-]?(k?[ ._-]?x*[ ._-]?[lms]?)?)+/i, '') // remove quant size
            .replace(/[ ._-]\d+(\.\d+)?bpw/i, '') // remove bpw
            .replace(/[ ._-]f(p|loat)?(8|16|32)/i, '')
            .replace(/^(debug-?)+/i, '')
            .trim();
    } while (normalizedModel !== currentModel);

    return normalizedModel
        .replace(/[ _-]+/ig, '-')
        .replace(/\.{2,}/, '-')
        .replace(/[ ._-]+$/ig, '')
        .trim();
}

export const approximateTokens = (prompt: string): number => Math.round(prompt.length / 4);