2023-03-13 12:06:14 +00:00
|
|
|
|
import os
|
2023-03-14 12:11:39 +00:00
|
|
|
|
import platform
|
2023-03-24 10:34:09 +00:00
|
|
|
|
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()
|
|
|
|
|
|
2023-03-14 12:11:39 +00:00
|
|
|
|
os_name = platform.system()
|
2023-03-19 06:33:05 +00:00
|
|
|
|
clear_command = 'cls' if os_name == 'Windows' else 'clear'
|
2023-03-24 10:34:09 +00:00
|
|
|
|
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
|
|
|
|
|
2023-03-24 10:34:09 +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 = []
|
2023-03-24 10:34:09 +00:00
|
|
|
|
global stop_stream
|
2023-03-19 06:33:05 +00:00
|
|
|
|
print("欢迎使用 ChatGLM-6B 模型,输入内容即可进行对话,clear 清空对话历史,stop 终止程序")
|
|
|
|
|
while True:
|
|
|
|
|
query = input("\n用户:")
|
2023-04-01 05:18:28 +00:00
|
|
|
|
if query.strip() == "stop":
|
2023-03-19 06:33:05 +00:00
|
|
|
|
break
|
2023-04-01 05:18:28 +00:00
|
|
|
|
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):
|
2023-03-24 10:34:09 +00:00
|
|
|
|
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()
|