fix(interest_calculator): 调整兴趣计算超时设置,优化性能和用户体验
This commit is contained in:
@@ -91,12 +91,12 @@ class InterestManager:
|
||||
logger.error(f"注册兴趣值计算组件失败: {e}")
|
||||
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:
|
||||
message: 数据库消息对象
|
||||
timeout: 最大等待时间(秒),超时则使用默认值返回
|
||||
timeout: 最大等待时间(秒),超时则使用默认值返回;为None时不设置超时
|
||||
|
||||
Returns:
|
||||
InterestCalculationResult: 计算结果或默认结果
|
||||
@@ -113,6 +113,9 @@ class InterestManager:
|
||||
# 使用 create_task 异步执行计算
|
||||
task = asyncio.create_task(self._async_calculate(message))
|
||||
|
||||
if timeout is None:
|
||||
return await task
|
||||
|
||||
try:
|
||||
# 等待计算结果,但有超时限制
|
||||
result = await asyncio.wait_for(task, timeout=timeout)
|
||||
|
||||
@@ -191,12 +191,12 @@ class AffinityInterestCalculator(BaseInterestCalculator):
|
||||
logger.debug(f"开始兴趣匹配计算,内容: {content[:50]}...")
|
||||
|
||||
try:
|
||||
# 使用机器人的兴趣标签系统进行智能匹配(1.5秒超时保护)
|
||||
# 使用机器人的兴趣标签系统进行智能匹配(5秒超时保护)
|
||||
match_result = await asyncio.wait_for(
|
||||
bot_interest_manager.calculate_interest_match(
|
||||
content, keywords or [], getattr(message, "semantic_embedding", None)
|
||||
),
|
||||
timeout=1.5
|
||||
timeout=5.0
|
||||
)
|
||||
logger.debug(f"兴趣匹配结果: {match_result}")
|
||||
|
||||
@@ -215,7 +215,7 @@ class AffinityInterestCalculator(BaseInterestCalculator):
|
||||
return 0.0
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning("[超时] 兴趣匹配计算超时(>1.5秒),返回默认分值0.5以保留其他分数")
|
||||
logger.warning("[超时] 兴趣匹配计算超时(>5秒),返回默认分值0.5以保留其他分数")
|
||||
return 0.5 # 超时时返回默认分值,避免丢失提及分和关系分
|
||||
except Exception as e:
|
||||
logger.warning(f"智能兴趣匹配失败: {e}")
|
||||
|
||||
Reference in New Issue
Block a user