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
2023-03-31 10:58:04 +08:00

58 lines
2.3 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 openai
from core.builtins import Bot
from core.component import module
from core.dirty_check import check_bool
from core.logger import Logger
from config import Config
openai.api_key = Config('openai_api_key')
s = module('summary', developers=['Dianliang233', 'OasisAkari'], desc='{summary.help}', available_for=['QQ', 'QQ|Group'])
@s.handle('{{summary.help.summary}}')
async def _(msg: Bot.MessageSession):
f_msg = await msg.waitNextMessage(msg.locale.t('summary.message'), append_instruction=False)
try:
f = re.search(r'\[Ke:forward,id=(.*?)\]', f_msg.asDisplay()).group(1)
except AttributeError:
await msg.finish(msg.locale.t('summary.message.not_found'))
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(msg.locale.t('summary.message.waiting', count=char_count, time=round(char_count / 33.5, 1)))
nth = 0
prev = ''
while nth < len(texts):
prompt = f'请总结下列聊天内容。要求语言简练,但必须含有所有要点,以一段话的形式输出。除了聊天记录的摘要以外,不要输出其他任何内容。' \
f'''{f"""同时<ctx_start>与<|ctx_end|>之间记录了聊天内容的上下文,请你同时结合这段上下文和聊天记录,并以这段聊天记录的原语言形式输出。
<|ctx_start|>{prev}<|ctx_end|>""" if nth != 0 else ""}'''
len_prompt = len(prompt)
post_texts = ''
for t in texts[nth:]:
if len(post_texts) + len_prompt < 1970:
post_texts += texts[nth]
nth += 1
else:
break
completion = openai.ChatCompletion.create(
model='gpt-3.5-turbo',
messages=[
{'role': 'system', "content": "你是一个助手"},
{'role': 'user', "content": f'''{prompt}
{post_texts}'''},
]
)
output = completion['choices'][0]['message']['content']
await wait_msg.delete()
if await check_bool(output):
await msg.finish('https://wdf.ink/6OUp')
await msg.finish(output, disable_secret_check=True)