书生·浦语大模型训练营

书生·浦语大模型训练营

2 InternLM-Chat-7B 智能对话 Demo

本章就是通过 transformers 载入本地模型进行推理。

  • 原始的 demo 会导致空输入也会进行交互,浪费计算资源。

这里对输入做了简单的处理,当输入为空时,不进行交互。
(平台有点不太会用,一会连接就断开了,后续更新图片)

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


model_name_or_path = "/root/model/Shanghai_AI_Laboratory/internlm-chat-7b"

tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_name_or_path, trust_remote_code=True, torch_dtype=torch.bfloat16, device_map='auto')
model = model.eval()

system_prompt = """You are an AI assistant whose name is InternLM (书生·浦语).
- InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless.
- InternLM (书生·浦语) can understand and communicate fluently in the language chosen by the user such as English and 中文.
"""

messages = [(system_prompt, '')]

print("=============Welcome to InternLM chatbot, type 'exit' to exit.=============")

while True:
    input_text = input("User  >>> ")
    input_text = input_text.replace(' ', '')
    if len(input_text) == 0: # 对输入做预处理
    	continue
    elif input_text == "exit":
        break
    response, history = model.chat(tokenizer, input_text, history=messages)
    messages.append((input_text, response))
    print(f"robot >>> {response}")

通过 hugging_face 镜像下载模型
书生·浦语大模型训练营_第1张图片

你可能感兴趣的:(语言模型,人工智能,自然语言处理,transformer)