mirror of
https://github.com/hwchase17/langchain.git
synced 2026-03-17 18:45:44 +00:00
Updated Notebook
This commit is contained in:
@@ -5,75 +5,9 @@
|
||||
"id": "14f8b67b",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## AutoGPT\n",
|
||||
"# AutoGPT\n",
|
||||
"\n",
|
||||
"* Implementation of https://github.com/Significant-Gravitas/Auto-GPT \n",
|
||||
"* With LangChain primitives (LLMs, PromptTemplates, VectorStores, Embeddings, Tools)\n",
|
||||
"\n",
|
||||
"### Workflow\n",
|
||||
"\n",
|
||||
"[Temporary] Ensure you have correct Langchain version locally\n",
|
||||
" \n",
|
||||
"* `export SERPAPI_API_KEY`"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a0866af1-43a8-4160-a9cf-9ffe64074493",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# !pip install playwright\n",
|
||||
"# !playwright install "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d27e21c5-8556-46bf-936a-f6e119ebd3a0",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# ! playwright install "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1cff42fd",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# General \n",
|
||||
"import os\n",
|
||||
"import pandas as pd\n",
|
||||
"from typing import Optional\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"from langchain.experimental.autonomous_agents.autogpt.agent import AutoGPT\n",
|
||||
"from langchain.chat_models import ChatOpenAI\n",
|
||||
"from langchain.utilities import SerpAPIWrapper\n",
|
||||
"from langchain.tools.human.tool import HumanInputRun\n",
|
||||
"from langchain.tools.file_management.read import ReadFileTool\n",
|
||||
"from langchain.tools.file_management.write import WriteFileTool\n",
|
||||
"from langchain.document_loaders.url_selenium import SeleniumURLLoader\n",
|
||||
"from langchain.agents.agent_toolkits.pandas.base import create_pandas_dataframe_agent"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "338dfc5d-178a-49ba-9187-bd08b648ab29",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.document_loaders.url_playwright import PlaywrightURLLoader, Document"
|
||||
"Implementation of https://github.com/Significant-Gravitas/Auto-GPT but with LangChain primitives (LLMs, PromptTemplates, VectorStores, Embeddings, Tools)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -81,158 +15,33 @@
|
||||
"id": "192496a7",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Set up tools\n",
|
||||
"## Set up tools\n",
|
||||
"\n",
|
||||
"* We'll set up an AutoGPT with a `search` tool, and `write-file` tool, and a `read-file` tool"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "708a426f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Define any `tools` you want to use here"
|
||||
"We'll set up an AutoGPT with a search tool, and write-file tool, and a read-file tool"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "cef4c150-0ef1-4a33-836b-01062fec134e",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"execution_count": 1,
|
||||
"id": "7c2c9b54",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Tools\n",
|
||||
"from typing import Optional\n",
|
||||
"from langchain.agents import Tool, tool\n",
|
||||
"from langchain.utilities import SerpAPIWrapper\n",
|
||||
"from langchain.chains.llm_requests import LLMRequestsChain\n",
|
||||
"from langchain.tools.file_management.read import ReadFileTool\n",
|
||||
"from langchain.agents import Tool\n",
|
||||
"from langchain.tools.file_management.write import WriteFileTool\n",
|
||||
"from langchain.tools.requests.tool import RequestsGetTool, TextRequestsWrapper\n",
|
||||
"from langchain.tools.file_management.read import ReadFileTool\n",
|
||||
"\n",
|
||||
"@tool\n",
|
||||
"def process_csv(csv_file_path: str, instructions: str, output_path: Optional[str] = None) -> str:\n",
|
||||
" \"\"\"Process a CSV by with pandas in a limited REPL. Only use this after writing data to disk as a csv file. Any figures must be saved to disk to be viewed by the human. Instructions should be written in natural language, not code. Assume the dataframe is already loaded.\"\"\"\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",
|
||||
" return agent.run(instructions)\n",
|
||||
" except Exception as e:\n",
|
||||
" return f\"Error: {e}\"\n",
|
||||
" \n",
|
||||
"@tool\n",
|
||||
"def show_image(image_path: str) -> str:\n",
|
||||
" \"\"\"Show an image from disk\"\"\"\n",
|
||||
" try:\n",
|
||||
" img = plt.imread(image_path)\n",
|
||||
" except Exception as e:\n",
|
||||
" return f\"Error: {e}\"\n",
|
||||
" plt.imshow(img)\n",
|
||||
" return f\"Showed image at {image_path}\"\n",
|
||||
"\n",
|
||||
"@tool\n",
|
||||
"def current_time() -> str:\n",
|
||||
" \"\"\"Show the current time\"\"\"\n",
|
||||
" return f\"Current time: {datetime.datetime.now()}\" \n",
|
||||
"\n",
|
||||
"@tool\n",
|
||||
"def scrape_links(url: str) -> str:\n",
|
||||
" \"\"\"Scrape links from a webpage.\"\"\"\n",
|
||||
" response, error_message = get_response(url)\n",
|
||||
" if error_message:\n",
|
||||
" return error_message\n",
|
||||
" if not response:\n",
|
||||
" return \"Error: Could not get response\"\n",
|
||||
" soup = BeautifulSoup(response.text, \"html.parser\")\n",
|
||||
"\n",
|
||||
" for script in soup([\"script\", \"style\"]):\n",
|
||||
" script.extract()\n",
|
||||
"\n",
|
||||
" hyperlinks = extract_hyperlinks(soup, url)\n",
|
||||
"\n",
|
||||
" return format_hyperlinks(hyperlinks)\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "fbd24d78-93a1-4645-bb3a-cac6fb2fc1a2",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Define Web Browser Tool\n",
|
||||
"\n",
|
||||
"This is optional but can provide more information to the model relative to the other tools"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e3dcd773-a462-404e-b3dd-822a5f773e0d",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# !pip install playwright\n",
|
||||
"# !playwright install >/dev/null\n",
|
||||
"# !pip install bs4\n",
|
||||
"# !pip install nest_asyncio"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"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",
|
||||
" import asyncio\n",
|
||||
" import nest_asyncio\n",
|
||||
" nest_asyncio.apply()\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",
|
||||
" \"\"\"Useful for browsing websites and scraping the text information.\"\"\"\n",
|
||||
" return run_async(async_load_playwright(url))"
|
||||
"search = SerpAPIWrapper()\n",
|
||||
"tools = [\n",
|
||||
" Tool(\n",
|
||||
" name = \"search\",\n",
|
||||
" func=search.run,\n",
|
||||
" description=\"useful for when you need to answer questions about current events. You should ask targeted questions\"\n",
|
||||
" ),\n",
|
||||
" WriteFileTool(),\n",
|
||||
" ReadFileTool(),\n",
|
||||
"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -240,27 +49,35 @@
|
||||
"id": "8e39ee28",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Set up memory\n",
|
||||
"## Set up memory\n",
|
||||
"\n",
|
||||
"* The memory here is used for the agents intermediate steps"
|
||||
"The memory here is used for the agents intermediate steps"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1df7b724",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"execution_count": 2,
|
||||
"id": "72bc204d",
|
||||
"metadata": {},
|
||||
"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",
|
||||
"\n",
|
||||
"from langchain.embeddings import OpenAIEmbeddings"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "1df7b724",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Define your embedding model\n",
|
||||
"embeddings_model = OpenAIEmbeddings()\n",
|
||||
"# Initialize the vectorstore as empty\n",
|
||||
"import faiss\n",
|
||||
"\n",
|
||||
"embedding_size = 1536\n",
|
||||
"index = faiss.IndexFlatL2(embedding_size)\n",
|
||||
"vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {})"
|
||||
@@ -271,56 +88,37 @@
|
||||
"id": "e40fd657",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Setup model and AutoGPT\n",
|
||||
"## Setup model and AutoGPT\n",
|
||||
"\n",
|
||||
"`Model set-up`"
|
||||
"Initialize everything! We will use ChatOpenAI model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "88c8b184-67d7-4c35-84ae-9b14bef8c4e3",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"execution_count": 4,
|
||||
"id": "3393bc23",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"search = SerpAPIWrapper()\n",
|
||||
"tools = [\n",
|
||||
" Tool(\n",
|
||||
" name = \"search\",\n",
|
||||
" func=search.run,\n",
|
||||
" description=\"Useful for when you need to answer questions about current events. You should ask targeted questions\"\n",
|
||||
" ),\n",
|
||||
" WriteFileTool(),\n",
|
||||
" ReadFileTool(),\n",
|
||||
" process_csv,\n",
|
||||
" show_image,\n",
|
||||
" RequestsGetTool(requests_wrapper=TextRequestsWrapper()),\n",
|
||||
" current_time,\n",
|
||||
" browse_web_page,\n",
|
||||
" # huggingface_image_generation,\n",
|
||||
"]"
|
||||
"from langchain.experimental.autonomous_agents.autogpt.agent import AutoGPT\n",
|
||||
"from langchain.chat_models import ChatOpenAI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 5,
|
||||
"id": "709c08c2",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"llm = ChatOpenAI(model_name=\"gpt-4\", temperature=0.3)\n",
|
||||
"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(),\n",
|
||||
" # human_in_the_loop=True, # Set to True if you want to add feedback at each step.\n",
|
||||
" llm=ChatOpenAI(temperature=0),\n",
|
||||
" memory=vectorstore.as_retriever()\n",
|
||||
")\n",
|
||||
"# Set verbose to be true\n",
|
||||
"agent.chain.verbose = True"
|
||||
]
|
||||
},
|
||||
@@ -329,127 +127,349 @@
|
||||
"id": "fc9b51ba",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### AutoGPT as a research / data munger \n",
|
||||
"## Run an example\n",
|
||||
"\n",
|
||||
"#### `inflation` and `college tuition`\n",
|
||||
" \n",
|
||||
"Let's use AutoGPT as researcher and data munger / cleaner.\n",
|
||||
" \n",
|
||||
"I spent a lot of time over the years crawling data sources and cleaning data. \n",
|
||||
"\n",
|
||||
"Let's see if AutoGPT can do all of this for us!\n",
|
||||
"\n",
|
||||
"Here is the prompt comparing `inflation` and `college tuition`."
|
||||
"Here we will make it write a weather report for SF"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3aba5e30",
|
||||
"execution_count": 6,
|
||||
"id": "c032b182",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
"scrolled": false
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"\n",
|
||||
"\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
|
||||
"Prompt after formatting:\n",
|
||||
"\u001b[32;1m\u001b[1;3mSystem: You are Tom, Assistant\n",
|
||||
"Your decisions must always be made independently \n",
|
||||
" without seeking user assistance. Play to your strengths \n",
|
||||
" as an LLM and pursue simple strategies with no legal complications. \n",
|
||||
" If you have completed all your tasks, \n",
|
||||
" make sure to use the \"finish\" command.\n",
|
||||
"\n",
|
||||
"GOALS:\n",
|
||||
"\n",
|
||||
"1. write a weather report for SF today\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Constraints:\n",
|
||||
"1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.\n",
|
||||
"2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.\n",
|
||||
"3. No user assistance\n",
|
||||
"4. Exclusively use the commands listed in double quotes e.g. \"command name\"\n",
|
||||
"\n",
|
||||
"Commands:\n",
|
||||
"1. search: useful for when you need to answer questions about current events. You should ask targeted questions, args: \"tool_input\": \"\"\n",
|
||||
"2. write_file: Write file to disk, args: \"file_path\": \"name of file\", \"text\": \"text to write to file\"\n",
|
||||
"3. read_file: Read file from disk, args: \"file_path\": \"name of file\"\n",
|
||||
"4. finish: use this to signal that you have finished all your objectives, args: \"response\": \"final response to let people know you have finished your objectives\"\n",
|
||||
"\n",
|
||||
"Resources:\n",
|
||||
"1. Internet access for searches and information gathering.\n",
|
||||
"2. Long Term memory management.\n",
|
||||
"3. GPT-3.5 powered Agents for delegation of simple tasks.\n",
|
||||
"4. File output.\n",
|
||||
"\n",
|
||||
"Performance Evaluation:\n",
|
||||
"1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.\n",
|
||||
"2. Constructively self-criticize your big-picture behavior constantly.\n",
|
||||
"3. Reflect on past decisions and strategies to refine your approach.\n",
|
||||
"4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.\n",
|
||||
"\n",
|
||||
"You should only respond in JSON format as described below \n",
|
||||
"Response Format: \n",
|
||||
"{\n",
|
||||
" \"thoughts\": {\n",
|
||||
" \"text\": \"thought\",\n",
|
||||
" \"reasoning\": \"reasoning\",\n",
|
||||
" \"plan\": \"- short bulleted\\n- list that conveys\\n- long-term plan\",\n",
|
||||
" \"criticism\": \"constructive self-criticism\",\n",
|
||||
" \"speak\": \"thoughts summary to say to user\"\n",
|
||||
" },\n",
|
||||
" \"command\": {\n",
|
||||
" \"name\": \"command name\",\n",
|
||||
" \"args\": {\n",
|
||||
" \"arg name\": \"value\"\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
"} \n",
|
||||
"Ensure the response can be parsed by Python json.loads\n",
|
||||
"System: The current time and date is Sun Apr 16 18:38:42 2023\n",
|
||||
"System: This reminds you of these events from your past:\n",
|
||||
"[]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Human: Determine which next command to use, and respond using the format specified above:\u001b[0m\n",
|
||||
"\n",
|
||||
"\u001b[1m> Finished chain.\u001b[0m\n",
|
||||
"{\n",
|
||||
" \"thoughts\": {\n",
|
||||
" \"text\": \"I think I should start by writing the weather report for SF today. I can use the 'search' command to find the current weather conditions.\",\n",
|
||||
" \"reasoning\": \"The first step to completing my goals is to write the weather report for SF today. I can use the 'search' command to find the current weather conditions.\",\n",
|
||||
" \"plan\": \"- Use the 'search' command to find the current weather conditions.\\n- Write the weather report for SF today.\",\n",
|
||||
" \"criticism\": \"I need to make sure that I am accurate in my reporting of the weather conditions.\",\n",
|
||||
" \"speak\": \"I will use the 'search' command to find the current weather conditions and then write the weather report for SF today.\"\n",
|
||||
" },\n",
|
||||
" \"command\": {\n",
|
||||
" \"name\": \"search\",\n",
|
||||
" \"args\": {\n",
|
||||
" \"tool_input\": \"current weather conditions in San Francisco\"\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
|
||||
"Prompt after formatting:\n",
|
||||
"\u001b[32;1m\u001b[1;3mSystem: You are Tom, Assistant\n",
|
||||
"Your decisions must always be made independently \n",
|
||||
" without seeking user assistance. Play to your strengths \n",
|
||||
" as an LLM and pursue simple strategies with no legal complications. \n",
|
||||
" If you have completed all your tasks, \n",
|
||||
" make sure to use the \"finish\" command.\n",
|
||||
"\n",
|
||||
"GOALS:\n",
|
||||
"\n",
|
||||
"1. write a weather report for SF today\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Constraints:\n",
|
||||
"1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.\n",
|
||||
"2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.\n",
|
||||
"3. No user assistance\n",
|
||||
"4. Exclusively use the commands listed in double quotes e.g. \"command name\"\n",
|
||||
"\n",
|
||||
"Commands:\n",
|
||||
"1. search: useful for when you need to answer questions about current events. You should ask targeted questions, args: \"tool_input\": \"\"\n",
|
||||
"2. write_file: Write file to disk, args: \"file_path\": \"name of file\", \"text\": \"text to write to file\"\n",
|
||||
"3. read_file: Read file from disk, args: \"file_path\": \"name of file\"\n",
|
||||
"4. finish: use this to signal that you have finished all your objectives, args: \"response\": \"final response to let people know you have finished your objectives\"\n",
|
||||
"\n",
|
||||
"Resources:\n",
|
||||
"1. Internet access for searches and information gathering.\n",
|
||||
"2. Long Term memory management.\n",
|
||||
"3. GPT-3.5 powered Agents for delegation of simple tasks.\n",
|
||||
"4. File output.\n",
|
||||
"\n",
|
||||
"Performance Evaluation:\n",
|
||||
"1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.\n",
|
||||
"2. Constructively self-criticize your big-picture behavior constantly.\n",
|
||||
"3. Reflect on past decisions and strategies to refine your approach.\n",
|
||||
"4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.\n",
|
||||
"\n",
|
||||
"You should only respond in JSON format as described below \n",
|
||||
"Response Format: \n",
|
||||
"{\n",
|
||||
" \"thoughts\": {\n",
|
||||
" \"text\": \"thought\",\n",
|
||||
" \"reasoning\": \"reasoning\",\n",
|
||||
" \"plan\": \"- short bulleted\\n- list that conveys\\n- long-term plan\",\n",
|
||||
" \"criticism\": \"constructive self-criticism\",\n",
|
||||
" \"speak\": \"thoughts summary to say to user\"\n",
|
||||
" },\n",
|
||||
" \"command\": {\n",
|
||||
" \"name\": \"command name\",\n",
|
||||
" \"args\": {\n",
|
||||
" \"arg name\": \"value\"\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
"} \n",
|
||||
"Ensure the response can be parsed by Python json.loads\n",
|
||||
"System: The current time and date is Sun Apr 16 18:38:59 2023\n",
|
||||
"System: This reminds you of these events from your past:\n",
|
||||
"['Assistant Reply: {\\n \"thoughts\": {\\n \"text\": \"I think I should start by writing the weather report for SF today. I can use the \\'search\\' command to find the current weather conditions.\",\\n \"reasoning\": \"The first step to completing my goals is to write the weather report for SF today. I can use the \\'search\\' command to find the current weather conditions.\",\\n \"plan\": \"- Use the \\'search\\' command to find the current weather conditions.\\\\n- Write the weather report for SF today.\",\\n \"criticism\": \"I need to make sure that I am accurate in my reporting of the weather conditions.\",\\n \"speak\": \"I will use the \\'search\\' command to find the current weather conditions and then write the weather report for SF today.\"\\n },\\n \"command\": {\\n \"name\": \"search\",\\n \"args\": {\\n \"tool_input\": \"current weather conditions in San Francisco\"\\n }\\n }\\n} \\nResult: Command search returned: Cloudy skies early, then partly cloudy in the afternoon. High 56F. Winds W at 15 to 25 mph. ']\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Human: Determine which next command to use, and respond using the format specified above:\n",
|
||||
"AI: {\n",
|
||||
" \"thoughts\": {\n",
|
||||
" \"text\": \"I think I should start by writing the weather report for SF today. I can use the 'search' command to find the current weather conditions.\",\n",
|
||||
" \"reasoning\": \"The first step to completing my goals is to write the weather report for SF today. I can use the 'search' command to find the current weather conditions.\",\n",
|
||||
" \"plan\": \"- Use the 'search' command to find the current weather conditions.\\n- Write the weather report for SF today.\",\n",
|
||||
" \"criticism\": \"I need to make sure that I am accurate in my reporting of the weather conditions.\",\n",
|
||||
" \"speak\": \"I will use the 'search' command to find the current weather conditions and then write the weather report for SF today.\"\n",
|
||||
" },\n",
|
||||
" \"command\": {\n",
|
||||
" \"name\": \"search\",\n",
|
||||
" \"args\": {\n",
|
||||
" \"tool_input\": \"current weather conditions in San Francisco\"\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
"}\n",
|
||||
"System: Command search returned: Cloudy skies early, then partly cloudy in the afternoon. High 56F. Winds W at 15 to 25 mph.\n",
|
||||
"Human: Determine which next command to use, and respond using the format specified above:\u001b[0m\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"\u001b[1m> Finished chain.\u001b[0m\n",
|
||||
"{\n",
|
||||
" \"thoughts\": {\n",
|
||||
" \"text\": \"Now that I have the current weather conditions for SF, I can write the weather report. I will use the 'write_file' command to save the report to a file.\",\n",
|
||||
" \"reasoning\": \"Now that I have the current weather conditions for SF, I can write the weather report. I will use the 'write_file' command to save the report to a file.\",\n",
|
||||
" \"plan\": \"- Use the 'write_file' command to save the weather report to a file.\",\n",
|
||||
" \"criticism\": \"I need to make sure that the weather report is clear and concise.\",\n",
|
||||
" \"speak\": \"I will use the 'write_file' command to save the weather report to a file.\"\n",
|
||||
" },\n",
|
||||
" \"command\": {\n",
|
||||
" \"name\": \"write_file\",\n",
|
||||
" \"args\": {\n",
|
||||
" \"file_path\": \"weather_report.txt\",\n",
|
||||
" \"text\": \"Weather Report for San Francisco on April 16, 2023:\\n\\nCloudy skies early, then partly cloudy in the afternoon. High 56F. Winds W at 15 to 25 mph.\"\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
|
||||
"Prompt after formatting:\n",
|
||||
"\u001b[32;1m\u001b[1;3mSystem: You are Tom, Assistant\n",
|
||||
"Your decisions must always be made independently \n",
|
||||
" without seeking user assistance. Play to your strengths \n",
|
||||
" as an LLM and pursue simple strategies with no legal complications. \n",
|
||||
" If you have completed all your tasks, \n",
|
||||
" make sure to use the \"finish\" command.\n",
|
||||
"\n",
|
||||
"GOALS:\n",
|
||||
"\n",
|
||||
"1. write a weather report for SF today\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Constraints:\n",
|
||||
"1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.\n",
|
||||
"2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.\n",
|
||||
"3. No user assistance\n",
|
||||
"4. Exclusively use the commands listed in double quotes e.g. \"command name\"\n",
|
||||
"\n",
|
||||
"Commands:\n",
|
||||
"1. search: useful for when you need to answer questions about current events. You should ask targeted questions, args: \"tool_input\": \"\"\n",
|
||||
"2. write_file: Write file to disk, args: \"file_path\": \"name of file\", \"text\": \"text to write to file\"\n",
|
||||
"3. read_file: Read file from disk, args: \"file_path\": \"name of file\"\n",
|
||||
"4. finish: use this to signal that you have finished all your objectives, args: \"response\": \"final response to let people know you have finished your objectives\"\n",
|
||||
"\n",
|
||||
"Resources:\n",
|
||||
"1. Internet access for searches and information gathering.\n",
|
||||
"2. Long Term memory management.\n",
|
||||
"3. GPT-3.5 powered Agents for delegation of simple tasks.\n",
|
||||
"4. File output.\n",
|
||||
"\n",
|
||||
"Performance Evaluation:\n",
|
||||
"1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.\n",
|
||||
"2. Constructively self-criticize your big-picture behavior constantly.\n",
|
||||
"3. Reflect on past decisions and strategies to refine your approach.\n",
|
||||
"4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.\n",
|
||||
"\n",
|
||||
"You should only respond in JSON format as described below \n",
|
||||
"Response Format: \n",
|
||||
"{\n",
|
||||
" \"thoughts\": {\n",
|
||||
" \"text\": \"thought\",\n",
|
||||
" \"reasoning\": \"reasoning\",\n",
|
||||
" \"plan\": \"- short bulleted\\n- list that conveys\\n- long-term plan\",\n",
|
||||
" \"criticism\": \"constructive self-criticism\",\n",
|
||||
" \"speak\": \"thoughts summary to say to user\"\n",
|
||||
" },\n",
|
||||
" \"command\": {\n",
|
||||
" \"name\": \"command name\",\n",
|
||||
" \"args\": {\n",
|
||||
" \"arg name\": \"value\"\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
"} \n",
|
||||
"Ensure the response can be parsed by Python json.loads\n",
|
||||
"System: The current time and date is Sun Apr 16 18:39:10 2023\n",
|
||||
"System: This reminds you of these events from your past:\n",
|
||||
"['Assistant Reply: {\\n \"thoughts\": {\\n \"text\": \"I think I should start by writing the weather report for SF today. I can use the \\'search\\' command to find the current weather conditions.\",\\n \"reasoning\": \"The first step to completing my goals is to write the weather report for SF today. I can use the \\'search\\' command to find the current weather conditions.\",\\n \"plan\": \"- Use the \\'search\\' command to find the current weather conditions.\\\\n- Write the weather report for SF today.\",\\n \"criticism\": \"I need to make sure that I am accurate in my reporting of the weather conditions.\",\\n \"speak\": \"I will use the \\'search\\' command to find the current weather conditions and then write the weather report for SF today.\"\\n },\\n \"command\": {\\n \"name\": \"search\",\\n \"args\": {\\n \"tool_input\": \"current weather conditions in San Francisco\"\\n }\\n }\\n} \\nResult: Command search returned: Cloudy skies early, then partly cloudy in the afternoon. High 56F. Winds W at 15 to 25 mph. ', 'Assistant Reply: {\\n \"thoughts\": {\\n \"text\": \"Now that I have the current weather conditions for SF, I can write the weather report. I will use the \\'write_file\\' command to save the report to a file.\",\\n \"reasoning\": \"Now that I have the current weather conditions for SF, I can write the weather report. I will use the \\'write_file\\' command to save the report to a file.\",\\n \"plan\": \"- Use the \\'write_file\\' command to save the weather report to a file.\",\\n \"criticism\": \"I need to make sure that the weather report is clear and concise.\",\\n \"speak\": \"I will use the \\'write_file\\' command to save the weather report to a file.\"\\n },\\n \"command\": {\\n \"name\": \"write_file\",\\n \"args\": {\\n \"file_path\": \"weather_report.txt\",\\n \"text\": \"Weather Report for San Francisco on April 16, 2023:\\\\n\\\\nCloudy skies early, then partly cloudy in the afternoon. High 56F. Winds W at 15 to 25 mph.\"\\n }\\n }\\n} \\nResult: Command write_file returned: File written to successfully. ']\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Human: Determine which next command to use, and respond using the format specified above:\n",
|
||||
"AI: {\n",
|
||||
" \"thoughts\": {\n",
|
||||
" \"text\": \"I think I should start by writing the weather report for SF today. I can use the 'search' command to find the current weather conditions.\",\n",
|
||||
" \"reasoning\": \"The first step to completing my goals is to write the weather report for SF today. I can use the 'search' command to find the current weather conditions.\",\n",
|
||||
" \"plan\": \"- Use the 'search' command to find the current weather conditions.\\n- Write the weather report for SF today.\",\n",
|
||||
" \"criticism\": \"I need to make sure that I am accurate in my reporting of the weather conditions.\",\n",
|
||||
" \"speak\": \"I will use the 'search' command to find the current weather conditions and then write the weather report for SF today.\"\n",
|
||||
" },\n",
|
||||
" \"command\": {\n",
|
||||
" \"name\": \"search\",\n",
|
||||
" \"args\": {\n",
|
||||
" \"tool_input\": \"current weather conditions in San Francisco\"\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
"}\n",
|
||||
"System: Command search returned: Cloudy skies early, then partly cloudy in the afternoon. High 56F. Winds W at 15 to 25 mph.\n",
|
||||
"Human: Determine which next command to use, and respond using the format specified above:\n",
|
||||
"AI: {\n",
|
||||
" \"thoughts\": {\n",
|
||||
" \"text\": \"Now that I have the current weather conditions for SF, I can write the weather report. I will use the 'write_file' command to save the report to a file.\",\n",
|
||||
" \"reasoning\": \"Now that I have the current weather conditions for SF, I can write the weather report. I will use the 'write_file' command to save the report to a file.\",\n",
|
||||
" \"plan\": \"- Use the 'write_file' command to save the weather report to a file.\",\n",
|
||||
" \"criticism\": \"I need to make sure that the weather report is clear and concise.\",\n",
|
||||
" \"speak\": \"I will use the 'write_file' command to save the weather report to a file.\"\n",
|
||||
" },\n",
|
||||
" \"command\": {\n",
|
||||
" \"name\": \"write_file\",\n",
|
||||
" \"args\": {\n",
|
||||
" \"file_path\": \"weather_report.txt\",\n",
|
||||
" \"text\": \"Weather Report for San Francisco on April 16, 2023:\\n\\nCloudy skies early, then partly cloudy in the afternoon. High 56F. Winds W at 15 to 25 mph.\"\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
"}\n",
|
||||
"System: Command write_file returned: File written to successfully.\n",
|
||||
"Human: Determine which next command to use, and respond using the format specified above:\u001b[0m\n",
|
||||
"\n",
|
||||
"\u001b[1m> Finished chain.\u001b[0m\n",
|
||||
"{\n",
|
||||
" \"thoughts\": {\n",
|
||||
" \"text\": \"I have completed all my tasks. I will use the 'finish' command to signal that I am done.\",\n",
|
||||
" \"reasoning\": \"I have completed all my tasks, so I will use the 'finish' command to signal that I am done.\",\n",
|
||||
" \"plan\": \"- Use the 'finish' command to signal that I am done.\",\n",
|
||||
" \"criticism\": \"I need to make sure that I have completed all my tasks before using the 'finish' command.\",\n",
|
||||
" \"speak\": \"I have completed all my tasks, so I will use the 'finish' command to signal that I am done.\"\n",
|
||||
" },\n",
|
||||
" \"command\": {\n",
|
||||
" \"name\": \"finish\",\n",
|
||||
" \"args\": {\n",
|
||||
" \"response\": \"I have completed all my tasks. Goodbye!\"\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
"}\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'I have completed all my tasks. Goodbye!'"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"agent.run([\"Using the 'data/' folder as scratch, get me the yearly % change in US college tuition and the yearly % change in US inflation (US CPI) every year since 1980.\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f78012cf",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"The command runs and writes output to `data`.\n",
|
||||
" \n",
|
||||
"`cleaned_college_tuition_inflation_percent_change.csv` is written.\n",
|
||||
"\n",
|
||||
"We write some simple code to plot this."
|
||||
"agent.run([\"write a weather report for SF today\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c33c7a24",
|
||||
"id": "aa264f26",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Read\n",
|
||||
"d = pd.read_csv(\"data/cleaned_college_tuition_inflation_percent_change.csv\")\n",
|
||||
"d.set_index(\"Year\",inplace=True)\n",
|
||||
"# Compute cumulative percent change\n",
|
||||
"d['College Tuition % Change Cumulative'] = (1 + d['College Tuition % Change']).cumprod() * 100\n",
|
||||
"d['Inflation % Change Cumulative'] = (1 + d['Inflation % Change']).cumprod() * 100\n",
|
||||
"# Plot\n",
|
||||
"d[['College Tuition % Change Cumulative','Inflation % Change Cumulative']].plot(color=['blue','green'])\n",
|
||||
"plt.ylabel(\"Cumulative Percent Change\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f31d09ea",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Of course, we would want to inspect and verify the results."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3c59b98d",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### AutoGPT as a data scietntist (research, cleaning, visualization)\n",
|
||||
"\n",
|
||||
"#### `hey jude` \n",
|
||||
"\n",
|
||||
"Let's try end-to-end data retrival and plotting.\n",
|
||||
"\n",
|
||||
"* See my full notes here: https://rlancemartin.notion.site/Auto-GPT-Notes-9481cbd0cb364580bf97d5f41144e5cf\n",
|
||||
"* Files to write \n",
|
||||
"* However, reading seems to not work\n",
|
||||
"* Even if I simply ask it to read from the saved file, it gets stuck and appears to loop\n",
|
||||
"\n",
|
||||
"`agent.run([\"Read the file hey_jude_lyrics.txt and plot the frequency of the top 5 words.\"])`"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "999f19bf",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"llm = ChatOpenAI(model_name=\"gpt-4\", temperature=0.3)\n",
|
||||
"agent = AutoGPT.from_llm_and_tools(\n",
|
||||
" ai_name=\"Tom\",\n",
|
||||
" ai_role=\"Look up song lyrics\",\n",
|
||||
" tools=tools,\n",
|
||||
" llm=llm,\n",
|
||||
" memory=vectorstore.as_retriever()\n",
|
||||
")\n",
|
||||
"agent.chain.verbose = True\n",
|
||||
"\n",
|
||||
"agent.run([\"Get the lyrics to the Beatles song 'Hey Jude' and plot the frequency of the top 5 words.\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8a54c746",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"llm = ChatOpenAI(model_name=\"gpt-4\", temperature=0.3)\n",
|
||||
"agent = AutoGPT.from_llm_and_tools(\n",
|
||||
" ai_name=\"Tom\",\n",
|
||||
" ai_role=\"Look up song lyrics\",\n",
|
||||
" tools=tools,\n",
|
||||
" llm=llm,\n",
|
||||
" memory=vectorstore.as_retriever()\n",
|
||||
")\n",
|
||||
"agent.chain.verbose = True\n",
|
||||
"\n",
|
||||
"agent.run([\"Read the file hey_jude_lyrics.txt and plot the frequency of the top 5 words.\"])"
|
||||
]
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
@@ -468,7 +488,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.2"
|
||||
"version": "3.10.1"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
Reference in New Issue
Block a user