(modelName, dims)
| 121 | } |
| 122 | |
| 123 | async function benchmarkModel(modelName, dims) { |
| 124 | console.log(`\n${'='.repeat(60)}`); |
| 125 | console.log(`MODEL: ${modelName} (${dims}d)`); |
| 126 | console.log('='.repeat(60)); |
| 127 | |
| 128 | const loadStart = performance.now(); |
| 129 | const embedder = await pipeline('feature-extraction', modelName, { |
| 130 | quantized: true, |
| 131 | }); |
| 132 | const loadTime = ((performance.now() - loadStart) / 1000).toFixed(1); |
| 133 | console.log(`Loaded in ${loadTime}s`); |
| 134 | |
| 135 | // Embed all packages |
| 136 | const pkgIds = Object.keys(PACKAGES); |
| 137 | const pkgTexts = Object.values(PACKAGES); |
| 138 | const pkgEmbeddings = {}; |
| 139 | |
| 140 | for (let i = 0; i < pkgIds.length; i++) { |
| 141 | const result = await embedder(pkgTexts[i], { pooling: 'mean', normalize: true }); |
| 142 | pkgEmbeddings[pkgIds[i]] = Array.from(result.data); |
| 143 | } |
| 144 | |
| 145 | // Benchmark each conversation |
| 146 | let correct = 0; |
| 147 | let total = 0; |
| 148 | const latencies = []; |
| 149 | |
| 150 | console.log('\n Results:'); |
| 151 | |
| 152 | for (const [convName, messages] of Object.entries(CONVERSATIONS)) { |
| 153 | const convText = formatConversation(messages); |
| 154 | |
| 155 | // Measure latency (average of 5 runs) |
| 156 | const times = []; |
| 157 | let embedding; |
| 158 | for (let i = 0; i < 5; i++) { |
| 159 | const start = performance.now(); |
| 160 | const result = await embedder(convText, { pooling: 'mean', normalize: true }); |
| 161 | times.push(performance.now() - start); |
| 162 | embedding = Array.from(result.data); |
| 163 | } |
| 164 | const avgMs = times.slice(1).reduce((a, b) => a + b, 0) / (times.length - 1); |
| 165 | latencies.push(avgMs); |
| 166 | |
| 167 | // Score against all packages |
| 168 | const scores = {}; |
| 169 | for (const [pkgId, pkgEmb] of Object.entries(pkgEmbeddings)) { |
| 170 | scores[pkgId] = cosineSim(embedding, pkgEmb); |
| 171 | } |
| 172 | |
| 173 | // Rank |
| 174 | const ranked = Object.entries(scores).sort((a, b) => b[1] - a[1]); |
| 175 | const top = ranked[0]; |
| 176 | const expected = EXPECTED[convName]; |
| 177 | |
| 178 | if (expected) { |
| 179 | total++; |
| 180 | const isCorrect = top[0] === expected; |
no test coverage detected