|
8 | 8 | "log" |
9 | 9 | "os" |
10 | 10 | "path/filepath" |
| 11 | + "strings" |
11 | 12 |
|
12 | 13 | candle "github.com/vllm-project/semantic-router/candle-binding" |
13 | 14 | ) |
@@ -209,34 +210,192 @@ func main() { |
209 | 210 | log.Fatalf("Failed to initialize LoRA classifier: %v", err) |
210 | 211 | } |
211 | 212 |
|
212 | | - // Test samples for intent classification (matching Python demo_inference) |
213 | | - testSamples := []string{ |
214 | | - "What is the best strategy for corporate mergers and acquisitions?", |
215 | | - "How do antitrust laws affect business competition?", |
216 | | - "What are the psychological factors that influence consumer behavior?", |
217 | | - "Explain the legal requirements for contract formation", |
218 | | - "What is the difference between civil and criminal law?", |
219 | | - "How does cognitive bias affect decision making?", |
| 213 | + // Test samples with expected intent categories for validation |
| 214 | + testSamples := []struct { |
| 215 | + text string |
| 216 | + description string |
| 217 | + expected string |
| 218 | + }{ |
| 219 | + { |
| 220 | + "What is the best strategy for corporate mergers and acquisitions?", |
| 221 | + "Business strategy question", |
| 222 | + "business", |
| 223 | + }, |
| 224 | + { |
| 225 | + "How do antitrust laws affect business competition?", |
| 226 | + "Business law question", |
| 227 | + "business", |
| 228 | + }, |
| 229 | + { |
| 230 | + "What are the psychological factors that influence consumer behavior?", |
| 231 | + "Psychology and behavior question", |
| 232 | + "psychology", |
| 233 | + }, |
| 234 | + { |
| 235 | + "Explain the legal requirements for contract formation", |
| 236 | + "Legal concepts question", |
| 237 | + "jurisprudence", |
| 238 | + }, |
| 239 | + { |
| 240 | + "What is the difference between civil and criminal law?", |
| 241 | + "Legal system question", |
| 242 | + "jurisprudence", |
| 243 | + }, |
| 244 | + { |
| 245 | + "How does cognitive bias affect decision making?", |
| 246 | + "Psychology and cognition question", |
| 247 | + "psychology", |
| 248 | + }, |
| 249 | + { |
| 250 | + "What is the derivative of e^x?", |
| 251 | + "Mathematical calculus question", |
| 252 | + "mathematics", |
| 253 | + }, |
| 254 | + { |
| 255 | + "Explain the concept of supply and demand in economics.", |
| 256 | + "Economic principles question", |
| 257 | + "economics", |
| 258 | + }, |
| 259 | + { |
| 260 | + "How does DNA replication work in eukaryotic cells?", |
| 261 | + "Biology and genetics question", |
| 262 | + "biology", |
| 263 | + }, |
| 264 | + { |
| 265 | + "What is the difference between a civil law and common law system?", |
| 266 | + "Legal systems comparison", |
| 267 | + "jurisprudence", |
| 268 | + }, |
| 269 | + { |
| 270 | + "Explain how transistors work in computer processors.", |
| 271 | + "Computer engineering question", |
| 272 | + "computer_science", |
| 273 | + }, |
| 274 | + { |
| 275 | + "Why do stars twinkle?", |
| 276 | + "Astronomical physics question", |
| 277 | + "physics", |
| 278 | + }, |
| 279 | + { |
| 280 | + "How do I create a balanced portfolio for retirement?", |
| 281 | + "Financial planning question", |
| 282 | + "economics", |
| 283 | + }, |
| 284 | + { |
| 285 | + "What causes mental illnesses?", |
| 286 | + "Mental health and psychology question", |
| 287 | + "psychology", |
| 288 | + }, |
| 289 | + { |
| 290 | + "How do computer algorithms work?", |
| 291 | + "Computer science fundamentals", |
| 292 | + "computer_science", |
| 293 | + }, |
| 294 | + { |
| 295 | + "Explain the historical significance of the Roman Empire.", |
| 296 | + "Historical analysis question", |
| 297 | + "history", |
| 298 | + }, |
| 299 | + { |
| 300 | + "What is the derivative of f(x) = x^3 + 2x^2 - 5x + 7?", |
| 301 | + "Calculus problem", |
| 302 | + "mathematics", |
| 303 | + }, |
| 304 | + { |
| 305 | + "Describe the process of photosynthesis in plants.", |
| 306 | + "Biological processes question", |
| 307 | + "biology", |
| 308 | + }, |
| 309 | + { |
| 310 | + "What are the principles of macroeconomic policy?", |
| 311 | + "Economic policy question", |
| 312 | + "economics", |
| 313 | + }, |
| 314 | + { |
| 315 | + "How does machine learning classification work?", |
| 316 | + "Machine learning concepts", |
| 317 | + "computer_science", |
| 318 | + }, |
| 319 | + { |
| 320 | + "What is the capital of France?", |
| 321 | + "General knowledge question", |
| 322 | + "other", |
| 323 | + }, |
220 | 324 | } |
221 | 325 |
|
222 | 326 | fmt.Println("\nTesting LoRA Intent Classification:") |
223 | 327 | fmt.Println("===================================") |
224 | 328 |
|
225 | | - for i, sample := range testSamples { |
226 | | - fmt.Printf("\nTest %d: %s\n", i+1, sample) |
| 329 | + // Statistics tracking |
| 330 | + var ( |
| 331 | + totalTests = len(testSamples) |
| 332 | + correctTests = 0 |
| 333 | + highConfidence = 0 |
| 334 | + lowConfidence = 0 |
| 335 | + ) |
| 336 | + |
| 337 | + for i, test := range testSamples { |
| 338 | + fmt.Printf("\nTest %d: %s\n", i+1, test.description) |
| 339 | + fmt.Printf(" Text: \"%s\"\n", test.text) |
227 | 340 |
|
228 | | - result, err := classifyIntentText(sample, config) |
| 341 | + result, err := classifyIntentText(test.text, config) |
229 | 342 | if err != nil { |
230 | | - fmt.Printf("Error: %v\n", err) |
| 343 | + fmt.Printf(" Classification failed: %v\n", err) |
231 | 344 | continue |
232 | 345 | } |
233 | 346 |
|
| 347 | + // Get the predicted label name |
| 348 | + labelName := "unknown" |
234 | 349 | if label, exists := categoryLabels[result.Class]; exists { |
235 | | - fmt.Printf("Classification: %s (Class ID: %d, Confidence: %.4f)\n", label, result.Class, result.Confidence) |
| 350 | + labelName = label |
| 351 | + } |
| 352 | + |
| 353 | + // Print the result |
| 354 | + fmt.Printf(" Classified as: %s (Class ID: %d, Confidence: %.4f)\n", |
| 355 | + labelName, result.Class, result.Confidence) |
| 356 | + |
| 357 | + // Check correctness |
| 358 | + isCorrect := labelName == test.expected |
| 359 | + if isCorrect { |
| 360 | + fmt.Printf(" ✓ CORRECT") |
| 361 | + correctTests++ |
236 | 362 | } else { |
237 | | - fmt.Printf("Unknown category index: %d (Confidence: %.4f)\n", result.Class, result.Confidence) |
| 363 | + fmt.Printf(" ✗ INCORRECT (Expected: %s)", test.expected) |
238 | 364 | } |
| 365 | + |
| 366 | + // Add confidence assessment |
| 367 | + if result.Confidence > 0.7 { |
| 368 | + fmt.Printf(" - HIGH CONFIDENCE\n") |
| 369 | + highConfidence++ |
| 370 | + } else if result.Confidence > 0.5 { |
| 371 | + fmt.Printf(" - MEDIUM CONFIDENCE\n") |
| 372 | + } else { |
| 373 | + fmt.Printf(" - LOW CONFIDENCE\n") |
| 374 | + lowConfidence++ |
| 375 | + } |
| 376 | + } |
| 377 | + |
| 378 | + // Print comprehensive summary |
| 379 | + fmt.Println("\n" + strings.Repeat("=", 50)) |
| 380 | + fmt.Println("INTENT CLASSIFICATION TEST SUMMARY") |
| 381 | + fmt.Println(strings.Repeat("=", 50)) |
| 382 | + fmt.Printf("Total Tests: %d\n", totalTests) |
| 383 | + fmt.Printf("Correct Predictions: %d/%d (%.1f%%)\n", correctTests, totalTests, float64(correctTests)/float64(totalTests)*100) |
| 384 | + fmt.Printf("High Confidence (>0.7): %d/%d (%.1f%%)\n", highConfidence, totalTests, float64(highConfidence)/float64(totalTests)*100) |
| 385 | + fmt.Printf("Low Confidence (<0.5): %d/%d (%.1f%%)\n", lowConfidence, totalTests, float64(lowConfidence)/float64(totalTests)*100) |
| 386 | + |
| 387 | + // Overall assessment |
| 388 | + accuracy := float64(correctTests) / float64(totalTests) * 100 |
| 389 | + fmt.Printf("\nOVERALL ASSESSMENT: ") |
| 390 | + if accuracy >= 85.0 { |
| 391 | + fmt.Printf("EXCELLENT (%.1f%% accuracy)\n", accuracy) |
| 392 | + } else if accuracy >= 70.0 { |
| 393 | + fmt.Printf("GOOD (%.1f%% accuracy)\n", accuracy) |
| 394 | + } else if accuracy >= 50.0 { |
| 395 | + fmt.Printf("FAIR (%.1f%% accuracy) - Consider retraining\n", accuracy) |
| 396 | + } else { |
| 397 | + fmt.Printf("POOR (%.1f%% accuracy) - Requires retraining\n", accuracy) |
239 | 398 | } |
240 | 399 |
|
241 | | - fmt.Println("\nLoRA Intent Classification test completed!") |
| 400 | + fmt.Println("\nLoRA Intent Classification verification completed!") |
242 | 401 | } |
0 commit comments