Lagent & AgentLego 智能体应用搭建
基础作业(结营必做)
完成 Lagent Web Demo 使用,并在作业中上传截图。文档可见 Lagent Web Demo
建立文件夹
mkdir -p /root/agent
安装 Lagent 和 AgentLego
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 ..
安装其他依赖
conda activate agent
pip install lmdeploy==0.3.0
这里我使用的是第六节课的环境,所以就不用再装lmdeploy了
安装Tutorial
cd /root/agent
git clone -b camp2 https://gitee.com/internlm/Tutorial.git
使用LMDeploy部署
conda activate agent
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
新建一个Terminal
conda activate agent
cd /root/agent/lagent/examples
streamlit run internlm2_agent_web_demo.py --server.address 127.0.0.1 --server.port 7860
完成 AgentLego 直接使用部分,并在作业中上传截图。文档可见 直接使用 AgentLego。
下载demo文件
cd /root/agent
wget http://download.openmmlab.com/agentlego/road.jpg
安装mim,并使用mim安装mmdet
conda activate agent
pip install openmim==0.3.9
mim install mmdet==3.3.0
创建代码程序
touch /root/agent/direct_use.py
import re
import cv2
from agentlego.apis import load_tool
# load tool
tool = load_tool('ObjectDetection', device='cuda')
# apply 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:
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)
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程序
python /root/agent/direct_use.py