From 690b15896591ef79b9476be787d9d6596ac13369 Mon Sep 17 00:00:00 2001 From: liuwei Date: Wed, 12 Feb 2025 14:21:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8A=9F=E8=83=BD=EF=BC=9A?= =?UTF-8?q?=E7=BE=A4=E6=80=BB=E7=BB=93=EF=BC=8C=E6=80=BB=E7=BB=93=E6=97=B6?= =?UTF-8?q?=E9=97=B48=E5=B0=8F=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- message_storage/message_to_db.py | 44 +++++++++++++++++- message_summary/message_split.py | 39 ++++++++++++++++ message_summary/message_summary_4o.py | 64 +++++++++++++++++++++++++++ robot.py | 25 ++++++++--- 4 files changed, 164 insertions(+), 8 deletions(-) create mode 100644 message_summary/message_split.py create mode 100644 message_summary/message_summary_4o.py diff --git a/message_storage/message_to_db.py b/message_storage/message_to_db.py index 7a1a497..0c68734 100644 --- a/message_storage/message_to_db.py +++ b/message_storage/message_to_db.py @@ -1,4 +1,5 @@ import pymysql +from datetime import datetime, timedelta # 配置数据库连接 db_config = { @@ -35,13 +36,52 @@ def archive_message(group_id, timestamp_str, sender, content, message_type, atta connection.close() +def get_messages(all_contacts: dict): + # 连接到数据库 + connection = pymysql.connect(**db_config) + + try: + with connection.cursor() as cursor: + # 获取当前时间并计算8小时前的时间 + current_time = datetime.now() + eight_hours_ago = current_time - timedelta(hours=8) + + # 转换为数据库中存储的时间格式 (假设timestamp是DATETIME类型) + eight_hours_ago_str = eight_hours_ago.strftime('%Y-%m-%d %H:%M:%S') + + # 执行查询,获取最近8小时的消息 + query = """ + SELECT group_id, timestamp, sender, content + FROM messages + WHERE timestamp >= %s AND message_type =1 + """ + cursor.execute(query, (eight_hours_ago_str,)) + + # 提取结果并组成带逗号的字符串 + result = [] + 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[sender]},{content}") + + # 将列表中的字符串连接成一个最终的结果 + result_str = "\n".join(result) + print(result_str) # 输出带逗号的字符串 + return result_str + + finally: + connection.close() # 关闭数据库连接 + + # 示例用法 if __name__ == "__main__": - group_id ='XXX@123123' + group_id = 'XXX@123123' timestamp_str = "2025-02-06 11:15:28" sender = "XXX" content = "This is a test message with a string timestamp." message_type = "text" attachment_url = "http://example.com/attachment.pdf" # 可以为None如果没有附件 - archive_message(group_id,timestamp_str, sender, content, message_type, attachment_url) + archive_message(group_id, timestamp_str, sender, content, message_type, attachment_url) diff --git a/message_summary/message_split.py b/message_summary/message_split.py new file mode 100644 index 0000000..2455b75 --- /dev/null +++ b/message_summary/message_split.py @@ -0,0 +1,39 @@ +import json + +# 模拟返回的API数据 +data_string = '''{ + "id":"chatcmpl-1739339885", + "object":"chat.completion", + "created":1739339885, + "model":"windsurf", + "choices":[ + { + "index":0, + "message":{ + "role":"assistant", + "content":"[聊天主题] 婚姻与创业话题讨论 \n☆☆参与者:wxid_atv7kzgxv9lg21, wxid_j1f8su26egm222, wxid_nn630aasb1nb22, wxid_q1ugj6gbjj3o12, wxid_ne15uczjd2mi21, wxid_ayf2rispnhwi21 \n时间点:2025-02-12 12:59:14 - 2025-02-12 13:56:02 \n内容摘要: 本次聊天涵盖了多个话题,包括使用道具、杭州新兴科技公司、医疗手术、杜蕾斯品牌、创业公司、以及婚姻中性行为的问题。wxid_j1f8su26egm222提到杭州出现了新的科技公司,而wxid_ayf2rispnhwi21则提出了关于婚姻和性行为的疑问,引发了群内的讨论。 \n☑ 点评: 讨论内容较为分散,涉及多个话题。关于杭州科技公司的讨论显示出对当地创业环境的关注,而关于婚姻和性行为的问题则反映了个人生活中的实际困惑。这些话题虽然不直接相关,但都反映了群成员对生活不同方面的关注。 \n· 趣味互动 wxid_ayf2rispnhwi21关于婚姻和性行为的提问引发了群内的笑声和调侃,体现了群成员之间轻松的互动氛围。 \n· 待跟进事项 无具体行动项。未来可以考虑深入讨论某一特定话题,如创业或婚姻问题。 \n· 结语 今日的讨论涉及多个生活领域,展现了群成员对不同话题的兴趣和幽默感。期待未来能有更多深入的讨论,鼓励大家继续积极参与。 \n注: 本次讨论话题较为多样,未深入展开某一单一主题。" + }, + "finish_reason":"stop" + } + ], + "usage":{ + "completion_tokens":1024, + "prompt_tokens":0, + "total_tokens":1024 + } +}''' + +# 解析JSON +def extract_content(data_string): + try: + data = json.loads(data_string) + # 提取content字段 + content = data["choices"][0]["message"].get("content", "") + return content + except json.JSONDecodeError: + print("Invalid JSON") + return None + +# 获取并打印content内容 +content = extract_content(data_string) +print(f"Extracted Content: {content}") diff --git a/message_summary/message_summary_4o.py b/message_summary/message_summary_4o.py new file mode 100644 index 0000000..b8ad298 --- /dev/null +++ b/message_summary/message_summary_4o.py @@ -0,0 +1,64 @@ +import requests +import json + + +# 解析JSON +def extract_content(data_string): + try: + data = json.loads(data_string) + # 提取content字段 + content = data["choices"][0]["message"].get("content", "") + return content + except json.JSONDecodeError: + print("Invalid JSON") + return None + + +def message_summary(content): + # 设置Authorization和URL + authorization = "46a5674a-e978-491b-a810-5d54605f2c36" # 请替换为真实的Authorization token + url = 'http://127.0.0.1:8080/v1/chat/completions' + + prompt = ('你是一个聊天记录总结助手,请使用如下格式进行聊天记录整理:' + '[聊天主题] ☆☆☆' + '参与者:[列出参与者名字]' + '时间点:[开始时间] - [结束时间]' + '内容摘要: [简要总结讨论的内容,包括讨论的主题、观点、信息等。]' + '☑ 点评: [对讨论内容的简要评价,结合实际情况分析其意义或影响。]' + '· 趣味互动' + '[列出群成员之间互动的有趣部分,例如个人经验、笑点或特别的观点分享等。]' + '· 待跟进事项' + '[列出具体的行动项或下一步的讨论计划,包括需要落实的具体任务。]' + '· 结语' + '[总结今日讨论的主题和氛围,简要表达对未来讨论的期待。鼓励群成员继续参与。]' + '注: [对本次讨论的特殊情况进行说明,例如讨论话题较简单或较复杂等。]') + # 设置请求的payload + data = { + # "stream": True, + "model": "windsurf/gpt4o", + "messages": [ + { + "role": "system", + "content": f"{prompt}" + }, + { + "role": "user", + "content": f"{content}" + } + + ] + } + + # 设置请求头 + headers = { + "Content-Type": "application/json; charset=utf-8", + "Authorization": authorization + } + + # 发送POST请求 + response = requests.post(url, headers=headers, data=json.dumps(data), ) + response.encoding = 'utf-8' + + # 输出响应内容 + print(response.status_code) + return extract_content(response.text) diff --git a/robot.py b/robot.py index da8de1c..e04c9b3 100644 --- a/robot.py +++ b/robot.py @@ -28,7 +28,8 @@ __version__ = "39.2.4.0" from message_report.process_message import process_message from message_report.write_db import write_to_db, generate_and_send_ranking -from message_storage.message_to_db import archive_message +from message_storage.message_to_db import archive_message, get_messages +from message_summary.message_summary_4o import message_summary from sehuatang.shehuatang import pdf_file_path @@ -127,8 +128,13 @@ class Robot(Job): q = re.sub(r"@.*?[\u2005|\s]", "", msg.content).replace(" ", "") if q == "今日百度新闻": self.newsBaiduReport() + return True elif q in ["nbc", "cnn", "abc", "fox", "bbc"]: self.newsEnReport(q) + return True + elif q == '/总结': + self.message_summary_robot((msg.roomid if msg.from_group() else msg.sender)) + return True else: rsp = self.chat.get_answer(q, (msg.roomid if msg.from_group() else msg.sender)) @@ -350,11 +356,18 @@ class Robot(Job): except Exception as e: self.LOG.error(f"SendRanking error:{e}") - def generateSehuatangPdf(self): try: - path =pdf_file_path() - #暂时只发4K群 - self.wcf.send_file(path,"45317011307@chatroom") + path = pdf_file_path() + # 暂时只发4K群 + self.wcf.send_file(path, "45317011307@chatroom") except Exception as e: - self.LOG.error(f"generateSehuatangPdf error:{e}") \ No newline at end of file + self.LOG.error(f"generateSehuatangPdf error:{e}") + + def message_summary_robot(self, sender: str = None): + try: + content = get_messages(self.allContacts) + summary = message_summary(content) + self.sendTextMsg(summary, sender) + except Exception as e: + self.LOG.error(f"message_summary_robot error:{e}")