You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何检测BPO坐席单实例下的并发聊天违规问题

坐席并发聊天违规检测实现方案

核心思路

要检测坐席是否超出并发阈值,核心是判断同一坐席在任意时间点同时进行的聊天数量是否超过阈值。具体步骤为:

  • 按坐席分组处理数据
  • 对每个坐席的聊天时间区间,找出所有重叠的聊天组并统计数量
  • 筛选出重叠数量超过阈值的聊天组

完整代码实现

import pandas as pd

def detect_concurrency_violations(df, concurrency_threshold):
    # 转换时间列为datetime类型,确保时间比较准确
    df['AgentAssignmentTimestamp'] = pd.to_datetime(df['AgentAssignmentTimestamp'])
    df['ChatEndTime'] = pd.to_datetime(df['ChatEndTime'])
    
    violation_groups = []
    
    # 按坐席分组处理数据
    for agent_email, agent_data in df.groupby('AgentEmail'):
        # 按聊天分配时间排序,方便后续重叠判断
        sorted_chats = agent_data.sort_values('AgentAssignmentTimestamp').reset_index(drop=True)
        total_chats = len(sorted_chats)
        
        # 遍历每个聊天,找出所有与之重叠的聊天
        for i in range(total_chats):
            current_chat = sorted_chats.iloc[i]
            # 筛选与当前聊天时间区间有交集的所有聊天
            overlapping_chats = sorted_chats[
                (sorted_chats['AgentAssignmentTimestamp'] <= current_chat['ChatEndTime']) &
                (sorted_chats['ChatEndTime'] >= current_chat['AgentAssignmentTimestamp'])
            ]
            
            # 若重叠数量超过阈值,且该组未被记录过则加入结果
            if len(overlapping_chats) > concurrency_threshold:
                chat_id_set = frozenset(overlapping_chats['RoomCode'])
                if chat_id_set not in violation_groups:
                    violation_groups.append(chat_id_set)
    
    # 将违规组转换为DataFrame格式输出
    result_cols = [f'Chat_{i+1}' for i in range(concurrency_threshold+1)]
    violation_results = pd.DataFrame([list(group) for group in violation_groups], columns=result_cols)
    return violation_results

# 示例测试
if __name__ == "__main__":
    # 模拟测试数据
    test_data = {
        'RoomCode': ['R1', 'R2', 'R3', 'R4', 'R5'],
        'AgentEmail': ['agent@example.com']*5,
        'AgentAssignmentTimestamp': ['2024-05-20 09:00:00', '2024-05-20 09:05:00', '2024-05-20 09:10:00', '2024-05-20 09:12:00', '2024-05-20 09:20:00'],
        'ChatEndTime': ['2024-05-20 09:30:00', '2024-05-20 09:25:00', '2024-05-20 09:40:00', '2024-05-20 09:35:00', '2024-05-20 09:45:00']
    }
    df = pd.DataFrame(test_data)
    # 设定并发阈值为3
    violations = detect_concurrency_violations(df, 3)
    print("违规聊天组:")
    print(violations)

代码说明

  • 时间类型转换:确保时间列是datetime格式,避免字符串比较导致的错误
  • 按坐席分组:每个坐席的聊天数据独立处理,避免跨坐席干扰判断
  • 重叠聊天判断:通过时间区间交叉条件筛选重叠聊天,只要两个聊天的时间区间有交集,即视为同时处理
  • 违规组去重:用frozenset存储聊天ID集合,避免同一违规组被多次记录
  • 结果格式化:将违规组转为DataFrame,方便查看和导出报表

注意事项

  • 若ChatEndTime存在空值,需提前按业务规则处理(比如填充为当前时间或标记为未结束状态)
  • 针对大规模数据集,可考虑使用区间树等数据结构优化重叠判断效率,上述代码适合中小规模数据场景

内容的提问来源于stack exchange,提问作者Ritesh Mukhopadhyay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 20:04:53