Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gen image tool #215

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion docs/guidebook/zh/2_2_3_集成的工具.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,22 @@ metadata:
headers 发送请求需要使用的 http的header,
json_parse 输入参数是否需要是要HTTP解析,POST请求时需要设置为True,GET请求需要设置为False
response_content_type http请求结果的解析方式,设置为json时,会返回json结果,设置为text时会返回text结果
该工具可以直接使用,无需任何keys
该工具可以直接使用,无需任何keys

## 4.文生图工具
### 4.1 GenImageTool
[工具地址](../../../sample_standard_app/app/core/tool/gen_image_tool.yaml)
该工具可以基于语段生成图片:
```yaml
name: 'gen_image_tool'
description: '使用该工具可以通过输入一段描述来生成一张图片。
```'
tool_type: 'api'
input_keys: ['input']
metadata:
type: 'TOOL'
module: 'sample_standard_app.app.core.tool.gen_image_tool'
class: 'GenImageTool'
```

该工具可以直接使用,无需任何key,但是为了系统安全,请不要在生产环境使用该工具,使用可参考[gen_image_bot agent](../../../sample_standard_app/app/examples/tool/gen_image_bot.py).
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# !/usr/bin/env python3
# -*- coding:utf-8 -*-

# @Time : 2024/12/03 19:10
# @Author : xutingdong
# @Email : [email protected]
# @FileName: demo_gen_image_agent.py

from agentuniverse.agent.agent import Agent
from agentuniverse.agent.input_object import InputObject
from agentuniverse.agent.action.tool.tool_manager import ToolManager


class GenImageAgent(Agent):
def input_keys(self) -> list[str]:
return ['input']

def output_keys(self) -> list[str]:
return ['output']

def parse_input(self, input_object: InputObject, agent_input: dict) -> dict:
agent_input['input'] = input_object.get_data('input')
return agent_input

def parse_result(self, planner_result: dict) -> dict:
return {'output': ''}

def execute(self, input_object: InputObject, agent_input: dict) -> dict:
action: dict = self.agent_model.action or dict()
tools: list = action.get('tool') or list()
for tool_name in tools:
tool = ToolManager().get_instance_obj(tool_name)
if tool is None:
continue
tool.run(**input_object.to_dict())
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
info:
name: 'demo_gen_image_agent'
description: 'gen image agent'
action:
tool:
- 'gen_image_tool'
metadata:
type: 'AGENT'
module: 'sample_standard_app.app.core.agent.gen_image_case.demo_gen_image_agent'
class: 'GenImageAgent'
44 changes: 44 additions & 0 deletions sample_standard_app/app/core/tool/gen_image_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# !/usr/bin/env python3
# -*- coding:utf-8 -*-

# @Time : 2024/12/03 19:10
# @Author : xutingdong
# @Email : [email protected]
# @FileName: gen_image_tool.py

from agentuniverse.agent.action.tool.tool import Tool, ToolInput
from PIL import Image
import requests
import io
from urllib import request, parse
import urllib.parse


class GenImageTool(Tool):
"""The mock search tool.

In this tool, we mocked the search engine's answers to search for information about BYD and Warren Buffett.

Note:
The tool is only suitable for users searching for Buffett or BYD related queries.
We recommend that you configure your `SERPER_API_KEY` and use google_search_tool to get information.
"""

def execute(self, tool_input: ToolInput):
input = tool_input.get_data("input")
"""Demonstrates the execute method of the Tool class."""
input = parse.quote(input, safe='/')
image_url = f'https://image.pollinations.ai/prompt/{input}'
# url_request = request.Request(image_url)
# url_response = request.urlopen(url_request)
# data = url_response.read()
# path = 'test.jpg'
# with open(path, 'wb') as f:
# f.write(data)

response = requests.get(image_url)
if response.status_code == 200:
image = Image.open(io.BytesIO(response.content))
image.show()
else:
print("图片生成失败")
9 changes: 9 additions & 0 deletions sample_standard_app/app/core/tool/gen_image_tool.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: 'gen_image_tool'
description: '使用该工具可以通过输入一段描述来生成一张图片。
```'
tool_type: 'api'
input_keys: ['input']
metadata:
type: 'TOOL'
module: 'sample_standard_app.app.core.tool.gen_image_tool'
class: 'GenImageTool'
28 changes: 28 additions & 0 deletions sample_standard_app/app/examples/gen_image_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# !/usr/bin/env python3
# -*- coding:utf-8 -*-

# @Time : 2024/12/03 19:10
# @Author : xutingdong
# @Email : [email protected]
# @FileName: gen_image_bot.py

from agentuniverse.agent.output_object import OutputObject
from agentuniverse.agent.agent import Agent
from agentuniverse.agent.agent_manager import AgentManager
from agentuniverse.base.agentuniverse import AgentUniverse

AgentUniverse().start(config_path='../../config/config.toml')


def chat(question: str):
""" Rag agent example.

The rag agent in agentUniverse becomes a chatbot and can ask questions to get the answer.
"""

instance: Agent = AgentManager().get_instance_obj('demo_gen_image_agent')
output_object: OutputObject = instance.run(input=question)


if __name__ == '__main__':
chat("猫")