大模型实战营第二次课笔记

这是大模型实战营第二次课的笔记

动手实践部分,后面有时间我会补充相关代码和我加的注释

大模型及InternLM模型介绍

InternLM-Chat-7B 智能对话 Demo

  • 模型介绍

  • 动手实践

作业此处

Lagent 智能体工具调用 Demo

  • 框架介绍

  • 动手实践

作业此处

浦语 · 灵笔图文创作理解 Demo

  • 模型介绍

  • 动手实践

作业此处

通用环境配置

当作一个字典来查吧

  1. pip 换源
1
2
python -m pip install --upgrade pip # 升级pip
pip config set global.index-url https://mirrors.cernet.edu.cn/pypi/web/simple # 换CERNET源
  1. conda 换源

需要修改.condarc文件内容

不同系统下的 .condarc 目录如下:

  • Linux${HOME}/.condarc
  • macOS${HOME}/.condarc
  • WindowsC:\Users\<YourUserName>\.condarc

Windows 用户无法直接创建名为 .condarc 的文件,可先执行 conda config --set show_channel_urls yes 生成该文件之后再修改

之后执行

1
2
3
4
5
6
7
8
9
10
11
12
cat <<'EOF' > ~/.condarc
channels:
- defaults
show_channel_urls: true
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
EOF
  1. SSH连接远程服务器

我之前做项目需要用,所以已经配置好了,直接把公钥复制,再在PyCharm(似乎需要Ultimate版)中进行一番配置,就可以用PyCharm连接开发机了,具体步骤如下:

1)点击 Tools -> Deployment -> Configuration,进入如下界面;

2)点击 '+' -> SFTP 输入自定义server名称后,进入如下界面;

3)点击上图红圈处,进入如下界面。在配置完成后,点击Autodetect可自动识别根目录;

4)输入Host, Username,选择验证方式,有3种方式,分别是密码,密钥对和SSH配置授权Agent,连接本例中的开发机应选择第3种方式;

5)点击Test Connection 测试连接。

  1. 模型下载
  • HuggingFace Hub

安装

1
pip install -U huggingface_hub

下载方法1(命令行):

1
2
3
4
import os

# 下载模型
os.system('huggingface-cli download --resume-download internlm/internlm-chat-7b --local-dir your_path')

下载方法2(Python脚本):

1
2
from huggingface_hub import hf_hub_download  # Load model directly 
hf_hub_download(repo_id="internlm/internlm-7b", filename="config.json",local_dir="your path")
  • ModelScope

安装

1
2
pip install modelscope==1.9.5
pip install transformers==4.35.2

下载方法

1
2
from modelscope import snapshot_download, AutoModel, AutoTokenizer
model_dir = snapshot_download('Shanghai_AI_Laboratory/internlm-chat-7b', cache_dir='your path', revision='master')
  • OpenXLab

安装

1
pip install -U openxlab

下载方法

1
2
from openxlab.model import download
download(model_repo='OpenLMLab/InternLM-7b', model_name='InternLM-7b', output='your local path')