大模型实战营-初夏营-第六次课笔记

这是大模型实战营-初夏营第六次课的笔记

Lagent和AgentLego的关系

flowchart LR
    subgraph Lagent
        tool[调用工具]
        subgraph AgentLego
            tool_support[工具功能支持]
        end
        tool_output(工具输出)
        tool --> tool_support --> tool_output
    end

    input(输入) --> LLM[大语言模型]
    LLM --> IF{是否需要调用工具}
    IF -->|否| output(一般输出)
    IF -->|是| tool
    tool_output -->|处理| agent_output(智能体输出)

从上图可以看出Lagent涉及的功能包括工具调用的全部流程,而AgentLego仅涉及开发可调用的工具

环境配置

conda 环境配置

运行如下命令在Intern Studio上配置Lagent和AgentLego运行所需的conda环境

1
2
mkdir -p /root/agent # 存放agent所需文件
studio-conda -t agent -o pytorch-2.1.2 # 创建conda环境

老套路,装完activate后用这两句清理一下缓存

1
2
pip cache purge
conda clean -y --all

这次更猛,清了将近17GB的缓存

安装Lagent和AgentLego

运行如下命令安装Lagent和AgentLego

1
2
3
4
5
6
cd /root/agent
conda activate agent
git clone https://gitee.com/internlm/lagent.git
cd lagent && git checkout 581d9fb && pip install -e . && cd ..
git clone https://gitee.com/internlm/agentlego.git
cd agentlego && git checkout 7769e0d && pip install -e . && cd ..

安装其他依赖

1
2
3
4
# 安装lmdeploy
pip install lmdeploy==0.3.0
# 克隆实战营代码,后续需要使用其中脚本
git clone -b camp2 https://gitee.com/internlm/Tutorial.git

动手实践

1. 直接使用 Lagent Web Demo

  • 部署 web demo

首先我们要启动一个大模型API推理服务:

1
2
3
4
lmdeploy serve api_server /root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-7b \
--server-name 127.0.0.1 \
--model-name internlm2-chat-7b \
--cache-max-entry-count 0.1 # 这是kv-cache占GPU内存的最大比例,默认为0.8

之后新建一个终端运行UI界面

1
2
cd /root/agent/lagent/examples
streamlit run internlm2_agent_web_demo.py --server.address 127.0.0.1 --server.port 7860

并修改大模型推理API地址为127.0.0.1:23333

就可以体验Lagent的 web demo了

  • 体验 web demo

我们选择ArxivSearch插件,赋予大模型在ArXiv上搜索论文的能力,测试一下:

正确地找到了ArXiv上的相关论文

2. 直接使用AgentLego

我们将使用AgentLego内置的目标检测工具检测一张图片上的目标,AgentLego 所实现的目标检测工具是基于 mmdet (MMDetection) 算法库中的 RTMDet-Large 模型。

  • 准备环境
  1. 下载demo文件
1
2
cd /root/agent
wget http://download.openmmlab.com/agentlego/road.jpg
  1. 下载依赖包
1
2
pip install openmim==0.3.9
mim install mmdet==3.3.0 # 用pip下了一个新的安装工具
  • 准备调用代码

agent目录下新建direct_use.py文件,并写入下面代码

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
import re

import cv2
from agentlego.apis import load_tool

# load object detection tool
tool = load_tool('ObjectDetection', device='cuda')

# apply the tool
visualization = tool('/root/agent/road.jpg')
print(visualization)

# visualize
image = cv2.imread('/root/agent/road.jpg')

preds = visualization.split('\n')
pattern = r'(\w+) \((\d+), (\d+), (\d+), (\d+)\), score (\d+)'

for pred in preds:
# match the pattern to get the names and boxes of prediction
name, x1, y1, x2, y2, score = re.match(pattern, pred).groups()
x1, y1, x2, y2, score = int(x1), int(y1), int(x2), int(y2), int(score)
# draw the box and score on the image
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 1)
cv2.putText(image, f'{name} {score}', (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 1)

cv2.imwrite('/root/agent/road_detection_direct.jpg', image)
  • 直接调用目标检测工具

运行python direct_use.py

得到目标检测输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
truck (345, 428, 528, 599), score 83
car (771, 510, 837, 565), score 81
car (604, 518, 677, 569), score 75
person (866, 503, 905, 595), score 74
person (287, 513, 320, 596), score 74
person (964, 501, 999, 604), score 72
person (1009, 503, 1047, 602), score 69
person (259, 510, 279, 575), score 65
car (1074, 524, 1275, 691), score 64
person (993, 508, 1016, 597), score 62
truck (689, 483, 764, 561), score 62
bicycle (873, 551, 903, 602), score 60
person (680, 523, 699, 567), score 55
bicycle (968, 551, 996, 609), score 53
bus (826, 482, 930, 560), score 52
bicycle (1011, 551, 1043, 617), score 51

检测图像如下: