Chong-U Lim
commited on
Commit
•
97a1f80
1
Parent(s):
bb94125
Use invoke vs run
Browse files- notebook.ipynb +116 -20
notebook.ipynb
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
-
"execution_count":
|
6 |
"metadata": {},
|
7 |
"outputs": [],
|
8 |
"source": [
|
@@ -12,7 +12,7 @@
|
|
12 |
},
|
13 |
{
|
14 |
"cell_type": "code",
|
15 |
-
"execution_count":
|
16 |
"metadata": {
|
17 |
"notebookRunGroups": {
|
18 |
"groupValue": ""
|
@@ -28,7 +28,7 @@
|
|
28 |
},
|
29 |
{
|
30 |
"cell_type": "code",
|
31 |
-
"execution_count":
|
32 |
"metadata": {},
|
33 |
"outputs": [
|
34 |
{
|
@@ -37,7 +37,7 @@
|
|
37 |
"text": [
|
38 |
"\n",
|
39 |
"\n",
|
40 |
-
"
|
41 |
]
|
42 |
}
|
43 |
],
|
@@ -50,7 +50,7 @@
|
|
50 |
},
|
51 |
{
|
52 |
"cell_type": "code",
|
53 |
-
"execution_count":
|
54 |
"metadata": {},
|
55 |
"outputs": [
|
56 |
{
|
@@ -59,7 +59,7 @@
|
|
59 |
"'I want to open a restaurant for Mexican food. Suggest a fancy name for this.'"
|
60 |
]
|
61 |
},
|
62 |
-
"execution_count":
|
63 |
"metadata": {},
|
64 |
"output_type": "execute_result"
|
65 |
}
|
@@ -77,24 +77,17 @@
|
|
77 |
},
|
78 |
{
|
79 |
"cell_type": "code",
|
80 |
-
"execution_count":
|
81 |
"metadata": {},
|
82 |
"outputs": [
|
83 |
-
{
|
84 |
-
"name": "stderr",
|
85 |
-
"output_type": "stream",
|
86 |
-
"text": [
|
87 |
-
"/home/chong-u/mambaforge/envs/codebasics-langchain-crash-course/lib/python3.10/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `run` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.\n",
|
88 |
-
" warn_deprecated(\n"
|
89 |
-
]
|
90 |
-
},
|
91 |
{
|
92 |
"data": {
|
93 |
"text/plain": [
|
94 |
-
"'
|
|
|
95 |
]
|
96 |
},
|
97 |
-
"execution_count":
|
98 |
"metadata": {},
|
99 |
"output_type": "execute_result"
|
100 |
}
|
@@ -103,7 +96,7 @@
|
|
103 |
"from langchain.chains import LLMChain\n",
|
104 |
"\n",
|
105 |
"name_chain = LLMChain(llm=llm, prompt=prompt_template_name)\n",
|
106 |
-
"name_chain.
|
107 |
]
|
108 |
},
|
109 |
{
|
@@ -253,19 +246,122 @@
|
|
253 |
},
|
254 |
{
|
255 |
"cell_type": "code",
|
256 |
-
"execution_count":
|
257 |
"metadata": {},
|
258 |
"outputs": [],
|
259 |
"source": [
|
260 |
"tools = load_tools([\"wikipedia\", \"llm-math\"], llm=llm)\n",
|
261 |
"\n",
|
262 |
-
"initialize_agent(\n",
|
263 |
" tools,\n",
|
264 |
" llm,\n",
|
265 |
" agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
|
|
|
266 |
")"
|
267 |
]
|
268 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
269 |
{
|
270 |
"cell_type": "code",
|
271 |
"execution_count": null,
|
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
+
"execution_count": 28,
|
6 |
"metadata": {},
|
7 |
"outputs": [],
|
8 |
"source": [
|
|
|
12 |
},
|
13 |
{
|
14 |
"cell_type": "code",
|
15 |
+
"execution_count": 29,
|
16 |
"metadata": {
|
17 |
"notebookRunGroups": {
|
18 |
"groupValue": ""
|
|
|
28 |
},
|
29 |
{
|
30 |
"cell_type": "code",
|
31 |
+
"execution_count": 30,
|
32 |
"metadata": {},
|
33 |
"outputs": [
|
34 |
{
|
|
|
37 |
"text": [
|
38 |
"\n",
|
39 |
"\n",
|
40 |
+
"\"Dragon's Den Cuisine\"\n"
|
41 |
]
|
42 |
}
|
43 |
],
|
|
|
50 |
},
|
51 |
{
|
52 |
"cell_type": "code",
|
53 |
+
"execution_count": 31,
|
54 |
"metadata": {},
|
55 |
"outputs": [
|
56 |
{
|
|
|
59 |
"'I want to open a restaurant for Mexican food. Suggest a fancy name for this.'"
|
60 |
]
|
61 |
},
|
62 |
+
"execution_count": 31,
|
63 |
"metadata": {},
|
64 |
"output_type": "execute_result"
|
65 |
}
|
|
|
77 |
},
|
78 |
{
|
79 |
"cell_type": "code",
|
80 |
+
"execution_count": 35,
|
81 |
"metadata": {},
|
82 |
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
{
|
84 |
"data": {
|
85 |
"text/plain": [
|
86 |
+
"{'cuisine': 'Mexican',\n",
|
87 |
+
" 'text': '\\n\\n\"El Jardín de Sabores\" (The Garden of Flavors)'}"
|
88 |
]
|
89 |
},
|
90 |
+
"execution_count": 35,
|
91 |
"metadata": {},
|
92 |
"output_type": "execute_result"
|
93 |
}
|
|
|
96 |
"from langchain.chains import LLMChain\n",
|
97 |
"\n",
|
98 |
"name_chain = LLMChain(llm=llm, prompt=prompt_template_name)\n",
|
99 |
+
"name_chain.invoke({\"cuisine\":\"Mexican\"})"
|
100 |
]
|
101 |
},
|
102 |
{
|
|
|
246 |
},
|
247 |
{
|
248 |
"cell_type": "code",
|
249 |
+
"execution_count": 25,
|
250 |
"metadata": {},
|
251 |
"outputs": [],
|
252 |
"source": [
|
253 |
"tools = load_tools([\"wikipedia\", \"llm-math\"], llm=llm)\n",
|
254 |
"\n",
|
255 |
+
"agent = initialize_agent(\n",
|
256 |
" tools,\n",
|
257 |
" llm,\n",
|
258 |
" agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
|
259 |
+
" verbose=True\n",
|
260 |
")"
|
261 |
]
|
262 |
},
|
263 |
+
{
|
264 |
+
"cell_type": "code",
|
265 |
+
"execution_count": 26,
|
266 |
+
"metadata": {},
|
267 |
+
"outputs": [
|
268 |
+
{
|
269 |
+
"data": {
|
270 |
+
"text/plain": [
|
271 |
+
"'Elon Musk was born on June 28, 1971 and will be 52 years old in 2023.'"
|
272 |
+
]
|
273 |
+
},
|
274 |
+
"execution_count": 26,
|
275 |
+
"metadata": {},
|
276 |
+
"output_type": "execute_result"
|
277 |
+
}
|
278 |
+
],
|
279 |
+
"source": [
|
280 |
+
"agent.run(\"When was Elon Musk born? What is his age right now in 2023?\")"
|
281 |
+
]
|
282 |
+
},
|
283 |
+
{
|
284 |
+
"cell_type": "code",
|
285 |
+
"execution_count": 27,
|
286 |
+
"metadata": {},
|
287 |
+
"outputs": [
|
288 |
+
{
|
289 |
+
"name": "stdout",
|
290 |
+
"output_type": "stream",
|
291 |
+
"text": [
|
292 |
+
"\n",
|
293 |
+
"\n",
|
294 |
+
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
295 |
+
"\u001b[32;1m\u001b[1;3m First, I need to find out when Elon Musk was born\n",
|
296 |
+
"Action: wikipedia\n",
|
297 |
+
"Action Input: Elon Musk\u001b[0m\n",
|
298 |
+
"Observation: \u001b[36;1m\u001b[1;3mPage: Elon Musk\n",
|
299 |
+
"Summary: Elon Reeve Musk (; EE-lon; born June 28, 1971) is a businessman and investor. He is the founder, chairman, CEO, and CTO of SpaceX; angel investor, CEO, product architect, and former chairman of Tesla, Inc.; owner, executive chairman, and CTO of X Corp.; founder of the Boring Company and xAI; co-founder of Neuralink and OpenAI; and president of the Musk Foundation. He is one of the wealthiest people in the world, with an estimated net worth of US$213 billion as of February 2024, according to the Bloomberg Billionaires Index, and $210 billion according to Forbes, primarily from his ownership stakes in Tesla and SpaceX.A member of the wealthy South African Musk family, Elon was born in Pretoria and briefly attended the University of Pretoria before immigrating to Canada at age 18, acquiring citizenship through his Canadian-born mother. Two years later, he matriculated at Queen's University at Kingston in Canada. Musk later transferred to the University of Pennsylvania, and received bachelor's degrees in economics and physics. He moved to California in 1995 to attend Stanford University, but dropped out after two days and, with his brother Kimbal, co-founded online city guide software company Zip2. The startup was acquired by Compaq for $307 million in 1999, and that same year Musk co-founded X.com, a direct bank. X.com merged with Confinity in 2000 to form PayPal.\n",
|
300 |
+
"In October 2002, eBay acquired PayPal for $1.5 billion, and that same year, with $100 million of the money he made, Musk founded SpaceX, a spaceflight services company. In 2004, he became an early investor in electric vehicle manufacturer Tesla Motors, Inc. (now Tesla, Inc.). He became its chairman and product architect, assuming the position of CEO in 2008. In 2006, Musk helped create SolarCity, a solar-energy company that was acquired by Tesla in 2016 and became Tesla Energy. In 2013, he proposed a hyperloop high-speed vactrain transportation system. In 2015, he co-founded OpenAI, a nonprofit artificial intelligence research company. The following year, Musk co-founded Neuralink—a neurotechnology company developing brain–computer interfaces—and the Boring Company, a tunnel construction company. In 2022, he acquired Twitter for $44 billion. He subsequently merged the company into newly created X Corp. and rebranded the service as X the following year. In March 2023, he founded xAI, an artificial intelligence company.\n",
|
301 |
+
"Musk has expressed views that have made him a polarizing figure. He has been criticized for making unscientific and misleading statements, including COVID-19 misinformation and antisemitic conspiracy theories. His ownership of Twitter has been similarly controversial, being marked by the laying off of a large number of employees, an increase in hate speech and misinformation and disinformation on the website, as well as changes to Twitter Blue verification. In 2018, the U.S. Securities and Exchange Commission (SEC) sued him, alleging that he had falsely announced that he had secured funding for a private takeover of Tesla. To settle the case, Musk stepped down as the chairman of Tesla and paid a $20 million fine.\n",
|
302 |
+
"\n",
|
303 |
+
"\n",
|
304 |
+
"\n",
|
305 |
+
"Page: Twitter under Elon Musk\n",
|
306 |
+
"Summary: Elon Musk completed his acquisition of Twitter in October 2022; Musk acted as CEO of Twitter until June 2023 when he was succeeded by Linda Yaccarino. Twitter was then rebranded to X in July 2023. Initially during Musk's tenure, Twitter introduced a series of reforms and management changes; the company reinstated a number of previously banned accounts, reduced the workforce by approximately 80%, closed one of Twitter's three data centers, and largely eliminated the content moderation team, replacing it with the crowd-sourced fact-checking system Community Notes.\n",
|
307 |
+
"In November 2022, Twitter then began offering paid verification checkmarks, followed by removing legacy verification. In December, the Twitter Files were released and a number of journalists suspended from the platform. The\u001b[0m\n",
|
308 |
+
"Thought:\u001b[32;1m\u001b[1;3m Now I have the information about Elon Musk's Twitter acquisition and his controversial actions as Twitter's CEO.\n",
|
309 |
+
"Action: wikipedia\n",
|
310 |
+
"Action Input: Elon Musk\u001b[0m\n",
|
311 |
+
"Observation: \u001b[36;1m\u001b[1;3mPage: Elon Musk\n",
|
312 |
+
"Summary: Elon Reeve Musk (; EE-lon; born June 28, 1971) is a businessman and investor. He is the founder, chairman, CEO, and CTO of SpaceX; angel investor, CEO, product architect, and former chairman of Tesla, Inc.; owner, executive chairman, and CTO of X Corp.; founder of the Boring Company and xAI; co-founder of Neuralink and OpenAI; and president of the Musk Foundation. He is one of the wealthiest people in the world, with an estimated net worth of US$213 billion as of February 2024, according to the Bloomberg Billionaires Index, and $210 billion according to Forbes, primarily from his ownership stakes in Tesla and SpaceX.A member of the wealthy South African Musk family, Elon was born in Pretoria and briefly attended the University of Pretoria before immigrating to Canada at age 18, acquiring citizenship through his Canadian-born mother. Two years later, he matriculated at Queen's University at Kingston in Canada. Musk later transferred to the University of Pennsylvania, and received bachelor's degrees in economics and physics. He moved to California in 1995 to attend Stanford University, but dropped out after two days and, with his brother Kimbal, co-founded online city guide software company Zip2. The startup was acquired by Compaq for $307 million in 1999, and that same year Musk co-founded X.com, a direct bank. X.com merged with Confinity in 2000 to form PayPal.\n",
|
313 |
+
"In October 2002, eBay acquired PayPal for $1.5 billion, and that same year, with $100 million of the money he made, Musk founded SpaceX, a spaceflight services company. In 2004, he became an early investor in electric vehicle manufacturer Tesla Motors, Inc. (now Tesla, Inc.). He became its chairman and product architect, assuming the position of CEO in 2008. In 2006, Musk helped create SolarCity, a solar-energy company that was acquired by Tesla in 2016 and became Tesla Energy. In 2013, he proposed a hyperloop high-speed vactrain transportation system. In 2015, he co-founded OpenAI, a nonprofit artificial intelligence research company. The following year, Musk co-founded Neuralink—a neurotechnology company developing brain–computer interfaces—and the Boring Company, a tunnel construction company. In 2022, he acquired Twitter for $44 billion. He subsequently merged the company into newly created X Corp. and rebranded the service as X the following year. In March 2023, he founded xAI, an artificial intelligence company.\n",
|
314 |
+
"Musk has expressed views that have made him a polarizing figure. He has been criticized for making unscientific and misleading statements, including COVID-19 misinformation and antisemitic conspiracy theories. His ownership of Twitter has been similarly controversial, being marked by the laying off of a large number of employees, an increase in hate speech and misinformation and disinformation on the website, as well as changes to Twitter Blue verification. In 2018, the U.S. Securities and Exchange Commission (SEC) sued him, alleging that he had falsely announced that he had secured funding for a private takeover of Tesla. To settle the case, Musk stepped down as the chairman of Tesla and paid a $20 million fine.\n",
|
315 |
+
"\n",
|
316 |
+
"\n",
|
317 |
+
"\n",
|
318 |
+
"Page: Twitter under Elon Musk\n",
|
319 |
+
"Summary: Elon Musk completed his acquisition of Twitter in October 2022; Musk acted as CEO of Twitter until June 2023 when he was succeeded by Linda Yaccarino. Twitter was then rebranded to X in July 2023. Initially during Musk's tenure, Twitter introduced a series of reforms and management changes; the company reinstated a number of previously banned accounts, reduced the workforce by approximately 80%, closed one of Twitter's three data centers, and largely eliminated the content moderation team, replacing it with the crowd-sourced fact-checking system Community Notes.\n",
|
320 |
+
"In November 2022, Twitter then began offering paid verification checkmarks, followed by removing legacy verification. In December, the Twitter Files were released and a number of journalists suspended from the platform. The\u001b[0m\n",
|
321 |
+
"Thought:\u001b[32;1m\u001b[1;3m Now I have the information about Elon Musk's Twitter acquisition and his controversial actions as Twitter's CEO. I also know that he stepped down as chairman of Tesla in 2018.\n",
|
322 |
+
"Action: wikipedia\n",
|
323 |
+
"Action Input: Elon Musk\u001b[0m\n",
|
324 |
+
"Observation: \u001b[36;1m\u001b[1;3mPage: Elon Musk\n",
|
325 |
+
"Summary: Elon Reeve Musk (; EE-lon; born June 28, 1971) is a businessman and investor. He is the founder, chairman, CEO, and CTO of SpaceX; angel investor, CEO, product architect, and former chairman of Tesla, Inc.; owner, executive chairman, and CTO of X Corp.; founder of the Boring Company and xAI; co-founder of Neuralink and OpenAI; and president of the Musk Foundation. He is one of the wealthiest people in the world, with an estimated net worth of US$213 billion as of February 2024, according to the Bloomberg Billionaires Index, and $210 billion according to Forbes, primarily from his ownership stakes in Tesla and SpaceX.A member of the wealthy South African Musk family, Elon was born in Pretoria and briefly attended the University of Pretoria before immigrating to Canada at age 18, acquiring citizenship through his Canadian-born mother. Two years later, he matriculated at Queen's University at Kingston in Canada. Musk later transferred to the University of Pennsylvania, and received bachelor's degrees in economics and physics. He moved to California in 1995 to attend Stanford University, but dropped out after two days and, with his brother Kimbal, co-founded online city guide software company Zip2. The startup was acquired by Compaq for $307 million in 1999, and that same year Musk co-founded X.com, a direct bank. X.com merged with Confinity in 2000 to form PayPal.\n",
|
326 |
+
"In October 2002, eBay acquired PayPal for $1.5 billion, and that same year, with $100 million of the money he made, Musk founded SpaceX, a spaceflight services company. In 2004, he became an early investor in electric vehicle manufacturer Tesla Motors, Inc. (now Tesla, Inc.). He became its chairman and product architect, assuming the position of CEO in 2008. In 2006, Musk helped create SolarCity, a solar-energy company that was acquired by Tesla in 2016 and became Tesla Energy. In 2013, he proposed a hyperloop high-speed vactrain transportation system. In 2015, he co-founded OpenAI, a nonprofit artificial intelligence research company. The following year, Musk co-founded Neuralink—a neurotechnology company developing brain–computer interfaces—and the Boring Company, a tunnel construction company. In 2022, he acquired Twitter for $44 billion. He subsequently merged the company into newly created X Corp. and rebranded the service as X the following year. In March 2023, he founded xAI, an artificial intelligence company.\n",
|
327 |
+
"Musk has expressed views that have made him a polarizing figure. He has been criticized for making unscientific and misleading statements, including COVID-19 misinformation and antisemitic conspiracy theories. His ownership of Twitter has been similarly controversial, being marked by the laying off of a large number of employees, an increase in hate speech and misinformation and disinformation on the website, as well as changes to Twitter Blue verification. In 2018, the U.S. Securities and Exchange Commission (SEC) sued him, alleging that he had falsely announced that he had secured funding for a private takeover of Tesla. To settle the case, Musk stepped down as the chairman of Tesla and paid a $20 million fine.\n",
|
328 |
+
"\n",
|
329 |
+
"\n",
|
330 |
+
"\n",
|
331 |
+
"Page: Twitter under Elon Musk\n",
|
332 |
+
"Summary: Elon Musk completed his acquisition of Twitter in October 2022; Musk acted as CEO of Twitter until June 2023 when he was succeeded by Linda Yaccarino. Twitter was then rebranded to X in July 2023. Initially during Musk's tenure, Twitter introduced a series of reforms and management changes; the company reinstated a number of previously banned accounts, reduced the workforce by approximately 80%, closed one of Twitter's three data centers, and largely eliminated the content moderation team, replacing it with the crowd-sourced fact-checking system Community Notes.\n",
|
333 |
+
"In November 2022, Twitter then began offering paid verification checkmarks, followed by removing legacy verification. In December, the Twitter Files were released and a number of journalists suspended from the platform. The\u001b[0m\n",
|
334 |
+
"Thought:\u001b[32;1m\u001b[1;3m Now I have the information about Elon Musk's Twitter acquisition and his controversial actions as Twitter's CEO. I also know that he stepped down as chairman of Tesla in 2018. Now I can calculate his age in 2023.\n",
|
335 |
+
"Action: Calculator\n",
|
336 |
+
"Action Input: 2023 - 1971\u001b[0m\n",
|
337 |
+
"Observation: \u001b[33;1m\u001b[1;3mAnswer: 52\u001b[0m\n",
|
338 |
+
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
|
339 |
+
"Final Answer: Elon Musk will be 52 years old in 2023.\u001b[0m\n",
|
340 |
+
"\n",
|
341 |
+
"\u001b[1m> Finished chain.\u001b[0m\n"
|
342 |
+
]
|
343 |
+
},
|
344 |
+
{
|
345 |
+
"data": {
|
346 |
+
"text/plain": [
|
347 |
+
"'Elon Musk will be 52 years old in 2023.'"
|
348 |
+
]
|
349 |
+
},
|
350 |
+
"execution_count": 27,
|
351 |
+
"metadata": {},
|
352 |
+
"output_type": "execute_result"
|
353 |
+
}
|
354 |
+
],
|
355 |
+
"source": [
|
356 |
+
"agent = initialize_agent(\n",
|
357 |
+
" tools,\n",
|
358 |
+
" llm,\n",
|
359 |
+
" agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
|
360 |
+
" verbose=True\n",
|
361 |
+
")\n",
|
362 |
+
"agent.run(\"When was Elon Musk born? What is his age right now in 2023?\")"
|
363 |
+
]
|
364 |
+
},
|
365 |
{
|
366 |
"cell_type": "code",
|
367 |
"execution_count": null,
|