|
| 1 | + |
1 | 2 |
|
| 3 | +# Lagent 自定义你的 Agent 智能体 |
| 4 | + |
| 5 | +## Lagent 介绍 |
| 6 | + |
| 7 | +Lagent 是一个轻量级开源智能体框架,旨在让用户可以高效地构建基于大语言模型的智能体。同时它也提供了一些典型工具以增强大语言模型的能力。 |
| 8 | + |
| 9 | +Lagent 目前已经支持了包括 AutoGPT、ReAct 等在内的多个经典智能体范式,也支持了如下工具: |
| 10 | + |
| 11 | +- Arxiv 搜索 |
| 12 | +- Bing 地图 |
| 13 | +- Google 学术搜索 |
| 14 | +- Google 搜索 |
| 15 | +- 交互式 IPython 解释器 |
| 16 | +- IPython 解释器 |
| 17 | +- PPT |
| 18 | +- Python 解释器 |
| 19 | + |
| 20 | +其基本结构如下所示: |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +## 环境配置 |
| 25 | + |
| 26 | +开发机选择 30% A100,镜像选择为 Cuda12.2-conda。 |
| 27 | + |
| 28 | +首先来为 Lagent 配置一个可用的环境。 |
| 29 | + |
| 30 | +```bash |
| 31 | +# 创建环境 |
| 32 | +conda create -n agent_camp3 python=3.10 -y |
| 33 | +# 激活环境 |
| 34 | +conda activate agent_camp3 |
| 35 | +# 安装 torch |
| 36 | +conda install pytorch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 pytorch-cuda=12.1 -c pytorch -c nvidia -y |
| 37 | +# 安装其他依赖包 |
| 38 | +pip install termcolor==2.4.0 |
| 39 | +pip install lmdeploy==0.5.2 |
| 40 | +``` |
| 41 | + |
| 42 | +接下来,我们通过源码安装的方式安装 lagent。 |
| 43 | + |
| 44 | +```bash |
| 45 | +# 创建目录以存放代码 |
| 46 | +mkdir -p /root/agent_camp3 |
| 47 | +cd /root/agent_camp3 |
| 48 | +git clone https://github.com/InternLM/lagent.git |
| 49 | +cd lagent && git checkout 81e7ace && pip install -e . && cd .. |
| 50 | +``` |
| 51 | + |
| 52 | +## Lagent Web Demo 使用 |
| 53 | + |
| 54 | +接下来,我们将使用 Lagent 的 Web Demo 来体验 InternLM2.5-7B-Chat 的智能体能力。 |
| 55 | + |
| 56 | +首先,我们先使用 LMDeploy 部署 InternLM2.5-7B-Chat,并启动一个 API Server。 |
| 57 | + |
| 58 | +```bash |
| 59 | +conda activate agent_camp3 |
| 60 | +lmdeploy serve api_server /share/new_models/Shanghai_AI_Laboratory/internlm2_5-7b-chat --model-name internlm2_5-7b-chat |
| 61 | +``` |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +然后,我们在另一个窗口中启动 Lagent 的 Web Demo。 |
| 66 | + |
| 67 | +```bash |
| 68 | +cd /root/agent_camp3/lagent |
| 69 | +conda activate agent_camp3 |
| 70 | +streamlit run examples/internlm2_agent_web_demo.py |
| 71 | +``` |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +在等待两个 server 都完全启动(如下图所示)后,我们在 **本地** 的 PowerShell 中输入如下指令来进行端口映射: |
| 76 | + |
| 77 | +```bash |
| 78 | +ssh -CNg -L 8501:127.0.0.1:8501 -L 23333:127.0.0.1:23333 [email protected] -p <你的 SSH 端口号 > |
| 79 | +``` |
| 80 | + |
| 81 | +| LMDeploy api_server | Lagent Web Demo | |
| 82 | +| --- | --- | |
| 83 | +|  |  | |
| 84 | + |
| 85 | +接下来,在本地浏览器中打开 `localhost:8501`,并修改**模型名称**一栏为 `internlm2_5-7b-chat`,修改**模型 ip**一栏为`127.0.0.1:23333`。 |
| 86 | + |
| 87 | +> [!IMPORTANT] |
| 88 | +> 输入后需要按下回车以确认! |
| 89 | +
|
| 90 | +然后,我们在插件选择一栏选择 `ArxivSearch`,并输入指令“帮我搜索一下 MindSearch 论文”。 |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +最后,可以看到,模型已经回复了相关信息。 |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +## 基于 Lagent 自定义智能体 |
| 99 | + |
| 100 | +在本节中,我们将带大家基于 Lagent 自定义自己的智能体。 |
| 101 | + |
| 102 | +Lagent 中关于工具部分的介绍文档位于 https://lagent.readthedocs.io/zh-cn/latest/tutorials/action.html 。 |
| 103 | + |
| 104 | +使用 Lagent 自定义工具主要分为以下几步: |
| 105 | + |
| 106 | +1. 继承 `BaseAction` 类 |
| 107 | +2. 实现简单工具的 `run` 方法;或者实现工具包内每个子工具的功能 |
| 108 | +3. 简单工具的 `run` 方法可选被 `tool_api` 装饰;工具包内每个子工具的功能都需要被 `tool_api` 装饰 |
| 109 | + |
| 110 | +下面我们将实现一个调用 MagicMaker API 以完成文生图的功能。 |
| 111 | + |
| 112 | +首先,我们先来创建工具文件: |
| 113 | + |
| 114 | +```bash |
| 115 | +cd /root/agent_camp3/lagent |
| 116 | +touch lagent/actions/magicmaker.py |
| 117 | +``` |
| 118 | + |
| 119 | +然后,我们将下面的代码复制进入 `/root/agent_camp3/lagent/lagent/actions/magicmaker.py` |
| 120 | + |
| 121 | +```python |
| 122 | +import json |
| 123 | +import requests |
| 124 | + |
| 125 | +from lagent.actions.base_action import BaseAction, tool_api |
| 126 | +from lagent.actions.parser import BaseParser, JsonParser |
| 127 | +from lagent.schema import ActionReturn, ActionStatusCode |
| 128 | + |
| 129 | + |
| 130 | +class MagicMaker(BaseAction): |
| 131 | + styles_option = [ |
| 132 | + 'dongman', # 动漫 |
| 133 | + 'guofeng', # 国风 |
| 134 | + 'xieshi', # 写实 |
| 135 | + 'youhua', # 油画 |
| 136 | + 'manghe', # 盲盒 |
| 137 | + ] |
| 138 | + aspect_ratio_options = [ |
| 139 | + '16:9', '4:3', '3:2', '1:1', |
| 140 | + '2:3', '3:4', '9:16' |
| 141 | + ] |
| 142 | + |
| 143 | + def __init__(self, |
| 144 | + style='guofeng', |
| 145 | + aspect_ratio='4:3'): |
| 146 | + super().__init__() |
| 147 | + if style in self.styles_option: |
| 148 | + self.style = style |
| 149 | + else: |
| 150 | + raise ValueError(f'The style must be one of {self.styles_option}') |
| 151 | + |
| 152 | + if aspect_ratio in self.aspect_ratio_options: |
| 153 | + self.aspect_ratio = aspect_ratio |
| 154 | + else: |
| 155 | + raise ValueError(f'The aspect ratio must be one of {aspect_ratio}') |
| 156 | + |
| 157 | + @tool_api |
| 158 | + def generate_image(self, keywords: str) -> dict: |
| 159 | + """Run magicmaker and get the generated image according to the keywords. |
| 160 | +
|
| 161 | + Args: |
| 162 | + keywords (:class:`str`): the keywords to generate image |
| 163 | +
|
| 164 | + Returns: |
| 165 | + :class:`dict`: the generated image |
| 166 | + * image (str): path to the generated image |
| 167 | + """ |
| 168 | + try: |
| 169 | + response = requests.post( |
| 170 | + url='https://magicmaker.openxlab.org.cn/gw/edit-anything/api/v1/bff/sd/generate', |
| 171 | + data=json.dumps({ |
| 172 | + "official": True, |
| 173 | + "prompt": keywords, |
| 174 | + "style": self.style, |
| 175 | + "poseT": False, |
| 176 | + "aspectRatio": self.aspect_ratio |
| 177 | + }), |
| 178 | + headers={'content-type': 'application/json'} |
| 179 | + ) |
| 180 | + except Exception as exc: |
| 181 | + return ActionReturn( |
| 182 | + errmsg=f'MagicMaker exception: {exc}', |
| 183 | + state=ActionStatusCode.HTTP_ERROR) |
| 184 | + image_url = response.json()['data']['imgUrl'] |
| 185 | + return {'image': image_url} |
| 186 | + |
| 187 | +``` |
| 188 | + |
| 189 | +最后,我们修改 `/root/agent_camp3/lagent/examples/internlm2_agent_web_demo.py` 来适配我们的自定义工具。 |
| 190 | + |
| 191 | +1. 在 `from lagent.actions import ActionExecutor, ArxivSearch, IPythonInterpreter` 的下一行添加 `from lagent.actions.magicmaker import MagicMaker` |
| 192 | +2. 在第27行添加 `MagicMaker()`。 |
| 193 | + |
| 194 | +```diff |
| 195 | +from lagent.actions import ActionExecutor, ArxivSearch, IPythonInterpreter |
| 196 | ++ from lagent.actions.magicmaker import MagicMaker |
| 197 | +from lagent.agents.internlm2_agent import INTERPRETER_CN, META_CN, PLUGIN_CN, Internlm2Agent, Internlm2Protocol |
| 198 | + |
| 199 | +... |
| 200 | + action_list = [ |
| 201 | + ArxivSearch(), |
| 202 | ++ MagicMaker(), |
| 203 | + ] |
| 204 | +``` |
| 205 | + |
| 206 | +接下来,启动 Web Demo 来体验一下吧!我们同时启用两个工具,然后输入“请帮我生成一幅山水画” |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | +然后,我们再试一下“帮我搜索一下 MindSearch 论文”。 |
| 213 | + |
| 214 | + |
0 commit comments