/ docs-website / versioned_docs / version-2.20 / 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 <div className="key-value-table"> 13 14 | | | 15 | --- | --- | 16 | **Most common position in a pipeline** | After a [`PromptBuilder`](../builders/promptbuilder.mdx), flexible | 17 | **Mandatory init variables** | `api_key`: An OpenAI API key. Can be set with `OPENAI_API_KEY` env var. | 18 | **Mandatory run variables** | `prompt`: A string containing the prompt for the model | 19 | **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 | 20 | **API reference** | [Generators](/reference/generators-api) | 21 | **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/generators/openai_dalle.py | 22 23 </div> 24 25 ## Overview 26 27 The `DALLEImageGenerator` component generates images using OpenAI's DALL-E model. 28 29 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. 30 31 `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`: 32 33 ``` 34 image_generator = DALLEImageGenerator(api_key=Secret.from_token("<your-api-key>")) 35 ``` 36 37 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. 38 39 ## Usage 40 41 ### On its own 42 43 ```python 44 from haystack.components.generators import DALLEImageGenerator 45 46 image_generator = DALLEImageGenerator() 47 response = image_generator.run("Show me a picture of a black cat.") 48 49 print(response) 50 ``` 51 52 ### In a pipeline 53 54 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. 55 56 ```python 57 from haystack import Pipeline 58 from haystack.components.generators import DALLEImageGenerator 59 from haystack.components.builders import PromptBuilder 60 61 prompt_builder = PromptBuilder( 62 template="""Create a {style} image with the following details: 63 64 Main subject: {prompt} 65 Artistic style: {art_style} 66 Lighting: {lighting} 67 Color palette: {colors} 68 Composition: {composition} 69 Additional details: {details}""", 70 ) 71 72 image_generator = DALLEImageGenerator() 73 74 pipeline = Pipeline() 75 pipeline.add_component("prompt_builder", prompt_builder) 76 pipeline.add_component("image_generator", image_generator) 77 78 pipeline.connect("prompt_builder.prompt", "image_generator.prompt") 79 80 results = pipeline.run( 81 { 82 "prompt": "a mystical treehouse library", 83 "style": "photorealistic", 84 "art_style": "fantasy concept art with intricate details", 85 "lighting": "dusk with warm lantern light glowing from within", 86 "colors": "rich earth tones, deep greens, and golden accents", 87 "composition": "wide angle view showing the entire structure nestled in an ancient oak tree", 88 "details": "spiral staircases wrapping around branches, stained glass windows, floating books, and magical fireflies providing ambient illumination", 89 }, 90 ) 91 92 generated_images = results["image_generator"]["images"] 93 revised_prompt = results["image_generator"]["revised_prompt"] 94 95 print(f"Generated image URL: {generated_images[0]}") 96 print(f"Revised prompt: {revised_prompt}") 97 ```