Class TaskClassifier

java.lang.Object
com.tnsai.llm.routing.TaskClassifier

public class TaskClassifier extends Object
Classifies user prompts into task types for intelligent model routing.

TaskClassifier analyzes the input text and determines the most appropriate TaskType based on keywords, patterns, and heuristics. This enables automatic model selection without manual intervention.

Classification Strategy

  1. Keyword Matching: Check for task-specific keywords
  2. Pattern Detection: Regex patterns for code, math, etc.
  3. Context Analysis: Consider message history if available
  4. Score Ranking: Select highest-scoring task type

Usage Example


 TaskClassifier classifier = TaskClassifier.defaultClassifier();

 // Classify single message
 TaskType type = classifier.classify("Write a Python function to sort a list");
 // Returns: CODING

 // With confidence score
 ClassificationResult result = classifier.classifyWithConfidence("Solve x² + 2x + 1 = 0");
 System.out.println(result.taskType());     // MATH
 System.out.println(result.confidence());   // 0.85

 // With history context
 List<String> history = List.of("I'm working on a React app", "Help me debug");
 TaskType type = classifier.classify("What's wrong with this code?", history);
 // Returns: CODING (boosted by history)
 

Customization


 // Add custom keywords
 TaskClassifier classifier = TaskClassifier.builder()
     .addKeywords(TaskType.CODING, Set.of("backend", "frontend", "docker"))
     .addPattern(TaskType.MATH, Pattern.compile("[0-9]+"))
     .setDefaultType(TaskType.CHAT)
     .build();
 
See Also:
  • Method Details

    • defaultClassifier

      public static TaskClassifier defaultClassifier()
      Returns the default classifier with standard settings.
    • builder

      public static TaskClassifier.Builder builder()
      Returns a builder for customizing the classifier.
    • classify

      public TaskType classify(String message)
      Classifies the given message into a task type.
      Parameters:
      message - The user message to classify
      Returns:
      The detected TaskType
    • classify

      public TaskType classify(String message, List<String> history)
      Classifies the message with context from conversation history.
      Parameters:
      message - The user message
      history - Previous messages in the conversation
      Returns:
      The detected TaskType
    • classifyWithConfidence

      public TaskClassifier.ClassificationResult classifyWithConfidence(String message)
      Classifies with confidence score.
      Parameters:
      message - The user message
      Returns:
      Classification result with confidence
    • classifyWithConfidence

      public TaskClassifier.ClassificationResult classifyWithConfidence(String message, List<String> history)
      Classifies with confidence score and history context.
      Parameters:
      message - The user message
      history - Previous messages
      Returns:
      Classification result with confidence