generated from RockChinQ/HelloPlugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
64 lines (51 loc) · 2.17 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import copy
import os
from pkg.plugin.context import register, handler, BasePlugin, APIHost, EventContext
from pkg.plugin.events import *
from plugins.SillyTavernPlugin.pkg.processor import SillyTavernProcessor
# 注册插件
@register(name="SillyTavernPlugin", description="SillyTavernPlugin插件,用于将酒馆角色卡转写为QChatGPT人格预设的插件",
version="1.0",
author="the-lazy-me")
class SillyTavernPlugin(BasePlugin):
def __init__(self, host: APIHost):
super().__init__(host)
create_data_dir()
silly_tavern_process()
pass
@handler(PromptPreProcessing)
async def _(self, ctx: EventContext):
# print(ctx.event.default_prompt)
prompt = copy.deepcopy(ctx.event.default_prompt)
type = ctx.event.query.message_event.type
user_name = ''
if type == 'FriendMessage':
user_name = ctx.event.query.message_event.sender.nickname
elif type == 'GroupMessage':
user_name = ctx.event.query.message_event.sender.member_name
prompt = alter_prompt(prompt, user_name)
ctx.event.default_prompt = prompt
# 插件卸载时触发
def __del__(self):
pass
# 修改提示词中的{{user}}变量
def alter_prompt(prompt: list[llm_entities.Message], user_name: str):
for i in range(len(prompt)):
prompt[i].content = prompt[i].content.replace("{{user}}", user_name)
return prompt
# 创建数据文件夹
def create_data_dir():
base_path = "data/plugins/SillyTavernPlugin"
# 在base_path下创建文件夹characters_cards
os.makedirs(base_path, exist_ok=True)
os.makedirs(base_path + "/characters_cards", exist_ok=True)
# 在characters_cards下创建文件夹processed和unprocessed
os.makedirs(base_path + "/characters_cards/processed", exist_ok=True)
os.makedirs(base_path + "/characters_cards/unprocessed", exist_ok=True)
# 角色卡处理
def silly_tavern_process():
stp = SillyTavernProcessor('data/plugins/SillyTavernPlugin/characters_cards',
'data/scenario')
result = stp.process_png_files()
print("SillyTavernPlugin插件处理完成")
return result