优化了群总结prompt,减少了总结相关内容的有效性。
This commit is contained in:
@@ -42,51 +42,58 @@ def archive_message(group_id, timestamp_str, sender, content, message_type, atta
|
||||
|
||||
def get_messages(group_id, all_contacts: dict):
|
||||
# 连接到数据库
|
||||
connection = pymysql.connect(**db_config)
|
||||
|
||||
try:
|
||||
# 获取redis 中的上次总结时间,本次从上次开始算,弱没有,则从8小时之前开始计算
|
||||
# 生成Redis key
|
||||
with pymysql.connect(**db_config) as connection:
|
||||
# 获取 redis 中的上次总结时间,本次从上次开始算,若没有,则从 8 小时之前开始计算
|
||||
key = f"{group_id}:summary_time"
|
||||
last_summary_time = r.get(key)
|
||||
|
||||
# 如果 Redis 返回值为字节类型,转换为字符串
|
||||
if last_summary_time:
|
||||
last_summary_time = last_summary_time.decode('utf-8')
|
||||
|
||||
current_time = datetime.now()
|
||||
current_date = current_time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
if last_summary_time is None:
|
||||
# 获取当前时间并计算8小时前的时间
|
||||
|
||||
if not last_summary_time:
|
||||
# 获取当前时间并计算 8 小时前的时间
|
||||
eight_hours_ago = current_time - timedelta(hours=8)
|
||||
|
||||
# 转换为数据库中存储的时间格式 (假设timestamp是DATETIME类型)
|
||||
last_summary_time = eight_hours_ago.strftime('%Y-%m-%d %H:%M:%S')
|
||||
r.set(key, current_date)
|
||||
with connection.cursor() as cursor:
|
||||
else:
|
||||
# 检查 redis 中的时间与当前时间差是否小于 3 小时
|
||||
last_summary_time_obj = datetime.strptime(last_summary_time, '%Y-%m-%d %H:%M:%S')
|
||||
time_diff = current_time - last_summary_time_obj
|
||||
|
||||
# 执行查询,获取最近8小时的消息
|
||||
if time_diff < timedelta(hours=3):
|
||||
# 如果小于 3 小时,取当天的内容
|
||||
last_summary_time = current_time.replace(hour=0, minute=0, second=0, microsecond=0).strftime(
|
||||
'%Y-%m-%d %H:%M:%S')
|
||||
elif time_diff > timedelta(days=1):
|
||||
# 如果超过 24 小时,将时间设置为当天 0 点
|
||||
last_summary_time = current_time.replace(hour=0, minute=0, second=0, microsecond=0).strftime(
|
||||
'%Y-%m-%d %H:%M:%S')
|
||||
|
||||
# 更新 Redis 存储的当前时间
|
||||
r.set(key, current_date)
|
||||
|
||||
with connection.cursor() as cursor:
|
||||
# 执行查询,获取最近 8 小时的消息
|
||||
query = """
|
||||
SELECT group_id, timestamp, sender, content
|
||||
FROM messages
|
||||
WHERE timestamp >= %s AND message_type =1 and group_id = %s
|
||||
"""
|
||||
SELECT timestamp, sender, content
|
||||
FROM messages
|
||||
WHERE timestamp >= %s AND message_type = 1 AND group_id = %s
|
||||
"""
|
||||
cursor.execute(query, (last_summary_time, group_id))
|
||||
|
||||
# 提取结果并组成带逗号的字符串
|
||||
# 构建最终的结果字符串
|
||||
result = []
|
||||
if cursor.rowcount > 0:
|
||||
result.append(f"聊天记录总行数:{cursor.rowcount}")
|
||||
for row in cursor.fetchall():
|
||||
group_id = row[0]
|
||||
timestamp = row[1]
|
||||
sender = row[2]
|
||||
content = row[3]
|
||||
result.append(f"{group_id},{timestamp},{all_contacts.get(sender, sender)},{content}")
|
||||
timestamp, sender, content = row
|
||||
sender_name = all_contacts.get(sender, sender) # 获取发送者的名字,若找不到则使用原 ID
|
||||
result.append(f"{timestamp},{sender_name},{content}")
|
||||
|
||||
# 将列表中的字符串连接成一个最终的结果
|
||||
result_str = "\n".join(result)
|
||||
# print(result_str) # 输出带逗号的字符串
|
||||
result_str = "\n".join(result) # 将结果拼接为最终字符串
|
||||
return result_str
|
||||
|
||||
finally:
|
||||
connection.close() # 关闭数据库连接
|
||||
|
||||
|
||||
# 示例用法
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user