{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2c75da30",
   "metadata": {},
   "source": [
    "# Agent Chat with Multimodal Models: DALLE  and GPT-4V\n",
    "\n",
    "Requires: OpenAI V1. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f51914c",
   "metadata": {},
   "source": [
    "### Before everything starts, install AutoGen with the `lmm` option\n",
    "```bash\n",
    "pip install \"autogen-agentchat[lmm]~=0.2\"\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "67d45964",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "import pdb\n",
    "import random\n",
    "import re\n",
    "import time\n",
    "from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import PIL\n",
    "import requests\n",
    "from diskcache import Cache\n",
    "from openai import OpenAI\n",
    "from PIL import Image\n",
    "from termcolor import colored\n",
    "\n",
    "import autogen\n",
    "from autogen import Agent, AssistantAgent, ConversableAgent, UserProxyAgent\n",
    "from autogen.agentchat.contrib.img_utils import _to_pil, get_image_data, get_pil_image, gpt4v_formatter\n",
    "from autogen.agentchat.contrib.multimodal_conversable_agent import MultimodalConversableAgent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b1db6f5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "config_list_4v = autogen.config_list_from_json(\n",
    "    \"OAI_CONFIG_LIST\",\n",
    "    filter_dict={\n",
    "        \"model\": [\"gpt-4-vision-preview\"],\n",
    "    },\n",
    ")\n",
    "\n",
    "config_list_gpt4 = autogen.config_list_from_json(\n",
    "    \"OAI_CONFIG_LIST\",\n",
    "    filter_dict={\n",
    "        \"model\": [\"gpt-4\", \"gpt-4-0314\", \"gpt4\", \"gpt-4-32k\", \"gpt-4-32k-0314\", \"gpt-4-32k-v0314\"],\n",
    "    },\n",
    ")\n",
    "\n",
    "config_list_dalle = autogen.config_list_from_json(\n",
    "    \"OAI_CONFIG_LIST\",\n",
    "    filter_dict={\n",
    "        \"model\": [\"dalle\"],\n",
    "    },\n",
    ")\n",
    "\n",
    "gpt4_llm_config = {\"config_list\": config_list_gpt4, \"cache_seed\": 42}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54de3579",
   "metadata": {},
   "source": [
    "The `config_list_dalle` should be something like:\n",
    "\n",
    "```python\n",
    "[\n",
    "    {\n",
    "        'model': 'dalle',\n",
    "        'api_key': 'Your API Key here',\n",
    "        'api_version': '2024-02-01'\n",
    "    }\n",
    "]\n",
    " ```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24850276",
   "metadata": {},
   "source": [
    "## Helper Functions\n",
    "\n",
    "We first create a warpper for DALLE call, make the "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "34a5e2f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def dalle_call(client: OpenAI, model: str, prompt: str, size: str, quality: str, n: int) -> str:\n",
    "    \"\"\"\n",
    "    Generate an image using OpenAI's DALL-E model and cache the result.\n",
    "\n",
    "    This function takes a prompt and other parameters to generate an image using OpenAI's DALL-E model.\n",
    "    It checks if the result is already cached; if so, it returns the cached image data. Otherwise,\n",
    "    it calls the DALL-E API to generate the image, stores the result in the cache, and then returns it.\n",
    "\n",
    "    Args:\n",
    "        client (OpenAI): The OpenAI client instance for making API calls.\n",
    "        model (str): The specific DALL-E model to use for image generation.\n",
    "        prompt (str): The text prompt based on which the image is generated.\n",
    "        size (str): The size specification of the image. TODO: This should allow specifying landscape, square, or portrait modes.\n",
    "        quality (str): The quality setting for the image generation.\n",
    "        n (int): The number of images to generate.\n",
    "\n",
    "    Returns:\n",
    "    str: The image data as a string, either retrieved from the cache or newly generated.\n",
    "\n",
    "    Note:\n",
    "    - The cache is stored in a directory named '.cache/'.\n",
    "    - The function uses a tuple of (model, prompt, size, quality, n) as the key for caching.\n",
    "    - The image data is obtained by making a secondary request to the URL provided by the DALL-E API response.\n",
    "    \"\"\"\n",
    "    # Function implementation...\n",
    "    cache = Cache(\".cache/\")  # Create a cache directory\n",
    "    key = (model, prompt, size, quality, n)\n",
    "    if key in cache:\n",
    "        return cache[key]\n",
    "\n",
    "    # If not in cache, compute and store the result\n",
    "    response = client.images.generate(\n",
    "        model=model,\n",
    "        prompt=prompt,\n",
    "        size=size,\n",
    "        quality=quality,\n",
    "        n=n,\n",
    "    )\n",
    "    image_url = response.data[0].url\n",
    "    img_data = get_image_data(image_url)\n",
    "    cache[key] = img_data\n",
    "\n",
    "    return img_data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76f4f43d",
   "metadata": {},
   "source": [
    "Here is a helper function to extract image from a DALLE agent. We will show the DALLE agent later."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8fee2643",
   "metadata": {},
   "outputs": [],
   "source": [
    "def extract_img(agent: Agent) -> PIL.Image:\n",
    "    \"\"\"\n",
    "    Extracts an image from the last message of an agent and converts it to a PIL image.\n",
    "\n",
    "    This function searches the last message sent by the given agent for an image tag,\n",
    "    extracts the image data, and then converts this data into a PIL (Python Imaging Library) image object.\n",
    "\n",
    "    Parameters:\n",
    "        agent (Agent): An instance of an agent from which the last message will be retrieved.\n",
    "\n",
    "    Returns:\n",
    "        PIL.Image: A PIL image object created from the extracted image data.\n",
    "\n",
    "    Note:\n",
    "    - The function assumes that the last message contains an <img> tag with image data.\n",
    "    - The image data is extracted using a regular expression that searches for <img> tags.\n",
    "    - It's important that the agent's last message contains properly formatted image data for successful extraction.\n",
    "    - The `_to_pil` function is used to convert the extracted image data into a PIL image.\n",
    "    - If no <img> tag is found, or if the image data is not correctly formatted, the function may raise an error.\n",
    "    \"\"\"\n",
    "    last_message = agent.last_message()[\"content\"]\n",
    "\n",
    "    if isinstance(last_message, str):\n",
    "        img_data = re.findall(\"<img (.*)>\", last_message)[0]\n",
    "    elif isinstance(last_message, list):\n",
    "        # The GPT-4V format, where the content is an array of data\n",
    "        assert isinstance(last_message[0], dict)\n",
    "        img_data = last_message[0][\"image_url\"][\"url\"]\n",
    "\n",
    "    pil_img = get_pil_image(img_data)\n",
    "    return pil_img"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3af86569",
   "metadata": {},
   "source": [
    "## The DALLE Agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5558caa2",
   "metadata": {},
   "outputs": [],
   "source": [
    "class DALLEAgent(ConversableAgent):\n",
    "    def __init__(self, name, llm_config: dict, **kwargs):\n",
    "        super().__init__(name, llm_config=llm_config, **kwargs)\n",
    "\n",
    "        try:\n",
    "            config_list = llm_config[\"config_list\"]\n",
    "            api_key = config_list[0][\"api_key\"]\n",
    "        except Exception as e:\n",
    "            print(\"Unable to fetch API Key, because\", e)\n",
    "            api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "        self._dalle_client = OpenAI(api_key=api_key)\n",
    "        self.register_reply([Agent, None], DALLEAgent.generate_dalle_reply)\n",
    "\n",
    "    def send(\n",
    "        self,\n",
    "        message: Union[Dict, str],\n",
    "        recipient: Agent,\n",
    "        request_reply: Optional[bool] = None,\n",
    "        silent: Optional[bool] = False,\n",
    "    ):\n",
    "        # override and always \"silent\" the send out message;\n",
    "        # otherwise, the print log would be super long!\n",
    "        super().send(message, recipient, request_reply, silent=True)\n",
    "\n",
    "    def generate_dalle_reply(self, messages: Optional[List[Dict]], sender: \"Agent\", config):\n",
    "        \"\"\"Generate a reply using OpenAI DALLE call.\"\"\"\n",
    "        client = self._dalle_client if config is None else config\n",
    "        if client is None:\n",
    "            return False, None\n",
    "        if messages is None:\n",
    "            messages = self._oai_messages[sender]\n",
    "\n",
    "        prompt = messages[-1][\"content\"]\n",
    "        # TODO: integrate with autogen.oai. For instance, with caching for the API call\n",
    "        img_data = dalle_call(\n",
    "            client=client,\n",
    "            model=\"dall-e-3\",\n",
    "            prompt=prompt,\n",
    "            size=\"1024x1024\",  # TODO: the size should be flexible, deciding landscape, square, or portrait mode.\n",
    "            quality=\"standard\",\n",
    "            n=1,\n",
    "        )\n",
    "\n",
    "        img_data = _to_pil(img_data)  # Convert to PIL image\n",
    "\n",
    "        # Return the OpenAI message format\n",
    "        return True, {\"content\": [{\"type\": \"image_url\", \"image_url\": {\"url\": img_data}}]}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd3f9e56",
   "metadata": {},
   "source": [
    "## Simple Example: Call directly from User"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d4095796",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33mUser_proxy\u001b[0m (to Dalle):\n",
      "\n",
      "Create an image with black background, a happy robot is showing a sign with \"I Love AutoGen\".\n",
      "\n",
      "--------------------------------------------------------------------------------\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/beibinli/autogen/autogen/agentchat/user_proxy_agent.py:83: UserWarning: Using None to signal a default code_execution_config is deprecated. Use {} to use default or False to disable code execution.\n",
      "  super().__init__(\n",
      "/home/beibinli/autogen/autogen/agentchat/conversable_agent.py:954: UserWarning: Cannot extract summary using last_msg: 'list' object has no attribute 'replace'\n",
      "  warnings.warn(f\"Cannot extract summary using last_msg: {e}\", UserWarning)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "ChatResult(chat_id=None, chat_history=[{'content': 'Create an image with black background, a happy robot is showing a sign with \"I Love AutoGen\".', 'role': 'assistant'}, {'content': [{'type': 'image_url', 'image_url': {'url': <PIL.PngImagePlugin.PngImageFile image mode=RGB size=1024x1024 at 0x7F8EB52561C0>}}], 'role': 'user'}], summary='', cost=({'total_cost': 0}, {'total_cost': 0}), human_input=[])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dalle = DALLEAgent(name=\"Dalle\", llm_config={\"config_list\": config_list_dalle})\n",
    "\n",
    "user_proxy = UserProxyAgent(\n",
    "    name=\"User_proxy\", system_message=\"A human admin.\", human_input_mode=\"NEVER\", max_consecutive_auto_reply=0\n",
    ")\n",
    "\n",
    "# Ask the question with an image\n",
    "user_proxy.initiate_chat(\n",
    "    dalle,\n",
    "    message=\"\"\"Create an image with black background, a happy robot is showing a sign with \"I Love AutoGen\".\"\"\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c77ae209",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 209132 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "img = extract_img(dalle)\n",
    "\n",
    "plt.imshow(img)\n",
    "plt.axis(\"off\")  # Turn off axis numbers\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e59f055",
   "metadata": {},
   "source": [
    "## Example With Critics: Iterate several times to improve"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "72214592",
   "metadata": {},
   "outputs": [],
   "source": [
    "class DalleCreator(AssistantAgent):\n",
    "    def __init__(self, n_iters=2, **kwargs):\n",
    "        \"\"\"\n",
    "        Initializes a DalleCreator instance.\n",
    "\n",
    "        This agent facilitates the creation of visualizations through a collaborative effort among\n",
    "        its child agents: dalle and critics.\n",
    "\n",
    "        Parameters:\n",
    "            - n_iters (int, optional): The number of \"improvement\" iterations to run. Defaults to 2.\n",
    "            - **kwargs: keyword arguments for the parent AssistantAgent.\n",
    "        \"\"\"\n",
    "        super().__init__(**kwargs)\n",
    "        self.register_reply([Agent, None], reply_func=DalleCreator._reply_user, position=0)\n",
    "        self._n_iters = n_iters\n",
    "\n",
    "    def _reply_user(self, messages=None, sender=None, config=None):\n",
    "        if all((messages is None, sender is None)):\n",
    "            error_msg = f\"Either {messages=} or {sender=} must be provided.\"\n",
    "            logger.error(error_msg)  # noqa: F821\n",
    "            raise AssertionError(error_msg)\n",
    "\n",
    "        if messages is None:\n",
    "            messages = self._oai_messages[sender]\n",
    "\n",
    "        img_prompt = messages[-1][\"content\"]\n",
    "\n",
    "        ## Define the agents\n",
    "        self.critics = MultimodalConversableAgent(\n",
    "            name=\"Critics\",\n",
    "            system_message=\"\"\"You need to improve the prompt of the figures you saw.\n",
    "How to create a figure that is better in terms of color, shape, text (clarity), and other things.\n",
    "Reply with the following format:\n",
    "\n",
    "CRITICS: the image needs to improve...\n",
    "PROMPT: here is the updated prompt!\n",
    "\n",
    "\"\"\",\n",
    "            llm_config={\"config_list\": config_list_4v, \"max_tokens\": 1000},\n",
    "            human_input_mode=\"NEVER\",\n",
    "            max_consecutive_auto_reply=3,\n",
    "        )\n",
    "\n",
    "        self.dalle = DALLEAgent(\n",
    "            name=\"Dalle\", llm_config={\"config_list\": config_list_dalle}, max_consecutive_auto_reply=0\n",
    "        )\n",
    "\n",
    "        # Data flow begins\n",
    "        self.send(message=img_prompt, recipient=self.dalle, request_reply=True)\n",
    "        img = extract_img(self.dalle)\n",
    "        plt.imshow(img)\n",
    "        plt.axis(\"off\")  # Turn off axis numbers\n",
    "        plt.show()\n",
    "        print(\"Image PLOTTED\")\n",
    "\n",
    "        for i in range(self._n_iters):\n",
    "            # Downsample the image s.t. GPT-4V can take\n",
    "            img = extract_img(self.dalle)\n",
    "            smaller_image = img.resize((128, 128), Image.Resampling.LANCZOS)\n",
    "            smaller_image.save(\"result.png\")\n",
    "\n",
    "            self.msg_to_critics = f\"\"\"Here is the prompt: {img_prompt}.\n",
    "            Here is the figure <img result.png>.\n",
    "            Now, critic and create a prompt so that DALLE can give me a better image.\n",
    "            Show me both \"CRITICS\" and \"PROMPT\"!\n",
    "            \"\"\"\n",
    "            self.send(message=self.msg_to_critics, recipient=self.critics, request_reply=True)\n",
    "            feedback = self._oai_messages[self.critics][-1][\"content\"]\n",
    "            img_prompt = re.findall(\"PROMPT: (.*)\", feedback)[0]\n",
    "\n",
    "            self.send(message=img_prompt, recipient=self.dalle, request_reply=True)\n",
    "            img = extract_img(self.dalle)\n",
    "            plt.imshow(img)\n",
    "            plt.axis(\"off\")  # Turn off axis numbers\n",
    "            plt.show()\n",
    "            print(f\"Image {i} PLOTTED\")\n",
    "\n",
    "        return True, \"result.jpg\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d5883009",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33mUser\u001b[0m (to DALLE Creator!):\n",
      "\n",
      "Create an image with black background, a happy robot is showing a sign with \"I Love AutoGen\".\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mDALLE Creator!\u001b[0m (to Dalle):\n",
      "\n",
      "Create an image with black background, a happy robot is showing a sign with \"I Love AutoGen\".\n",
      "\n",
      "--------------------------------------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 209132 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Image PLOTTED\n",
      "\u001b[33mDALLE Creator!\u001b[0m (to Critics):\n",
      "\n",
      "Here is the prompt: Create an image with black background, a happy robot is showing a sign with \"I Love AutoGen\"..\n",
      "            Here is the figure <image>.\n",
      "            Now, critic and create a prompt so that DALLE can give me a better image.\n",
      "            Show me both \"CRITICS\" and \"PROMPT\"!\n",
      "            \n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mCritics\u001b[0m (to DALLE Creator!):\n",
      "\n",
      "CRITICS: The image needs to improve in the following aspects:\n",
      "\n",
      "1. Lighting: The robot and the sign could benefit from additional lighting to enhance details and textures, ensuring that they stand out more against the black background.\n",
      "2. Legibility: The text on the sign could be more prominent and the font size increased for better readability. Additionally, a contrasting color could be used for the text to ensure it pops against the background.\n",
      "3. Robot's Expression: While the robot appears happy, its expression could be made more apparent with clearer facial features or more exaggerated happiness indicators in its body language or facial features.\n",
      "4. Composition: The robot and the sign could be positioned in a way that creates a more dynamic composition, keeping the viewer’s eye engaged.\n",
      "5. Resolution: A higher resolution would make the image sharper, improving the overall quality and detail.\n",
      "\n",
      "PROMPT: Create a high-resolution image with a richly detailed, happy robot made of shiny metal, standing center frame against a stark black background. The robot is holding up a large, rectangular sign with rounded corners that reads \"I ❤️ AutoGen\" in bold, white sans-serif font, with the heart symbol in a vivid red color. The sign should be well-lit with a soft glow that highlights the text and makes it stand out. Ensure the robot's features clearly convey joy, perhaps through a broad smile and posture conveying enthusiasm. The composition should be balanced and visually appealing, with an intelligent use of space that guides the viewer's attention to the robot and the sign.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mDALLE Creator!\u001b[0m (to Dalle):\n",
      "\n",
      "Create a high-resolution image with a richly detailed, happy robot made of shiny metal, standing center frame against a stark black background. The robot is holding up a large, rectangular sign with rounded corners that reads \"I ❤️ AutoGen\" in bold, white sans-serif font, with the heart symbol in a vivid red color. The sign should be well-lit with a soft glow that highlights the text and makes it stand out. Ensure the robot's features clearly convey joy, perhaps through a broad smile and posture conveying enthusiasm. The composition should be balanced and visually appealing, with an intelligent use of space that guides the viewer's attention to the robot and the sign.\n",
      "\n",
      "--------------------------------------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 243352 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Image 0 PLOTTED\n",
      "\u001b[33mDALLE Creator!\u001b[0m (to Critics):\n",
      "\n",
      "Here is the prompt: Create a high-resolution image with a richly detailed, happy robot made of shiny metal, standing center frame against a stark black background. The robot is holding up a large, rectangular sign with rounded corners that reads \"I ❤️ AutoGen\" in bold, white sans-serif font, with the heart symbol in a vivid red color. The sign should be well-lit with a soft glow that highlights the text and makes it stand out. Ensure the robot's features clearly convey joy, perhaps through a broad smile and posture conveying enthusiasm. The composition should be balanced and visually appealing, with an intelligent use of space that guides the viewer's attention to the robot and the sign..\n",
      "            Here is the figure <image>.\n",
      "            Now, critic and create a prompt so that DALLE can give me a better image.\n",
      "            Show me both \"CRITICS\" and \"PROMPT\"!\n",
      "            \n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mCritics\u001b[0m (to DALLE Creator!):\n",
      "\n",
      "CRITICS: The image could be improved in the following ways:\n",
      "\n",
      "1. Color Contrast: The overall color contrast between the robot and the sign could be enhanced to make the elements more distinct from one another.\n",
      "2. Clarity and Details: The details of the robot's material and structure could be made sharper and more intricate to accentuate its shiny metal look.\n",
      "3. Sign's Design: The design of the sign could be simplified by using negative space more effectively, ensuring the message \"I ❤️ AutoGen\" is instantly recognizable and stands out more.\n",
      "4. Lighting and Shadows: The lighting could be diversified to cast subtle shadows, which would add depth and volume, making the image more three-dimensional.\n",
      "5. Emotion and Posture: The robot's expression and posture could be exaggerated further to emphasize its joyfulness and the message it is conveying.\n",
      "6. Background: While the background is appropriately black, adding a subtle texture or gradient could give the image more depth without distracting from the main subject.\n",
      "\n",
      "PROMPT: Generate a high-resolution 3D rendering of an exuberant, animated-style robot constructed from glossy, reflective metal surfaces. It stands in the center of a pure black background with a soft, radial gradient to provide subtle depth. The robot is displaying a sizable sign with prominent \"I ❤️ AutoGen\" lettering in a bold, white, sans-serif font, the heart being a luminous red, creating a stark, elegant contrast. Incorporate adequate lighting from multiple angles to cast dynamic, gentle shadows around the robot, enhancing its dimensional appearance. Ensure that the robot's facial features and stance radiate delight, featuring an exaggerated smile and arms raised in a victorious, welcoming gesture. The sign should be backlit with a soft halo effect, making it vibrant and eye-catching. The overall composition must be striking yet harmonious, drawing attention to both the robot’s delighted demeanor and the message it presents.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mDALLE Creator!\u001b[0m (to Dalle):\n",
      "\n",
      "Generate a high-resolution 3D rendering of an exuberant, animated-style robot constructed from glossy, reflective metal surfaces. It stands in the center of a pure black background with a soft, radial gradient to provide subtle depth. The robot is displaying a sizable sign with prominent \"I ❤️ AutoGen\" lettering in a bold, white, sans-serif font, the heart being a luminous red, creating a stark, elegant contrast. Incorporate adequate lighting from multiple angles to cast dynamic, gentle shadows around the robot, enhancing its dimensional appearance. Ensure that the robot's facial features and stance radiate delight, featuring an exaggerated smile and arms raised in a victorious, welcoming gesture. The sign should be backlit with a soft halo effect, making it vibrant and eye-catching. The overall composition must be striking yet harmonious, drawing attention to both the robot’s delighted demeanor and the message it presents.\n",
      "\n",
      "--------------------------------------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 235096 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Image 1 PLOTTED\n",
      "\u001b[33mDALLE Creator!\u001b[0m (to User):\n",
      "\n",
      "result.jpg\n",
      "\n",
      "--------------------------------------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "ChatResult(chat_id=None, chat_history=[{'content': 'Create an image with black background, a happy robot is showing a sign with \"I Love AutoGen\".', 'role': 'assistant'}, {'content': 'result.jpg', 'role': 'user'}], summary='result.jpg', cost=({'total_cost': 0}, {'total_cost': 0}), human_input=[])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "creator = DalleCreator(\n",
    "    name=\"DALLE Creator!\",\n",
    "    max_consecutive_auto_reply=0,\n",
    "    system_message=\"Help me coordinate generating image\",\n",
    "    llm_config=gpt4_llm_config,\n",
    ")\n",
    "\n",
    "user_proxy = UserProxyAgent(name=\"User\", human_input_mode=\"NEVER\", max_consecutive_auto_reply=0)\n",
    "\n",
    "user_proxy.initiate_chat(\n",
    "    creator, message=\"\"\"Create an image with black background, a happy robot is showing a sign with \"I Love AutoGen\".\"\"\"\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
