mirror of
https://github.com/proffesor-for-testing/agentic-qe.git
synced 2026-09-19 08:45:47 +08:00
0a7a1c8b09
## Added - ML-Based Complexity Classification with 4-level task analysis - Model Capability Registry with 25+ December 2025 models - Claude Opus 4.5, DeepSeek R1/V3, GPT-5, Gemini 2.5 Pro - Cost Optimization Strategies (PromptCompressor, Caching, Batching) - HybridRouter integration with complexity-based model selection ## Changed - README.md Quick Start section (user-focused, no version numbers) - .env.example rewritten for Phase 2 features - CONTRIBUTORS.md updated with @fndlalit's n8n contribution (#151) ## Fixed - HybridRouter RuVector test isolation - Compression benchmarks with honest expectations (2-8% vs false 50%) ## New Tests - ComplexityClassifier, ModelCapabilityRegistry, CompressionBenchmark - HybridRouter integration, model selection, and cost tracking tests 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
260 lines
7.7 KiB
Markdown
260 lines
7.7 KiB
Markdown
# Task 2.2.2: Model Selection Integration - Implementation Complete
|
|
|
|
**Status**: ✅ Complete
|
|
**Date**: 2025-12-24
|
|
**Task**: Integrate ModelCapabilityRegistry for intelligent model selection in HybridRouter
|
|
|
|
## Overview
|
|
|
|
Created `HybridRouterModelSelection` module that provides intelligent model selection capabilities using the `ModelCapabilityRegistry`. This enables the HybridRouter to make context-aware routing decisions based on task types, model capabilities, and performance metrics.
|
|
|
|
## Implemented Files
|
|
|
|
### 1. Core Module
|
|
**File**: `/workspaces/agentic-qe-cf/src/providers/HybridRouterModelSelection.ts` (15KB)
|
|
|
|
**Key Features**:
|
|
- Task type detection from prompt content (8 task types)
|
|
- Intelligent model selection based on task complexity
|
|
- Constraint-based filtering (cost, local-only, context window, capabilities)
|
|
- Quality rating updates with adaptive learning
|
|
- Primary + alternative model recommendations
|
|
|
|
**Main Functions**:
|
|
```typescript
|
|
class HybridRouterModelSelection {
|
|
// Detect task type from completion options
|
|
detectTaskType(options: LLMCompletionOptions): TaskType
|
|
|
|
// Select best model for a specific task
|
|
selectBestModel(
|
|
taskType: TaskType,
|
|
complexity: TaskComplexity,
|
|
constraints?: ModelConstraints
|
|
): { provider: string; model: string; reason: string }
|
|
|
|
// Get comprehensive recommendation with alternatives
|
|
getModelRecommendation(
|
|
options: LLMCompletionOptions,
|
|
constraints?: ModelConstraints
|
|
): ModelSelectionResult
|
|
|
|
// Update quality ratings based on performance
|
|
updateModelQuality(
|
|
modelId: string,
|
|
taskType: TaskType,
|
|
success: boolean,
|
|
latency: number
|
|
): void
|
|
}
|
|
```
|
|
|
|
### 2. Comprehensive Test Suite
|
|
**File**: `/workspaces/agentic-qe-cf/tests/unit/providers/HybridRouter-model-selection.test.ts` (19KB)
|
|
|
|
**Test Coverage**: 29 passing tests across 6 test suites
|
|
- ✅ Task type detection (7 tests)
|
|
- ✅ Model selection with constraints (6 tests)
|
|
- ✅ Model recommendations (6 tests)
|
|
- ✅ Quality rating updates (5 tests)
|
|
- ✅ Integration scenarios (3 tests)
|
|
- ✅ Registry access (2 tests)
|
|
|
|
## Key Implementation Details
|
|
|
|
### Task Type Detection
|
|
Uses regex patterns to detect 8 task types from prompt content:
|
|
- `test-generation` - Unit test creation, test suites
|
|
- `coverage-analysis` - Coverage gaps, uncovered code
|
|
- `code-review` - PR reviews, code quality checks
|
|
- `bug-detection` - Debugging, error finding
|
|
- `documentation` - API docs, README generation
|
|
- `refactoring` - Code cleanup, design patterns
|
|
- `performance-testing` - Load tests, benchmarks
|
|
- `security-scanning` - Vulnerability detection, OWASP
|
|
|
|
### Model Selection Strategy
|
|
1. **Filter by Task Support**: Only models that support the detected task type
|
|
2. **Apply Constraints**: Cost limits, local-only, context window requirements
|
|
3. **Score & Rank**: Based on quality ratings, benchmarks, and complexity match
|
|
4. **Return Top Choice**: With reasoning and confidence score
|
|
|
|
### Adaptive Learning
|
|
Quality ratings are updated using exponential moving average (70% old, 30% new):
|
|
- **Base Rating**: 0.8 for success, 0.2 for failure
|
|
- **Latency Bonus**: Up to +0.2 for fast responses (<3s)
|
|
- **Latency Penalty**: Up to -0.2 for slow responses (>6s)
|
|
|
|
### Integration Points
|
|
The module is designed as a composable utility that can be:
|
|
1. Used directly by HybridRouter for routing decisions
|
|
2. Integrated into completion options analysis
|
|
3. Used for model recommendation APIs
|
|
4. Extended with custom task types and patterns
|
|
|
|
## Test Results
|
|
|
|
```
|
|
PASS tests/unit/providers/HybridRouter-model-selection.test.ts
|
|
HybridRouterModelSelection
|
|
✓ detectTaskType (7 tests)
|
|
✓ selectBestModel (6 tests)
|
|
✓ getModelRecommendation (6 tests)
|
|
✓ updateModelQuality (5 tests)
|
|
✓ Integration scenarios (3 tests)
|
|
✓ Registry access (2 tests)
|
|
|
|
Test Suites: 1 passed
|
|
Tests: 29 passed
|
|
Time: 70.279s
|
|
```
|
|
|
|
## Usage Examples
|
|
|
|
### Basic Model Selection
|
|
```typescript
|
|
import { HybridRouterModelSelection } from './providers/HybridRouterModelSelection';
|
|
import { TaskComplexity } from './providers/HybridRouter';
|
|
|
|
const selection = new HybridRouterModelSelection();
|
|
|
|
// Select model for test generation
|
|
const result = selection.selectBestModel(
|
|
'test-generation',
|
|
TaskComplexity.MODERATE,
|
|
{ requiresLocal: true, maxCostPer1M: 1.0 }
|
|
);
|
|
|
|
console.log(result);
|
|
// {
|
|
// provider: 'ollama',
|
|
// model: 'qwen-2.5-coder-32b',
|
|
// reason: 'Best match for test-generation, Complexity: moderate, Quality rating: 85%, Local deployment required, Cost limit: $1/1M tokens'
|
|
// }
|
|
```
|
|
|
|
### Automatic Task Detection
|
|
```typescript
|
|
const options: LLMCompletionOptions = {
|
|
model: 'auto',
|
|
messages: [
|
|
{
|
|
role: 'user',
|
|
content: 'Generate unit tests for the UserService class'
|
|
}
|
|
]
|
|
};
|
|
|
|
const recommendation = selection.getModelRecommendation(options);
|
|
|
|
console.log(recommendation);
|
|
// {
|
|
// primary: { modelId: 'qwen-2.5-coder-32b', ... },
|
|
// alternatives: [{ modelId: 'deepseek-coder-33b', ... }, ...],
|
|
// reasoning: 'Selected qwen-2.5-coder-32b for test-generation. Task complexity: simple. Quality rating: 85%. ...',
|
|
// confidence: 0.82
|
|
// }
|
|
```
|
|
|
|
### Adaptive Learning
|
|
```typescript
|
|
// After using a model, update its quality rating
|
|
selection.updateModelQuality(
|
|
'qwen-2.5-coder-32b',
|
|
'test-generation',
|
|
true, // success
|
|
1500 // latency in ms
|
|
);
|
|
|
|
// Quality rating improves due to:
|
|
// - Successful outcome (+0.8 base)
|
|
// - Fast response (+0.2 bonus for <3s)
|
|
// - New rating: 0.7 * old_rating + 0.3 * 1.0 = improved
|
|
```
|
|
|
|
## Benefits
|
|
|
|
1. **Intelligent Routing**: Automatically selects optimal models based on task requirements
|
|
2. **Cost Optimization**: Respects cost constraints while maximizing quality
|
|
3. **Adaptive Learning**: Improves model selection over time based on actual performance
|
|
4. **Flexibility**: Supports multiple constraints and provides alternatives
|
|
5. **Transparency**: Detailed reasoning for each selection decision
|
|
|
|
## Next Steps for Integration
|
|
|
|
To integrate this module into HybridRouter:
|
|
|
|
1. **Import the module** in HybridRouter.ts:
|
|
```typescript
|
|
import { HybridRouterModelSelection } from './HybridRouterModelSelection';
|
|
```
|
|
|
|
2. **Initialize in constructor**:
|
|
```typescript
|
|
private modelSelection: HybridRouterModelSelection;
|
|
|
|
constructor(config: HybridRouterConfig) {
|
|
this.modelSelection = new HybridRouterModelSelection();
|
|
// ... rest of constructor
|
|
}
|
|
```
|
|
|
|
3. **Use in routing decisions**:
|
|
```typescript
|
|
private makeRoutingDecision(options: LLMCompletionOptions): RoutingDecision {
|
|
const taskType = this.modelSelection.detectTaskType(options);
|
|
const complexity = this.analyzeComplexity(options);
|
|
|
|
const modelRec = this.modelSelection.selectBestModel(
|
|
taskType,
|
|
complexity,
|
|
this.getConstraints()
|
|
);
|
|
|
|
return this.createDecision(
|
|
modelRec.provider === 'ollama' ? 'local' : 'cloud',
|
|
modelRec.model,
|
|
modelRec.reason,
|
|
complexity,
|
|
RequestPriority.NORMAL
|
|
);
|
|
}
|
|
```
|
|
|
|
4. **Update quality after requests**:
|
|
```typescript
|
|
private recordOutcome(outcome: RoutingOutcome): void {
|
|
// Existing tracking...
|
|
|
|
// Update model quality
|
|
this.modelSelection.updateModelQuality(
|
|
outcome.decision.model,
|
|
this.modelSelection.detectTaskType(/* original options */),
|
|
outcome.success,
|
|
outcome.actualLatency
|
|
);
|
|
}
|
|
```
|
|
|
|
## Files Created
|
|
|
|
- ✅ `/workspaces/agentic-qe-cf/src/providers/HybridRouterModelSelection.ts`
|
|
- ✅ `/workspaces/agentic-qe-cf/tests/unit/providers/HybridRouter-model-selection.test.ts`
|
|
|
|
## Verification
|
|
|
|
```bash
|
|
# Run tests
|
|
npm run test:unit -- --testNamePattern="HybridRouterModelSelection"
|
|
|
|
# Expected: All 29 tests pass
|
|
# Test Suites: 1 passed
|
|
# Tests: 29 passed
|
|
```
|
|
|
|
---
|
|
|
|
**Implementation Status**: ✅ Complete
|
|
**Test Status**: ✅ All Passing (29/29)
|
|
**Ready for Integration**: ✅ Yes
|