Files
abot/message_summary/message_summary_4o.py
2025-03-11 16:59:01 +08:00

128 lines
4.3 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import requests
import json
def extract_content(data_string):
try:
data = json.loads(data_string)
# 提取content字段
content = data["choices"][0]["message"].get("content", "")
# 提取tokens相关内容加入容错处理
tokens_usage = data.get("usage", {})
# 确保tokens_usage是字典类型
if isinstance(tokens_usage, dict):
prompt_tokens = tokens_usage.get("prompt_tokens", 0)
completion_tokens = tokens_usage.get("completion_tokens", 0)
total_tokens = tokens_usage.get("total_tokens", 0)
else:
prompt_tokens = completion_tokens = total_tokens = 0
# 如果tokens信息为空提供默认值或提示
if prompt_tokens == 0 and completion_tokens == 0 and total_tokens == 0:
tokens_info = "\n\n【tokens】暂无数据"
else:
tokens_info = (f"\n\n【tokens】输入: {prompt_tokens} 生成: {completion_tokens} 总: {total_tokens}")
# 将tokens信息添加到content后面返回为字符串
content_with_tokens = content + tokens_info
return content_with_tokens
except json.JSONDecodeError:
print("Invalid JSON")
return None
def message_summary(content):
# 设置Authorization和URL
authorization = "Bearer b8586595-eb81-483d-8e91-a35cc789729e" # 请替换为真实的Authorization token
url = 'https://ark.cn-beijing.volces.com/api/v3/chat/completions'
# 群聊精华总结生成指令
prompt = """
【风格要求】
📢 轻松幽默,融入网络流行语+Emoji如👍/🎉等)。
【结构框架】
📌 标题10字内带Emoji
📊 数据概览(消息总数/最活跃时段)
🕒 聊天时段(自动识别最新日期,显示完整时间范围)
🌌 话题概览3-5个核心话题自动聚类过滤广告/无效内容)
🏆 今日MVP贡献最高成员+获奖理由)
【话题评分维度】
🎯 参与度(参与人数/消息密度)
💡 信息量(实用知识/资源分享)
🎭 娱乐值(段子/梗图/神回复)
⚡ 热议指数(讨论时长)
📌 评分方式基于话题热度动态生成⭐评分1-5星
【贡献者识别】
🏅 标记话题发起者 + 气氛担当。
📊 统计优质内容贡献者(表情包/段子/资源等)。
🎖️ 自动生成趣味头衔(如"段子王""知识达人")。
【输出模板】
「[群名]-[最新日期]群聊总结」
📊 今日数据快报:
- 总计 [消息数] 条
- 最活跃时段:[时段][消息数]条/小时)
- 聊天时段:[开始时间] - [结束时间]
🌌 话题总结:
1⃣ 【[话题1]】 ⭐️⭐️⭐️⭐️⭐️
🔍 [3句话简要回顾]
🏅 热点人物:[@用户A] [@用户B]
💬 精彩语录:"[精选金句]"...
2⃣ 【[话题2]】 ⭐️⭐️⭐️⭐️
🔍 [3句话高能回顾]
📌 实用内容:[资源链接/知识点总结]...
🎖️ 今日荣誉榜:
🏆 群聊MVP[@用户D]
👑 获奖理由:发起 [话题数] 个热门话题,贡献 [数量] 个表情包/段子,发明了“薯片袋静音开封法”已申请专利。
【参数调节】
⚙ 严谨度:调整评分权重,决定专业度 vs. 趣味性。
🔍 敏感词过滤:自动屏蔽政治/广告/违规内容。
🎭 个性化适配:根据群成员特点生成个性化头衔。
"""
data = {
# "stream": True,
"model": "doubao-1-5-lite-32k-250115",
"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)
print(response.text)
return extract_content(response.text)