70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import { models } from 'token.js'
|
|
|
|
export const defaultModel = (provider?: string) => {
|
|
if (!provider || !Object.keys(models).includes(provider)) {
|
|
return models.openai.models[0]
|
|
}
|
|
return (
|
|
models[provider as unknown as keyof typeof models]
|
|
.models as unknown as string[]
|
|
)[0]
|
|
}
|
|
|
|
/**
|
|
* This utility is to extract cortexso model description from README.md file
|
|
* @returns
|
|
*/
|
|
export const extractDescription = (text?: string) => {
|
|
if (!text) return text
|
|
const normalizedText = removeYamlFrontMatter(text)
|
|
const overviewPattern = /(?:##\s*Overview\s*\n)([\s\S]*?)(?=\n\s*##|$)/
|
|
const matches = normalizedText?.match(overviewPattern)
|
|
let extractedText =
|
|
matches && matches[1]
|
|
? matches[1].trim()
|
|
: normalizedText?.slice(0, 500).trim()
|
|
|
|
// Remove image markdown syntax 
|
|
extractedText = extractedText?.replace(/!\[.*?\]\(.*?\)/g, '')
|
|
|
|
// Remove <img> HTML tags
|
|
extractedText = extractedText?.replace(/<img[^>]*>/g, '')
|
|
|
|
return extractedText
|
|
}
|
|
/**
|
|
* Remove YAML (HF metadata) front matter from content
|
|
* @param content
|
|
* @returns
|
|
*/
|
|
export const removeYamlFrontMatter = (content: string): string => {
|
|
return content.replace(/^---\n([\s\S]*?)\n---\n/, '')
|
|
}
|
|
|
|
/**
|
|
* Extract model name from repo path, e.g. cortexso/tinyllama -> tinyllama
|
|
* @param modelId
|
|
* @returns
|
|
*/
|
|
export const extractModelName = (model?: string) => {
|
|
return model?.split('/')[1] ?? model
|
|
}
|
|
|
|
/**
|
|
* Extract model name from repo path, e.g. https://huggingface.co/cortexso/tinyllama -> cortexso/tinyllama
|
|
* @param modelId
|
|
* @returns
|
|
*/
|
|
export const extractModelRepo = (model?: string) => {
|
|
return model?.replace('https://huggingface.co/', '')
|
|
}
|
|
|
|
/**
|
|
* Normalize the provider name to match the format used in the models object
|
|
* @param provider - The provider name to normalize
|
|
*/
|
|
export const normalizeProvider = (provider: string) => {
|
|
// TODO: After migrating to the new provider extension, remove this function
|
|
return provider === 'llama.cpp' ? 'cortex' : provider
|
|
}
|