Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/summary/__init__.py

31 lines
1.4 KiB
Python
Raw Normal View History

2023-02-07 01:35:05 +00:00
import re
2023-02-07 06:37:14 +00:00
import ujson as json
2023-02-07 01:35:05 +00:00
from core.builtins import Bot
from core.component import on_command
2023-02-07 05:27:25 +00:00
from core.logger import Logger
2023-02-07 06:37:14 +00:00
from core.utils.http import post_url
2023-02-07 01:35:05 +00:00
2023-02-07 06:37:14 +00:00
s = on_command('summary', developers=['Dianliang233'], desc='生成聊天记录摘要', available_for=['QQ|Group'])
2023-02-07 01:35:05 +00:00
@s.handle()
async def _(msg: Bot.MessageSession):
2023-02-07 06:37:14 +00:00
f_msg = await msg.waitNextMessage('请发送要生成摘要的合并转发消息。', append_instruction=False)
2023-02-07 05:27:25 +00:00
f = re.search(r'\[Ke:forward,id=(.*?)\]', f_msg.asDisplay()).group(1)
2023-02-07 06:37:14 +00:00
if not f:
await msg.finish('未检测到合并转发消息。')
return
2023-02-07 05:27:25 +00:00
Logger.info(f)
data = await f_msg.call_api('get_forward_msg', message_id=f)
msgs = data['messages']
2023-02-07 01:51:40 +00:00
text = ''
2023-02-07 01:35:05 +00:00
for m in msgs:
2023-02-07 01:51:40 +00:00
text += f'\n{m["sender"]["nickname"]}ID{m["sender"]["user_id"]}Unix时间{m["time"]}{m["content"]}'
2023-02-07 06:38:31 +00:00
wait_msg = await msg.sendMessage(f'正在生成摘要。您的聊天记录共 {len(text)} 个字符,大约需要 {round(len(text) / 33.5, 1)} 秒请稍候……')
2023-02-07 06:41:43 +00:00
res = await post_url('https://chat-simplifier.imzbb.cc/api/generate', data=json.dumps({'prompt': f'''把以下聊天记录概括为一段完整的纪要。当遇到!!!CHATENDS时聊天记录结束请在下方续写其摘要
2023-02-07 06:37:14 +00:00
{text}
!!!CHATENDS'''}), headers={'Content-Type': 'application/json'})
2023-02-07 06:38:31 +00:00
await wait_msg.delete()
2023-02-07 06:37:14 +00:00
await msg.finish(res.removesuffix('<|im_end|>'), disable_secret_check=True)