-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecommender.py
More file actions
343 lines (288 loc) · 11.4 KB
/
Copy pathrecommender.py
File metadata and controls
343 lines (288 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""Template recommendation engine.
Provides ML-based template recommendations based on workload features.
Supports both trained model inference and rule-based fallback.
"""
import pickle
from pathlib import Path
import numpy as np
from app.core.config import settings
from app.schemas.ml import (
RecommendationResponse,
TemplateRecommendation,
TemplateType,
WorkloadFeatures,
)
class TemplateRecommender:
"""ML-powered template recommender.
Uses a trained scikit-learn model when available,
falls back to rule-based recommendations otherwise.
"""
def __init__(self, model_path: str | None = None):
"""Initialize recommender.
Args:
model_path: Path to trained model file. Uses config default if None.
"""
self.model_path = Path(model_path or settings.ml_model_path)
self.model = None
self.feature_names: list[str] = []
self._load_model()
def _load_model(self) -> None:
"""Load trained model from disk if available."""
if self.model_path.exists():
try:
with open(self.model_path, "rb") as f:
data = pickle.load(f)
self.model = data.get("model")
self.feature_names = data.get("feature_names", [])
except Exception:
# Fall back to rule-based if model load fails
self.model = None
def _features_to_vector(self, features: WorkloadFeatures) -> np.ndarray:
"""Convert WorkloadFeatures to numeric feature vector.
Args:
features: Input workload features
Returns:
Numpy array of numeric features
"""
# Language encoding (one-hot style as numeric)
lang_map = {
"python": 0,
"javascript": 1,
"typescript": 2,
"go": 3,
"java": 4,
"rust": 5,
}
lang_idx = lang_map.get(features.language.value, 0)
# Workload type encoding
workload_map = {
"api": 0,
"worker": 1,
"cron": 2,
"ml-pipeline": 3,
"frontend": 4,
"batch": 5,
}
workload_idx = workload_map.get(features.workload_type.value, 0)
# Database type encoding
db_map = {
"none": 0,
"postgresql": 1,
"mongodb": 2,
"redis": 3,
"mysql": 4,
}
db_idx = db_map.get(features.database_type.value, 0)
# Build feature vector
vector = np.array(
[
lang_idx,
workload_idx,
db_idx,
1 if features.needs_database else 0,
1 if features.needs_queue else 0,
1 if features.needs_cache else 0,
1 if features.needs_gpu else 0,
min(features.expected_rps / 10000, 1.0), # Normalize
min(features.expected_memory_mb / 8192, 1.0), # Normalize
min(features.team_size / 100, 1.0), # Normalize
1 if features.compliance_required else 0,
]
)
return vector.reshape(1, -1)
def _rule_based_recommend(
self, features: WorkloadFeatures
) -> list[tuple[TemplateType, float, list[str]]]:
"""Rule-based recommendation fallback.
Args:
features: Input workload features
Returns:
List of (template, confidence, reasons) tuples
"""
scores: dict[TemplateType, tuple[float, list[str]]] = {}
# Score each template based on rules
for template in TemplateType:
score, reasons = self._score_template(template, features)
scores[template] = (score, reasons)
# Sort by score descending
sorted_templates = sorted(
scores.items(),
key=lambda x: x[1][0],
reverse=True,
)
return [(template, score, reasons) for template, (score, reasons) in sorted_templates]
def _score_template(
self, template: TemplateType, features: WorkloadFeatures
) -> tuple[float, list[str]]:
"""Score a template for given features.
Args:
template: Template to score
features: Input features
Returns:
Tuple of (score, reasons)
"""
score = 0.0
reasons: list[str] = []
if template == TemplateType.MICROSERVICE_PYTHON:
if features.language.value == "python":
score += 0.4
reasons.append("Python language matches")
if features.workload_type.value == "api":
score += 0.3
reasons.append("API workload type")
if features.database_type.value == "postgresql":
score += 0.2
reasons.append("PostgreSQL support included")
if features.needs_cache:
score += 0.1
reasons.append("Redis caching included")
elif template == TemplateType.MICROSERVICE_NODE:
if features.language.value in ("javascript", "typescript"):
score += 0.4
reasons.append("JavaScript/TypeScript matches")
if features.workload_type.value == "api":
score += 0.3
reasons.append("API workload type")
if features.database_type.value == "mongodb":
score += 0.2
reasons.append("MongoDB support included")
if features.expected_rps > 1000:
score += 0.1
reasons.append("High throughput optimized")
elif template == TemplateType.WORKER_SERVICE:
if features.workload_type.value in ("worker", "cron", "batch"):
score += 0.5
reasons.append("Worker/background job workload")
if features.needs_queue:
score += 0.3
reasons.append("Queue processing support")
if not features.needs_database or features.database_type.value == "redis":
score += 0.2
reasons.append("Lightweight data storage")
elif template == TemplateType.ML_PIPELINE:
if features.workload_type.value == "ml-pipeline":
score += 0.5
reasons.append("ML pipeline workload type")
if features.needs_gpu:
score += 0.3
reasons.append("GPU support required")
if features.language.value == "python":
score += 0.2
reasons.append("Python ML ecosystem")
elif template == TemplateType.FRONTEND_APP:
if features.workload_type.value == "frontend":
score += 0.5
reasons.append("Frontend workload type")
if features.language.value in ("javascript", "typescript"):
score += 0.3
reasons.append("JavaScript/TypeScript frontend")
if not features.needs_database:
score += 0.2
reasons.append("Static/client-side focus")
return score, reasons
def recommend(self, features: WorkloadFeatures) -> RecommendationResponse:
"""Get template recommendation for workload features.
Args:
features: Input workload features
Returns:
RecommendationResponse with primary and alternative recommendations
"""
if self.model is not None:
return self._model_recommend(features)
return self._fallback_recommend(features)
def _model_recommend(self, features: WorkloadFeatures) -> RecommendationResponse:
"""Get recommendation using trained ML model.
Args:
features: Input workload features
Returns:
RecommendationResponse
"""
# Type guard - model is guaranteed to be not None when this method is called
assert self.model is not None, "Model must be loaded before calling _model_recommend"
vector = self._features_to_vector(features)
# Get probability predictions
probas = self.model.predict_proba(vector)[0]
classes = self.model.classes_
# Build recommendations from probabilities
recommendations: list[tuple[TemplateType, float, list[str]]] = []
for i, cls in enumerate(classes):
template = TemplateType(cls)
confidence = float(probas[i])
_, reasons = self._score_template(template, features)
recommendations.append((template, confidence, reasons))
# Sort by confidence
recommendations.sort(key=lambda x: x[1], reverse=True)
# Build response
primary = recommendations[0]
alternatives = recommendations[1:4] # Top 3 alternatives
return RecommendationResponse(
primary=TemplateRecommendation(
template=primary[0],
confidence=primary[1],
reasons=primary[2],
),
alternatives=[
TemplateRecommendation(
template=t[0],
confidence=t[1],
reasons=t[2],
)
for t in alternatives
if t[1] >= settings.ml_confidence_threshold * 0.3
],
input_summary={
"language": features.language.value,
"workload_type": features.workload_type.value,
"needs_database": features.needs_database,
"needs_gpu": features.needs_gpu,
},
)
def _fallback_recommend(self, features: WorkloadFeatures) -> RecommendationResponse:
"""Get recommendation using rule-based fallback.
Args:
features: Input workload features
Returns:
RecommendationResponse
"""
recommendations = self._rule_based_recommend(features)
# Normalize scores to [0, 1]
max_score = max(r[1] for r in recommendations) if recommendations else 1.0
if max_score == 0:
max_score = 1.0
normalized = [(r[0], r[1] / max_score, r[2]) for r in recommendations]
primary = normalized[0]
alternatives = [
r
for r in normalized[1:4]
if r[1] >= 0.3 # At least 30% of max score
]
return RecommendationResponse(
primary=TemplateRecommendation(
template=primary[0],
confidence=primary[1],
reasons=primary[2] if primary[2] else ["Best match based on rules"],
),
alternatives=[
TemplateRecommendation(
template=t[0],
confidence=t[1],
reasons=t[2] if t[2] else ["Alternative option"],
)
for t in alternatives
],
input_summary={
"language": features.language.value,
"workload_type": features.workload_type.value,
"needs_database": features.needs_database,
"needs_gpu": features.needs_gpu,
"mode": "rule-based",
},
)
# Global recommender instance (lazy loaded)
_recommender: TemplateRecommender | None = None
def get_recommender() -> TemplateRecommender:
"""Get or create the global recommender instance."""
global _recommender
if _recommender is None:
_recommender = TemplateRecommender()
return _recommender