【书生大模型实战营】Lagent 自定义你的 Agent 智能体——调用通义万向API

【书生大模型实战营】Lagent 自定义你的 Agent 智能体

任务

使用 Lagent 自定义一个智能体,并使用 Lagent Web Demo 成功部署与调用。

环境创建

cuda 12.2,使用如下命令创建环境:

# 创建环境
conda create -n agent_camp3 python=3.10 -y
# 激活环境
conda activate agent_camp3
# 安装 torch
conda install pytorch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 pytorch-cuda=12.1 -c pytorch -c nvidia -y
# 安装其他依赖包
pip install termcolor==2.4.0
pip install lmdeploy==0.5.2

创建agent_camp3文件夹,将lagent仓库克隆在这里,即:

git clone https://github.com/InternLM/lagent.git

然后以源码的方式安装lagent:

cd lagent && git checkout 81e7ace && pip install -e . && cd ..

Lagent

推荐在VScode命令行运行,它会自动进行端口转发。使用如下命令部署InternLM2.5-7B-Chat,并启动一个 API Server:

lmdeploy serve api_server /share/new_models/Shanghai_AI_Laboratory/internlm2_5-7b-chat --model-name internlm2_5-7b-chat

然后在另一个窗口中启动 Lagent 的 Web Demo:

streamlit run examples/internlm2_agent_web_demo.py

浏览器输入http://localhost:8501/有如下界面:
在这里插入图片描述
并修改模型名称一栏为 internlm2_5-7b-chat,修改模型 ip一栏为127.0.0.1:23333(因为23333对应的是之前运行的internlm2_5-7b-chat这个模型),并选择插件为:ArxivSearch,具体如下:
在这里插入图片描述
然后输入指令:帮我搜索一下 MindSearch 论文,结果如下:
在这里插入图片描述

基于 Lagent 自定义智能体

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

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

下面以一个文生图的agent为例。首先在lagent/lagent/actions创建一个magicmaker.py文件,然后复制如下内容:

import json
import requests

from lagent.actions.base_action import BaseAction, tool_api
from lagent.actions.parser import BaseParser, JsonParser
from lagent.schema import ActionReturn, ActionStatusCode


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

    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}')
    
    @tool_api
    def generate_image(self, keywords: str) -> dict:
        """Run magicmaker and get the generated image according to the keywords.

        Args:
            keywords (:class:`str`): the keywords to generate image

        Returns:
            :class:`dict`: the generated image
                * image (str): path to the generated image
        """
        try:
            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'}
            )
        except Exception as exc:
            return ActionReturn(
                errmsg=f'MagicMaker exception: {exc}',
                state=ActionStatusCode.HTTP_ERROR)
        image_url = response.json()['data']['imgUrl']
        return {'image': image_url}

generate_image函数中可以看出它是通过调用API来实现的。

然后修改修改 lagent/examples/internlm2_agent_web_demo.py来适配我们的自定义工具。

首先导入创建的MagicMaker,然后将七加入到action_list变量,如下:

from lagent.actions.magicmaker import MagicMaker
action_list = [
            ArxivSearch(),
			MagicMaker(),
 ]

然后重新启动上面两个demo,在agent界面可以选择两个插件,功能也没有问题。首先输入请生成一只鸡在打篮球,生成的结果如下:

在这里插入图片描述
感觉这个agent对动作不敏感。

然后让它找半监督视频目标分割的论文STCN,也是可以的:
在这里插入图片描述
不过它应该只是根据摘要的标题来找的。

自定义Agent

使用通义万相作为自己的agent,详细使用见阿里云官网:

export DASHSCOPE_API_KEY="你的API Key"

然后创建在latent/actions文件夹下创建qwen_wanxiang.py,内容为:

# internlm2_5-7b-chat, 127.0.0.1:23333
import json

from lagent.actions.base_action import BaseAction, tool_api
from lagent.actions.parser import BaseParser, JsonParser
from lagent.schema import ActionReturn, ActionStatusCode
from dashscope import ImageSynthesis
import dashscope
from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests


class QWen_Wanxiang(BaseAction):

    def __init__(self):
        super().__init__()
    
    @tool_api
    def generate_image(self, keywords: str) -> dict:
        """Run magicmaker and get the generated image according to the keywords.

        Args:
            keywords (:class:`str`): the keywords to generate image

        Returns:
            :class:`dict`: the generated image
                * image (str): path to the generated image
        """
        rsp = dashscope.ImageSynthesis.call(model=dashscope.ImageSynthesis.Models.wanx_v1,
                              prompt=keywords,
                              n=4,
                              size='1024*1024')
        img_url = ""
        if rsp.status_code == HTTPStatus.OK:
            for result in rsp.output.results:
                img_url = result.url
                break
        else:
            print('Failed, status_code: %s, code: %s, message: %s' %
                (rsp.status_code, rsp.code, rsp.message))
        return {'image': img_url}

然后更改internlm2_agent_web_demo.py中的action_list,最后执行命令streamlit run examples/internlm2_agent_web_demo.py,其他跟之前的步骤一样。

然后让模型生成狗在打篮球的图片,结果如下:
在这里插入图片描述
对动作的理解也不行。

  • 9
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lzl2040

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

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

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

打赏作者

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

抵扣说明:

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

余额充值