-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathload_cache.py
More file actions
65 lines (60 loc) · 3.68 KB
/
load_cache.py
File metadata and controls
65 lines (60 loc) · 3.68 KB
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
65
import os
def init(cfg):
if os.path.exists(cfg['setting_cache_path'].value):
# ========== 加载角色卡-缓存 ==========
tmp = cfg['model'].load_session(cfg['setting_cache_path'].value)
print(f"load cache from {cfg['setting_cache_path'].value} {tmp}")
tmp = cfg['chat_template']('system',
cfg['text_format'](cfg['role_char_d'].value,
char=cfg['role_char'].value,
user=cfg['role_usr'].value))
cfg['setting_n_keep'].value = len(tmp)
tmp = cfg['chat_template'](cfg['role_char'].value,
cfg['text_format'](cfg['role_chat_style'].value,
char=cfg['role_char'].value,
user=cfg['role_usr'].value))
cfg['setting_n_keep'].value += len(tmp)
# ========== 加载角色卡-第一条消息 ==========
cfg['chatbot'] = []
for one in cfg["role_char_first"]:
one['name'] = cfg['text_format'](one['name'],
char=cfg['role_char'].value,
user=cfg['role_usr'].value)
one['value'] = cfg['text_format'](one['value'],
char=cfg['role_char'].value,
user=cfg['role_usr'].value)
if one['name'] == cfg['role_char'].value:
cfg['chatbot'].append((None, cfg['chat_display_format'](one['value'])))
print(one)
else:
# ========== 加载角色卡-角色描述 ==========
tmp = cfg['chat_template']('system',
cfg['text_format'](cfg['role_char_d'].value,
char=cfg['role_char'].value,
user=cfg['role_usr'].value))
cfg['setting_n_keep'].value = cfg['model'].eval_t(tmp) # 此内容永久存在
# ========== 加载角色卡-回复示例 ==========
tmp = cfg['chat_template'](cfg['role_char'].value,
cfg['text_format'](cfg['role_chat_style'].value,
char=cfg['role_char'].value,
user=cfg['role_usr'].value))
cfg['setting_n_keep'].value = cfg['model'].eval_t(tmp) # 此内容永久存在
# ========== 加载角色卡-第一条消息 ==========
cfg['chatbot'] = []
for one in cfg["role_char_first"]:
one['name'] = cfg['text_format'](one['name'],
char=cfg['role_char'].value,
user=cfg['role_usr'].value)
one['value'] = cfg['text_format'](one['value'],
char=cfg['role_char'].value,
user=cfg['role_usr'].value)
if one['name'] == cfg['role_char'].value:
cfg['chatbot'].append((None, cfg['chat_display_format'](one['value'])))
print(one)
tmp = cfg['chat_template'](one['name'], one['value'])
cfg['model'].eval_t(tmp) # 此内容随上下文增加将被丢弃
# ========== 保存角色卡-缓存 ==========
with open(cfg['setting_cache_path'].value, 'wb') as f:
pass
tmp = cfg['model'].save_session(cfg['setting_cache_path'].value)
print(f'save cache {tmp}')