2023-02-07 01:35:05 +00:00
|
|
|
|
import re
|
2023-04-19 07:45:29 +00:00
|
|
|
|
|
2023-03-18 09:06:45 +00:00
|
|
|
|
import openai
|
|
|
|
|
|
2023-04-19 07:45:29 +00:00
|
|
|
|
from config import Config
|
2023-02-07 01:35:05 +00:00
|
|
|
|
from core.builtins import Bot
|
2023-03-04 08:51:56 +00:00
|
|
|
|
from core.component import module
|
2023-07-14 07:59:01 +00:00
|
|
|
|
from core.dirty_check import check_bool, rickroll
|
2023-02-07 05:27:25 +00:00
|
|
|
|
from core.logger import Logger
|
2023-03-18 09:06:45 +00:00
|
|
|
|
|
|
|
|
|
openai.api_key = Config('openai_api_key')
|
2023-02-07 01:35:05 +00:00
|
|
|
|
|
2023-04-30 03:30:59 +00:00
|
|
|
|
s = module(
|
|
|
|
|
'summary',
|
|
|
|
|
developers=[
|
|
|
|
|
'Dianliang233',
|
|
|
|
|
'OasisAkari'],
|
|
|
|
|
desc='{summary.help.desc}',
|
|
|
|
|
available_for=[
|
|
|
|
|
'QQ',
|
|
|
|
|
'QQ|Group'])
|
2023-02-07 01:35:05 +00:00
|
|
|
|
|
|
|
|
|
|
2023-04-05 02:22:37 +00:00
|
|
|
|
@s.handle('{{summary.help}}')
|
2023-02-07 01:35:05 +00:00
|
|
|
|
async def _(msg: Bot.MessageSession):
|
2023-03-19 07:44:17 +00:00
|
|
|
|
f_msg = await msg.waitNextMessage(msg.locale.t('summary.message'), append_instruction=False)
|
2023-02-08 06:12:53 +00:00
|
|
|
|
try:
|
|
|
|
|
f = re.search(r'\[Ke:forward,id=(.*?)\]', f_msg.asDisplay()).group(1)
|
|
|
|
|
except AttributeError:
|
2023-03-19 07:47:47 +00:00
|
|
|
|
await msg.finish(msg.locale.t('summary.message.not_found'))
|
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-08 05:57:29 +00:00
|
|
|
|
texts = [f'\n{m["sender"]["nickname"]}:{m["content"]}' for m in msgs]
|
|
|
|
|
|
|
|
|
|
char_count = sum([len(i) for i in texts])
|
2023-03-19 07:44:17 +00:00
|
|
|
|
wait_msg = await msg.sendMessage(msg.locale.t('summary.message.waiting', count=char_count, time=round(char_count / 33.5, 1)))
|
2023-02-07 06:37:14 +00:00
|
|
|
|
|
2023-02-08 03:26:01 +00:00
|
|
|
|
nth = 0
|
2023-03-18 09:06:45 +00:00
|
|
|
|
prev = ''
|
2023-02-08 05:57:29 +00:00
|
|
|
|
while nth < len(texts):
|
2023-03-31 07:49:37 +00:00
|
|
|
|
prompt = f'请总结下列聊天内容。要求语言简练,但必须含有所有要点,以一段话的形式输出。请使用{msg.locale.locale}输出结果。除了聊天记录的摘要以外,不要输出其他任何内容。' \
|
|
|
|
|
f'''{f"""同时<ctx_start>与<|ctx_end|>之间记录了聊天内容的上下文,请你同时结合这段上下文和聊天记录来输出,并使用这段聊天记录的原语言。
|
2023-03-31 07:38:41 +00:00
|
|
|
|
|
|
|
|
|
<|ctx_start|>{prev}<|ctx_end|>""" if nth != 0 else ""}'''
|
2023-02-08 05:57:29 +00:00
|
|
|
|
len_prompt = len(prompt)
|
|
|
|
|
post_texts = ''
|
|
|
|
|
for t in texts[nth:]:
|
2023-02-08 06:34:26 +00:00
|
|
|
|
if len(post_texts) + len_prompt < 1970:
|
2023-02-08 05:57:29 +00:00
|
|
|
|
post_texts += texts[nth]
|
|
|
|
|
nth += 1
|
|
|
|
|
else:
|
|
|
|
|
break
|
2023-03-18 09:06:45 +00:00
|
|
|
|
completion = openai.ChatCompletion.create(
|
2023-04-30 03:30:59 +00:00
|
|
|
|
model='gpt-3.5-turbo',
|
|
|
|
|
messages=[
|
|
|
|
|
{'role': 'system', "content": "You are a helpful assistants who summarizes chat logs."},
|
|
|
|
|
{'role': 'user', "content": f'''{prompt}
|
2023-03-18 09:06:45 +00:00
|
|
|
|
|
|
|
|
|
{post_texts}'''},
|
2023-04-30 03:30:59 +00:00
|
|
|
|
]
|
|
|
|
|
)
|
2023-03-18 09:10:33 +00:00
|
|
|
|
output = completion['choices'][0]['message']['content']
|
2023-02-07 06:38:31 +00:00
|
|
|
|
await wait_msg.delete()
|
2023-03-18 14:08:49 +00:00
|
|
|
|
if await check_bool(output):
|
2023-07-27 19:31:53 +00:00
|
|
|
|
rickroll(msg)
|
2023-02-08 03:26:01 +00:00
|
|
|
|
await msg.finish(output, disable_secret_check=True)
|