File size: 4,093 Bytes
0523803
 
 
 
341b821
0523803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d5fff1
0523803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
import os


def init(cfg):
    print(cfg['setting_cache_path'].value)
    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}')
        # ========== 上传缓存 ==========
        if os.environ.get("HF_TOKEN"):
            from huggingface_hub import login, CommitScheduler
            login(token=os.environ.get("HF_TOKEN"), write_permission=True)
            CommitScheduler(repo_id='Limour/llama-python-streamingllm-cache', repo_type='dataset', folder_path='cache')