| 101 | |
| 102 | |
| 103 | def main(): |
| 104 | past_key_values, history = None, [system_item] |
| 105 | role = "user" |
| 106 | global stop_stream |
| 107 | print("欢迎使用 ChatGLM3-6B 模型,输入内容即可进行对话,clear 清空对话历史,stop 终止程序") |
| 108 | while True: |
| 109 | query = input("\n用户:") if role == "user" else input("\n结果:") |
| 110 | if query.strip() == "stop": |
| 111 | break |
| 112 | if query.strip() == "clear": |
| 113 | past_key_values, history = None, [system_item] |
| 114 | role = "user" |
| 115 | os.system(clear_command) |
| 116 | print("欢迎使用 ChatGLM3-6B 模型,输入内容即可进行对话,clear 清空对话历史,stop 终止程序") |
| 117 | continue |
| 118 | print("\nChatGLM:", end="") |
| 119 | response, history = model.chat(tokenizer, query, history=history, role=role) |
| 120 | print(response, end="", flush=True) |
| 121 | print("") |
| 122 | if isinstance(response, dict): |
| 123 | role = "observation" |
| 124 | else: |
| 125 | role = "user" |
| 126 | |
| 127 | |
| 128 | if __name__ == "__main__": |