OpenAIImageToolkit

class OpenAIImageToolkit(BaseToolkit):
A class representing a toolkit for image generation using OpenAI’s DALL-E model.

init

def __init__(self, timeout: Optional[float] = None):
Initializes a new instance of the OpenAIImageToolkit class. Parameters:
  • timeout (Optional[float]): The timeout value for API requests in seconds. If None, no timeout is applied. (default: :obj:None)

base64_to_image

def base64_to_image(self, base64_string: str):
Converts a base64 encoded string into a PIL Image object. Parameters:
  • base64_string (str): The base64 encoded string of the image.
Returns: Optional[Image.Image]: The PIL Image object or None if conversion fails.

image_path_to_base64

def image_path_to_base64(self, image_path: str):
Converts the file path of an image to a Base64 encoded string. Parameters:
  • image_path (str): The path to the image file.
Returns: str: A Base64 encoded string representing the content of the image file.

image_to_base64

def image_to_base64(self, image: Image.Image):
Converts an image into a base64-encoded string. This function takes an image object as input, encodes the image into a PNG format base64 string, and returns it. If the encoding process encounters an error, it prints the error message and returns None. Parameters:
  • image: The image object to be encoded, supports any image format that can be saved in PNG format.
Returns: str: A base64-encoded string of the image.

get_dalle_img

def get_dalle_img(self, prompt: str, image_dir: str = 'img'):
Generate an image using OpenAI’s DALL-E model. The generated image is saved to the specified directory. Parameters:
  • prompt (str): The text prompt based on which the image is generated.
  • image_dir (str): The directory to save the generated image. Defaults to ‘img’.
Returns: str: The path to the saved image.

get_tools

def get_tools(self):
Returns: List[FunctionTool]: A list of FunctionTool objects representing the functions in the toolkit.