diff --git "a/docs/guidebook/zh/2_2_3_\351\233\206\346\210\220\347\232\204\345\267\245\345\205\267.md" "b/docs/guidebook/zh/2_2_3_\351\233\206\346\210\220\347\232\204\345\267\245\345\205\267.md" index 0e663e28..783a0050 100644 --- "a/docs/guidebook/zh/2_2_3_\351\233\206\346\210\220\347\232\204\345\267\245\345\205\267.md" +++ "b/docs/guidebook/zh/2_2_3_\351\233\206\346\210\220\347\232\204\345\267\245\345\205\267.md" @@ -185,4 +185,22 @@ metadata: headers 发送请求需要使用的 http的header, json_parse 输入参数是否需要是要HTTP解析,POST请求时需要设置为True,GET请求需要设置为False response_content_type http请求结果的解析方式,设置为json时,会返回json结果,设置为text时会返回text结果 -该工具可以直接使用,无需任何keys \ No newline at end of file +该工具可以直接使用,无需任何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). \ No newline at end of file diff --git a/sample_standard_app/app/core/agent/gen_image_case/__init__.py b/sample_standard_app/app/core/agent/gen_image_case/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/sample_standard_app/app/core/agent/gen_image_case/demo_gen_image_agent.py b/sample_standard_app/app/core/agent/gen_image_case/demo_gen_image_agent.py new file mode 100644 index 00000000..33973d4a --- /dev/null +++ b/sample_standard_app/app/core/agent/gen_image_case/demo_gen_image_agent.py @@ -0,0 +1,35 @@ +# !/usr/bin/env python3 +# -*- coding:utf-8 -*- + +# @Time : 2024/12/03 19:10 +# @Author : xutingdong +# @Email : xutingdong.xtd@antgroup.com +# @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()) \ No newline at end of file diff --git a/sample_standard_app/app/core/agent/gen_image_case/demo_gen_image_agent.yaml b/sample_standard_app/app/core/agent/gen_image_case/demo_gen_image_agent.yaml new file mode 100644 index 00000000..de2bd1b8 --- /dev/null +++ b/sample_standard_app/app/core/agent/gen_image_case/demo_gen_image_agent.yaml @@ -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' \ No newline at end of file diff --git a/sample_standard_app/app/core/tool/gen_image_tool.py b/sample_standard_app/app/core/tool/gen_image_tool.py new file mode 100644 index 00000000..ab21cb55 --- /dev/null +++ b/sample_standard_app/app/core/tool/gen_image_tool.py @@ -0,0 +1,44 @@ +# !/usr/bin/env python3 +# -*- coding:utf-8 -*- + +# @Time : 2024/12/03 19:10 +# @Author : xutingdong +# @Email : xutingdong.xtd@antgroup.com +# @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("图片生成失败") diff --git a/sample_standard_app/app/core/tool/gen_image_tool.yaml b/sample_standard_app/app/core/tool/gen_image_tool.yaml new file mode 100644 index 00000000..14eb50e1 --- /dev/null +++ b/sample_standard_app/app/core/tool/gen_image_tool.yaml @@ -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' \ No newline at end of file diff --git a/sample_standard_app/app/examples/gen_image_bot.py b/sample_standard_app/app/examples/gen_image_bot.py new file mode 100644 index 00000000..3faf580a --- /dev/null +++ b/sample_standard_app/app/examples/gen_image_bot.py @@ -0,0 +1,28 @@ +# !/usr/bin/env python3 +# -*- coding:utf-8 -*- + +# @Time : 2024/12/03 19:10 +# @Author : xutingdong +# @Email : xutingdong.xtd@antgroup.com +# @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("猫")