Spaces:
Sleeping
Sleeping
Nguyễn Thanh Tùng commited on
Commit ·
edffc65
1
Parent(s): 1b7ef16
Fix: Restore upsert_mentor method
Browse files
services/recommendation_service.py
CHANGED
|
@@ -21,7 +21,53 @@ class RecommendationService:
|
|
| 21 |
self.settings = get_settings()
|
| 22 |
self.data_service = DataService()
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def recommend_mentors(
|
| 27 |
self,
|
|
|
|
| 21 |
self.settings = get_settings()
|
| 22 |
self.data_service = DataService()
|
| 23 |
|
| 24 |
+
def upsert_mentor(
|
| 25 |
+
self,
|
| 26 |
+
mentor_data: Dict[str, Any]
|
| 27 |
+
) -> bool:
|
| 28 |
+
try:
|
| 29 |
+
mentor_text = build_mentor_text(mentor_data)
|
| 30 |
+
embedding = self.embedding_service.encode(mentor_text, is_query=False)
|
| 31 |
+
|
| 32 |
+
def safe_float(value, default=0.0):
|
| 33 |
+
if value is None:
|
| 34 |
+
return default
|
| 35 |
+
try:
|
| 36 |
+
return float(value)
|
| 37 |
+
except (ValueError, TypeError):
|
| 38 |
+
return default
|
| 39 |
+
|
| 40 |
+
def safe_int(value, default=0):
|
| 41 |
+
if value is None:
|
| 42 |
+
return default
|
| 43 |
+
try:
|
| 44 |
+
return int(value)
|
| 45 |
+
except (ValueError, TypeError):
|
| 46 |
+
return default
|
| 47 |
+
|
| 48 |
+
metadata = {
|
| 49 |
+
"mentor_id": str(mentor_data["mentor_id"]),
|
| 50 |
+
"rating": safe_float(mentor_data.get("rating"), 0.0),
|
| 51 |
+
"total_ratings": safe_int(mentor_data.get("total_ratings"), 0),
|
| 52 |
+
"session_count": safe_int(mentor_data.get("session_count"), 0),
|
| 53 |
+
"career_id": safe_int(mentor_data.get("career_id")) if mentor_data.get("career_id") else None,
|
| 54 |
+
"status": str(mentor_data.get("status") or "ACTIVATED"),
|
| 55 |
+
"mentor_text": mentor_text
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
if mentor_data.get("skill_ids"):
|
| 59 |
+
metadata["skill_ids"] = [str(int(id)) for id in mentor_data["skill_ids"]]
|
| 60 |
+
if mentor_data.get("domain_ids"):
|
| 61 |
+
metadata["domain_ids"] = [str(int(id)) for id in mentor_data["domain_ids"]]
|
| 62 |
+
|
| 63 |
+
return self.pinecone_service.upsert_mentor(
|
| 64 |
+
mentor_id=str(mentor_data["mentor_id"]),
|
| 65 |
+
vector=embedding,
|
| 66 |
+
metadata=metadata
|
| 67 |
+
)
|
| 68 |
+
except Exception as e:
|
| 69 |
+
logger.error(f"Failed to upsert mentor: {str(e)}")
|
| 70 |
+
raise
|
| 71 |
|
| 72 |
def recommend_mentors(
|
| 73 |
self,
|