Package com.tnsai.llm.routing
Class TaskClassifier
java.lang.Object
com.tnsai.llm.routing.TaskClassifier
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
- Keyword Matching: Check for task-specific keywords
- Pattern Detection: Regex patterns for code, math, etc.
- Context Analysis: Consider message history if available
- 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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for TaskClassifier.static final recordClassification result with confidence score. -
Method Summary
Modifier and TypeMethodDescriptionstatic TaskClassifier.Builderbuilder()Returns a builder for customizing the classifier.Classifies the given message into a task type.Classifies the message with context from conversation history.classifyWithConfidence(String message) Classifies with confidence score.classifyWithConfidence(String message, List<String> history) Classifies with confidence score and history context.static TaskClassifierReturns the default classifier with standard settings.
-
Method Details
-
defaultClassifier
Returns the default classifier with standard settings. -
builder
Returns a builder for customizing the classifier. -
classify
Classifies the given message into a task type.- Parameters:
message- The user message to classify- Returns:
- The detected TaskType
-
classify
Classifies the message with context from conversation history.- Parameters:
message- The user messagehistory- Previous messages in the conversation- Returns:
- The detected TaskType
-
classifyWithConfidence
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 messagehistory- Previous messages- Returns:
- Classification result with confidence
-