Skip to content

Commit 4f9f97b

Browse files
aaronlaitnerccurme
andauthored
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 <[email protected]>
1 parent e935da0 commit 4f9f97b

File tree

1 file changed

+37
-49
lines changed

1 file changed

+37
-49
lines changed

docs/docs/integrations/tools/dalle_image_generator.ipynb

+37-49
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
{
2626
"cell_type": "code",
27-
"execution_count": 4,
27+
"execution_count": null,
2828
"metadata": {
2929
"id": "q-k8wmp0zquh"
3030
},
@@ -34,7 +34,7 @@
3434
"\n",
3535
"from langchain_openai import OpenAI\n",
3636
"\n",
37-
"os.environ[\"OPENAI_API_KEY\"] = \"<your-key-here>\""
37+
"os.environ[\"OPENAI_API_KEY\"] = \"insertapikey\""
3838
]
3939
},
4040
{
@@ -47,7 +47,7 @@
4747
},
4848
{
4949
"cell_type": "code",
50-
"execution_count": 2,
50+
"execution_count": null,
5151
"metadata": {},
5252
"outputs": [],
5353
"source": [
@@ -75,20 +75,9 @@
7575
},
7676
{
7777
"cell_type": "code",
78-
"execution_count": 4,
78+
"execution_count": null,
7979
"metadata": {},
80-
"outputs": [
81-
{
82-
"data": {
83-
"text/plain": [
84-
"'https://oaidalleapiprodscus.blob.core.windows.net/private/org-i0zjYONU3PemzJ222esBaAzZ/user-f6uEIOFxoiUZivy567cDSWni/img-i7Z2ZxvJ4IbbdAiO6OXJgS3v.png?st=2023-08-11T14%3A03%3A14Z&se=2023-08-11T16%3A03%3A14Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-08-10T20%3A58%3A32Z&ske=2023-08-11T20%3A58%3A32Z&sks=b&skv=2021-08-06&sig=/sECe7C0EAq37ssgBm7g7JkVIM/Q1W3xOstd0Go6slA%3D'"
85-
]
86-
},
87-
"execution_count": 4,
88-
"metadata": {},
89-
"output_type": "execute_result"
90-
}
91-
],
80+
"outputs": [],
9281
"source": [
9382
"image_url"
9483
]
@@ -135,34 +124,38 @@
135124
},
136125
{
137126
"cell_type": "code",
138-
"execution_count": 9,
127+
"execution_count": null,
139128
"metadata": {},
140-
"outputs": [
141-
{
142-
"name": "stdout",
143-
"output_type": "stream",
144-
"text": [
145-
"\n",
146-
"\n",
147-
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
148-
"\u001b[32;1m\u001b[1;3m What is the best way to turn this description into an image?\n",
149-
"Action: Dall-E Image Generator\n",
150-
"Action Input: A spooky Halloween night at a haunted museum\u001b[0mhttps://oaidalleapiprodscus.blob.core.windows.net/private/org-rocrupyvzgcl4yf25rqq6d1v/user-WsxrbKyP2c8rfhCKWDyMfe8N/img-ogKfqxxOS5KWVSj4gYySR6FY.png?st=2023-01-31T07%3A38%3A25Z&se=2023-01-31T09%3A38%3A25Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-01-30T22%3A19%3A36Z&ske=2023-01-31T22%3A19%3A36Z&sks=b&skv=2021-08-06&sig=XsomxxBfu2CP78SzR9lrWUlbask4wBNnaMsHamy4VvU%3D\n",
151-
"\n",
152-
"Observation: \u001b[36;1m\u001b[1;3mhttps://oaidalleapiprodscus.blob.core.windows.net/private/org-rocrupyvzgcl4yf25rqq6d1v/user-WsxrbKyP2c8rfhCKWDyMfe8N/img-ogKfqxxOS5KWVSj4gYySR6FY.png?st=2023-01-31T07%3A38%3A25Z&se=2023-01-31T09%3A38%3A25Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-01-30T22%3A19%3A36Z&ske=2023-01-31T22%3A19%3A36Z&sks=b&skv=2021-08-06&sig=XsomxxBfu2CP78SzR9lrWUlbask4wBNnaMsHamy4VvU%3D\u001b[0m\n",
153-
"Thought:\u001b[32;1m\u001b[1;3m With the image generated, I can now make my final answer.\n",
154-
"Final Answer: An image of a Halloween night at a haunted museum can be seen here: https://oaidalleapiprodscus.blob.core.windows.net/private/org-rocrupyvzgcl4yf25rqq6d1v/user-WsxrbKyP2c8rfhCKWDyMfe8N/img-ogKfqxxOS5KWVSj4gYySR6FY.png?st=2023-01-31T07%3A38%3A25Z&se=2023-01-31T09%3A38%3A25Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-01-30T22\u001b[0m\n",
155-
"\n",
156-
"\u001b[1m> Finished chain.\u001b[0m\n"
157-
]
158-
}
159-
],
129+
"outputs": [],
160130
"source": [
161-
"from langchain.agents import initialize_agent, load_tools\n",
131+
"from langchain_community.tools.openai_dalle_image_generation import (\n",
132+
" OpenAIDALLEImageGenerationTool,\n",
133+
")\n",
134+
"from langchain_community.utilities.dalle_image_generator import DallEAPIWrapper\n",
135+
"from langchain_openai import ChatOpenAI\n",
136+
"from langgraph.prebuilt import create_react_agent\n",
162137
"\n",
163-
"tools = load_tools([\"dalle-image-generator\"])\n",
164-
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)\n",
165-
"output = agent.run(\"Create an image of a halloween night at a haunted museum\")"
138+
"llm = ChatOpenAI(model=\"gpt-3.5-turbo\", temperature=0)\n",
139+
"api_wrapper = DallEAPIWrapper()\n",
140+
"dalle_tool = OpenAIDALLEImageGenerationTool(api_wrapper=api_wrapper)\n",
141+
"\n",
142+
"tools = [dalle_tool]\n",
143+
"\n",
144+
"agent = create_react_agent(llm, tools, debug=True)\n",
145+
"\n",
146+
"# User prompt\n",
147+
"prompt = \"Create an image of a halloween night at a haunted museum\"\n",
148+
"\n",
149+
"messages = [\n",
150+
" # \"role\": \"user\" Indicates message is coming from user\n",
151+
" # \"content\": prompt is where the user's input is placed\n",
152+
" {\"role\": \"user\", \"content\": prompt}\n",
153+
"]\n",
154+
"\n",
155+
"# Sending the message to be processed and adjusted by ChatGPT, after which is sent through DALL-E\n",
156+
"response = agent.invoke({\"messages\": messages})\n",
157+
"\n",
158+
"print(response)"
166159
]
167160
}
168161
],
@@ -171,9 +164,9 @@
171164
"provenance": []
172165
},
173166
"kernelspec": {
174-
"display_name": "Python 3 (ipykernel)",
167+
"display_name": "Python (langchain-env)",
175168
"language": "python",
176-
"name": "python3"
169+
"name": "langchain-env"
177170
},
178171
"language_info": {
179172
"codemirror_mode": {
@@ -185,12 +178,7 @@
185178
"name": "python",
186179
"nbconvert_exporter": "python",
187180
"pygments_lexer": "ipython3",
188-
"version": "3.10.12"
189-
},
190-
"vscode": {
191-
"interpreter": {
192-
"hash": "3570c8892273ffbeee7ead61dc7c022b73551d9f55fb2584ac0e8e8920b18a89"
193-
}
181+
"version": "3.12.9"
194182
}
195183
},
196184
"nbformat": 4,

0 commit comments

Comments
 (0)