run_cli.py 2.38 KB
Newer Older
陈正乐 committed
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 73 74 75 76 77
import os
import platform
import signal
import sys
sys.path.append("../..") 

from llm.loader import ModelLoader

from common import consts
## --------- config -------------
from argparse import Namespace
cfg = Namespace()

cfg.max_source_length = 64 
cfg.max_target_length = 128

#model
cfg.model_name_or_path = consts.MODEL_PATH_ChatGLM  #远程'THUDM/chatglm-6b' 
cfg.quantization_bit = None #仅仅预测时可以选 4 or 8 

cfg.ckpt_path = '../../../model/ckpt/chatglm-6b-lora-spdsvb-INSv4-1e-03-50'
## --------- end config -------------

loader = ModelLoader(cfg.model_name_or_path,  cfg.quantization_bit)
loader.load_lora(cfg.ckpt_path,)
model,tokenizer = loader.models()
model = model.eval()

# response=model.chat(tokenizer,"客户在对账时,提示:请您先修改PIN码后才能使用USBKey进行安全操作")
# print(response)


os_name = platform.system()
clear_command = 'cls' if os_name == 'Windows' else 'clear'
stop_stream = False

def signal_handler(signal, frame):
    global stop_stream
    stop_stream = True

def build_prompt(history):
    prompt = "欢迎使用 ChatGLM-6B 模型,输入内容即可进行对话,clear 清空对话历史,stop 终止程序"
    for query, response in history:
        prompt += f"\n\n用户:{query}"
        prompt += f"\n\nChatGLM-6B:{response}"
    return prompt

def main():
    history = []
    global stop_stream
    print("欢迎使用 ChatGLM-6B 模型,输入内容即可进行对话,clear 清空对话历史,stop 终止程序")
    while True:
        query = input("\n用户:")
        if query.strip() == "stop":
            break
        if query.strip() == "clear":
            history = []
            os.system(clear_command)
            print("欢迎使用 ChatGLM-6B 模型,输入内容即可进行对话,clear 清空对话历史,stop 终止程序")
            continue
        count = 0
        for response, history in model.stream_chat(tokenizer, query, history=history):
            if stop_stream:
                stop_stream = False
                break
            else:
                count += 1
                if count % 8 == 0:
                    os.system(clear_command)
                    print(build_prompt(history), flush=True)
                    signal.signal(signal.SIGINT, signal_handler)
        os.system(clear_command)
        print(build_prompt(history), flush=True)


if __name__ == "__main__":
    main()