From 753efe5b49dc1c795af1c96325dc0e223f7c263e Mon Sep 17 00:00:00 2001 From: DosticJelena Date: Tue, 22 Aug 2023 16:47:55 +0200 Subject: [PATCH 1/2] Update version of the SQLDatabaseChain and fix descriptions --- .../handbook/06-langchain-agents.ipynb | 594 +++++++----------- 1 file changed, 243 insertions(+), 351 deletions(-) diff --git a/learn/generation/langchain/handbook/06-langchain-agents.ipynb b/learn/generation/langchain/handbook/06-langchain-agents.ipynb index aa08a2b5..0987fe70 100644 --- a/learn/generation/langchain/handbook/06-langchain-agents.ipynb +++ b/learn/generation/langchain/handbook/06-langchain-agents.ipynb @@ -1,7 +1,6 @@ { "cells": [ { - "attachments": {}, "cell_type": "markdown", "id": "49e9c69e", "metadata": { @@ -12,7 +11,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "1571b632", "metadata": { @@ -39,22 +37,18 @@ "base_uri": "https://localhost:8080/" }, "id": "uZR3iGJJtdDE", - "outputId": "196a7da0-6099-4721-d8bc-9247fb852620" + "outputId": "04116858-96f2-447d-83d4-324f1c1f7164" }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m472.9/472.9 KB\u001b[0m \u001b[31m5.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m70.2/70.2 KB\u001b[0m \u001b[31m3.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/1.5 MB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91m━\u001b[0m\u001b[90m╺\u001b[0m\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/1.5 MB\u001b[0m \u001b[31m1.0 MB/s\u001b[0m eta \u001b[36m0:00:02\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.6/1.5 MB\u001b[0m \u001b[31m9.1 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.5/1.5 MB\u001b[0m \u001b[31m16.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25h\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/73.6 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m73.6/73.6 kB\u001b[0m \u001b[31m8.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m18.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m264.6/264.6 KB\u001b[0m \u001b[31m24.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m158.8/158.8 KB\u001b[0m \u001b[31m14.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m114.2/114.2 KB\u001b[0m \u001b[31m10.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.1/49.1 KB\u001b[0m \u001b[31m4.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.4/49.4 kB\u001b[0m \u001b[31m5.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Building wheel for google-search-results (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Building wheel for wikipedia (setup.py) ... \u001b[?25l\u001b[?25hdone\n" ] @@ -65,14 +59,13 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "wPdWz1IdxyBR", "metadata": { "id": "wPdWz1IdxyBR" }, "source": [ - "To run this notebook, we will need to use an OpenAI LLM. Here we will setup the LLM we will use for the whole notebook, just input your openai api key when prompted. " + "To run this notebook, we will need to use an OpenAI LLM. Here we will setup the LLM we will use for the whole notebook, just input your openai api key when prompted." ] }, { @@ -84,7 +77,7 @@ "base_uri": "https://localhost:8080/" }, "id": "c02c4fa2", - "outputId": "f5843a3b-2e86-4616-88a4-e85c828fb9ba" + "outputId": "dffcdc44-08ff-44f5-9df2-0f4d40123e71" }, "outputs": [ { @@ -119,7 +112,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "309g_2pqxzzB", "metadata": { @@ -149,7 +141,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "bd60a97c", "metadata": { @@ -160,7 +151,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "6e1f31b4", "metadata": { @@ -171,7 +161,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "5b919c3a", "metadata": { @@ -187,7 +176,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "6c9c13e9", "metadata": { @@ -198,7 +186,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "a93dc920", "metadata": { @@ -209,14 +196,13 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "b601f903", "metadata": { "id": "b601f903" }, "source": [ - "We will use the agents to interact with a small sample database of stocks. We will not dive into the details because this is just a dummy tool we will build for illustrative purposes. Let's create it. " + "We will use the agents to interact with a small sample database of stocks. We will not dive into the details because this is just a dummy tool we will build for illustrative purposes. Let's create it." ] }, { @@ -250,13 +236,13 @@ " Column(\"obs_id\", Integer, primary_key=True),\n", " Column(\"stock_ticker\", String(4), nullable=False),\n", " Column(\"price\", Float, nullable=False),\n", - " Column(\"date\", Date, nullable=False), \n", + " Column(\"date\", Date, nullable=False),\n", ")" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "id": "c9a9571a", "metadata": { "id": "c9a9571a" @@ -271,7 +257,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "id": "81c3081f", "metadata": { "id": "81c3081f" @@ -296,7 +282,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "id": "85fd20fa", "metadata": { "id": "85fd20fa" @@ -307,8 +293,8 @@ "\n", "def insert_obs(obs):\n", " stmt = insert(stocks).values(\n", - " obs_id=obs[0], \n", - " stock_ticker=obs[1], \n", + " obs_id=obs[0],\n", + " stock_ticker=obs[1],\n", " price=obs[2],\n", " date=obs[3]\n", " )\n", @@ -319,7 +305,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 10, "id": "6766f1f7", "metadata": { "id": "6766f1f7" @@ -331,57 +317,70 @@ ] }, { - "cell_type": "code", - "execution_count": 12, - "id": "9721648e", + "cell_type": "markdown", + "source": [ + "We are installing the `langchain_experimental` library here, since the `SQLDatabaseChain` is located there. This might be changed in the future and moved into official `langchain` library." + ], "metadata": { - "id": "9721648e" + "id": "H3_tIYndeNAt" }, - "outputs": [], - "source": [ - "from langchain.sql_database import SQLDatabase\n", - "from langchain.chains import SQLDatabaseChain\n", - "\n", - "db = SQLDatabase(engine)\n", - "sql_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)" - ] + "id": "H3_tIYndeNAt" }, { - "attachments": {}, - "cell_type": "markdown", - "id": "fe2af8c8", + "cell_type": "code", + "source": [ + "!pip install langchain_experimental -qU" + ], "metadata": { - "id": "fe2af8c8" + "id": "KJJz9xXvb1Ej", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "d7a9b285-4fbc-44d0-cab4-ebb715c0118c" }, - "source": [ - "Finally, we will create a tool with this chain. To create a custom tool we need only three inputs:\n", - "* name: the name of the tool (sent to the llm as context)\n", - "* func: the function that will be applied on the LLM's request\n", - "* description: the description of the tool, what it should be used for (sent to the llm as context)" + "id": "KJJz9xXvb1Ej", + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/70.7 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m70.7/70.7 kB\u001b[0m \u001b[31m2.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25h" + ] + } ] }, { "cell_type": "code", - "execution_count": 13, - "id": "7ef519b1", + "execution_count": 12, + "id": "9721648e", "metadata": { - "id": "7ef519b1" + "id": "9721648e", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "df971483-41e8-434b-edf9-6228dab23c8a" }, - "outputs": [], + "outputs": [ + { + "output_type": "stream", + "name": "stderr", + "text": [ + "/usr/local/lib/python3.10/dist-packages/langchain_experimental/sql/base.py:66: UserWarning: Directly instantiating an SQLDatabaseChain with an llm is deprecated. Please instantiate with llm_chain argument or using the from_llm class method.\n", + " warnings.warn(\n" + ] + } + ], "source": [ - "from langchain.agents import Tool\n", + "from langchain.utilities import SQLDatabase\n", + "from langchain_experimental.sql import SQLDatabaseChain\n", "\n", - "sql_tool = Tool(\n", - " name='Stock DB',\n", - " func=sql_chain.run,\n", - " description=\"Useful for when you need to answer questions about stocks \" \\\n", - " \"and their prices.\"\n", - " \n", - ")" + "db = SQLDatabase(engine)\n", + "sql_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)" ] }, { - "attachments": {}, "cell_type": "markdown", "id": "6325f758", "metadata": { @@ -392,7 +391,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "4d732b7a", "metadata": { @@ -403,15 +401,14 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "8ee98631", "metadata": { "id": "8ee98631" }, "source": [ - "Using one of langchain's pre-built agents involves three variables: \n", - "* defining the tools\n", + "Using one of langchain's pre-built agents involves three variables:\n", + "* defining the tools or the toolkit\n", "* defining the llm\n", "* defining the agent type\n", "\n", @@ -419,7 +416,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "1eaf306a", "metadata": { @@ -430,226 +426,193 @@ ] }, { - "attachments": {}, "cell_type": "markdown", - "id": "13951c49", - "metadata": { - "id": "13951c49" - }, - "source": [ - "We will be using only a few tools but you can combine the ones you prefer. Remember that tools are only utility chains under the hood which are chains that serve one specific purpose." - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "id": "aabcff15", - "metadata": { - "id": "aabcff15" - }, "source": [ - "We first need to initialize the tools we'll be using in this example. Here we will want our agent to do math so we will use 'llm-math', the tool we have for solving math problems. This is one of the several tools we can load with the `load_tools` utility - you can check out more by checking out the [docs](https://langchain.readthedocs.io/en/latest/modules/agents/tools.html?highlight=tools#tools)." - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "id": "0c6bd290", + "In this first example we will use slightly different type of agent - SQL Agent which can be instantiated with it's own method `create_sql_agent`. Other agents will be instantiated in more generic way as we will see below in other examples.\n", + "

\n", + "This method uses *toolkit* instead of simple list of `tools`. You can read more about them in the [documentation](https://python.langchain.com/docs/modules/agents/toolkits/). For this use case, we will use `SQLDatabaseToolkit`." + ], "metadata": { - "id": "0c6bd290" + "id": "Tgn6dRLEcxli" }, - "outputs": [], - "source": [ - "from langchain.agents import load_tools\n", - "\n", - "tools = load_tools(\n", - " [\"llm-math\"], \n", - " llm=llm\n", - ")" - ] + "id": "Tgn6dRLEcxli" }, { - "attachments": {}, "cell_type": "markdown", - "id": "bf01b924", - "metadata": { - "id": "bf01b924" - }, "source": [ - "To be able to search across our stock prices, we will also use the custom tool we built beforehand with the sql data ('Stock DB'). We will append this tool to our list of tools." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "id": "e9d016d7", + "As the name suggests, we will use this agent to perform 'zero shot' tasks on the input. That means that we will not have several, interdependent interactions but only one. In other words, this agent will have no memory." + ], "metadata": { - "id": "e9d016d7" + "id": "mie_37ERl_ac" }, - "outputs": [], - "source": [ - "tools.append(sql_tool)" - ] + "id": "mie_37ERl_ac" }, { - "attachments": {}, "cell_type": "markdown", - "id": "bc9bf6b5", - "metadata": { - "id": "bc9bf6b5" - }, "source": [ - "As the name suggests, we will use this agent to perform 'zero shot' tasks on the input. That means that we will not have several, interdependent interactions but only one. In other words, this agent will have no memory." - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "id": "a6e51d51", + "Now we are ready to initialize the agent! We will use `verbose` in `True` so we can see what is our agent's 'thinking' process." + ], "metadata": { - "id": "a6e51d51" + "id": "O_pYBlCcl11l" }, - "source": [ - "Now we are ready to initialize the agent! We will use `verbose` in `True` so we can see what is our agent's 'thinking' process." - ] + "id": "O_pYBlCcl11l" }, { - "attachments": {}, "cell_type": "markdown", - "id": "ca2d95ce", - "metadata": { - "id": "ca2d95ce" - }, "source": [ "**Important Note:** *When interacting with agents it is really important to set the `max_iterations` parameters because agents can get stuck in infinite loops that consume plenty of tokens. The default value is 15 to allow for many tools and complex reasoning but for most applications you should keep it much lower.*" - ] + ], + "metadata": { + "id": "GtSMUCaIlupp" + }, + "id": "GtSMUCaIlupp" }, { "cell_type": "code", - "execution_count": 16, - "id": "5e4eb28c", - "metadata": { - "id": "5e4eb28c" - }, - "outputs": [], "source": [ - "from langchain.agents import initialize_agent\n", + "from langchain.agents import create_sql_agent\n", + "from langchain.agents.agent_toolkits import SQLDatabaseToolkit\n", + "from langchain.agents.agent_types import AgentType\n", "\n", - "zero_shot_agent = initialize_agent(\n", - " agent=\"zero-shot-react-description\", \n", - " tools=tools, \n", + "agent_executor = create_sql_agent(\n", " llm=llm,\n", + " toolkit=SQLDatabaseToolkit(db=db, llm=llm),\n", " verbose=True,\n", - " max_iterations=3,\n", + " agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n", + " max_iterations=3\n", ")" - ] + ], + "metadata": { + "id": "5Z4EmMmqiOvZ" + }, + "id": "5Z4EmMmqiOvZ", + "execution_count": 13, + "outputs": [] }, { - "attachments": {}, "cell_type": "markdown", - "id": "28c239c0", - "metadata": { - "id": "28c239c0" - }, "source": [ "Let's see our newly created agent in action! We will ask it a question that involves a math operation over the stock prices." - ] + ], + "metadata": { + "id": "iBqz6aFzj-2d" + }, + "id": "iBqz6aFzj-2d" }, { "cell_type": "code", - "execution_count": 17, - "id": "9c775bc6", + "source": [ + "result = count_tokens(\n", + " agent_executor,\n", + " \"What is the multiplication of the ratio between stock \" +\n", + " \"prices for 'ABC' and 'XYZ' in January 3rd and the ratio \" +\n", + " \"between the same stock prices in January the 4th?\"\n", + ")" + ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, - "id": "9c775bc6", - "outputId": "979f31fc-c606-473d-deb3-3b1dc0222a9e" + "id": "MdvgpwHRic3W", + "outputId": "11090a7b-6ee2-45b5-b91e-0ac42a49e386" }, + "id": "MdvgpwHRic3W", + "execution_count": 14, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3m I need to compare the stock prices of 'ABC' and 'XYZ' on two different days\n", - "Action: Stock DB\n", - "Action Input: Stock prices of 'ABC' and 'XYZ' on January 3rd and January 4th\u001b[0m\n", - "\n", - "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n", - "Stock prices of 'ABC' and 'XYZ' on January 3rd and January 4th \n", - "SQLQuery:\u001b[32;1m\u001b[1;3m SELECT stock_ticker, price, date FROM stocks WHERE (stock_ticker = 'ABC' OR stock_ticker = 'XYZ') AND (date = '2023-01-03' OR date = '2023-01-04')\u001b[0m\n", - "SQLResult: \u001b[33;1m\u001b[1;3m[('ABC', 232.0, '2023-01-03'), ('ABC', 225.0, '2023-01-04'), ('XYZ', 798.0, '2023-01-03'), ('XYZ', 795.0, '2023-01-04')]\u001b[0m\n", - "Answer:\u001b[32;1m\u001b[1;3m The stock prices of 'ABC' and 'XYZ' on January 3rd and January 4th were 232.0 and 798.0 respectively on January 3rd and 225.0 and 795.0 respectively on January 4th.\u001b[0m\n", - "\u001b[1m> Finished chain.\u001b[0m\n", + "\u001b[32;1m\u001b[1;3mAction: sql_db_list_tables\n", + "Action Input: \u001b[0m\n", + "Observation: \u001b[38;5;200m\u001b[1;3mstocks\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I should query the schema of the stocks table\n", + "Action: sql_db_schema\n", + "Action Input: stocks\u001b[0m\n", + "Observation: \u001b[33;1m\u001b[1;3m\n", + "CREATE TABLE stocks (\n", + "\tobs_id INTEGER NOT NULL, \n", + "\tstock_ticker VARCHAR(4) NOT NULL, \n", + "\tprice FLOAT NOT NULL, \n", + "\tdate DATE NOT NULL, \n", + "\tPRIMARY KEY (obs_id)\n", + ")\n", "\n", - "Observation: \u001b[33;1m\u001b[1;3m The stock prices of 'ABC' and 'XYZ' on January 3rd and January 4th were 232.0 and 798.0 respectively on January 3rd and 225.0 and 795.0 respectively on January 4th.\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I need to calculate the ratio between the two stock prices on each day\n", - "Action: Calculator\n", - "Action Input: 232.0/798.0 and 225.0/795.0\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mAnswer: 0.2907268170426065\n", - "0.2830188679245283\n", - "\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I need to calculate the multiplication of the two ratios\n", - "Action: Calculator\n", - "Action Input: 0.2907268170426065 * 0.2830188679245283\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mAnswer: 0.08228117463469994\n", - "\u001b[0m\n", + "/*\n", + "3 rows from stocks table:\n", + "obs_id\tstock_ticker\tprice\tdate\n", + "1\tABC\t200.0\t2023-01-01\n", + "2\tABC\t208.0\t2023-01-02\n", + "3\tABC\t232.0\t2023-01-03\n", + "*/\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I should query the ratio between the stock prices for 'ABC' and 'XYZ' in January 3rd and the ratio between the same stock prices in January the 4th\n", + "Action: sql_db_query\n", + "Action Input: SELECT (SELECT price FROM stocks WHERE stock_ticker = 'ABC' AND date = '2023-01-03') / (SELECT price FROM stocks WHERE stock_ticker = 'XYZ' AND date = '2023-01-03') / (SELECT price FROM stocks WHERE stock_ticker = 'ABC' AND date = '2023-01-04') / (SELECT price FROM stocks WHERE stock_ticker = 'XYZ' AND date = '2023-01-04')\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3m[(1.6253071532780239e-06,)]\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", - "Spent a total of 2334 tokens\n" + "Spent a total of 2378 tokens\n" ] } - ], - "source": [ - "result = count_tokens(\n", - " zero_shot_agent, \n", - " \"What is the multiplication of the ratio between stock \" +\n", - " \"prices for 'ABC' and 'XYZ' in January 3rd and the ratio \" + \n", - " \"between the same stock prices in January the 4th?\"\n", - ")" ] }, { - "attachments": {}, "cell_type": "markdown", - "id": "b8b23c42", - "metadata": { - "id": "b8b23c42" - }, "source": [ "As always, let's see what the prompt is here:" - ] + ], + "metadata": { + "id": "LqzuNMgWmFXS" + }, + "id": "LqzuNMgWmFXS" }, { "cell_type": "code", - "execution_count": 18, - "id": "3e50e81b", + "source": [ + "print(agent_executor.agent.llm_chain.prompt.template)" + ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, - "id": "3e50e81b", - "outputId": "c1fb3eb0-01b5-46e1-f17b-d1df2bff2df3" + "id": "ny7m_wMCmF4b", + "outputId": "225a840f-f306-4edc-dcaa-963b0d2a9da3" }, + "id": "ny7m_wMCmF4b", + "execution_count": 15, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ - "Answer the following questions as best you can. You have access to the following tools:\n", + "You are an agent designed to interact with a SQL database.\n", + "Given an input question, create a syntactically correct sqlite query to run, then look at the results of the query and return the answer.\n", + "Unless the user specifies a specific number of examples they wish to obtain, always limit your query to at most 10 results.\n", + "You can order the results by a relevant column to return the most interesting examples in the database.\n", + "Never query for all the columns from a specific table, only ask for the relevant columns given the question.\n", + "You have access to tools for interacting with the database.\n", + "Only use the below tools. Only use the information returned by the below tools to construct your final answer.\n", + "You MUST double check your query before executing it. If you get an error while executing a query, rewrite the query and try again.\n", + "\n", + "DO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the database.\n", "\n", - "Calculator: Useful for when you need to answer questions about math.\n", - "Stock DB: Useful for when you need to answer questions about stocks and their prices.\n", + "If the question does not seem related to the database, just return \"I don't know\" as the answer.\n", + "\n", + "\n", + "sql_db_query: Input to this tool is a detailed and correct SQL query, output is a result from the database. If the query is not correct, an error message will be returned. If an error is returned, rewrite the query, check the query, and try again. If you encounter an issue with Unknown column 'xxxx' in 'field list', using sql_db_schema to query the correct table fields.\n", + "sql_db_schema: Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables. Be sure that the tables actually exist by calling sql_db_list_tables first! Example Input: 'table1, table2, table3'\n", + "sql_db_list_tables: Input is an empty string, output is a comma separated list of tables in the database.\n", + "sql_db_query_checker: Use this tool to double check if your query is correct before executing it. Always use this tool before executing a query with sql_db_query!\n", "\n", "Use the following format:\n", "\n", "Question: the input question you must answer\n", "Thought: you should always think about what to do\n", - "Action: the action to take, should be one of [Calculator, Stock DB]\n", + "Action: the action to take, should be one of [sql_db_query, sql_db_schema, sql_db_list_tables, sql_db_query_checker]\n", "Action Input: the input to the action\n", "Observation: the result of the action\n", "... (this Thought/Action/Action Input/Observation can repeat N times)\n", @@ -659,16 +622,13 @@ "Begin!\n", "\n", "Question: {input}\n", - "Thought:{agent_scratchpad}\n" + "Thought: I should look at the tables in the database to see what I can query. Then I should query the schema of the most relevant tables.\n", + "{agent_scratchpad}\n" ] } - ], - "source": [ - "print(zero_shot_agent.agent.llm_chain.prompt.template)" ] }, { - "attachments": {}, "cell_type": "markdown", "id": "5b2b47ed", "metadata": { @@ -679,7 +639,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "f73aaf07", "metadata": { @@ -690,18 +649,16 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "3a698f78", "metadata": { "id": "3a698f78" }, "source": [ - "Suffice it to say for now that **the LLM now has the ability to 'reason' on how to best use tools** to solve our query and can combine them in intelligent ways with just a brief description of each of them. If you want to learn more about this paradigm (MRKL) in detail, please refer to [this](https://arxiv.org/pdf/2205.00445.pdf) paper. " + "Suffice it to say for now that **the LLM now has the ability to 'reason' on how to best use tools** to solve our query and can combine them in intelligent ways with just a brief description of each of them. If you want to learn more about this paradigm (MRKL) in detail, please refer to [this](https://arxiv.org/pdf/2205.00445.pdf) paper." ] }, { - "attachments": {}, "cell_type": "markdown", "id": "aff9e65d", "metadata": { @@ -712,7 +669,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "04d70642", "metadata": { @@ -723,7 +679,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "1ec589b1", "metadata": { @@ -734,45 +689,33 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "5c26c50f", "metadata": { "id": "5c26c50f" }, "source": [ - "We will use the same tools as we specified earlier:" + "We will use the math tool in this example and load it as below:" ] }, { "cell_type": "code", - "execution_count": 19, - "id": "11ae1e8e", + "execution_count": 16, + "id": "4b6faff3", "metadata": { - "id": "11ae1e8e" + "id": "4b6faff3" }, "outputs": [], "source": [ + "from langchain.agents import load_tools\n", + "\n", "tools = load_tools(\n", - " [\"llm-math\"], \n", + " [\"llm-math\"],\n", " llm=llm\n", ")" ] }, { - "cell_type": "code", - "execution_count": 20, - "id": "4b6faff3", - "metadata": { - "id": "4b6faff3" - }, - "outputs": [], - "source": [ - "tools.append(sql_tool)" - ] - }, - { - "attachments": {}, "cell_type": "markdown", "id": "be45b903", "metadata": { @@ -784,7 +727,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 17, "id": "0aff4edf", "metadata": { "id": "0aff4edf" @@ -798,16 +741,18 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 29, "id": "6579cef0", "metadata": { "id": "6579cef0" }, "outputs": [], "source": [ + "from langchain.agents import initialize_agent\n", + "\n", "conversational_agent = initialize_agent(\n", - " agent='conversational-react-description', \n", - " tools=tools, \n", + " agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,\n", + " tools=tools,\n", " llm=llm,\n", " verbose=True,\n", " max_iterations=3,\n", @@ -817,53 +762,40 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 30, "id": "cabbea50", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "cabbea50", - "outputId": "6bdd0b13-134f-4dfd-9fb7-66c746e5a07a" + "outputId": "c1289467-d0f7-4383-e492-95fffc6d0dc7" }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3m\n", - "Thought: Do I need to use a tool? Yes\n", - "Action: Stock DB\n", - "Action Input: ABC on January the 1st\u001b[0m\n", - "\n", - "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n", - "ABC on January the 1st \n", - "SQLQuery:\u001b[32;1m\u001b[1;3m SELECT price FROM stocks WHERE stock_ticker = 'ABC' AND date = '2023-01-01'\u001b[0m\n", - "SQLResult: \u001b[33;1m\u001b[1;3m[(200.0,)]\u001b[0m\n", - "Answer:\u001b[32;1m\u001b[1;3m The price of ABC on January the 1st was 200.0.\u001b[0m\n", - "\u001b[1m> Finished chain.\u001b[0m\n", - "\n", - "Observation: \u001b[33;1m\u001b[1;3m The price of ABC on January the 1st was 200.0.\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m Do I need to use a tool? No\n", - "AI: Is there anything else I can help you with?\u001b[0m\n", + "Thought: Do I need to use a tool? No\n", + "AI: The total revenue from cupcake sales in the morning is $300.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", - "Spent a total of 1754 tokens\n" + "Spent a total of 728 tokens\n" ] } ], "source": [ "result = count_tokens(\n", - " conversational_agent, \n", - " \"Please provide me the stock prices for ABC on January the 1st\"\n", - " )" + " conversational_agent,\n", + " \"A bakery sold 120 cupcakes in the morning and 80 cupcakes in the afternoon. Each cupcake costs $2.50. What is the total revenue from cupcake sales in the morning?\"\n", + ")" ] }, { - "attachments": {}, "cell_type": "markdown", "id": "bdcd9847", "metadata": { @@ -875,19 +807,19 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 31, "id": "de413fd3", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "de413fd3", - "outputId": "faf8eeb7-94df-4e92-f1a8-dfde49d1703c" + "outputId": "2ffea0db-d7c0-49a8-805b-06090f7f723b" }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "Assistant is a large language model trained by OpenAI.\n", "\n", @@ -903,13 +835,12 @@ "Assistant has access to the following tools:\n", "\n", "> Calculator: Useful for when you need to answer questions about math.\n", - "> Stock DB: Useful for when you need to answer questions about stocks and their prices.\n", "\n", "To use a tool, please use the following format:\n", "\n", "```\n", "Thought: Do I need to use a tool? Yes\n", - "Action: the action to take, should be one of [Calculator, Stock DB]\n", + "Action: the action to take, should be one of [Calculator]\n", "Action Input: the input to the action\n", "Observation: the result of the action\n", "```\n", @@ -936,75 +867,51 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "32ee8041", "metadata": { "id": "32ee8041" }, "source": [ - "The conversational assitant is towards conversation and has a bit more trouble combining a complex combination of tools. Let's see what happens if we make it try and answer the same question we posed to our previous agent:" + "Let's see what happens if we try to answer the question that is related to the previous one:" ] }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 32, "id": "5e109878", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "5e109878", - "outputId": "6d7cab70-338e-4d88-9257-f34dee165514" + "outputId": "2e4dd1d3-5af2-4d2f-9684-8f4bc88907fa" }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3mThought: Do I need to use a tool? Yes\n", - "Action: Stock DB\n", - "Action Input: Get the ratio of the prices of stocks 'ABC' and 'XYZ' in January 3rd and the ratio of the same prices of the same stocks in January the 4th\u001b[0m\n", - "\n", - "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n", - "Get the ratio of the prices of stocks 'ABC' and 'XYZ' in January 3rd and the ratio of the same prices of the same stocks in January the 4th \n", - "SQLQuery:\u001b[32;1m\u001b[1;3m SELECT (SELECT price FROM stocks WHERE stock_ticker = 'ABC' AND date = '2023-01-03') / (SELECT price FROM stocks WHERE stock_ticker = 'XYZ' AND date = '2023-01-03') AS ratio_jan_3, (SELECT price FROM stocks WHERE stock_ticker = 'ABC' AND date = '2023-01-04') / (SELECT price FROM stocks WHERE stock_ticker = 'XYZ' AND date = '2023-01-04') AS ratio_jan_4 FROM stocks LIMIT 5;\u001b[0m\n", - "SQLResult: \u001b[33;1m\u001b[1;3m[(0.2907268170426065, 0.2830188679245283), (0.2907268170426065, 0.2830188679245283), (0.2907268170426065, 0.2830188679245283), (0.2907268170426065, 0.2830188679245283), (0.2907268170426065, 0.2830188679245283)]\u001b[0m\n", - "Answer:\u001b[32;1m\u001b[1;3m The ratio of the prices of stocks 'ABC' and 'XYZ' in January 3rd is 0.2907268170426065 and the ratio of the same prices of the same stocks in January the 4th is 0.2830188679245283.\u001b[0m\n", - "\u001b[1m> Finished chain.\u001b[0m\n", - "\n", - "Observation: \u001b[33;1m\u001b[1;3m The ratio of the prices of stocks 'ABC' and 'XYZ' in January 3rd is 0.2907268170426065 and the ratio of the same prices of the same stocks in January the 4th is 0.2830188679245283.\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m Do I need to use a tool? No\n", - "AI: The answer is 0.4444444444444444. Is there anything else I can help you with?\u001b[0m\n", + "\u001b[32;1m\u001b[1;3m\n", + "Thought: Do I need to use a tool? No\n", + "AI: The total revenue from cupcake sales for the entire day is $450.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", - "Spent a total of 2518 tokens\n" + "Spent a total of 763 tokens\n" ] } ], "source": [ "result = count_tokens(\n", - " conversational_agent, \n", - " \"What is the multiplication of the ratio of the prices of stocks 'ABC' and 'XYZ' in January 3rd and the ratio of the same prices of the same stocks in January the 4th?\"\n", + " conversational_agent,\n", + " \"What is the total revenue from cupcake sales for the entire day?\"\n", ")" ] }, { - "attachments": {}, - "cell_type": "markdown", - "id": "fe11c646", - "metadata": { - "id": "fe11c646" - }, - "source": [ - "As we can see, it solves the problem but in a different manner, by using the SQL tool to solve the math as well. Interesting!" - ] - }, - { - "attachments": {}, "cell_type": "markdown", "id": "44135b8d", "metadata": { @@ -1015,7 +922,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "1e6d0d13", "metadata": { @@ -1026,7 +932,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "d569fd41", "metadata": { @@ -1038,7 +943,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 34, "id": "ecc452af", "metadata": { "id": "ecc452af" @@ -1046,7 +951,7 @@ "outputs": [], "source": [ "from langchain import Wikipedia\n", - "from langchain.agents.react.base import DocstoreExplorer\n", + "from langchain.agents.react.base import DocstoreExplorer, Tool\n", "\n", "docstore=DocstoreExplorer(Wikipedia())\n", "tools = [\n", @@ -1065,7 +970,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 35, "id": "595938a1", "metadata": { "id": "595938a1" @@ -1073,9 +978,9 @@ "outputs": [], "source": [ "docstore_agent = initialize_agent(\n", - " tools, \n", - " llm, \n", - " agent=\"react-docstore\", \n", + " tools,\n", + " llm,\n", + " agent=\"react-docstore\",\n", " verbose=True,\n", " max_iterations=3\n", ")" @@ -1083,48 +988,51 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 36, "id": "bba6b065", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "bba6b065", - "outputId": "40179869-4d21-432c-fba3-2f3f548b3034" + "outputId": "84348634-8ee5-4151-dfc0-230b47d4416d" }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3mThought: I need to search Archimedes and find his last words.\n", "Action: Search[Archimedes]\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mArchimedes of Syracuse (; c. 287 – c. 212 BC) was a Greek mathematician, physicist, engineer, astronomer, and inventor from the ancient city of Syracuse in Sicily. Although few details of his life are known, he is regarded as one of the leading scientists in classical antiquity. Considered the greatest mathematician of ancient history, and one of the greatest of all time, Archimedes anticipated modern calculus and analysis by applying the concept of the infinitely small and the method of exhaustion to derive and rigorously prove a range of geometrical theorems. These include the area of a circle, the surface area and volume of a sphere, the area of an ellipse, the area under a parabola, the volume of a segment of a paraboloid of revolution, the volume of a segment of a hyperboloid of revolution, and the area of a spiral.Archimedes' other mathematical achievements include deriving an approximation of pi, defining and investigating the Archimedean spiral, and devising a system using exponentiation for expressing very large numbers. He was also one of the first to apply mathematics to physical phenomena, working on statics and hydrostatics. Archimedes' achievements in this area include a proof of the law of the lever, the widespread use of the concept of center of gravity, and the enunciation of the law of buoyancy or Archimedes' principle. He is also credited with designing innovative machines, such as his screw pump, compound pulleys, and defensive war machines to protect his native Syracuse from invasion.\n", + "Observation: \u001b[36;1m\u001b[1;3mArchimedes of Syracuse (, ARK-ihm-EE-deez; c. 287 – c. 212 BC) was an Ancient Greek mathematician, physicist, engineer, astronomer, and inventor from the ancient city of Syracuse in Sicily. Although few details of his life are known, he is regarded as one of the leading scientists in classical antiquity. Considered the greatest mathematician of ancient history, and one of the greatest of all time, Archimedes anticipated modern calculus and analysis by applying the concept of the infinitely small and the method of exhaustion to derive and rigorously prove a range of geometrical theorems. These include the area of a circle, the surface area and volume of a sphere, the area of an ellipse, the area under a parabola, the volume of a segment of a paraboloid of revolution, the volume of a segment of a hyperboloid of revolution, and the area of a spiral.Archimedes' other mathematical achievements include deriving an approximation of pi, defining and investigating the Archimedean spiral, and devising a system using exponentiation for expressing very large numbers. He was also one of the first to apply mathematics to physical phenomena, working on statics and hydrostatics. Archimedes' achievements in this area include a proof of the law of the lever, the widespread use of the concept of center of gravity, and the enunciation of the law of buoyancy or Archimedes' principle. He is also credited with designing innovative machines, such as his screw pump, compound pulleys, and defensive war machines to protect his native Syracuse from invasion.\n", "Archimedes died during the siege of Syracuse, when he was killed by a Roman soldier despite orders that he should not be harmed. Cicero describes visiting Archimedes' tomb, which was surmounted by a sphere and a cylinder that Archimedes requested be placed there to represent his mathematical discoveries.\n", "Unlike his inventions, Archimedes' mathematical writings were little known in antiquity. Mathematicians from Alexandria read and quoted him, but the first comprehensive compilation was not made until c. 530 AD by Isidore of Miletus in Byzantine Constantinople, while commentaries on the works of Archimedes by Eutocius in the 6th century opened them to wider readership for the first time. The relatively few copies of Archimedes' written work that survived through the Middle Ages were an influential source of ideas for scientists during the Renaissance and again in the 17th century, while the discovery in 1906 of previously lost works by Archimedes in the Archimedes Palimpsest has provided new insights into how he obtained mathematical results.\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m The paragraph does not mention Archimedes' last words. I need to look up \"last words\".\n", "Action: Lookup[last words]\u001b[0m\n", - "Observation: \u001b[33;1m\u001b[1;3m(Result 1/1) Plutarch (45–119 AD) wrote in his Parallel Lives that Archimedes was related to King Hiero II, the ruler of Syracuse. He also provides at least two accounts on how Archimedes died after the city was taken. According to the most popular account, Archimedes was contemplating a mathematical diagram when the city was captured. A Roman soldier commanded him to come and meet Marcellus, but he declined, saying that he had to finish working on the problem. This enraged the soldier, who killed Archimedes with his sword. Another story has Archimedes carrying mathematical instruments before being killed because a soldier thought they were valuable items. Marcellus was reportedly angered by Archimedes' death, as he considered him a valuable scientific asset (he called Archimedes \"a geometrical Briareus\") and had ordered that he should not be harmed.The last words attributed to Archimedes are \"Do not disturb my circles\" (Latin, \"Noli turbare circulos meos\"; Katharevousa Greek, \"μὴ μου τοὺς κύκλους τάραττε\"), a reference to the mathematical drawing that he was supposedly studying when disturbed by the Roman soldier. There is no reliable evidence that Archimedes uttered these words and they do not appear in Plutarch's account. A similar quotation is found in the work of Valerius Maximus (fl. 30 AD), who wrote in Memorable Doings and Sayings, \"... sed protecto manibus puluere 'noli' inquit, 'obsecro, istum disturbare'\" (\"... but protecting the dust with his hands, said 'I beg of you, do not disturb this'\").\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m The last words attributed to Archimedes are \"Do not disturb my circles\", so the answer is \"Do not disturb my circles\".\n", - "Action: Finish[\"Do not disturb my circles\"]\u001b[0m\n", + "Observation: \u001b[33;1m\u001b[1;3m(Result 1/1) \n", + "== Biography ==\n", + "Archimedes was born c. 287 BC in the seaport city of Syracuse, Sicily, at that time a self-governing colony in Magna Graecia. The date of birth is based on a statement by the Byzantine Greek historian John Tzetzes that Archimedes lived for 75 years before his death in 212 BC. In the Sand-Reckoner, Archimedes gives his father's name as Phidias, an astronomer about whom nothing else is known. A biography of Archimedes was written by his friend Heracleides, but this work has been lost, leaving the details of his life obscure. It is unknown, for instance, whether he ever married or had children, or if he ever visited Alexandria, Egypt, during his youth. From his surviving written works, it is clear that he maintained collegiate relations with scholars based there, including his friend Conon of Samos and the head librarian Eratosthenes of Cyrene.The standard versions of Archimedes' life were written long after his death by Greek and Roman historians. The earliest reference to Archimedes occurs in The Histories by Polybius (c. 200–118 BC), written about 70 years after his death. It sheds little light on Archimedes as a person, and focuses on the war machines that he is said to have built in order to defend the city from the Romans. Polybius remarks how, during the Second Punic War, Syracuse switched allegiances from Rome to Carthage, resulting in a military campaign under the command of Marcus Claudius Marcellus and Appius Claudius Pulcher, who besieged the city from 213 to 212 BC. He notes that the Romans underestimated Syracuse's defenses, and mentions several machines Archimedes designed, including improved catapults, crane-like machines that could be swung around in an arc, and other stone-throwers. Although the Romans ultimately captured the city, they suffered considerable losses due to Archimedes' inventiveness.Cicero (106–43 BC) mentions Archimedes in some of his works. While serving as a quaestor in Sicily, Cicero found what was presumed to be Archimedes' tomb near the Agrigentine gate in Syracuse, in a neglected condition and overgrown with bushes. Cicero had the tomb cleaned up and was able to see the carving and read some of the verses that had been added as an inscription. The tomb carried a sculpture illustrating Archimedes' favorite mathematical proof, that the volume and surface area of the sphere are two-thirds that of an enclosing cylinder including its bases. He also mentions that Marcellus brought to Rome two planetariums Archimedes built. The Roman historian Livy (59 BC–17 AD) retells Polybius' story of the capture of Syracuse and Archimedes' role in it.\n", + "Plutarch (45–119 AD) wrote in his Parallel Lives that Archimedes was related to King Hiero II, the ruler of Syracuse. He also provides at least two accounts on how Archimedes died after the city was taken. According to the most popular account, Archimedes was contemplating a mathematical diagram when the city was captured. A Roman soldier commanded him to come and meet Marcellus, but he declined, saying that he had to finish working on the problem. This enraged the soldier, who killed Archimedes with his sword. Another story has Archimedes carrying mathematical instruments before being killed because a soldier thought they were valuable items. Marcellus was reportedly angered by Archimedes' death, as he considered him a valuable scientific asset (he called Archimedes \"a geometrical Briareus\") and had ordered that he should not be harmed.The last words attributed to Archimedes are \"Do not disturb my circles\" (Latin, \"Noli turbare circulos meos\"; Katharevousa Greek, \"μὴ μου τοὺς κύκλους τάραττε\"), a reference to the mathematical drawing that he was supposedly studying when disturbed by the Roman soldier. There is no reliable evidence that Archimedes uttered these words and they do not appear in Plutarch's account. A similar quotation is found in the work of Valerius Maximus (fl. 30 AD), who wrote in Memorable Doings and Sayings, \"... sed protecto manibus puluere 'noli' inquit, 'obsecro, istum disturbare'\" (\"... but protecting the dust with his hands, said 'I beg of you, do not disturb this'\").\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m The paragraph mentions that the last words attributed to Archimedes are \"Do not disturb my circles\". So the answer is \"Do not disturb my circles\".\n", + "Action: Finish[Do not disturb my circles]\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", - "Spent a total of 6064 tokens\n" + "Spent a total of 6692 tokens\n" ] }, { + "output_type": "execute_result", "data": { "text/plain": [ "{'input': \"What were Archimedes' last words?\",\n", - " 'output': '\"Do not disturb my circles\"'}" + " 'output': 'Do not disturb my circles'}" ] }, - "execution_count": 30, "metadata": {}, - "output_type": "execute_result" + "execution_count": 36 } ], "source": [ @@ -1132,7 +1040,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "6a5f77ac", "metadata": { @@ -1147,7 +1054,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "4aae3b5b", "metadata": { @@ -1158,19 +1064,18 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "c07039e5", "metadata": { "id": "c07039e5" }, "source": [ - "This is the first-choice agent to use when using LLM's to extract information with a search engine. The agent will ask follow-up questions and use the search functionality to get intermediate answers that help it get to a final answer. " + "This is the first-choice agent to use when using LLM's to extract information with a search engine. The agent will ask follow-up questions and use the search functionality to get intermediate answers that help it get to a final answer." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "id": "903660b2", "metadata": { "id": "903660b2" @@ -1193,7 +1098,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "0ec1c4c8", "metadata": { @@ -1205,16 +1109,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "id": "e4134930", "metadata": { "id": "e4134930", - "outputId": "eed4d667-5e20-4db8-e8e2-75313c022928" + "outputId": "fb4c9648-c01c-4d26-8f12-7508c166446c", + "colab": { + "base_uri": "https://localhost:8080/" + } }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "Question: Who lived longer, Muhammad Ali or Alan Turing?\n", "Are follow up questions needed here: Yes.\n", @@ -1262,7 +1169,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "3e7790ae", "metadata": { @@ -1273,7 +1179,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "5df324ba", "metadata": { @@ -1284,7 +1189,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "4cd80a08", "metadata": { @@ -1295,7 +1199,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "97d108e2", "metadata": { @@ -1309,7 +1212,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "e33dd90a", "metadata": { @@ -1325,21 +1227,11 @@ "provenance": [] }, "kernelspec": { - "display_name": "langchain", - "language": "python", - "name": "langchain" + "display_name": "Python 3", + "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" + "name": "python" }, "vscode": { "interpreter": { @@ -1349,4 +1241,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} +} \ No newline at end of file From 4589f13f888878b52024db74123f508a889c512c Mon Sep 17 00:00:00 2001 From: DosticJelena Date: Wed, 23 Aug 2023 09:52:55 +0200 Subject: [PATCH 2/2] Update example used by conversational agent --- .../handbook/06-langchain-agents.ipynb | 98 ++++++++++--------- 1 file changed, 53 insertions(+), 45 deletions(-) diff --git a/learn/generation/langchain/handbook/06-langchain-agents.ipynb b/learn/generation/langchain/handbook/06-langchain-agents.ipynb index 0987fe70..4acd1e01 100644 --- a/learn/generation/langchain/handbook/06-langchain-agents.ipynb +++ b/learn/generation/langchain/handbook/06-langchain-agents.ipynb @@ -37,18 +37,18 @@ "base_uri": "https://localhost:8080/" }, "id": "uZR3iGJJtdDE", - "outputId": "04116858-96f2-447d-83d4-324f1c1f7164" + "outputId": "0119ff4e-61fc-4d5f-9292-a719c54b8cb5" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ - "\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/1.5 MB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91m━\u001b[0m\u001b[90m╺\u001b[0m\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/1.5 MB\u001b[0m \u001b[31m1.0 MB/s\u001b[0m eta \u001b[36m0:00:02\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.6/1.5 MB\u001b[0m \u001b[31m9.1 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.5/1.5 MB\u001b[0m \u001b[31m16.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[?25h\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/73.6 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m73.6/73.6 kB\u001b[0m \u001b[31m8.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.5/1.5 MB\u001b[0m \u001b[31m10.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.5/75.5 kB\u001b[0m \u001b[31m6.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.4/49.4 kB\u001b[0m \u001b[31m5.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.4/49.4 kB\u001b[0m \u001b[31m4.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Building wheel for google-search-results (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Building wheel for wikipedia (setup.py) ... \u001b[?25l\u001b[?25hdone\n" ] @@ -77,7 +77,7 @@ "base_uri": "https://localhost:8080/" }, "id": "c02c4fa2", - "outputId": "dffcdc44-08ff-44f5-9df2-0f4d40123e71" + "outputId": "ad22dddc-6180-48ac-86dd-265b6b7afa57" }, "outputs": [ { @@ -336,16 +336,16 @@ "colab": { "base_uri": "https://localhost:8080/" }, - "outputId": "d7a9b285-4fbc-44d0-cab4-ebb715c0118c" + "outputId": "9533f802-804d-4c4c-c19f-87827bbc178c" }, "id": "KJJz9xXvb1Ej", - "execution_count": 11, + "execution_count": 12, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ - "\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/70.7 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m70.7/70.7 kB\u001b[0m \u001b[31m2.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/70.7 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m70.7/70.7 kB\u001b[0m \u001b[31m2.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h" ] } @@ -353,14 +353,14 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "id": "9721648e", "metadata": { "id": "9721648e", "colab": { "base_uri": "https://localhost:8080/" }, - "outputId": "df971483-41e8-434b-edf9-6228dab23c8a" + "outputId": "9e77c155-c662-47c2-b585-deb88e8ad51b" }, "outputs": [ { @@ -486,7 +486,7 @@ "id": "5Z4EmMmqiOvZ" }, "id": "5Z4EmMmqiOvZ", - "execution_count": 13, + "execution_count": 14, "outputs": [] }, { @@ -514,10 +514,10 @@ "base_uri": "https://localhost:8080/" }, "id": "MdvgpwHRic3W", - "outputId": "11090a7b-6ee2-45b5-b91e-0ac42a49e386" + "outputId": "5e63199b-2d92-404a-c712-5e8f4f55e23d" }, "id": "MdvgpwHRic3W", - "execution_count": 14, + "execution_count": 15, "outputs": [ { "output_type": "stream", @@ -529,7 +529,7 @@ "\u001b[32;1m\u001b[1;3mAction: sql_db_list_tables\n", "Action Input: \u001b[0m\n", "Observation: \u001b[38;5;200m\u001b[1;3mstocks\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I should query the schema of the stocks table\n", + "Thought:\u001b[32;1m\u001b[1;3m I should query the schema of the stocks table to see what columns I can use.\n", "Action: sql_db_schema\n", "Action Input: stocks\u001b[0m\n", "Observation: \u001b[33;1m\u001b[1;3m\n", @@ -548,14 +548,14 @@ "2\tABC\t208.0\t2023-01-02\n", "3\tABC\t232.0\t2023-01-03\n", "*/\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I should query the ratio between the stock prices for 'ABC' and 'XYZ' in January 3rd and the ratio between the same stock prices in January the 4th\n", + "Thought:\u001b[32;1m\u001b[1;3m I should query the ratio between the stock prices for 'ABC' and 'XYZ' in January 3rd and the ratio between the same stock prices in January the 4th.\n", "Action: sql_db_query\n", - "Action Input: SELECT (SELECT price FROM stocks WHERE stock_ticker = 'ABC' AND date = '2023-01-03') / (SELECT price FROM stocks WHERE stock_ticker = 'XYZ' AND date = '2023-01-03') / (SELECT price FROM stocks WHERE stock_ticker = 'ABC' AND date = '2023-01-04') / (SELECT price FROM stocks WHERE stock_ticker = 'XYZ' AND date = '2023-01-04')\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3m[(1.6253071532780239e-06,)]\u001b[0m\n", + "Action Input: SELECT (SELECT price FROM stocks WHERE stock_ticker = 'ABC' AND date = '2023-01-03') / (SELECT price FROM stocks WHERE stock_ticker = 'XYZ' AND date = '2023-01-03') / (SELECT price FROM stocks WHERE stock_ticker = 'ABC' AND date = '2023-01-04') * (SELECT price FROM stocks WHERE stock_ticker = 'XYZ' AND date = '2023-01-04') AS ratio;\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3m[(1.027234753550543,)]\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", - "Spent a total of 2378 tokens\n" + "Spent a total of 2398 tokens\n" ] } ] @@ -580,10 +580,10 @@ "base_uri": "https://localhost:8080/" }, "id": "ny7m_wMCmF4b", - "outputId": "225a840f-f306-4edc-dcaa-963b0d2a9da3" + "outputId": "ab8fdf8a-6f83-4f3f-e9dc-7313b48f81ef" }, "id": "ny7m_wMCmF4b", - "execution_count": 15, + "execution_count": 16, "outputs": [ { "output_type": "stream", @@ -700,7 +700,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 17, "id": "4b6faff3", "metadata": { "id": "4b6faff3" @@ -727,7 +727,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 18, "id": "0aff4edf", "metadata": { "id": "0aff4edf" @@ -741,7 +741,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 19, "id": "6579cef0", "metadata": { "id": "6579cef0" @@ -762,14 +762,14 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 20, "id": "cabbea50", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "cabbea50", - "outputId": "c1289467-d0f7-4383-e492-95fffc6d0dc7" + "outputId": "760bde78-a553-46e0-da3b-433cd0196613" }, "outputs": [ { @@ -780,18 +780,22 @@ "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3m\n", - "Thought: Do I need to use a tool? No\n", - "AI: The total revenue from cupcake sales in the morning is $300.\u001b[0m\n", + "Thought: Do I need to use a tool? Yes\n", + "Action: Calculator\n", + "Action Input: 10,000, 8%, 5 years\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mAnswer: 14693.280768000006\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m Do I need to use a tool? No\n", + "AI: The result of an investment of $10,000 growing at 8% annually for 5 years with compound interest is $14,693.28.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", - "Spent a total of 728 tokens\n" + "Spent a total of 1246 tokens\n" ] } ], "source": [ "result = count_tokens(\n", " conversational_agent,\n", - " \"A bakery sold 120 cupcakes in the morning and 80 cupcakes in the afternoon. Each cupcake costs $2.50. What is the total revenue from cupcake sales in the morning?\"\n", + " \"What's the result of an investment of $10,000 growing at 8% annually for 5 years with compound interest?\"\n", ")" ] }, @@ -807,14 +811,14 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 21, "id": "de413fd3", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "de413fd3", - "outputId": "2ffea0db-d7c0-49a8-805b-06090f7f723b" + "outputId": "f79dc083-db80-4dbb-8fb6-da5f6f603645" }, "outputs": [ { @@ -878,14 +882,14 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 22, "id": "5e109878", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "5e109878", - "outputId": "2e4dd1d3-5af2-4d2f-9684-8f4bc88907fa" + "outputId": "51a80167-1e1b-4717-e3bc-bf25ad1c6cb7" }, "outputs": [ { @@ -896,18 +900,22 @@ "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3m\n", - "Thought: Do I need to use a tool? No\n", - "AI: The total revenue from cupcake sales for the entire day is $450.\u001b[0m\n", + "Thought: Do I need to use a tool? Yes\n", + "Action: Calculator\n", + "Action Input: 15000, 8%, 5 years, compound interest\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mAnswer: 22039.92115200001\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m Do I need to use a tool? No\n", + "AI: If we start with $15,000 instead and follow the same 8% annual growth for 5 years with compound interest, we would have $22,039.92, which is $7,346.64 more than the previous scenario.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", - "Spent a total of 763 tokens\n" + "Spent a total of 1416 tokens\n" ] } ], "source": [ "result = count_tokens(\n", " conversational_agent,\n", - " \"What is the total revenue from cupcake sales for the entire day?\"\n", + " \"If we start with $15,000 instead and follow the same 8% annual growth for 5 years with compound interest, how much more would we have compared to the previous scenario?\"\n", ")" ] }, @@ -943,7 +951,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 23, "id": "ecc452af", "metadata": { "id": "ecc452af" @@ -970,7 +978,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 24, "id": "595938a1", "metadata": { "id": "595938a1" @@ -988,14 +996,14 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 25, "id": "bba6b065", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "bba6b065", - "outputId": "84348634-8ee5-4151-dfc0-230b47d4416d" + "outputId": "52d4f8f6-b56b-4894-9f18-dc126be884b4" }, "outputs": [ { @@ -1032,7 +1040,7 @@ ] }, "metadata": {}, - "execution_count": 36 + "execution_count": 25 } ], "source": [ @@ -1075,7 +1083,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 26, "id": "903660b2", "metadata": { "id": "903660b2" @@ -1109,11 +1117,11 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 27, "id": "e4134930", "metadata": { "id": "e4134930", - "outputId": "fb4c9648-c01c-4d26-8f12-7508c166446c", + "outputId": "2f58884d-0c4e-4b40-de45-6928e0c60ef4", "colab": { "base_uri": "https://localhost:8080/" }