/ docs-website / versioned_docs / version-2.18 / pipeline-components / generators / dalleimagegenerator.mdx
dalleimagegenerator.mdx
1 --- 2 title: "DALLEImageGenerator" 3 id: dalleimagegenerator 4 slug: "/dalleimagegenerator" 5 description: "Generate images using OpenAI's DALL-E model." 6 --- 7 8 # DALLEImageGenerator 9 10 Generate images using OpenAI's DALL-E model. 11 12 | | | 13 | --- | --- | 14 | **Most common position in a pipeline** | After a [`PromptBuilder`](../builders/promptbuilder.mdx), flexible | 15 | **Mandatory init variables** | "api_key": An OpenAI API key. Can be set with `OPENAI_API_KEY` env var. | 16 | **Mandatory run variables** | “prompt”: A string containing the prompt for the model | 17 | **Output variables** | “images”: A list of generated images <br /> <br />”revised_prompt”: A string containing the prompt that was used to generate the image, if there was any revision to the prompt made by OpenAI | 18 | **API reference** | [Generators](/reference/generators-api) | 19 | **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/generators/openai_dalle.py | 20 21 ## Overview 22 23 The `DALLEImageGenerator` component generates images using OpenAI's DALL-E model. 24 25 By default, the component uses `dall-e-3` model, standard picture quality, and 1024x1024 resolution. You can change these parameters using `model` (during component initialization), `quality`, and `size` (during component initialization or run) parameters. 26 27 `DALLEImageGenerator` needs an OpenAI key to work. It uses an `OPENAI_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with `api_key`: 28 29 ``` 30 image_generator = DALLEImageGenerator(api_key=Secret.from_token("<your-api-key>")) 31 ``` 32 33 Check our [API reference](/reference/generators-api#dalleimagegenerator) for the detailed component parameters description, or the [OpenAI documentation](https://platform.openai.com/docs/api-reference/images/create) for the details on OpenAI API parameters. 34 35 ## Usage 36 37 ### On its own 38 39 ```python 40 from haystack.components.generators import DALLEImageGenerator 41 42 image_generator = DALLEImageGenerator() 43 response = image_generator.run("Show me a picture of a black cat.") 44 45 print(response) 46 ``` 47 48 ### In a pipeline 49 50 In the following pipeline, we first set up a `PromptBuilder` that will structure the image description with a detailed template describing various artistic elements. The pipeline then passes this structured prompt into a `DALLEImageGenerator` to generate the image based on this detailed description. 51 52 ```python 53 from haystack import Pipeline 54 from haystack.components.generators import DALLEImageGenerator 55 from haystack.components.builders import PromptBuilder 56 57 prompt_builder = PromptBuilder( 58 template="""Create a {style} image with the following details: 59 60 Main subject: {prompt} 61 Artistic style: {art_style} 62 Lighting: {lighting} 63 Color palette: {colors} 64 Composition: {composition} 65 Additional details: {details}""", 66 ) 67 68 image_generator = DALLEImageGenerator() 69 70 pipeline = Pipeline() 71 pipeline.add_component("prompt_builder", prompt_builder) 72 pipeline.add_component("image_generator", image_generator) 73 74 pipeline.connect("prompt_builder.prompt", "image_generator.prompt") 75 76 results = pipeline.run( 77 { 78 "prompt": "a mystical treehouse library", 79 "style": "photorealistic", 80 "art_style": "fantasy concept art with intricate details", 81 "lighting": "dusk with warm lantern light glowing from within", 82 "colors": "rich earth tones, deep greens, and golden accents", 83 "composition": "wide angle view showing the entire structure nestled in an ancient oak tree", 84 "details": "spiral staircases wrapping around branches, stained glass windows, floating books, and magical fireflies providing ambient illumination", 85 }, 86 ) 87 88 generated_images = results["image_generator"]["images"] 89 revised_prompt = results["image_generator"]["revised_prompt"] 90 91 print(f"Generated image URL: {generated_images[0]}") 92 print(f"Revised prompt: {revised_prompt}") 93 ```