Skip to content

Commit f7fc638

Browse files
authored
[Feature] Add agent for camp3 (InternLM#1567)
* [Feature] Add agent for camp3
1 parent c6dbab1 commit f7fc638

File tree

3 files changed

+231
-1
lines changed

3 files changed

+231
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
||关卡名称|资料|闯关激励|
3939
|:-----|:----|:----|:-----|
4040
|第 1 关| 探索 InternLM 模型能力边界 | 任务、文档、视频| 100元算力点 |
41-
|第 2 关| Lagent 自定义你的 Agent 智能体 |任务、文档、视频| 100元算力点 |
41+
|第 2 关| Lagent 自定义你的 Agent 智能体 | [任务](docs/L2/Lagent/task.md)[文档](docs/L2/Lagent/readme.md)、视频| 100元算力点 |
4242
|第 3 关| LMDeploy 量化部署进阶实践 | 任务、文档、视频| 100元算力点 |
4343
|第 4 关| InternVL 多模态模型部署微调实践 | 任务、文档、视频| 100元算力点 |
4444
|第 5 关| 茴香豆:企业级知识库问答工具 | 任务、文档、视频| 100元算力点 |

docs/L2/Lagent/readme.md

+213
Original file line numberDiff line numberDiff line change
@@ -1 +1,214 @@
1+
![head](https://github.com/user-attachments/assets/4a9cd258-07c0-41db-a192-1442df9dd098)
12

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+
![image](https://github.com/InternLM/lagent/assets/24351120/cefc4145-2ad8-4f80-b88b-97c05d1b9d3e)
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+
![lmdeploy server](https://github.com/user-attachments/assets/7765bce4-50bc-4204-b217-ab1bb5a269ef)
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+
![lagent web demo](https://github.com/user-attachments/assets/1b7af814-78d9-458a-8c3b-eaa39a588638)
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+
| ![LMDeploy done](https://github.com/user-attachments/assets/820f4ceb-4337-484f-997b-001d5532816a) | ![Lagent done](https://github.com/user-attachments/assets/b9ccff2b-6e05-4c4e-b85b-860f8b9e2f41) |
84+
85+
接下来,在本地浏览器中打开 `localhost:8501`,并修改**模型名称**一栏为 `internlm2_5-7b-chat`,修改**模型 ip**一栏为`127.0.0.1:23333`
86+
87+
> [!IMPORTANT]
88+
> 输入后需要按下回车以确认!
89+
90+
然后,我们在插件选择一栏选择 `ArxivSearch`,并输入指令“帮我搜索一下 MindSearch 论文”。
91+
92+
![Web Demo](https://github.com/user-attachments/assets/34ac1001-8bfa-4d2a-8346-d871a0e0f03c)
93+
94+
最后,可以看到,模型已经回复了相关信息。
95+
96+
![result](https://github.com/user-attachments/assets/d21b64c2-acf4-48e1-a1e5-73775e6b36d4)
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+
![instruction](https://github.com/user-attachments/assets/699308cd-6b17-4515-a42e-d120bd8e9a2b)
209+
210+
![result](https://github.com/user-attachments/assets/c62cea67-1b9f-4a45-ba7f-6c5836d6db7e)
211+
212+
然后,我们再试一下“帮我搜索一下 MindSearch 论文”。
213+
214+
![result](https://github.com/user-attachments/assets/03a39808-db97-4321-883e-7a0446e95343)

docs/L2/Lagent/task.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Lagent 自定义你的 Agent 智能体
2+
3+
记录复现过程并截图
4+
5+
## 基础任务(完成此任务即完成闯关)
6+
7+
- 成功使用 Lagent Web Demo 部署一个智能体,并调用成功。
8+
9+
## 进阶任务(闯关不要求完成此任务)
10+
11+
- 使用 Lagent 自定义一个智能体,并使用 Lagent Web Demo 成功部署与调用,记录复现过程并截图。
12+
13+
## 闯关材料提交(完成任务并且提交材料视为闯关成功)
14+
15+
- 闯关作业总共分为一个任务,一个任务完成视作闯关成功。
16+
- 请将作业发布到知乎、CSDN等任一社交媒体,将作业链接提交到以下问卷,助教老师批改后将获得 100 算力点奖励!!!
17+
- 提交地址:https://aicarrier.feishu.cn/share/base/form/shrcnZ4bQ4YmhEtMtnKxZUcf1vd

0 commit comments

Comments
 (0)