Files
abot/message_summary/message_summary_4o.py
2025-03-14 10:06:35 +08:00

110 lines
3.4 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 = """
# 🌟「[最新日期] 群聊总结」🌟
## 📊 今日数据快报
- **总消息数**:📩 [消息数] 条
- **最活跃时段**:🔥 [时段] (📈 [消息数] 条/小时)
- **聊天时段**:🕒 [开始时间] - [结束时间]
## 🌌 话题总结
### 1⃣ 【[话题1]】 ⭐⭐⭐⭐⭐
🕒**聊天时段**[开始时间] - [结束时间]
🔍 **精彩回顾**
> [精彩回顾内容提炼]
🏅 **热点人物**[@用户A] [@用户B]
💬 **精彩语录**"[精选金句]"
### 2⃣ 【[话题2]】 ⭐⭐⭐⭐
🕒**聊天时段**[开始时间] - [结束时间]
🔍 **高能回顾**
> [精彩回顾内容提炼]
📌 **实用内容**[资源链接/知识点总结]
## 🎖️ 今日荣誉榜
🏆 **群聊 MVP**[@用户D]
👑 **获奖理由**
✅ 发起 [话题数] 个热门话题
✅ 贡献 [数量] 个表情包/段子
✅ **创新贡献**"薯片袋静音开封法"(已申请专利 🎉)
✨ *本总结由 AI 自动生成,快来看看你今天是不是最靓的崽!🔥*
"""
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)