()
| 7 | |
| 8 | |
| 9 | def main(): |
| 10 | # 加载模型相关 |
| 11 | tokenizer = AutoTokenizer.from_pretrained(PATH,trust_remote_code=True) |
| 12 | model = AutoModelForCausalLM.from_pretrained(PATH, trust_remote_code=True, device_map="auto", |
| 13 | torch_dtype=torch.float16) |
| 14 | generate_config = GenerationConfig.from_pretrained(PATH) |
| 15 | model.eval() |
| 16 | |
| 17 | # chat(bot)模型多轮演示 |
| 18 | print("*" * 10 + "多轮输入演示" + "*" * 10) |
| 19 | question = "你是谁?" |
| 20 | print("提问:", question) |
| 21 | answer, history = model.chat(tokenizer = tokenizer, question=question, history=[], generation_config=generate_config, |
| 22 | stream=False) |
| 23 | print("回答:", answer) |
| 24 | print("截至目前的聊天记录是:", history) |
| 25 | |
| 26 | question = "你是谁训练的" |
| 27 | print("提问:", question) |
| 28 | # 将history传入 |
| 29 | answer, history = model.chat(tokenizer, question=question, history=history, generation_config=generate_config, |
| 30 | stream=False) |
| 31 | print("回答是:", answer) |
| 32 | print("截至目前的聊天记录是:", history) |
| 33 | |
| 34 | # 也可以这么调用传入history |
| 35 | history = [ |
| 36 | {"role": "user", "content": "你是谁"}, |
| 37 | {"role": "bot", "content": "我是telechat"}, |
| 38 | ] |
| 39 | |
| 40 | question = "你是谁训练的" |
| 41 | print("提问:", question) |
| 42 | answer, history = model.chat(tokenizer, question=question, history=history, generation_config=generate_config, |
| 43 | stream=False) |
| 44 | print("回答是:", answer) |
| 45 | print("截至目前的聊天记录是:", history) |
| 46 | |
| 47 | # chat(bot)模型 流式返回演示 |
| 48 | print("*" * 10 + "流式输入演示" + "*" * 10) |
| 49 | question = "你是谁?" |
| 50 | print("提问:", question) |
| 51 | gen = model.chat(tokenizer, question=question, history=[], generation_config=generate_config, |
| 52 | stream=True) |
| 53 | for answer, history in gen: |
| 54 | print("回答是:", answer) |
| 55 | print("截至目前的聊天记录是:", history) |
| 56 | |
| 57 | |
| 58 | if __name__ == '__main__': |
no test coverage detected