|
| 1 | +// Template-based dynamic model description generator |
| 2 | +// Replaces static API descriptions with context-aware text based on device/framework |
| 3 | + |
| 4 | +import type { GroupedFoundryModel } from '../../routes/models/types'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Acceleration display names mapping |
| 8 | + */ |
| 9 | +const ACCELERATION_DISPLAY_NAMES: Record<string, string> = { |
| 10 | + qnn: 'Qualcomm AI Engine', |
| 11 | + vitis: 'AMD Vitis AI', |
| 12 | + openvino: 'Intel OpenVINO', |
| 13 | + cuda: 'NVIDIA CUDA', |
| 14 | + 'trt-rtx': 'NVIDIA TensorRT', |
| 15 | + trtrtx: 'NVIDIA TensorRT', |
| 16 | + webgpu: 'WebGPU' |
| 17 | +}; |
| 18 | + |
| 19 | +/** |
| 20 | + * Format a list of devices into a human-readable string |
| 21 | + * e.g., ['cpu', 'gpu', 'npu'] -> "CPU, GPU, and NPU" |
| 22 | + */ |
| 23 | +function formatDeviceList(devices: string[]): string { |
| 24 | + if (devices.length === 0) return 'various devices'; |
| 25 | + |
| 26 | + const formatted = devices.map((d) => d.toUpperCase()); |
| 27 | + |
| 28 | + if (formatted.length === 1) { |
| 29 | + return formatted[0]; |
| 30 | + } else if (formatted.length === 2) { |
| 31 | + return `${formatted[0]} and ${formatted[1]}`; |
| 32 | + } else { |
| 33 | + const last = formatted.pop(); |
| 34 | + return `${formatted.join(', ')}, and ${last}`; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Get the display name for an acceleration type |
| 40 | + */ |
| 41 | +function getAccelerationDisplayName(acceleration: string): string { |
| 42 | + return ACCELERATION_DISPLAY_NAMES[acceleration.toLowerCase()] || acceleration; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Get unique accelerations from a grouped model's variants |
| 47 | + */ |
| 48 | +function getUniqueAccelerations(model: GroupedFoundryModel): string[] { |
| 49 | + if (!model.variants || model.variants.length === 0) { |
| 50 | + return model.acceleration ? [model.acceleration] : []; |
| 51 | + } |
| 52 | + |
| 53 | + const accelerations = new Set<string>(); |
| 54 | + for (const variant of model.variants) { |
| 55 | + if (variant.acceleration) { |
| 56 | + accelerations.add(variant.acceleration); |
| 57 | + } |
| 58 | + } |
| 59 | + return Array.from(accelerations); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Generate a dynamic description for a model based on its properties |
| 64 | + */ |
| 65 | +export function generateModelDescription(model: GroupedFoundryModel): string { |
| 66 | + const displayName = model.displayName || model.alias || 'this model'; |
| 67 | + const devices = model.deviceSupport || []; |
| 68 | + const accelerations = getUniqueAccelerations(model); |
| 69 | + |
| 70 | + // Base description |
| 71 | + let description = `${displayName} is optimized for local inference`; |
| 72 | + |
| 73 | + // Add device information |
| 74 | + if (devices.length > 0) { |
| 75 | + description += ` on ${formatDeviceList(devices)}`; |
| 76 | + } |
| 77 | + |
| 78 | + description += '.'; |
| 79 | + |
| 80 | + // Add acceleration framework information |
| 81 | + if (accelerations.length > 0) { |
| 82 | + const accelerationNames = accelerations.map(getAccelerationDisplayName); |
| 83 | + if (accelerationNames.length === 1) { |
| 84 | + description += ` Uses ${accelerationNames[0]} for acceleration.`; |
| 85 | + } else if (accelerationNames.length === 2) { |
| 86 | + description += ` Available with ${accelerationNames[0]} or ${accelerationNames[1]} acceleration.`; |
| 87 | + } else { |
| 88 | + const last = accelerationNames.pop(); |
| 89 | + description += ` Available with ${accelerationNames.join(', ')}, or ${last} acceleration.`; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return description; |
| 94 | +} |
0 commit comments