File size: 26,113 Bytes
cfd3735
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "14f8b67b",
   "metadata": {},
   "source": [
    "## AutoGPT example finding Winning Marathon Times\n",
    "\n",
    "* Implementation of https://github.com/Significant-Gravitas/Auto-GPT \n",
    "* With LangChain primitives (LLMs, PromptTemplates, VectorStores, Embeddings, Tools)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ef972313-c05a-4c49-8fd1-03e599e21033",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# !pip install bs4\n",
    "# !pip install nest_asyncio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1cff42fd",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# General \n",
    "import os\n",
    "import pandas as pd\n",
    "from langchain.experimental.autonomous_agents.autogpt.agent import AutoGPT\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "\n",
    "from langchain.agents.agent_toolkits.pandas.base import create_pandas_dataframe_agent\n",
    "from langchain.docstore.document import Document\n",
    "import asyncio\n",
    "import nest_asyncio\n",
    "\n",
    "\n",
    "# Needed synce jupyter runs an async eventloop\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "01283ac7-1da0-41ba-8011-bd455d21dd82",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "llm = ChatOpenAI(model_name=\"gpt-4\", temperature=1.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "192496a7",
   "metadata": {},
   "source": [
    "### Set up tools\n",
    "\n",
    "* We'll set up an AutoGPT with a `search` tool, and `write-file` tool, and a `read-file` tool, a web browsing tool, and a tool to interact with a CSV file via a python REPL"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "708a426f",
   "metadata": {},
   "source": [
    "Define any other `tools` you want to use below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cef4c150-0ef1-4a33-836b-01062fec134e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Tools\n",
    "import os\n",
    "from contextlib import contextmanager\n",
    "from typing import Optional\n",
    "from langchain.agents import tool\n",
    "from langchain.tools.file_management.read import ReadFileTool\n",
    "from langchain.tools.file_management.write import WriteFileTool\n",
    "\n",
    "ROOT_DIR = \"./data/\"\n",
    "\n",
    "@contextmanager\n",
    "def pushd(new_dir):\n",
    "    \"\"\"Context manager for changing the current working directory.\"\"\"\n",
    "    prev_dir = os.getcwd()\n",
    "    os.chdir(new_dir)\n",
    "    try:\n",
    "        yield\n",
    "    finally:\n",
    "        os.chdir(prev_dir)\n",
    "\n",
    "@tool\n",
    "def process_csv(\n",
    "    csv_file_path: str, instructions: str, output_path: Optional[str] = None\n",
    ") -> str:\n",
    "    \"\"\"Process a CSV by with pandas in a limited REPL.\\\n",
    " Only use this after writing data to disk as a csv file.\\\n",
    " Any figures must be saved to disk to be viewed by the human.\\\n",
    " Instructions should be written in natural language, not code. Assume the dataframe is already loaded.\"\"\"\n",
    "    with pushd(ROOT_DIR):\n",
    "        try:\n",
    "            df = pd.read_csv(csv_file_path)\n",
    "        except Exception as e:\n",
    "            return f\"Error: {e}\"\n",
    "        agent = create_pandas_dataframe_agent(llm, df, max_iterations=30, verbose=True)\n",
    "        if output_path is not None:\n",
    "            instructions += f\" Save output to disk at {output_path}\"\n",
    "        try:\n",
    "            result = agent.run(instructions)\n",
    "            return result\n",
    "        except Exception as e:\n",
    "            return f\"Error: {e}\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69975008-654a-4cbb-bdf6-63c8bae07eaa",
   "metadata": {
    "tags": []
   },
   "source": [
    "**Browse a web page with PlayWright**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6bb5e47b-0f54-4faa-ae42-49a28fa5497b",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# !pip install playwright\n",
    "# !playwright install"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "26b497d7-8e52-4c7f-8e7e-da0a48820a3c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "async def async_load_playwright(url: str) -> str:\n",
    "    \"\"\"Load the specified URLs using Playwright and parse using BeautifulSoup.\"\"\"\n",
    "    from bs4 import BeautifulSoup\n",
    "    from playwright.async_api import async_playwright\n",
    "\n",
    "    results = \"\"\n",
    "    async with async_playwright() as p:\n",
    "        browser = await p.chromium.launch(headless=True)\n",
    "        try:\n",
    "            page = await browser.new_page()\n",
    "            await page.goto(url)\n",
    "\n",
    "            page_source = await page.content()\n",
    "            soup = BeautifulSoup(page_source, \"html.parser\")\n",
    "\n",
    "            for script in soup([\"script\", \"style\"]):\n",
    "                script.extract()\n",
    "\n",
    "            text = soup.get_text()\n",
    "            lines = (line.strip() for line in text.splitlines())\n",
    "            chunks = (phrase.strip() for line in lines for phrase in line.split(\"  \"))\n",
    "            results = \"\\n\".join(chunk for chunk in chunks if chunk)\n",
    "        except Exception as e:\n",
    "            results = f\"Error: {e}\"\n",
    "        await browser.close()\n",
    "    return results\n",
    "\n",
    "def run_async(coro):\n",
    "    event_loop = asyncio.get_event_loop()\n",
    "    return event_loop.run_until_complete(coro)\n",
    "\n",
    "@tool\n",
    "def browse_web_page(url: str) -> str:\n",
    "    \"\"\"Verbose way to scrape a whole webpage. Likely to cause issues parsing.\"\"\"\n",
    "    return run_async(async_load_playwright(url))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ea71762-67ca-4e75-8c4d-00563064be71",
   "metadata": {},
   "source": [
    "**Q&A Over a webpage**\n",
    "\n",
    "Help the model ask more directed questions of web pages to avoid cluttering its memory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1842929d-f18d-4edc-9fdd-82c929181141",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langchain.tools import BaseTool, DuckDuckGoSearchRun\n",
    "from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
    "\n",
    "from pydantic import Field\n",
    "from langchain.chains.qa_with_sources.loading import load_qa_with_sources_chain, BaseCombineDocumentsChain\n",
    "\n",
    "def _get_text_splitter():\n",
    "    return RecursiveCharacterTextSplitter(\n",
    "        # Set a really small chunk size, just to show.\n",
    "        chunk_size = 500,\n",
    "        chunk_overlap  = 20,\n",
    "        length_function = len,\n",
    "    )\n",
    "\n",
    "\n",
    "class WebpageQATool(BaseTool):\n",
    "    name = \"query_webpage\"\n",
    "    description = \"Browse a webpage and retrieve the information relevant to the question.\"\n",
    "    text_splitter: RecursiveCharacterTextSplitter = Field(default_factory=_get_text_splitter)\n",
    "    qa_chain: BaseCombineDocumentsChain\n",
    "    \n",
    "    def _run(self, url: str, question: str) -> str:\n",
    "        \"\"\"Useful for browsing websites and scraping the text information.\"\"\"\n",
    "        result = browse_web_page.run(url)\n",
    "        docs = [Document(page_content=result, metadata={\"source\": url})]\n",
    "        web_docs = self.text_splitter.split_documents(docs)\n",
    "        results = []\n",
    "        # TODO: Handle this with a MapReduceChain\n",
    "        for i in range(0, len(web_docs), 4):\n",
    "            input_docs = web_docs[i:i+4]\n",
    "            window_result = self.qa_chain({\"input_documents\": input_docs, \"question\": question}, return_only_outputs=True)\n",
    "            results.append(f\"Response from window {i} - {window_result}\")\n",
    "        results_docs = [Document(page_content=\"\\n\".join(results), metadata={\"source\": url})]\n",
    "        return self.qa_chain({\"input_documents\": results_docs, \"question\": question}, return_only_outputs=True)\n",
    "    \n",
    "    async def _arun(self, url: str, question: str) -> str:\n",
    "        raise NotImplementedError\n",
    "      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e6f72bd0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "query_website_tool = WebpageQATool(qa_chain=load_qa_with_sources_chain(llm))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e39ee28",
   "metadata": {},
   "source": [
    "### Set up memory\n",
    "\n",
    "* The memory here is used for the agents intermediate steps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1df7b724",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Memory\n",
    "import faiss\n",
    "from langchain.vectorstores import FAISS\n",
    "from langchain.docstore import InMemoryDocstore\n",
    "from langchain.embeddings import OpenAIEmbeddings\n",
    "from langchain.tools.human.tool import HumanInputRun\n",
    "\n",
    "embeddings_model = OpenAIEmbeddings()\n",
    "embedding_size = 1536\n",
    "index = faiss.IndexFlatL2(embedding_size)\n",
    "vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e40fd657",
   "metadata": {},
   "source": [
    "### Setup model and AutoGPT\n",
    "\n",
    "`Model set-up`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1233caf3-fbc9-4acb-9faa-01008200633d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install duckduckgo_search\n",
    "web_search = DuckDuckGoSearchRun()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "88c8b184-67d7-4c35-84ae-9b14bef8c4e3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "tools = [\n",
    "    web_search,\n",
    "    WriteFileTool(root_dir=\"./data\"),\n",
    "    ReadFileTool(root_dir=\"./data\"),\n",
    "    process_csv,\n",
    "    query_website_tool,\n",
    "    # HumanInputRun(), # Activate if you want the permit asking for help from the human\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "709c08c2",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "agent = AutoGPT.from_llm_and_tools(\n",
    "    ai_name=\"Tom\",\n",
    "    ai_role=\"Assistant\",\n",
    "    tools=tools,\n",
    "    llm=llm,\n",
    "    memory=vectorstore.as_retriever(search_kwargs={\"k\": 8}),\n",
    "    # human_in_the_loop=True, # Set to True if you want to add feedback at each step.\n",
    ")\n",
    "# agent.chain.verbose = True"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc9b51ba",
   "metadata": {},
   "source": [
    "### AutoGPT for Querying the Web\n",
    " \n",
    "  \n",
    "I've spent a lot of time over the years crawling data sources and cleaning data. Let's see if AutoGPT can help with this!\n",
    "\n",
    "Here is the prompt for looking up recent boston marathon times and converting them to tabular form."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "64455d70-a134-4d11-826a-33e34c2ce287",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"I need to find the winning Boston Marathon times for the past 5 years. I can use the DuckDuckGo Search command to search for this information.\",\n",
      "        \"reasoning\": \"Using DuckDuckGo Search will help me gather information on the winning times without complications.\",\n",
      "        \"plan\": \"- Use DuckDuckGo Search to find the winning Boston Marathon times\\n- Generate a table with the year, name, country of origin, and times\\n- Ensure there are no legal complications\",\n",
      "        \"criticism\": \"None\",\n",
      "        \"speak\": \"I will use the DuckDuckGo Search command to find the winning Boston Marathon times for the past 5 years.\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"DuckDuckGo Search\",\n",
      "        \"args\": {\n",
      "            \"query\": \"winning Boston Marathon times for the past 5 years ending in 2022\"\n",
      "        }\n",
      "    }\n",
      "}\n",
      "{\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"The DuckDuckGo Search command did not provide the specific information I need. I must switch my approach and use query_webpage command to browse a webpage containing the Boston Marathon winning times for the past 5 years.\",\n",
      "        \"reasoning\": \"The query_webpage command may give me more accurate and comprehensive results compared to the search command.\",\n",
      "        \"plan\": \"- Use query_webpage command to find the winning Boston Marathon times\\n- Generate a table with the year, name, country of origin, and times\\n- Ensure there are no legal complications\",\n",
      "        \"criticism\": \"I may face difficulty in finding the right webpage with the desired information.\",\n",
      "        \"speak\": \"I will use the query_webpage command to find the winning Boston Marathon times for the past 5 years.\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"DuckDuckGo Search\",\n",
      "        \"args\": {\n",
      "            \"query\": \"site with winning Boston Marathon times for the past 5 years ending in 2022\"\n",
      "        }\n",
      "    }\n",
      "}\n",
      "{\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"I need to use the query_webpage command to find the information about the winning Boston Marathon times for the past 5 years.\",\n",
      "        \"reasoning\": \"The previous DuckDuckGo Search command did not provide specific enough results. The query_webpage command might give more accurate and comprehensive results.\",\n",
      "        \"plan\": \"- Use query_webpage command to find the winning Boston Marathon times\\\\n- Generate a table with the year, name, country of origin, and times\\\\n- Ensure there are no legal complications\",\n",
      "        \"criticism\": \"I may face difficulty in finding the right webpage with the desired information.\",\n",
      "        \"speak\": \"I will use the query_webpage command to find the winning Boston Marathon times for the past 5 years.\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"query_webpage\",\n",
      "        \"args\": {\n",
      "            \"url\": \"https://en.wikipedia.org/wiki/List_of_winners_of_the_Boston_Marathon\",\n",
      "            \"question\": \"What were the winning Boston Marathon times for the past 5 years ending in 2022?\"\n",
      "        }\n",
      "    }\n",
      "}\n",
      "{\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"I have already found the winning Boston Marathon times for the past 5 years. Now, I need to generate a table with the information.\",\n",
      "        \"reasoning\": \"Using the information I already have, I can create a table containing year, name, country of origin, and times.\",\n",
      "        \"plan\": \"- Write the marathon data to a CSV file\\n- Process the CSV file to display the table\",\n",
      "        \"criticism\": \"None\",\n",
      "        \"speak\": \"I will generate a table with the year, name, country of origin, and times for the winning Boston Marathon times for the past 5 years.\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"write_file\",\n",
      "        \"args\": {\n",
      "            \"file_path\": \"boston_marathon_winners.csv\",\n",
      "            \"text\": \"Year,Name,Country,Time\\n2022,Evans Chebet,KEN,2:06:51\\n2021,Benson Kipruto,KEN,2:09:51\\n2019,Lawrence Cherono,KEN,2:07:57\\n2018,Yuki Kawauchi,JPN,2:15:58\"\n",
      "        }\n",
      "    }\n",
      "}\n",
      "{\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"I have retrieved the winning Boston Marathon times for the past 5 years. Now, I need to generate a table with the year, name, country of origin, and times.\",\n",
      "        \"reasoning\": \"Creating a table will help organize the data in a clear and accessible format.\",\n",
      "        \"plan\": \"- Write the data to a CSV file\\n- Process the CSV file to generate the table\\n- Complete the task\",\n",
      "        \"criticism\": \"None\",\n",
      "        \"speak\": \"I will generate a table with the year, name, country of origin, and winning times using the recently retrieved data.\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"write_file\",\n",
      "        \"args\": {\n",
      "            \"file_path\": \"winning_boston_marathon_data.csv\",\n",
      "            \"text\": \"Year,Name,Country,Time\\n2022,Evans Chebet,KEN,2:06:51\\n2021,Benson Kipruto,KEN,2:09:51\\n2019,Lawrence Cherono,KEN,2:07:57\\n2018,Yuki Kawauchi,JPN,2:15:58\\n\"\n",
      "        }\n",
      "    }\n",
      "}\n",
      "{\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"I have found the winning Boston Marathon times for the past five years ending in 2022. Next, I need to create a table with the year, name, country of origin, and times.\",\n",
      "        \"reasoning\": \"Generating a table will help organize the information in a structured format.\",\n",
      "        \"plan\": \"- Create a table with the year, name, country of origin, and times\\n- Ensure there are no legal complications\",\n",
      "        \"criticism\": \"None\",\n",
      "        \"speak\": \"I will generate a table with the winning Boston Marathon times for the past 5 years ending in 2022.\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"write_file\",\n",
      "        \"args\": {\n",
      "            \"file_path\": \"winning_times.csv\",\n",
      "            \"text\": \"Year,Name,Country,Time\\n2022,Evans Chebet,Kenya,2:06:51\\n2021,Benson Kipruto,Kenya,2:09:51\\n2020,Canceled due to COVID-19 pandemic,,\\n2019,Lawrence Cherono,Kenya,2:07:57\\n2018,Yuki Kawauchi,Japan,2:15:58\"\n",
      "        }\n",
      "    }\n",
      "}\n",
      "{\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"I need to process the CSV file to generate the table with the year, name, country of origin, and winning times.\",\n",
      "        \"reasoning\": \"I have already written the data to a file named 'winning_times.csv'. Now, I need to process this CSV file to properly display the data as a table.\",\n",
      "        \"plan\": \"- Use the process_csv command to read the 'winning_times.csv' file and generate the table\",\n",
      "        \"criticism\": \"None\",\n",
      "        \"speak\": \"I will process the 'winning_times.csv' file to display the table with the winning Boston Marathon times for the past 5 years.\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"process_csv\",\n",
      "        \"args\": {\n",
      "            \"csv_file_path\": \"winning_times.csv\",\n",
      "            \"instructions\": \"Read the CSV file and display the data as a table\"\n",
      "        }\n",
      "    }\n",
      "}\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: The CSV file has already been read and saved into a pandas dataframe called `df`. Hence, I can simply display the data by printing the whole dataframe. Since `df.head()` returns the first 5 rows, I can use that to showcase the contents.\n",
      "\n",
      "Action: python_repl_ast\n",
      "Action Input: print(df.head())\u001b[0m   Year                               Name Country     Time\n",
      "0  2022                       Evans Chebet   Kenya  2:06:51\n",
      "1  2021                     Benson Kipruto   Kenya  2:09:51\n",
      "2  2020  Canceled due to COVID-19 pandemic     NaN      NaN\n",
      "3  2019                   Lawrence Cherono   Kenya  2:07:57\n",
      "4  2018                      Yuki Kawauchi   Japan  2:15:58\n",
      "\n",
      "Observation: \u001b[36;1m\u001b[1;3mNone\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3mI used the wrong tool to perform the action. I should have used the given data and not interacted with the Python shell. I can now provide the displayed data as the answer since the information in the printed dataframe would look like a table when typed as text.\n",
      "\n",
      "Final Answer: \n",
      "   Year                               Name Country     Time\n",
      "0  2022                       Evans Chebet   Kenya  2:06:51\n",
      "1  2021                     Benson Kipruto   Kenya  2:09:51\n",
      "2  2020  Canceled due to COVID-19 pandemic     NaN      NaN\n",
      "3  2019                   Lawrence Cherono   Kenya  2:07:57\n",
      "4  2018                      Yuki Kawauchi   Japan  2:15:58\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "{\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"I already have the winning Boston Marathon times for the past 5 years saved in the file 'winning_times.csv'. Now, I need to process the CSV and display the table.\",\n",
      "        \"reasoning\": \"I am choosing the process_csv command because I already have the required data saved as a CSV file, and I can use this command to read and display the data as a table.\",\n",
      "        \"plan\": \"- Use the process_csv command to read the 'winning_times.csv' file and generate the table\",\n",
      "        \"criticism\": \"None\",\n",
      "        \"speak\": \"I will process the 'winning_times.csv' file to display the table with the winning Boston Marathon times for the past 5 years.\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"process_csv\",\n",
      "        \"args\": {\n",
      "            \"csv_file_path\": \"winning_times.csv\",\n",
      "            \"instructions\": \"Read the CSV file and display the data as a table\"\n",
      "        }\n",
      "    }\n",
      "}\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: Since the data is already loaded in a pandas dataframe, I just need to display the top rows of the dataframe.\n",
      "Action: python_repl_ast\n",
      "Action Input: df.head()\u001b[0m\n",
      "Observation: \u001b[36;1m\u001b[1;3m   Year                               Name Country     Time\n",
      "0  2022                       Evans Chebet   Kenya  2:06:51\n",
      "1  2021                     Benson Kipruto   Kenya  2:09:51\n",
      "2  2020  Canceled due to COVID-19 pandemic     NaN      NaN\n",
      "3  2019                   Lawrence Cherono   Kenya  2:07:57\n",
      "4  2018                      Yuki Kawauchi   Japan  2:15:58\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3mI now know the final answer.\n",
      "Final Answer: \n",
      "   Year                               Name Country     Time\n",
      "0  2022                       Evans Chebet   Kenya  2:06:51\n",
      "1  2021                     Benson Kipruto   Kenya  2:09:51\n",
      "2  2020  Canceled due to COVID-19 pandemic     NaN      NaN\n",
      "3  2019                   Lawrence Cherono   Kenya  2:07:57\n",
      "4  2018                      Yuki Kawauchi   Japan  2:15:58\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "{\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"I have already generated a table with the winning Boston Marathon times for the past 5 years. Now, I can finish the task.\",\n",
      "        \"reasoning\": \"I have completed the required actions and obtained the desired data. The task is complete.\",\n",
      "        \"plan\": \"- Use the finish command\",\n",
      "        \"criticism\": \"None\",\n",
      "        \"speak\": \"I have generated the table with the winning Boston Marathon times for the past 5 years. Task complete.\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"finish\",\n",
      "        \"args\": {\n",
      "            \"response\": \"I have generated the table with the winning Boston Marathon times for the past 5 years. Task complete.\"\n",
      "        }\n",
      "    }\n",
      "}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'I have generated the table with the winning Boston Marathon times for the past 5 years. Task complete.'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.run([\"What were the winning boston marathon times for the past 5 years (ending in 2022)? Generate a table of the year, name, country of origin, and times.\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6b4f96e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}