Skip to content

Commit 55b0cee

Browse files
committed
add gen image tool
1 parent afca9df commit 55b0cee

File tree

7 files changed

+143
-1
lines changed

7 files changed

+143
-1
lines changed

docs/guidebook/zh/2_2_3_集成的工具.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,22 @@ metadata:
185185
headers 发送请求需要使用的 http的header,
186186
json_parse 输入参数是否需要是要HTTP解析,POST请求时需要设置为True,GET请求需要设置为False
187187
response_content_type http请求结果的解析方式,设置为json时,会返回json结果,设置为text时会返回text结果
188-
该工具可以直接使用,无需任何keys
188+
该工具可以直接使用,无需任何keys
189+
190+
## 4.文生图工具
191+
### 4.1 GenImageTool
192+
[工具地址](../../../sample_standard_app/app/core/tool/gen_image_tool.yaml)
193+
该工具可以基于语段生成图片:
194+
```yaml
195+
name: 'gen_image_tool'
196+
description: '使用该工具可以通过输入一段描述来生成一张图片。
197+
```'
198+
tool_type: 'api'
199+
input_keys: ['input']
200+
metadata:
201+
type: 'TOOL'
202+
module: 'sample_standard_app.app.core.tool.gen_image_tool'
203+
class: 'GenImageTool'
204+
```
205+
206+
该工具可以直接使用,无需任何key,但是为了系统安全,请不要在生产环境使用该工具,使用可参考[gen_image_bot agent](../../../sample_standard_app/app/examples/tool/gen_image_bot.py).

sample_standard_app/app/core/agent/gen_image_case/__init__.py

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# !/usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
4+
# @Time : 2024/6/12 19:56
5+
# @Author : wangchongshi
6+
# @Email : wangchongshi.wcs@antgroup.com
7+
# @FileName: law_rag_agent.py
8+
from agentuniverse.agent.agent import Agent
9+
from agentuniverse.agent.input_object import InputObject
10+
from agentuniverse.agent.action.tool.tool_manager import ToolManager
11+
12+
13+
class GenImageAgent(Agent):
14+
def input_keys(self) -> list[str]:
15+
return ['input']
16+
17+
def output_keys(self) -> list[str]:
18+
return ['output']
19+
20+
def parse_input(self, input_object: InputObject, agent_input: dict) -> dict:
21+
agent_input['input'] = input_object.get_data('input')
22+
return agent_input
23+
24+
def parse_result(self, planner_result: dict) -> dict:
25+
return {'output': ''}
26+
27+
def execute(self, input_object: InputObject, agent_input: dict) -> dict:
28+
action: dict = self.agent_model.action or dict()
29+
tools: list = action.get('tool') or list()
30+
for tool_name in tools:
31+
tool = ToolManager().get_instance_obj(tool_name)
32+
if tool is None:
33+
continue
34+
tool.run(**input_object.to_dict())
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
info:
2+
name: 'demo_gen_image_agent'
3+
description: 'gen image agent'
4+
action:
5+
tool:
6+
- 'gen_image_tool'
7+
metadata:
8+
type: 'AGENT'
9+
module: 'sample_standard_app.app.core.agent.gen_image_case.demo_gen_image_agent'
10+
class: 'GenImageAgent'
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# !/usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
4+
# @Time : 2024/12/03 19:10
5+
# @Author : xutingdong
6+
# @Email : xutingdong.xtd@antgroup.com
7+
# @FileName: gen_image_tool.py
8+
9+
from agentuniverse.agent.action.tool.tool import Tool, ToolInput
10+
from PIL import Image
11+
import requests
12+
import io
13+
from urllib import request, parse
14+
import urllib.parse
15+
16+
17+
class GenImageTool(Tool):
18+
"""The mock search tool.
19+
20+
In this tool, we mocked the search engine's answers to search for information about BYD and Warren Buffett.
21+
22+
Note:
23+
The tool is only suitable for users searching for Buffett or BYD related queries.
24+
We recommend that you configure your `SERPER_API_KEY` and use google_search_tool to get information.
25+
"""
26+
27+
def execute(self, tool_input: ToolInput):
28+
input = tool_input.get_data("input")
29+
"""Demonstrates the execute method of the Tool class."""
30+
input = parse.quote(input, safe='/')
31+
image_url = f'https://image.pollinations.ai/prompt/{input}'
32+
# url_request = request.Request(image_url)
33+
# url_response = request.urlopen(url_request)
34+
# data = url_response.read()
35+
# path = 'test.jpg'
36+
# with open(path, 'wb') as f:
37+
# f.write(data)
38+
39+
response = requests.get(image_url)
40+
if response.status_code == 200:
41+
image = Image.open(io.BytesIO(response.content))
42+
image.show()
43+
else:
44+
print("图片生成失败")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: 'gen_image_tool'
2+
description: '使用该工具可以通过输入一段描述来生成一张图片。
3+
```'
4+
tool_type: 'api'
5+
input_keys: ['input']
6+
metadata:
7+
type: 'TOOL'
8+
module: 'sample_standard_app.app.core.tool.gen_image_tool'
9+
class: 'GenImageTool'
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# !/usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
4+
# @Time : 2024/5/8 11:44
5+
# @Author : wangchongshi
6+
# @Email : wangchongshi.wcs@antgroup.com
7+
# @FileName: rag_chat_bot.py
8+
from agentuniverse.agent.output_object import OutputObject
9+
from agentuniverse.agent.agent import Agent
10+
from agentuniverse.agent.agent_manager import AgentManager
11+
from agentuniverse.base.agentuniverse import AgentUniverse
12+
13+
AgentUniverse().start(config_path='../../config/config.toml')
14+
15+
16+
def chat(question: str):
17+
""" Rag agent example.
18+
19+
The rag agent in agentUniverse becomes a chatbot and can ask questions to get the answer.
20+
"""
21+
22+
instance: Agent = AgentManager().get_instance_obj('demo_gen_image_agent')
23+
output_object: OutputObject = instance.run(input=question)
24+
25+
26+
if __name__ == '__main__':
27+
chat("猫")

0 commit comments

Comments
 (0)