fix(interest_calculator): 调整兴趣计算超时设置,优化性能和用户体验

This commit is contained in:
Windpicker-owo
2025-12-10 11:34:42 +08:00
parent 685a43da02
commit c2c3c062b7
2 changed files with 8 additions and 5 deletions

View File

@@ -91,12 +91,12 @@ class InterestManager:
logger.error(f"注册兴趣值计算组件失败: {e}") logger.error(f"注册兴趣值计算组件失败: {e}")
return False return False
async def calculate_interest(self, message: "DatabaseMessages", timeout: float = 2.0) -> InterestCalculationResult: async def calculate_interest(self, message: "DatabaseMessages", timeout: float | None = None) -> InterestCalculationResult:
"""计算消息兴趣值 """计算消息兴趣值
Args: Args:
message: 数据库消息对象 message: 数据库消息对象
timeout: 最大等待时间(秒),超时则使用默认值返回 timeout: 最大等待时间(秒),超时则使用默认值返回为None时不设置超时
Returns: Returns:
InterestCalculationResult: 计算结果或默认结果 InterestCalculationResult: 计算结果或默认结果
@@ -113,6 +113,9 @@ class InterestManager:
# 使用 create_task 异步执行计算 # 使用 create_task 异步执行计算
task = asyncio.create_task(self._async_calculate(message)) task = asyncio.create_task(self._async_calculate(message))
if timeout is None:
return await task
try: try:
# 等待计算结果,但有超时限制 # 等待计算结果,但有超时限制
result = await asyncio.wait_for(task, timeout=timeout) result = await asyncio.wait_for(task, timeout=timeout)

View File

@@ -191,12 +191,12 @@ class AffinityInterestCalculator(BaseInterestCalculator):
logger.debug(f"开始兴趣匹配计算,内容: {content[:50]}...") logger.debug(f"开始兴趣匹配计算,内容: {content[:50]}...")
try: try:
# 使用机器人的兴趣标签系统进行智能匹配(1.5秒超时保护 # 使用机器人的兴趣标签系统进行智能匹配5秒超时保护
match_result = await asyncio.wait_for( match_result = await asyncio.wait_for(
bot_interest_manager.calculate_interest_match( bot_interest_manager.calculate_interest_match(
content, keywords or [], getattr(message, "semantic_embedding", None) content, keywords or [], getattr(message, "semantic_embedding", None)
), ),
timeout=1.5 timeout=5.0
) )
logger.debug(f"兴趣匹配结果: {match_result}") logger.debug(f"兴趣匹配结果: {match_result}")
@@ -215,7 +215,7 @@ class AffinityInterestCalculator(BaseInterestCalculator):
return 0.0 return 0.0
except asyncio.TimeoutError: except asyncio.TimeoutError:
logger.warning("[超时] 兴趣匹配计算超时(>1.5秒)返回默认分值0.5以保留其他分数") logger.warning("[超时] 兴趣匹配计算超时(>5秒)返回默认分值0.5以保留其他分数")
return 0.5 # 超时时返回默认分值,避免丢失提及分和关系分 return 0.5 # 超时时返回默认分值,避免丢失提及分和关系分
except Exception as e: except Exception as e:
logger.warning(f"智能兴趣匹配失败: {e}") logger.warning(f"智能兴趣匹配失败: {e}")