ChatGLM-6B/cli_demo.py

59 lines
1.9 KiB
Python
Raw Normal View History

2023-03-13 12:06:14 +00:00
import os
import platform
import signal
2023-03-13 12:06:14 +00:00
from transformers import AutoTokenizer, AutoModel
2023-05-05 14:04:01 +00:00
import readline
2023-03-13 12:06:14 +00:00
2023-03-28 11:52:32 +00:00
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda()
2023-03-13 12:06:14 +00:00
model = model.eval()
os_name = platform.system()
2023-03-19 06:33:05 +00:00
clear_command = 'cls' if os_name == 'Windows' else 'clear'
stop_stream = False
2023-03-19 06:33:05 +00:00
2023-03-28 11:52:32 +00:00
2023-03-19 06:33:05 +00:00
def build_prompt(history):
prompt = "欢迎使用 ChatGLM-6B 模型输入内容即可进行对话clear 清空对话历史stop 终止程序"
for query, response in history:
2023-03-19 06:56:34 +00:00
prompt += f"\n\n用户:{query}"
prompt += f"\n\nChatGLM-6B{response}"
2023-03-19 06:33:05 +00:00
return prompt
2023-03-28 11:52:32 +00:00
def signal_handler(signal, frame):
global stop_stream
stop_stream = True
2023-03-19 06:33:05 +00:00
2023-03-28 11:52:32 +00:00
2023-03-19 06:33:05 +00:00
def main():
history = []
global stop_stream
2023-03-19 06:33:05 +00:00
print("欢迎使用 ChatGLM-6B 模型输入内容即可进行对话clear 清空对话历史stop 终止程序")
while True:
query = input("\n用户:")
if query.strip() == "stop":
2023-03-19 06:33:05 +00:00
break
if query.strip() == "clear":
2023-03-19 06:33:05 +00:00
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)
2023-03-28 11:52:32 +00:00
signal.signal(signal.SIGINT, signal_handler)
2023-03-19 06:33:05 +00:00
os.system(clear_command)
print(build_prompt(history), flush=True)
if __name__ == "__main__":
main()