openmmlab 书生·浦语大模型 笔记/作业2 初体验

轻松玩转书生·浦语大模型趣味 Demo_哔哩哔哩_bilibili

目录

1.InternLM2-Chat-1.8B

2.八戒-Chat-1.8B

3.InternLM2-Chat-7B  Lagent 智能体 Demo

4.浦语·灵笔2

NOTE:

使用 huggingface_hub 下载文件


本文包括一些笔记和简单的作业 

1.InternLM2-Chat-1.8B

先安装modelscope把模型下载下来,这种安装都非常简单我直接把命令贴这里了,后面要运行程序顺便把pytorch cuda什么的都一起装上吧,直接pip install 就可以我就不往这里贴了,一般是没什么问题的除非你的机器配置不太正常

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安装好以后写个脚本把模型下载下来 download.py,不写脚本直接python -c下载也行就怕网不好shell断了

import os
from modelscope.hub.snapshot_download import snapshot_download

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

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

 这模型不大很快就下好了,写个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)

 加载模型的速度看硬盘了,模型本身不大不会用很长时间

写个300字的小故事,大概算了下还真差不多300字 ( 不过上面那个demo里面model.streamchat没把历史信息输入进去所以不能多轮对话了,记录历史信息应该用model.chat ) ,写英文故事也是可以的,一个英语token大约3-5个字符,总共输出算上标点和空格不到2000个字符,大约300多个英文token

 1.8B的模型这样已经可以了还要啥自行车

2.八戒-Chat-1.8B

魔搭社区

八戒-Chat-1.8B是用InternLM2-Chat-1.8B 模型微调的,具体就是,利用《西游记》剧本中所有关于猪八戒的台词和语句,比如下面这样的对八戒的描述和台词,把有关猪八戒的内容全部收集起来:

收集完后用Chat-GPT-3.5生成问题和回答,比如类似这样的:Q:"你是谁啊", A:"嘿嘿俺是猪八戒"

数据准备好后基于InternLM2-chat-1.8b进行全量微调得到模仿猪八戒语气的聊天语言模型。看情况数据够的话可能先预训练再SFT,不过模仿语气一般SFT就够了,最后再加个DPO应该没啥问题

效果像这样,感觉还不错:

3.InternLM2-Chat-7B  Lagent 智能体 Demo

Lagent 是一个轻量开源的LLM agent框架,所谓框架嘛就是把一堆东西整合在一起变成工具包,像Lagent就是集成了多个action executor(比如:python executor, search, calculator),planning & action策略(比如:plan-act iteration, plan then act等等),同时支持人工介入(human feedback, human instruction, observation等等)

 比如输入: "请解方程 2*X=1360 之中 X 的结果",

模型会生成一段代码,指定需要的action比如Interpreter,然后你调用预先定义的interpreter执行它生成的代码返回结果

具体实现过程展开:

首先LLM还是那个LLM,只是在prompt上加了一些别的东西,让它扮演工具人

1. 输入:请解方程 2*X=1360 之中 X 的结果

2.包装prompt:  

[{'role': 'system', 'content': '当开启工具以及代码时,根据需求选择合适的工具进行调用'},
{'role': 'system', 'content': '你现在已经能够在一个有状态的 Jupyter 笔记本环境中运行 Python 代码。
当你向 python 发送含有 Python 代码的消息时,它将在该环境中执行。这个工具适用于多种场景,
如数据分析或处理(包括数据操作、统计分析、图表绘制),复杂的计算问题(解决数学和物理难题),
编程示例(理解编程概念或特性),文本处理和分析(比如文本解析和自然语言处理),
机器学习和数据科学(用于展示模型训练和数据可视化),
以及文件操作和数据导入(处理CSV、JSON等格式的文件)。', 'name': 'interpreter'},
{'role': 'user', 'content': '请解方程 2*X=1360 之中 X 的结果'}]

3.将prompt输入llm,得到llm的输出: 

为了解方程 \(2X = 1360\),我们需要将方程两边同时除以 \(2\) 来分离 \(X\)。
<|action_start|><|interpreter|>
```python
from sympy import symbols, Eq, solve
X = symbols('X')
equation = Eq(2 * X, 1360)
solution = solve(equation, X)
solution
```<|action_end|>

4.解析LLM的输出:

name: interpreter,
language: 为了解方程 \(2X = 1360\),我们需要将方程两边同时除以 \(2\) 来分离 \(X\)。,
action: {'name': 'IPythonInterpreter', 
'parameters': {'command': "```python\nfrom sympy import symbols, Eq, solve\n\n# 定义变量\nX = symbols('X')\n\n# 创建方程\nequation = Eq(2 * X, 1360)\n\n# 解方程\nsolution = solve(equation, X)\nsolution\n```"}}

5.调用 IPythonInterpreter 执行

class IPythonInterpreter(BaseAction):
    @staticmethod
    def start_kernel():
        from jupyter_client import KernelManager
        # start the kernel and manager
        km = KernelManager()
        km.start_kernel()
        kc = km.client()
        return km, kc

    def _call(self,
              command: str,
              timeout: Optional[int] = None) -> Tuple[str, bool]:
        self.initialize()
        command = extract_code(command)
        ......
        self.kernel_client.execute(command)

最后 msg = self.kernel_client.get_iopub_msg(timeout=20) 取出结果

4.浦语·灵笔2

https://gitee.com/internlm/InternLM-XComposer.git

NOTE:

这个需要24G的显存,如果出现OOM,看examples/gradio_demo_composition.py 155行

改成:self.model = AutoModelForCausalLM.from_pretrained(code_path, device_map='cuda', trust_remote_code=True, torch_dtype=torch.float16).eval()

torch关闭梯度计算 with torch.no_grad():  

图文写作

 图片理解

使用 huggingface_hub 下载文件

python -c 'from huggingface_hub import hf_hub_download;hf_hub_download(repo_id="internlm/internlm2-7b", filename="config.json", cache_dir="./")'

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值