• 书生·浦语大模型实战营(第二期):书生·浦语大模型趣味Demo


    部署InternLM2-Chat-1.8B模型进行对话

    环境配置

    # 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
    
    • 1
    • 2
    • 3

    requirement:

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    下载InternLM2-Chat-1.8B模型

    按路径创建文件夹,并进入到对应文件目录中:

    mkdir -p /root/demo
    touch /root/demo/cli_demo.py
    touch /root/demo/download_mini.py
    cd /root/demo
    
    • 1
    • 2
    • 3
    • 4

    /root/demo/download_mini.py

    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')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    执行命令,下载模型参数文件:

    python /root/demo/download_mini.py
    
    • 1

    运行cli_demo

    /root/demo/cli_demo.py

    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)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    输入命令,执行 Demo 程序:

    conda activate demo
    python /root/demo/cli_demo.py
    
    • 1
    • 2

    基础作业:使用 InternLM2-Chat-1.8B 模型生成 300 字的小故事(需截图)

    在这里插入图片描述

    部署实战营优秀作品 八戒-Chat-1.8B 模型

    八戒-Chat-1.8B:https://www.modelscope.cn/models/JimmyMa99/BaJie-Chat-mini/summary
    使用 git 命令来获得仓库内的 Demo 文件:

    cd /root/
    git clone https://gitee.com/InternLM/Tutorial -b camp2
    # git clone https://github.com/InternLM/Tutorial -b camp2
    cd /root/Tutorial
    
    • 1
    • 2
    • 3
    • 4

    下载运行Chat-八戒 Demo

    执行 bajie_download.py:

    python /root/Tutorial/helloworld/bajie_download.py
    
    • 1

    待程序下载完成后,输入运行命令:

    streamlit run /root/Tutorial/helloworld/bajie_chat.py --server.address 127.0.0.1 --server.port 6006
    
    • 1

    待程序运行的同时,对端口环境配置本地 PowerShell

    # 从本地使用 ssh 连接 studio 端口
    # 将下方端口号 38374 替换成自己的端口号
    ssh -CNg -L 6006:127.0.0.1:6006 root@ssh.intern-ai.org.cn -p 38374
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    使用 Lagent 运行 智能体Demo

    Lagent 是一个轻量级、开源的基于大语言模型的智能体(agent)框架,支持用户快速地将一个大语言模型转变为多种类型的智能体,并提供了一些典型工具为大语言模型赋能。它的整个框架图如下:
    在这里插入图片描述
    使用 git 命令下载 Lagent 相关的代码库:

    cd /root/demo
    git clone https://gitee.com/internlm/lagent.git
    # git clone https://github.com/internlm/lagent.git
    cd /root/demo/lagent
    git checkout 581d9fb8987a5d9b72bb9ebd37a95efd47d479ac
    pip install -e . # 源码安装
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    使用 Lagent 运行 InternLM2-Chat-7B 模型为内核的智能体

    打开 lagent 路径:

    cd /root/demo/lagent
    
    • 1

    打开 lagent 路径下 examples/internlm2_agent_web_demo_hf.py 文件,并修改对应位置 (71行左右) 代码:
    在这里插入图片描述
    更换为本地模型所在路径

    输入运行命令

    streamlit run /root/demo/lagent/examples/internlm2_agent_web_demo_hf.py --server.address 127.0.0.1 --server.port 6006
    
    • 1

    进阶作业:完成 Lagent 工具调用 数据分析 Demo 部署(需截图)

    在这里插入图片描述

    部署 灵笔 InternLM-XComposer2

    浦语·灵笔2 是基于 书生·浦语2 大语言模型研发的突破性的图文多模态大模型,具有非凡的图文写作和图像理解能力,在多种应用场景表现出色,总结起来其具有:

    • 自由指令输入的图文写作能力: 浦语·灵笔2 可以理解自由形式的图文指令输入,包括大纲、文章细节要求、参考图片等,为用户打造图文并貌的专属文章。生成的文章文采斐然,图文相得益彰,提供沉浸式的阅读体验。
    • 准确的图文问题解答能力:浦语·灵笔2 具有海量图文知识,可以准确的回复各种图文问答难题,在识别、感知、细节描述、视觉推理等能力上表现惊人。
    • 杰出的综合能力: 浦语·灵笔2-7B 基于 书生·浦语2-7B 模型,在13项多模态评测中大幅领先同量级多模态模型,在其中6项评测中超过 GPT-4V 和 Gemini Pro。

    补充环境包:

    pip install timm==0.4.12 sentencepiece==0.1.99 markdown2==2.4.10 xlsxwriter==3.1.2 gradio==4.13.0 modelscope==1.9.5
    
    • 1

    下载 InternLM-XComposer 仓库 相关的代码资源:

    cd /root/demo
    git clone https://gitee.com/internlm/InternLM-XComposer.git
    # git clone https://github.com/internlm/InternLM-XComposer.git
    cd /root/demo/InternLM-XComposer
    git checkout f31220eddca2cf6246ee2ddf8e375a40457ff626
    
    • 1
    • 2
    • 3
    • 4
    • 5

    进阶作业:图文写作

    继续输入指令,用于启动 InternLM-XComposer:

    cd /root/demo/InternLM-XComposer
    python /root/demo/InternLM-XComposer/examples/gradio_demo_composition.py  \
    --code_path /root/models/internlm-xcomposer2-7b \
    --private \
    --num_gpus 1 \
    --port 6006
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    进阶作业:图文理解

    在这里插入图片描述

    进阶作业:熟悉 huggingface 下载功能,使用 huggingface_hub python 包,下载 InternLM2-Chat-7B 的 config.json 文件到本地(需截图下载过程)

    import os
    from huggingface_hub import hf_hub_download  # Load model directly
    os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
    hf_hub_download(repo_id="internlm/internlm2-chat-7b", filename="config.json")
    
    • 1
    • 2
    • 3
    • 4

    下载完后的文件会在root用户下的.cache 文件夹下
    在这里插入图片描述

  • 相关阅读:
    Spring MVC拦截器
    JWT登录认证(3拦截器)
    【深圳1024开发者城市聚会定向征文】
    基于springboot零食商城管理系统
    【Ant Design Pro】使用ant design pro做为你的开发模板(完结篇)上线部署项目
    HTML+CSS+Jquery实现北大官网所有效果
    SpringCloudAlibaba-window安装Nocas
    SCSS的嵌套规则可以减少重复代码,那么如何在嵌套规则中使用父选择器?
    [剑指 Offer]数组中出现次数超过一半的数字
    第二周:计算机网络概述(下)
  • 原文地址:https://blog.csdn.net/weixin_44288092/article/details/137341640