Dall-E Image Generator
OpenAI Dall-E are text-to-image models developed by
OpenAI
using deep learning methodologies to generate digital images from natural language descriptions, called "prompts".
This notebook shows how you can generate images from a prompt synthesized using an OpenAI LLM. The images are generated using Dall-E
, which uses the same OpenAI API key as the LLM.
# Needed if you would like to display images in the notebook
%pip install --upgrade --quiet opencv-python scikit-image langchain-community
import os
from langchain_openai import OpenAI
os.environ["OPENAI_API_KEY"] = "insertapikey"
API Reference:OpenAI
Run as a chainโ
from langchain.chains import LLMChain
from langchain_community.utilities.dalle_image_generator import DallEAPIWrapper
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI
llm = OpenAI(temperature=0.9)
prompt = PromptTemplate(
input_variables=["image_desc"],
template="Generate a detailed prompt to generate an image based on the following description: {image_desc}",
)
chain = LLMChain(llm=llm, prompt=prompt)
image_url = DallEAPIWrapper().run(chain.run("halloween night at a haunted museum"))
image_url
# You can click on the link above to display the image
# Or you can try the options below to display the image inline in this notebook
try:
import google.colab
IN_COLAB = True
except ImportError:
IN_COLAB = False
if IN_COLAB:
from google.colab.patches import cv2_imshow # for image display
from skimage import io
image = io.imread(image_url)
cv2_imshow(image)
else:
import cv2
from skimage import io
image = io.imread(image_url)
cv2.imshow("image", image)
cv2.waitKey(0) # wait for a keyboard input
cv2.destroyAllWindows()
Run as a tool with an agentโ
from langchain_community.tools.openai_dalle_image_generation import OpenAIDALLEImageGenerationTool
from langchain_community.utilities.dalle_image_generator import DallEAPIWrapper
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
api_wrapper = DallEAPIWrapper()
dalle_tool = OpenAIDALLEImageGenerationTool(api_wrapper=api_wrapper)
tools = [dalle_tool]
agent = create_react_agent(llm, tools, debug=True)
# User prompt
prompt = "Create an image of a halloween night at a haunted museum"
messages = [
# "role": "user" Indicates message is coming from user
# "content": prompt is where the user's input is placed
{"role": "user", "content": prompt}
]
# Sending the message to be processed and adjusted by ChatGPT, after which is sent through DALL-E
response = agent.invoke({"messages": messages})
print(response)
[36;1m[1;3m[-1:checkpoint][0m [1mState at the end of step -1:
[0m{'messages': []}
[36;1m[1;3m[0:tasks][0m [1mStarting 1 task for step 0:
[0m- [32;1m[1;3m__start__[0m -> {'messages': [{'content': 'Create an image of a halloween night at a haunted '
'museum',
'role': 'user'}]}
[36;1m[1;3m[0:writes][0m [1mFinished step 0 with writes to 1 channel:
[0m- [33;1m[1;3mmessages[0m -> [{'content': 'Create an image of a halloween night at a haunted museum',
'role': 'user'}]
[36;1m[1;3m[0:checkpoint][0m [1mState at the end of step 0:
[0m{'messages': [HumanMessage(content='Create an image of a halloween night at a haunted museum', additional_kwargs={}, response_metadata={}, id='e2f2afe1-d2de-4a8f-be61-b9a52ef3b578')]}
[36;1m[1;3m[1:tasks][0m [1mStarting 1 task for step 1:
[0m- [32;1m[1;3magent[0m -> {'is_last_step': False,
'messages': [HumanMessage(content='Create an image of a halloween night at a haunted museum', additional_kwargs={}, response_metadata={}, id='e2f2afe1-d2de-4a8f-be61-b9a52ef3b578')],
'remaining_steps': 24}
[36;1m[1;3m[1:writes][0m [1mFinished step 1 with writes to 1 channel:
[0m- [33;1m[1;3mmessages[0m -> [AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_x4a8NCh0YlsURdUlsRrzHwCk', 'function': {'arguments': '{"query":"A spooky Halloween night at a haunted museum with eerie lighting, ghostly figures, ancient artifacts, and mysterious shadows."}', 'name': 'openai_dalle'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 39, 'prompt_tokens': 95, 'total_tokens': 134, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'id': 'chatcmpl-BJLyJVIp5hGgL2wCd2tUsokYUp1j6', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-ae10a1a7-2025-4109-a12e-773f1a924ed4-0', tool_calls=[{'name': 'openai_dalle', 'args': {'query': 'A spooky Halloween night at a haunted museum with eerie lighting, ghostly figures, ancient artifacts, and mysterious shadows.'}, 'id': 'call_x4a8NCh0YlsURdUlsRrzHwCk', 'type': 'tool_call'}], usage_metadata={'input_tokens': 95, 'output_tokens': 39, 'total_tokens': 134, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]
[36;1m[1;3m[1:checkpoint][0m [1mState at the end of step 1:
[0m{'messages': [HumanMessage(content='Create an image of a halloween night at a haunted museum', additional_kwargs={}, response_metadata={}, id='e2f2afe1-d2de-4a8f-be61-b9a52ef3b578'),
AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_x4a8NCh0YlsURdUlsRrzHwCk', 'function': {'arguments': '{"query":"A spooky Halloween night at a haunted museum with eerie lighting, ghostly figures, ancient artifacts, and mysterious shadows."}', 'name': 'openai_dalle'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 39, 'prompt_tokens': 95, 'total_tokens': 134, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'id': 'chatcmpl-BJLyJVIp5hGgL2wCd2tUsokYUp1j6', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-ae10a1a7-2025-4109-a12e-773f1a924ed4-0', tool_calls=[{'name': 'openai_dalle', 'args': {'query': 'A spooky Halloween night at a haunted museum with eerie lighting, ghostly figures, ancient artifacts, and mysterious shadows.'}, 'id': 'call_x4a8NCh0YlsURdUlsRrzHwCk', 'type': 'tool_call'}], usage_metadata={'input_tokens': 95, 'output_tokens': 39, 'total_tokens': 134, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]}
[36;1m[1;3m[2:tasks][0m [1mStarting 1 task for step 2:
[0m- [32;1m[1;3mtools[0m -> {'is_last_step': False,
'messages': [HumanMessage(content='Create an image of a halloween night at a haunted museum', additional_kwargs={}, response_metadata={}, id='e2f2afe1-d2de-4a8f-be61-b9a52ef3b578'),
AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_x4a8NCh0YlsURdUlsRrzHwCk', 'function': {'arguments': '{"query":"A spooky Halloween night at a haunted museum with eerie lighting, ghostly figures, ancient artifacts, and mysterious shadows."}', 'name': 'openai_dalle'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 39, 'prompt_tokens': 95, 'total_tokens': 134, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'id': 'chatcmpl-BJLyJVIp5hGgL2wCd2tUsokYUp1j6', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-ae10a1a7-2025-4109-a12e-773f1a924ed4-0', tool_calls=[{'name': 'openai_dalle', 'args': {'query': 'A spooky Halloween night at a haunted museum with eerie lighting, ghostly figures, ancient artifacts, and mysterious shadows.'}, 'id': 'call_x4a8NCh0YlsURdUlsRrzHwCk', 'type': 'tool_call'}], usage_metadata={'input_tokens': 95, 'output_tokens': 39, 'total_tokens': 134, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})],
'remaining_steps': 23}
[36;1m[1;3m[2:writes][0m [1mFinished step 2 with writes to 1 channel:
[0m- [33;1m[1;3mmessages[0m -> [ToolMessage(content='https://oaidalleapiprodscus.blob.core.windows.net/private/org-T46Yai80eL4AIF0AxfsmEFbU/user-Tui1z3t5wiuCXwu6Vt2dQOET/img-9AO6mE65EoBEzMdJkYVVvUv6.png?st=2025-04-06T14%3A13%3A23Z&se=2025-04-06T16%3A13%3A23Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2025-04-06T01%3A43%3A21Z&ske=2025-04-07T01%3A43%3A21Z&sks=b&skv=2024-08-04&sig=WZn%2BL6jImFMzkIRI2SVRTJq2hESEaJY26EHKjF/EdFk%3D', name='openai_dalle', tool_call_id='call_x4a8NCh0YlsURdUlsRrzHwCk')]
[36;1m[1;3m[2:checkpoint][0m [1mState at the end of step 2:
[0m{'messages': [HumanMessage(content='Create an image of a halloween night at a haunted museum', additional_kwargs={}, response_metadata={}, id='e2f2afe1-d2de-4a8f-be61-b9a52ef3b578'),
AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_x4a8NCh0YlsURdUlsRrzHwCk', 'function': {'arguments': '{"query":"A spooky Halloween night at a haunted museum with eerie lighting, ghostly figures, ancient artifacts, and mysterious shadows."}', 'name': 'openai_dalle'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 39, 'prompt_tokens': 95, 'total_tokens': 134, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'id': 'chatcmpl-BJLyJVIp5hGgL2wCd2tUsokYUp1j6', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-ae10a1a7-2025-4109-a12e-773f1a924ed4-0', tool_calls=[{'name': 'openai_dalle', 'args': {'query': 'A spooky Halloween night at a haunted museum with eerie lighting, ghostly figures, ancient artifacts, and mysterious shadows.'}, 'id': 'call_x4a8NCh0YlsURdUlsRrzHwCk', 'type': 'tool_call'}], usage_metadata={'input_tokens': 95, 'output_tokens': 39, 'total_tokens': 134, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}}),
ToolMessage(content='https://oaidalleapiprodscus.blob.core.windows.net/private/org-T46Yai80eL4AIF0AxfsmEFbU/user-Tui1z3t5wiuCXwu6Vt2dQOET/img-9AO6mE65EoBEzMdJkYVVvUv6.png?st=2025-04-06T14%3A13%3A23Z&se=2025-04-06T16%3A13%3A23Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2025-04-06T01%3A43%3A21Z&ske=2025-04-07T01%3A43%3A21Z&sks=b&skv=2024-08-04&sig=WZn%2BL6jImFMzkIRI2SVRTJq2hESEaJY26EHKjF/EdFk%3D', name='openai_dalle', id='1db11c93-4c9c-4cf9-97eb-2c2cd3250f1b', tool_call_id='call_x4a8NCh0YlsURdUlsRrzHwCk')]}
[36;1m[1;3m[3:tasks][0m [1mStarting 1 task for step 3:
[0m- [32;1m[1;3magent[0m -> {'is_last_step': False,
'messages': [HumanMessage(content='Create an image of a halloween night at a haunted museum', additional_kwargs={}, response_metadata={}, id='e2f2afe1-d2de-4a8f-be61-b9a52ef3b578'),
AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_x4a8NCh0YlsURdUlsRrzHwCk', 'function': {'arguments': '{"query":"A spooky Halloween night at a haunted museum with eerie lighting, ghostly figures, ancient artifacts, and mysterious shadows."}', 'name': 'openai_dalle'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 39, 'prompt_tokens': 95, 'total_tokens': 134, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'id': 'chatcmpl-BJLyJVIp5hGgL2wCd2tUsokYUp1j6', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-ae10a1a7-2025-4109-a12e-773f1a924ed4-0', tool_calls=[{'name': 'openai_dalle', 'args': {'query': 'A spooky Halloween night at a haunted museum with eerie lighting, ghostly figures, ancient artifacts, and mysterious shadows.'}, 'id': 'call_x4a8NCh0YlsURdUlsRrzHwCk', 'type': 'tool_call'}], usage_metadata={'input_tokens': 95, 'output_tokens': 39, 'total_tokens': 134, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}}),
ToolMessage(content='https://oaidalleapiprodscus.blob.core.windows.net/private/org-T46Yai80eL4AIF0AxfsmEFbU/user-Tui1z3t5wiuCXwu6Vt2dQOET/img-9AO6mE65EoBEzMdJkYVVvUv6.png?st=2025-04-06T14%3A13%3A23Z&se=2025-04-06T16%3A13%3A23Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2025-04-06T01%3A43%3A21Z&ske=2025-04-07T01%3A43%3A21Z&sks=b&skv=2024-08-04&sig=WZn%2BL6jImFMzkIRI2SVRTJq2hESEaJY26EHKjF/EdFk%3D', name='openai_dalle', id='1db11c93-4c9c-4cf9-97eb-2c2cd3250f1b', tool_call_id='call_x4a8NCh0YlsURdUlsRrzHwCk')],
'remaining_steps': 22}
[36;1m[1;3m[3:writes][0m [1mFinished step 3 with writes to 1 channel:
[0m- [33;1m[1;3mmessages[0m -> [AIMessage(content='Here is the image of a spooky Halloween night at a haunted museum with eerie lighting, ghostly figures, ancient artifacts, and mysterious shadows.', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 30, 'prompt_tokens': 427, 'total_tokens': 457, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'id': 'chatcmpl-BJLyVIpZ3bPiNVKXU2R8FLRshMRZn', 'finish_reason': 'stop', 'logprobs': None}, id='run-32e479d4-d67f-4f7c-a6d8-17462799067e-0', usage_metadata={'input_tokens': 427, 'output_tokens': 30, 'total_tokens': 457, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]
[36;1m[1;3m[3:checkpoint][0m [1mState at the end of step 3:
[0m{'messages': [HumanMessage(content='Create an image of a halloween night at a haunted museum', additional_kwargs={}, response_metadata={}, id='e2f2afe1-d2de-4a8f-be61-b9a52ef3b578'),
AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_x4a8NCh0YlsURdUlsRrzHwCk', 'function': {'arguments': '{"query":"A spooky Halloween night at a haunted museum with eerie lighting, ghostly figures, ancient artifacts, and mysterious shadows."}', 'name': 'openai_dalle'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 39, 'prompt_tokens': 95, 'total_tokens': 134, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'id': 'chatcmpl-BJLyJVIp5hGgL2wCd2tUsokYUp1j6', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-ae10a1a7-2025-4109-a12e-773f1a924ed4-0', tool_calls=[{'name': 'openai_dalle', 'args': {'query': 'A spooky Halloween night at a haunted museum with eerie lighting, ghostly figures, ancient artifacts, and mysterious shadows.'}, 'id': 'call_x4a8NCh0YlsURdUlsRrzHwCk', 'type': 'tool_call'}], usage_metadata={'input_tokens': 95, 'output_tokens': 39, 'total_tokens': 134, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}}),
ToolMessage(content='https://oaidalleapiprodscus.blob.core.windows.net/private/org-T46Yai80eL4AIF0AxfsmEFbU/user-Tui1z3t5wiuCXwu6Vt2dQOET/img-9AO6mE65EoBEzMdJkYVVvUv6.png?st=2025-04-06T14%3A13%3A23Z&se=2025-04-06T16%3A13%3A23Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2025-04-06T01%3A43%3A21Z&ske=2025-04-07T01%3A43%3A21Z&sks=b&skv=2024-08-04&sig=WZn%2BL6jImFMzkIRI2SVRTJq2hESEaJY26EHKjF/EdFk%3D', name='openai_dalle', id='1db11c93-4c9c-4cf9-97eb-2c2cd3250f1b', tool_call_id='call_x4a8NCh0YlsURdUlsRrzHwCk'),
AIMessage(content='Here is the image of a spooky Halloween night at a haunted museum with eerie lighting, ghostly figures, ancient artifacts, and mysterious shadows.', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 30, 'prompt_tokens': 427, 'total_tokens': 457, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'id': 'chatcmpl-BJLyVIpZ3bPiNVKXU2R8FLRshMRZn', 'finish_reason': 'stop', 'logprobs': None}, id='run-32e479d4-d67f-4f7c-a6d8-17462799067e-0', usage_metadata={'input_tokens': 427, 'output_tokens': 30, 'total_tokens': 457, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]}
Relatedโ
- Tool conceptual guide
- Tool how-to guides