feat(implicit memory): upgrade pydantic v2 compatibility and confidence level handling

- Replace deprecated `.dict()` with `.model_dump(mode='json')` for pydantic v2 compatibility
- Convert confidence level from enum-based strings to numerical values (0-100 scale)
- Add confidence level mapping in controller (high: 85, medium: 50, low: 20)
- Update dimension analyzer to handle both string and numeric confidence inputs
- Refactor habit analyzer confidence level validation logic
- Remove ConfidenceLevel enum import and replace with integer-based approach
- Update memory config validators for numerical confidence level support
- Ensure all implicit memory schemas use model_dump for serialization
- Improve type consistency across memory analytics modules
This commit is contained in:
Ke Sun
2026-01-08 17:50:01 +08:00
parent e05f33b286
commit 7167c2002f
7 changed files with 195 additions and 105 deletions

View File

@@ -171,7 +171,7 @@ async def get_preference_tags(
)
api_logger.info(f"Retrieved {len(tags)} preference tags for user: {user_id}")
return success(data=[tag.dict() for tag in tags], msg="偏好标签获取成功")
return success(data=[tag.model_dump(mode='json') for tag in tags], msg="偏好标签获取成功")
except Exception as e:
return handle_implicit_memory_error(e, "偏好标签获取", user_id)
@@ -210,7 +210,7 @@ async def get_dimension_portrait(
)
api_logger.info(f"Dimension portrait retrieved for user: {user_id}")
return success(data=portrait.dict(), msg="四维画像获取成功")
return success(data=portrait.model_dump(mode='json'), msg="四维画像获取成功")
except Exception as e:
return handle_implicit_memory_error(e, "四维画像获取", user_id)
@@ -249,7 +249,7 @@ async def get_interest_area_distribution(
)
api_logger.info(f"Interest area distribution retrieved for user: {user_id}")
return success(data=distribution.dict(), msg="兴趣领域分布获取成功")
return success(data=distribution.model_dump(mode='json'), msg="兴趣领域分布获取成功")
except Exception as e:
return handle_implicit_memory_error(e, "兴趣领域分布获取", user_id)
@@ -283,18 +283,28 @@ async def get_behavior_habits(
# Validate inputs
validate_user_id(user_id)
# Convert string confidence level to numerical
numerical_confidence = None
if confidence_level:
confidence_mapping = {
"high": 85,
"medium": 50,
"low": 20
}
numerical_confidence = confidence_mapping.get(confidence_level.lower())
# Create service with user-specific config
service = ImplicitMemoryService(db=db, end_user_id=user_id)
habits = await service.get_behavior_habits(
user_id=user_id,
confidence_level=confidence_level,
confidence_level=numerical_confidence,
frequency_pattern=frequency_pattern,
time_period=time_period
)
api_logger.info(f"Retrieved {len(habits)} behavior habits for user: {user_id}")
return success(data=[habit.dict() for habit in habits], msg="行为习惯获取成功")
return success(data=[habit.model_dump(mode='json') for habit in habits], msg="行为习惯获取成功")
except Exception as e:
return handle_implicit_memory_error(e, "行为习惯获取", user_id)