Lagent 自定义你的 Agent 智能体
- 1 环境准备
- 2 启动模型
- 3 部署lagent
- 4 可视化页面及配置
- 5 自定义工具的智能体
1 环境准备
直接复用之前的xtuner或者llamaindex环境
# 安装其他依赖包
conda activate llamainde
pip install termcolor==2.4.0
pip install lmdeploy==0.5.2
pip install griffe==0.48 # 重要
2 启动模型
# 这里我使用的是早前下载的internlm2-chat-7b
conda activate llamainde
lmdeploy serve api_server /project/serve/xtuner/Shanghai_AI_Laboratory/internlm2-chat-7b --model-name internlm2-chat-7b
3 部署lagent
另启动一个窗口
# 创建目录以存放代码
mkdir -p /project/serve/camp3
cd /project/serve/camp3
git clone https://github.com/InternLM/lagent.git
cd lagent && git checkout 81e7ace && pip install -e . && cd ..
cd /project/serve/camp3/lagent
conda activate llamaindex
streamlit run examples/internlm2_agent_web_demo.py
4 可视化页面及配置
注意模型ip不要带http
这里用的internlm2-chat-7b, 使用工具的效果不是很直接,它会再询问一下。
5 自定义工具的智能体
(base) [root@localhost ~]# cd /project/serve/camp3/lagent/
(base) [root@localhost lagent]# touch lagent/actions/magicmaker.py
(base) [root@localhost lagent]# vim lagent/actions/magicmaker.py
(base) [root@localhost lagent]# vim examples/internlm2_agent_web_demo.py
(base) [root@localhost lagent]#
#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}
-
创建magicmaker.py文件
-
修改
/root/agent_camp3/lagent/examples/internlm2_agent_web_demo.py
来适配我们的自定义工具。- 在
from lagent.actions import ActionExecutor, ArxivSearch, IPythonInterpreter
的下一行添加from lagent.actions.magicmaker import MagicMaker
- 在第27行添加
MagicMaker()
。
- 在
-
重新启动**streamlit run examples/internlm2_agent_web_demo.py
这里与教程不一样的是,图片并没有直接展示出来,要点击才能查看
bug
ModuleNotFoundError: No module named 'griffe.enumerations
pip install griffe==0.48