From b343c71b9bcecf1b77aa8a98eb79e0bf5d7897a4 Mon Sep 17 00:00:00 2001 From: Eduardo Chiarotti Date: Tue, 30 Jul 2024 22:17:23 -0300 Subject: [PATCH 1/2] feat: Add Dall-E tool to generate images --- src/crewai_tools/tools/dalle_tool/README.MD | 0 .../tools/dalle_tool/dalle_tool.py | 48 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/crewai_tools/tools/dalle_tool/README.MD create mode 100644 src/crewai_tools/tools/dalle_tool/dalle_tool.py diff --git a/src/crewai_tools/tools/dalle_tool/README.MD b/src/crewai_tools/tools/dalle_tool/README.MD new file mode 100644 index 000000000..e69de29bb diff --git a/src/crewai_tools/tools/dalle_tool/dalle_tool.py b/src/crewai_tools/tools/dalle_tool/dalle_tool.py new file mode 100644 index 000000000..a4f7738a6 --- /dev/null +++ b/src/crewai_tools/tools/dalle_tool/dalle_tool.py @@ -0,0 +1,48 @@ +import json +from typing import Type + +from crewai_tools.tools.base_tool import BaseTool +from openai import OpenAI +from pydantic.v1 import BaseModel + + +class ImagePromptSchema(BaseModel): + """Input for Dall-E Tool.""" + + image_description: str = "Description of the image to be generated by Dall-E." + + +class DallETool(BaseTool): + name: str = "Dall-E Tool" + description: str = "Generates images using OpenAI's Dall-E model." + args_schema: Type[BaseModel] = ImagePromptSchema + + model: str = "dall-e-3" + size: str = "1024x1024" + quality: str = "standard" + n: int = 1 + + def _run(self, **kwargs) -> str: + client = OpenAI() + + image_description = kwargs.get("image_description") + + if not image_description: + return "Image description is required." + + response = client.images.generate( + model=self.model, + prompt=image_description, + size=self.size, + quality=self.quality, + n=self.n, + ) + + image_data = json.dumps( + { + "image_url": response.data[0].url, + "image_description": response.data[0].revised_prompt, + } + ) + + return image_data From 0070df7451a1a79b33e655628ab2f0d324d6c3eb Mon Sep 17 00:00:00 2001 From: Eduardo Chiarotti Date: Tue, 30 Jul 2024 22:29:45 -0300 Subject: [PATCH 2/2] docs: Add documentation for the DallETool --- src/crewai_tools/tools/dalle_tool/README.MD | 41 +++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/crewai_tools/tools/dalle_tool/README.MD b/src/crewai_tools/tools/dalle_tool/README.MD index e69de29bb..a315c7c10 100644 --- a/src/crewai_tools/tools/dalle_tool/README.MD +++ b/src/crewai_tools/tools/dalle_tool/README.MD @@ -0,0 +1,41 @@ +# DALL-E Tool + +## Description +This tool is used to give the Agent the ability to generate images using the DALL-E model. It is a transformer-based model that generates images from textual descriptions. This tool allows the Agent to generate images based on the text input provided by the user. + +## Installation +Install the crewai_tools package +```shell +pip install 'crewai[tools]' +``` + +## Example + +Remember that when using this tool, the text must be generated by the Agent itself. The text must be a description of the image you want to generate. + +```python +from crewai_tools import DallETool + +Agent( + ... + tools=[DallETool()], +) +``` + +If needed you can also tweak the parameters of the DALL-E model by passing them as arguments to the `DallETool` class. For example: + +```python +from crewai_tools import DallETool + +dalle_tool = DallETool(model: str = "dall-e-3", + size: str = "1024x1024", + quality: str = "standard", + n: int = 1) + +Agent( + ... + tools=[dalle_tool] +) +``` + +The parameter are based on the `client.images.generate` method from the OpenAI API. For more information on the parameters, please refer to the [OpenAI API documentation](https://platform.openai.com/docs/guides/images/introduction?lang=python).