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

59 lines
2.4 KiB
Python
Raw Normal View History

2023-02-07 01:35:05 +00:00
import re
2023-03-18 09:06:45 +00:00
import openai
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-03-18 14:08:49 +00:00
from core.dirty_check import check_bool
2023-02-07 05:27:25 +00:00
from core.logger import Logger
2023-03-18 09:06:45 +00:00
from config import Config
openai.api_key = Config('openai_api_key')
2023-02-07 01:35:05 +00:00
2023-03-19 07:45:21 +00:00
s = module('summary', developers=['Dianliang233', 'OasisAkari'], desc='{summary.help}', available_for=['QQ', 'QQ|Group'])
2023-02-07 01:35:05 +00:00
2023-03-19 07:44:17 +00:00
@s.handle('{{summary.help.summary}}')
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']
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 = ''
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 ""}'''
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:
post_texts += texts[nth]
nth += 1
else:
break
2023-03-18 09:06:45 +00:00
completion = openai.ChatCompletion.create(
model='gpt-3.5-turbo',
messages=[
2023-03-31 07:49:37 +00:00
{'role': 'system', "content": "You are a helpful assistants who summarizes chat logs."},
2023-03-18 09:06:45 +00:00
{'role': 'user', "content": f'''{prompt}
{post_texts}'''},
]
)
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):
await msg.finish('https://wdf.ink/6OUp')
2023-02-08 03:26:01 +00:00
await msg.finish(output, disable_secret_check=True)