【 书生·浦语大模型实战营】作业(六):Lagent & AgentLego 智能体应用搭建

【 书生·浦语大模型实战营】作业(六):Lagent & AgentLego 智能体应用搭建

在这里插入图片描述

🎉AI学习星球推荐: GoAI的学习社区 知识星球是一个致力于提供《机器学习 | 深度学习 | CV | NLP | 大模型 | 多模态 | AIGC 》各个最新AI方向综述、论文等成体系的学习资料,配有全面而有深度的专栏内容,包括不限于 前沿论文解读、资料共享、行业最新动态以、实践教程、求职相关(简历撰写技巧、面经资料与心得)多方面综合学习平台,强烈推荐AI小白及AI爱好者学习,性价比非常高!加入星球➡️点击链接

【 书生·浦语大模型实战营】作业(六):Lagent & AgentLego 智能体应用搭建

本篇笔记内容主要为 【书生·浦语大模型实战营】作业(六):Lagent & AgentLego 智能体应用搭建智能体的作业 ,主要对智能体应用理论部分及具体搭建流程详细介绍,并分别进行实战应用及可视化展示,欢迎大家交流学习!

本次学习资料

1.【视频】:Lagent & AgentLego 智能体应用搭建

2.【文档】:https://github.com/InternLM/Tutorial/blob/camp2/agent/lagent.md#1-lagent-web-demo

3.【作业】:https://github.com/InternLM/Tutorial

本次作业内容:

完成 Lagent Web Demo 使用,并在作业中上传截图。
完成 AgentLego 直接使用部分,并在作业中上传截图。

1.1 Lagent:轻量级智能体框架

安装环境

# 新建环境
mkdir -p /root/agent
studio-conda -t agent -o pytorch-2.1.2

#下载github有关仓库
cd /root/agent
git clone https://gitee.com/internlm/agentlego.git
git clone https://gitee.com/internlm/lagent.git
git clone -b camp2 https://gitee.com/internlm/Tutorial.git

# 激活环境并安装对应工具包
conda activate agent
cd lagent && git checkout 581d9fb && pip install -e . && cd ..
cd agentlego && git checkout 7769e0d && pip install -e . && cd ..
pip install lmdeploy==0.3.0

1.2 Lagent:轻量级智能体框架

Lagent Web Demo

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

在这里插入图片描述

2.启动并使用 Lagent Web Demo

新建的 terminal 中执行下述命令,启动 Lagent Web Demo

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

在这里插入图片描述

在等待 LMDeploy 的 api_server 与 Lagent Web Demo 完全启动后,在本地进行端口映射,将 LMDeploy api_server 的23333端口以及 Lagent Web Demo 的7860端口映射到本地。执行:

ssh -CNg -L 7860:127.0.0.1:7860 -L 23333:127.0.0.1:23333 root@ssh.intern-ai.org.cn -p 你的 ssh 41562

效果:

在这里插入图片描述

在这里插入图片描述

1.3 用 Lagent 自定义工具

使用 Lagent 自定义工具主要分为以下几步:

  • 继承 BaseAction 类 实现简单工具的 run 方法;
  • 或者实现工具包内每个子工具的功能 简单工具的 run 方法可选被
    tool_api 装饰;
  • 工具包内每个子工具的功能都需要被 tool_api 装饰

目标:实现一个调用和风天气 API 的工具以完成实时天气查询的功能

在这里插入图片描述

重启服务:

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

export WEATHER_API_KEY=获取的API KEY
conda activate agent
cd /root/agent/Tutorial/agent
streamlit run internlm2_weather_web_demo.py --server.address 127.0.0.1 --server.port 7860
#本地进行端口映射:
ssh -CNg -L 7860:127.0.0.1:7860 -L 23333:127.0.0.1:23333 root@ssh.intern-ai.org.cn -p 41562

效果:

在这里插入图片描述

2.1 AgentLego:组装智能体“乐高”

直接使用 AgentLego

cd /root/agent
wget http://download.openmmlab.com/agentlego/road.jpg
conda activate agent
pip install openmim==0.3.9
mim install mmdet==3.3.0

python /root/agent/direct_use.py

推理前:

Inference
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
在这里插入图片描述

推理后

在这里插入图片描述

2.2 智能体工具使用

使用 AgentLego WebUI

在本地的浏览器页面中打开http://localhost:7860以使用 AgentLego WebUI。

在这里插入图片描述

在这里插入图片描述

效果

在这里插入图片描述

2.3 自定义工具

基于 AgentLego 构建自己的自定义工具(文档教程),自定义工具主要分为以下几步

继承 BaseTool 类
修改 default_desc 属性(工具功能描述)
如有需要,重载 setup 方法(重型模块延迟加载)
重载 apply 方法(工具功能实现)

通过touch /root/agent/agentlego/agentlego/tools/magicmaker_image_generation.py(大小写敏感)的方法新建工具文件。该文件的内容如下:

import json
import requests

import numpy as np

from agentlego.types import Annotated, ImageIO, Info
from agentlego.utils import require
from .base import BaseTool


class MagicMakerImageGeneration(BaseTool):

    default_desc = ('This tool can call the api of magicmaker to '
                    'generate an image according to the given keywords.')

    styles_option = [
        'dongman',  # 动漫
        'guofeng',  # 国风
        'xieshi',   # 写实
        'youhua',   # 油画
        'manghe',   # 盲盒
    ]
    aspect_ratio_options = [
        '16:9', '4:3', '3:2', '1:1',
        '2:3', '3:4', '9:16'
    ]

    @require('opencv-python')
    def __init__(self,
                 style='guofeng',
                 aspect_ratio='4:3'):
        super().__init__()
        if style in self.styles_option:
            self.style = style
        else:
            raise ValueError(f'The style must be one of {self.styles_option}')
        
        if aspect_ratio in self.aspect_ratio_options:
            self.aspect_ratio = aspect_ratio
        else:
            raise ValueError(f'The aspect ratio must be one of {aspect_ratio}')

    def apply(self,
              keywords: Annotated[str,
                                  Info('A series of Chinese keywords separated by comma.')]
        ) -> ImageIO:
        import cv2
        response = requests.post(
            url='https://magicmaker.openxlab.org.cn/gw/edit-anything/api/v1/bff/sd/generate',
            data=json.dumps({
                "official": True,
                "prompt": keywords,
                "style": self.style,
                "poseT": False,
                "aspectRatio": self.aspect_ratio
            }),
            headers={'content-type': 'application/json'}
        )
        image_url = response.json()['data']['imgUrl']
        image_response = requests.get(image_url)
        image = cv2.cvtColor(cv2.imdecode(np.frombuffer(image_response.content, np.uint8), cv2.IMREAD_COLOR),cv2.COLOR_BGR2RGB)
        return ImageIO(image)

注册新工具,在重复2.2步骤即可运行结果。

在这里插入图片描述

  • 32
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GoAI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值