Files
langchain/docs/docs/integrations/tools/dalle_image_generator.ipynb
aaronlaitner 4f9f97bd12 docs: replaced initialize_agent with create_react_agent in dalle_image_generator.ipynb (#30697)
## Description:

Replaced deprecated 'initialize_agent' with 'create_react_agent' in
dalle_image_generator.ipynb
## Issue:
#29277

## Dependencies:
None

## Twitter handle:
@Thatopman

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
2025-04-07 13:33:52 +00:00

187 lines
5.0 KiB
Plaintext

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Dall-E Image Generator\n",
"\n",
">[OpenAI Dall-E](https://openai.com/dall-e-3) are text-to-image models developed by `OpenAI` using deep learning methodologies to generate digital images from natural language descriptions, called \"prompts\".\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Needed if you would like to display images in the notebook\n",
"%pip install --upgrade --quiet opencv-python scikit-image langchain-community"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "q-k8wmp0zquh"
},
"outputs": [],
"source": [
"import os\n",
"\n",
"from langchain_openai import OpenAI\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"insertapikey\""
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Run as a chain"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains import LLMChain\n",
"from langchain_community.utilities.dalle_image_generator import DallEAPIWrapper\n",
"from langchain_core.prompts import PromptTemplate\n",
"from langchain_openai import OpenAI\n",
"\n",
"llm = OpenAI(temperature=0.9)\n",
"prompt = PromptTemplate(\n",
" input_variables=[\"image_desc\"],\n",
" template=\"Generate a detailed prompt to generate an image based on the following description: {image_desc}\",\n",
")\n",
"chain = LLMChain(llm=llm, prompt=prompt)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"image_url = DallEAPIWrapper().run(chain.run(\"halloween night at a haunted museum\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"image_url"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# You can click on the link above to display the image\n",
"# Or you can try the options below to display the image inline in this notebook\n",
"\n",
"try:\n",
" import google.colab\n",
"\n",
" IN_COLAB = True\n",
"except ImportError:\n",
" IN_COLAB = False\n",
"\n",
"if IN_COLAB:\n",
" from google.colab.patches import cv2_imshow # for image display\n",
" from skimage import io\n",
"\n",
" image = io.imread(image_url)\n",
" cv2_imshow(image)\n",
"else:\n",
" import cv2\n",
" from skimage import io\n",
"\n",
" image = io.imread(image_url)\n",
" cv2.imshow(\"image\", image)\n",
" cv2.waitKey(0) # wait for a keyboard input\n",
" cv2.destroyAllWindows()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Run as a tool with an agent"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.tools.openai_dalle_image_generation import (\n",
" OpenAIDALLEImageGenerationTool,\n",
")\n",
"from langchain_community.utilities.dalle_image_generator import DallEAPIWrapper\n",
"from langchain_openai import ChatOpenAI\n",
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"llm = ChatOpenAI(model=\"gpt-3.5-turbo\", temperature=0)\n",
"api_wrapper = DallEAPIWrapper()\n",
"dalle_tool = OpenAIDALLEImageGenerationTool(api_wrapper=api_wrapper)\n",
"\n",
"tools = [dalle_tool]\n",
"\n",
"agent = create_react_agent(llm, tools, debug=True)\n",
"\n",
"# User prompt\n",
"prompt = \"Create an image of a halloween night at a haunted museum\"\n",
"\n",
"messages = [\n",
" # \"role\": \"user\" Indicates message is coming from user\n",
" # \"content\": prompt is where the user's input is placed\n",
" {\"role\": \"user\", \"content\": prompt}\n",
"]\n",
"\n",
"# Sending the message to be processed and adjusted by ChatGPT, after which is sent through DALL-E\n",
"response = agent.invoke({\"messages\": messages})\n",
"\n",
"print(response)"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python (langchain-env)",
"language": "python",
"name": "langchain-env"
},
"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.12.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}