轻松玩转书生·浦语大模型趣味 Demo

本文详细介绍了如何在conda环境中安装和配置Python及其相关库,如PyTorch、transformers等,用于加载和运行HuggingFace的预训练模型,以及使用Lagent进行对话系统开发。还涉及了SSH密钥配置以实现远程访问。
摘要由CSDN通过智能技术生成

conda环境(可以在自己的环境中配置,记下来方便自己本地部署)

建立环境

# conda create -n demo python==3.10 -y
# conda activate demo
# conda install pytorch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 pytorch-cuda=11.7 -c pytorch -c nvidia

激活环境

conda activate demo

配置环境

pip install huggingface-hub==0.17.3
pip install transformers==4.34 
pip install psutil==5.9.8
pip install accelerate==0.24.1
pip install streamlit==1.32.2 
pip install matplotlib==3.8.3 
pip install modelscope==1.9.5
pip install sentencepiece==0.1.99

模型下载

这利用的是 modelscope.,感觉和huggingfac下载大同小异

import os
from modelscope.hub.snapshot_download import snapshot_download

# 创建保存模型目录
os.system("mkdir /root/models")

# save_dir是模型保存到本地的目录
save_dir="/root/models"

snapshot_download("Shanghai_AI_Laboratory/internlm2-chat-1_8b", 
                  cache_dir=save_dir, 
                  revision='v1.1.0')

运行模型

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


model_name_or_path = "/root/models/Shanghai_AI_Laboratory/internlm2-chat-1_8b"

tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, trust_remote_code=True, device_map='cuda:0')
model = AutoModelForCausalLM.from_pretrained(model_name_or_path, trust_remote_code=True, torch_dtype=torch.bfloat16, device_map='cuda:0')
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("\nUser  >>> ")
    input_text = input_text.replace(' ', '')
    if input_text == "exit":
        break

    length = 0
    for response, _ in model.stream_chat(tokenizer, input_text, messages):
        if response is not None:
            print(response[length:], flush=True, end="")
            length = len(response)

主要包括

模型加载:

model_name_or_path:指定预训练模型的路径或名称。
tokenizer:使用AutoTokenizer.from_pretrained方法加载internlm2-chat-1_8b的标记器。
model:使用AutoModelForCausalLM.from_pretrained方法加载internlm2-chat-1_8b。

system_prompt:这是对话系统的初始提示或介绍。
messages:这是一个列表,包含用户输入和机器人响应的元组。初始时,它只包含系统提示。

对话循环:

进入一个while循环,直到用户输入 “exit”。
在循环中,获取用户输入。
使用模型的stream_chat方法处理输入。该方法可能根据用户输入和对话历史(messages)生成响应。

作业

300字故事

在这里插入图片描述
已经远超300字,模型本身可能训练了太多书生浦语的知识,固定化输出指定文字,不具备变通性。

huggingfac下载

注意配置镜像
export HF_ENDPOINT=https://hf-mirror.com

import os 
from huggingface_hub import hf_hub_download  # Load model directly 

hf_hub_download(repo_id="internlm/internlm2-7b", filename="config.json")

在这里插入图片描述

Lagent

大模型使用Lagent和人用工具差不多
这里教程少了端口配置
ssh-keygen -t rsa
cat ~.ssh\id_rsa.pub
添加公钥
在这里插入图片描述
在这里插入图片描述
能正常登录
在这里插入图片描述
这里没出现密码
在这里插入图片描述
超显存了
在这里插入图片描述
做不了了。。。。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值