书生大模型 - 进阶岛 - 第二关 -Lagent 自定义 Agent 智能体

Agent 很火,非常普及。目前像Coze、百度Appbuilder、MetaGPT、天工skyagent、讯飞等都提出了自己的agent智能体,雨后春笋 都在布局和发声。

1. Agent是什么: 具体agent内涵和定义  这个解释我比较认可: 智能体/智能代理(Artificial Intelligence Agent):被定义为运行在特定环境下具有智能行为的计算机系统,具有感知、推理、决策和行动能力并可 以通过学习和适应来改进其性能,按照已有的知识或者通过自主学习,并与其他智能体进行沟通协作,可自主地完成设定的目标。

2. Agent的能力,主要包含了反思学习适应能力 主动预测分析能力 复杂环境感知能力 复杂目标规划能力 独立自主执行能力,智能体主要通过与环境交互体现其能力;

基于大语言模型的智能体,展现了实现类人智能的重要潜力,采用LLM作为核心控制器来构建具有类人决策能力的自主 代理研究领域正在形成,同时还与世界模型、多模态大模型以及强化学习等多种能力耦合迭代

agent-workflow、世界模型、agent-tuning、高级RAG等技术是当前agent的主流技术方向。

智能体能力等级划分,主要考察五大维度能力,并从Level1到level5(100% 匹配完全技能化人类)

 3. Lagent架构介绍:

Lagent 是一个轻量级开源智能体框架,旨在让用户可以高效地构建基于大语言模型的智能体。同时它也提供了一些典型工具以增强大语言模型的能力。Lagent 目前已经支持了包括 AutoGPT、ReAct 等在内的多个经典智能体范式,也支持了如下工具:

  • Arxiv 搜索
  • Bing 地图
  • Google 学术搜索
  • Google 搜索
  • 交互式 IPython 解释器
  • IPython 解释器
  • PPT
  • Python 解释器

4. 使用lagent 构建智能体,这一块主要是安装智能体包和环境依赖, 没有太多需要具体说明的,

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

# 创建空间,安装lagent
mkdir -p /root/agent_camp3
cd /root/agent_camp3
git clone https://github.com/InternLM/lagent.git
cd lagent && git checkout 81e7ace && pip install -e . && cd ..

5.  Lagent 的 Web Demo。

cd /root/agent_camp3/lagent
conda activate agent_camp3
streamlit run examples/internlm2_agent_web_demo.py

这个ARXIV search工具,还是挺好玩,让他搜索了线性探针问题,Linear Probing,搜索了几篇arxiv文章,并进行了总结

自行构建action,这一块和metaGPT逻辑一样,构建action,具体逻辑如下,构建了MagicMaker aciton,适时供agent调用

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}
希望生成黑悟空风格,效果图如下,和黑悟空很相似对吧,天下猴子还是一样的,嘿嘿。  
                                                 
  • 24
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值