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

48 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

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 re
import ujson as json
from core.builtins import Bot
from core.component import on_command
from core.logger import Logger
from core.utils.http import post_url
s = on_command('summary', developers=['Dianliang233'], desc='生成聊天记录摘要', available_for=['QQ', 'QQ|Group'])
def remove_suffix(string, suffix):
return string[:-len(suffix)] if string.endswith(suffix) else string
@s.handle()
async def _(msg: Bot.MessageSession):
f_msg = await msg.waitNextMessage('接下来,请发送要生成摘要的合并转发消息。', append_instruction=False)
f = re.search(r'\[Ke:forward,id=(.*?)\]', f_msg.asDisplay()).group(1)
if not f:
await msg.finish('未检测到合并转发消息。')
return
Logger.info(f)
data = await f_msg.call_api('get_forward_msg', message_id=f)
msgs = data['messages']
texts = [f'\n{m["sender"]["nickname"]}{m["content"]}' for m in msgs]
char_count = sum([len(i) for i in texts])
wait_msg = await msg.sendMessage(f'正在生成摘要。您的聊天记录共 {char_count} 个字符,大约需要 {round(char_count / 33.5, 1)} 秒。请稍候……')
nth = 0
output = ''
while nth < len(texts):
prompt = f'请总结<|chat_start|>与<|chat_end|>之间的聊天内容。要求语言简练,但必须含有所有要点,以一段话的形式输出。' \
f'{f"<|ctx_start|>与<|ctx_end|>之间记录了聊天内容的上下文,你可以作为参考,但请你务必在输出结果之前将其原样复制。<|ctx_start|>{output}<|ctx_end|>" if nth != 0 else ""}'
len_prompt = len(prompt)
post_texts = ''
for t in texts[nth:]:
if len(post_texts) + len_prompt < 1980:
post_texts += texts[nth]
nth += 1
else:
break
output += remove_suffix(await post_url('https://chat-simplifier.imzbb.cc/api/generate', data=json.dumps(
{'prompt': f'''{prompt}<|start|>
{post_texts}
<|end|>'''}), headers={'Content-Type': 'application/json'}, timeout=9999999), '<|im_end|>')
await wait_msg.delete()
await msg.finish(output, disable_secret_check=True)