{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "5b6ddc1d", "metadata": {}, "outputs": [], "source": [ "#pip install langchain" ] }, { "cell_type": "code", "execution_count": 3, "id": "428de9cd", "metadata": {}, "outputs": [], "source": [ "#pip install --upgrade pip" ] }, { "cell_type": "code", "execution_count": 4, "id": "c6963d71", "metadata": {}, "outputs": [], "source": [ "#pip install --upgrade setuptools" ] }, { "cell_type": "code", "execution_count": 5, "id": "f107f9f5", "metadata": {}, "outputs": [], "source": [ "from langchain.llms import GooglePalm" ] }, { "cell_type": "code", "execution_count": 6, "id": "0cf73123", "metadata": {}, "outputs": [], "source": [ "#pip install google-generativeai" ] }, { "cell_type": "code", "execution_count": 7, "id": "03866604", "metadata": {}, "outputs": [], "source": [ "import google.generativeai as genai" ] }, { "cell_type": "code", "execution_count": 8, "id": "2a11da2a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "**Verse 1**\n", "America, America,\n", "Land of the free, home of the brave.\n", "You're a beacon of hope,\n", "For all the world to see.\n", "**Chorus**\n", "We're proud to be Americans,\n", "We'll fight for our freedom,\n", "We'll stand together,\n", "As one nation, under God.\n", "**Verse 2**\n", "We're a melting pot,\n", "Of cultures and races.\n", "We're all different,\n", "But we're all united.\n", "**Chorus**\n", "We're proud to be Americans,\n", "We'll fight for our freedom,\n", "We'll stand together,\n", "As one nation, under God.\n", "**Bridge**\n", "Through thick and thin,\n", "We'll always stick together.\n", "We're Americans,\n", "And we're proud.\n", "**Chorus**\n", "We're proud to be Americans,\n", "We'll fight for our freedom,\n", "We'll stand together,\n", "As one nation, under God.\n" ] } ], "source": [ "api_key = \"AIzaSyBAsa1XcZysgK7DKo9Bs-9MNfixaRR0ocw\"\n", "llm = GooglePalm(google_api_key=api_key, temperature=0.2)\n", "poem = llm(\"write a song on America in 5 lines\")\n", "print(poem)" ] }, { "cell_type": "code", "execution_count": 9, "id": "09a438b5", "metadata": {}, "outputs": [], "source": [ "#pip install pymysql" ] }, { "cell_type": "code", "execution_count": 10, "id": "74b4e2ef", "metadata": {}, "outputs": [], "source": [ "#pip install mysql-connector-python\n" ] }, { "cell_type": "code", "execution_count": 11, "id": "d0ff0a6a", "metadata": {}, "outputs": [], "source": [ "#pip install mysqlclient\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "ff9174a2", "metadata": {}, "outputs": [], "source": [ "#pip install psycopg2" ] }, { "cell_type": "code", "execution_count": 13, "id": "bf08a5a6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "CREATE TABLE discounts (\n", "\tdiscount_id INTEGER NOT NULL AUTO_INCREMENT, \n", "\tt_shirt_id INTEGER NOT NULL, \n", "\tpct_discount DECIMAL(5, 2), \n", "\tPRIMARY KEY (discount_id), \n", "\tCONSTRAINT discounts_ibfk_1 FOREIGN KEY(t_shirt_id) REFERENCES t_shirts (t_shirt_id), \n", "\tCONSTRAINT discounts_chk_1 CHECK ((`pct_discount` between 0 and 100))\n", ")COLLATE utf8mb4_0900_ai_ci ENGINE=InnoDB DEFAULT CHARSET=utf8mb4\n", "\n", "/*\n", "3 rows from discounts table:\n", "discount_id\tt_shirt_id\tpct_discount\n", "\n", "*/\n", "\n", "\n", "CREATE TABLE t_shirts (\n", "\tt_shirt_id INTEGER NOT NULL AUTO_INCREMENT, \n", "\tbrand ENUM('Van Huesen','Levi','Nike','Adidas') NOT NULL, \n", "\tcolor ENUM('Red','Blue','Black','White') NOT NULL, \n", "\tsize ENUM('XS','S','M','L','XL') NOT NULL, \n", "\tprice INTEGER, \n", "\tstock_quantity INTEGER NOT NULL, \n", "\tPRIMARY KEY (t_shirt_id), \n", "\tCONSTRAINT t_shirts_chk_1 CHECK ((`price` between 10 and 50))\n", ")COLLATE utf8mb4_0900_ai_ci ENGINE=InnoDB DEFAULT CHARSET=utf8mb4\n", "\n", "/*\n", "3 rows from t_shirts table:\n", "t_shirt_id\tbrand\tcolor\tsize\tprice\tstock_quantity\n", "1\tLevi\tWhite\tL\t33\t85\n", "2\tLevi\tBlack\tL\t30\t64\n", "4\tNike\tWhite\tL\t45\t33\n", "*/\n" ] } ], "source": [ "from langchain.utilities import SQLDatabase\n", "\n", "db_user = \"root\"\n", "db_password = \"root\"\n", "db_host = \"localhost\"\n", "db_name = \"atliq_tshirts\"\n", "\n", "db = SQLDatabase.from_uri(f\"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}\",sample_rows_in_table_info=3)\n", "print(db.table_info)" ] }, { "cell_type": "code", "execution_count": 14, "id": "4bad6deb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: langchain-experimental in /opt/homebrew/lib/python3.11/site-packages (0.0.25)\n", "Requirement already satisfied: langchain>=0.0.239 in /opt/homebrew/lib/python3.11/site-packages (from langchain-experimental) (0.0.284)\n", "Requirement already satisfied: PyYAML>=5.3 in /opt/homebrew/lib/python3.11/site-packages (from langchain>=0.0.239->langchain-experimental) (6.0.1)\n", "Requirement already satisfied: SQLAlchemy<3,>=1.4 in /opt/homebrew/lib/python3.11/site-packages (from langchain>=0.0.239->langchain-experimental) (2.0.29)\n", "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /opt/homebrew/lib/python3.11/site-packages (from langchain>=0.0.239->langchain-experimental) (3.9.3)\n", "Requirement already satisfied: dataclasses-json<0.6.0,>=0.5.7 in /opt/homebrew/lib/python3.11/site-packages (from langchain>=0.0.239->langchain-experimental) (0.5.14)\n", "Requirement already satisfied: langsmith<0.1.0,>=0.0.21 in /opt/homebrew/lib/python3.11/site-packages (from langchain>=0.0.239->langchain-experimental) (0.0.92)\n", "Requirement already satisfied: numexpr<3.0.0,>=2.8.4 in /opt/homebrew/lib/python3.11/site-packages (from langchain>=0.0.239->langchain-experimental) (2.10.0)\n", "Requirement already satisfied: numpy<2,>=1 in /opt/homebrew/lib/python3.11/site-packages (from langchain>=0.0.239->langchain-experimental) (1.24.2)\n", "Requirement already satisfied: pydantic<3,>=1 in /opt/homebrew/lib/python3.11/site-packages (from langchain>=0.0.239->langchain-experimental) (2.6.4)\n", "Requirement already satisfied: requests<3,>=2 in /opt/homebrew/lib/python3.11/site-packages (from langchain>=0.0.239->langchain-experimental) (2.31.0)\n", "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /opt/homebrew/lib/python3.11/site-packages (from langchain>=0.0.239->langchain-experimental) (8.2.3)\n", "Requirement already satisfied: aiosignal>=1.1.2 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain>=0.0.239->langchain-experimental) (1.3.1)\n", "Requirement already satisfied: attrs>=17.3.0 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain>=0.0.239->langchain-experimental) (23.2.0)\n", "Requirement already satisfied: frozenlist>=1.1.1 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain>=0.0.239->langchain-experimental) (1.4.1)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain>=0.0.239->langchain-experimental) (6.0.5)\n", "Requirement already satisfied: yarl<2.0,>=1.0 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain>=0.0.239->langchain-experimental) (1.9.4)\n", "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in /opt/homebrew/lib/python3.11/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain>=0.0.239->langchain-experimental) (3.21.1)\n", "Requirement already satisfied: typing-inspect<1,>=0.4.0 in /opt/homebrew/lib/python3.11/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain>=0.0.239->langchain-experimental) (0.9.0)\n", "Requirement already satisfied: annotated-types>=0.4.0 in /opt/homebrew/lib/python3.11/site-packages (from pydantic<3,>=1->langchain>=0.0.239->langchain-experimental) (0.6.0)\n", "Requirement already satisfied: pydantic-core==2.16.3 in /opt/homebrew/lib/python3.11/site-packages (from pydantic<3,>=1->langchain>=0.0.239->langchain-experimental) (2.16.3)\n", "Requirement already satisfied: typing-extensions>=4.6.1 in /opt/homebrew/lib/python3.11/site-packages (from pydantic<3,>=1->langchain>=0.0.239->langchain-experimental) (4.10.0)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/lib/python3.11/site-packages (from requests<3,>=2->langchain>=0.0.239->langchain-experimental) (3.3.2)\n", "Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/lib/python3.11/site-packages (from requests<3,>=2->langchain>=0.0.239->langchain-experimental) (3.6)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/homebrew/lib/python3.11/site-packages (from requests<3,>=2->langchain>=0.0.239->langchain-experimental) (2.2.1)\n", "Requirement already satisfied: certifi>=2017.4.17 in /opt/homebrew/lib/python3.11/site-packages (from requests<3,>=2->langchain>=0.0.239->langchain-experimental) (2024.2.2)\n", "Requirement already satisfied: packaging>=17.0 in /opt/homebrew/lib/python3.11/site-packages (from marshmallow<4.0.0,>=3.18.0->dataclasses-json<0.6.0,>=0.5.7->langchain>=0.0.239->langchain-experimental) (23.2)\n", "Requirement already satisfied: mypy-extensions>=0.3.0 in /opt/homebrew/lib/python3.11/site-packages (from typing-inspect<1,>=0.4.0->dataclasses-json<0.6.0,>=0.5.7->langchain>=0.0.239->langchain-experimental) (1.0.0)\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install langchain-experimental" ] }, { "cell_type": "code", "execution_count": 15, "id": "23d042fa", "metadata": {}, "outputs": [], "source": [ "from langchain_experimental.sql import SQLDatabaseChain\n", "\n", "db_chain = SQLDatabaseChain.from_llm(llm,db,verbose=True)" ] }, { "cell_type": "code", "execution_count": 16, "id": "ac08c264", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n", "how many t-shirts do we have for Levi in extra small and white color\n", "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT stock_quantity FROM t_shirts WHERE brand = 'Levi' AND color = 'White' AND size = 'XS'\u001b[0m\n", "SQLResult: \u001b[33;1m\u001b[1;3m\u001b[0m\n", "Answer:\u001b[32;1m\u001b[1;3m0\u001b[0m\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] } ], "source": [ "qs1 = db_chain.run(\"how many t-shirts do we have for Levi in extra small and white color\")" ] }, { "cell_type": "code", "execution_count": 17, "id": "3cf99da1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n", "how much is the price of the inventory for all small size t-shirts?\n", "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT SUM(price) FROM t_shirts WHERE size = 'S'\u001b[0m\n", "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('371'),)]\u001b[0m\n", "Answer:\u001b[32;1m\u001b[1;3m371\u001b[0m\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] } ], "source": [ "qs2 = db_chain.run(\"how much is the price of the inventory for all small size t-shirts?\")" ] }, { "cell_type": "code", "execution_count": 18, "id": "7944d470", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n", "SELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'\n", "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'\u001b[0m\n", "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('20416'),)]\u001b[0m\n", "Answer:\u001b[32;1m\u001b[1;3m20416\u001b[0m\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] } ], "source": [ "qs2 = db_chain.run(\"SELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'\")" ] }, { "cell_type": "code", "execution_count": 19, "id": "2105b4e3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'20416'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "qs2" ] }, { "cell_type": "code", "execution_count": 20, "id": "083c6c80", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n", "if we have to see all my levi's t-shirts today with discounts applied. how much revenue our store will generate (post discounts)?\n", "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT SUM(price - price * pct_discount) FROM t_shirts JOIN discounts ON t_shirts.t_shirt_id = discounts.t_shirt_id WHERE brand = 'Levi' AND CURDATE() BETWEEN discounts.start_date AND discounts.end_date\u001b[0m" ] }, { "ename": "OperationalError", "evalue": "(pymysql.err.OperationalError) (1054, \"Unknown column 'discounts.start_date' in 'where clause'\")\n[SQL: SELECT SUM(price - price * pct_discount) FROM t_shirts JOIN discounts ON t_shirts.t_shirt_id = discounts.t_shirt_id WHERE brand = 'Levi' AND CURDATE() BETWEEN discounts.start_date AND discounts.end_date]\n(Background on this error at: https://sqlalche.me/e/20/e3q8)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mOperationalError\u001b[0m Traceback (most recent call last)", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/sqlalchemy/engine/base.py:1971\u001b[0m, in \u001b[0;36mConnection._exec_single_context\u001b[0;34m(self, dialect, context, statement, parameters)\u001b[0m\n\u001b[1;32m 1970\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m evt_handled:\n\u001b[0;32m-> 1971\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdialect\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdo_execute\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1972\u001b[0m \u001b[43m \u001b[49m\u001b[43mcursor\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstr_statement\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43meffective_parameters\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcontext\u001b[49m\n\u001b[1;32m 1973\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1975\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_has_events \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mengine\u001b[38;5;241m.\u001b[39m_has_events:\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/sqlalchemy/engine/default.py:919\u001b[0m, in \u001b[0;36mDefaultDialect.do_execute\u001b[0;34m(self, cursor, statement, parameters, context)\u001b[0m\n\u001b[1;32m 918\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdo_execute\u001b[39m(\u001b[38;5;28mself\u001b[39m, cursor, statement, parameters, context\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[0;32m--> 919\u001b[0m \u001b[43mcursor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstatement\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparameters\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/pymysql/cursors.py:153\u001b[0m, in \u001b[0;36mCursor.execute\u001b[0;34m(self, query, args)\u001b[0m\n\u001b[1;32m 151\u001b[0m query \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmogrify(query, args)\n\u001b[0;32m--> 153\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_query\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquery\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 154\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_executed \u001b[38;5;241m=\u001b[39m query\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/pymysql/cursors.py:322\u001b[0m, in \u001b[0;36mCursor._query\u001b[0;34m(self, q)\u001b[0m\n\u001b[1;32m 321\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_clear_result()\n\u001b[0;32m--> 322\u001b[0m \u001b[43mconn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mquery\u001b[49m\u001b[43m(\u001b[49m\u001b[43mq\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 323\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_do_get_result()\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/pymysql/connections.py:558\u001b[0m, in \u001b[0;36mConnection.query\u001b[0;34m(self, sql, unbuffered)\u001b[0m\n\u001b[1;32m 557\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_execute_command(COMMAND\u001b[38;5;241m.\u001b[39mCOM_QUERY, sql)\n\u001b[0;32m--> 558\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_affected_rows \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_read_query_result\u001b[49m\u001b[43m(\u001b[49m\u001b[43munbuffered\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43munbuffered\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 559\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_affected_rows\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/pymysql/connections.py:822\u001b[0m, in \u001b[0;36mConnection._read_query_result\u001b[0;34m(self, unbuffered)\u001b[0m\n\u001b[1;32m 821\u001b[0m result \u001b[38;5;241m=\u001b[39m MySQLResult(\u001b[38;5;28mself\u001b[39m)\n\u001b[0;32m--> 822\u001b[0m \u001b[43mresult\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 823\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_result \u001b[38;5;241m=\u001b[39m result\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/pymysql/connections.py:1200\u001b[0m, in \u001b[0;36mMySQLResult.read\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1199\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m-> 1200\u001b[0m first_packet \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_read_packet\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1202\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m first_packet\u001b[38;5;241m.\u001b[39mis_ok_packet():\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/pymysql/connections.py:772\u001b[0m, in \u001b[0;36mConnection._read_packet\u001b[0;34m(self, packet_type)\u001b[0m\n\u001b[1;32m 771\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_result\u001b[38;5;241m.\u001b[39munbuffered_active \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[0;32m--> 772\u001b[0m \u001b[43mpacket\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mraise_for_error\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 773\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m packet\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/pymysql/protocol.py:221\u001b[0m, in \u001b[0;36mMysqlPacket.raise_for_error\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 220\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124merrno =\u001b[39m\u001b[38;5;124m\"\u001b[39m, errno)\n\u001b[0;32m--> 221\u001b[0m \u001b[43merr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mraise_mysql_exception\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_data\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/pymysql/err.py:143\u001b[0m, in \u001b[0;36mraise_mysql_exception\u001b[0;34m(data)\u001b[0m\n\u001b[1;32m 142\u001b[0m errorclass \u001b[38;5;241m=\u001b[39m InternalError \u001b[38;5;28;01mif\u001b[39;00m errno \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m1000\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m OperationalError\n\u001b[0;32m--> 143\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m errorclass(errno, errval)\n", "\u001b[0;31mOperationalError\u001b[0m: (1054, \"Unknown column 'discounts.start_date' in 'where clause'\")", "\nThe above exception was the direct cause of the following exception:\n", "\u001b[0;31mOperationalError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[20], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m qs3 \u001b[38;5;241m=\u001b[39m \u001b[43mdb_chain\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mif we have to see all my levi\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43ms t-shirts today with discounts applied. how much revenue our store will generate (post discounts)?\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/langchain/chains/base.py:487\u001b[0m, in \u001b[0;36mChain.run\u001b[0;34m(self, callbacks, tags, metadata, *args, **kwargs)\u001b[0m\n\u001b[1;32m 485\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(args) \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m 486\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m`run` supports only one positional argument.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m--> 487\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43margs\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtags\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtags\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmetadata\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmetadata\u001b[49m\u001b[43m)\u001b[49m[\n\u001b[1;32m 488\u001b[0m _output_key\n\u001b[1;32m 489\u001b[0m ]\n\u001b[1;32m 491\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m kwargs \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m args:\n\u001b[1;32m 492\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m(kwargs, callbacks\u001b[38;5;241m=\u001b[39mcallbacks, tags\u001b[38;5;241m=\u001b[39mtags, metadata\u001b[38;5;241m=\u001b[39mmetadata)[\n\u001b[1;32m 493\u001b[0m _output_key\n\u001b[1;32m 494\u001b[0m ]\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/langchain/chains/base.py:292\u001b[0m, in \u001b[0;36mChain.__call__\u001b[0;34m(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)\u001b[0m\n\u001b[1;32m 290\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (\u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m, \u001b[38;5;167;01mException\u001b[39;00m) \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 291\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_chain_error(e)\n\u001b[0;32m--> 292\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m e\n\u001b[1;32m 293\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_chain_end(outputs)\n\u001b[1;32m 294\u001b[0m final_outputs: Dict[\u001b[38;5;28mstr\u001b[39m, Any] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprep_outputs(\n\u001b[1;32m 295\u001b[0m inputs, outputs, return_only_outputs\n\u001b[1;32m 296\u001b[0m )\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/langchain/chains/base.py:286\u001b[0m, in \u001b[0;36mChain.__call__\u001b[0;34m(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)\u001b[0m\n\u001b[1;32m 279\u001b[0m run_manager \u001b[38;5;241m=\u001b[39m callback_manager\u001b[38;5;241m.\u001b[39mon_chain_start(\n\u001b[1;32m 280\u001b[0m dumpd(\u001b[38;5;28mself\u001b[39m),\n\u001b[1;32m 281\u001b[0m inputs,\n\u001b[1;32m 282\u001b[0m name\u001b[38;5;241m=\u001b[39mrun_name,\n\u001b[1;32m 283\u001b[0m )\n\u001b[1;32m 284\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 285\u001b[0m outputs \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m--> 286\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call\u001b[49m\u001b[43m(\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 287\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m new_arg_supported\n\u001b[1;32m 288\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_call(inputs)\n\u001b[1;32m 289\u001b[0m )\n\u001b[1;32m 290\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (\u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m, \u001b[38;5;167;01mException\u001b[39;00m) \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 291\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_chain_error(e)\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/langchain_experimental/sql/base.py:198\u001b[0m, in \u001b[0;36mSQLDatabaseChain._call\u001b[0;34m(self, inputs, run_manager)\u001b[0m\n\u001b[1;32m 194\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m exc:\n\u001b[1;32m 195\u001b[0m \u001b[38;5;66;03m# Append intermediate steps to exception, to aid in logging and later\u001b[39;00m\n\u001b[1;32m 196\u001b[0m \u001b[38;5;66;03m# improvement of few shot prompt seeds\u001b[39;00m\n\u001b[1;32m 197\u001b[0m exc\u001b[38;5;241m.\u001b[39mintermediate_steps \u001b[38;5;241m=\u001b[39m intermediate_steps \u001b[38;5;66;03m# type: ignore\u001b[39;00m\n\u001b[0;32m--> 198\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/langchain_experimental/sql/base.py:143\u001b[0m, in \u001b[0;36mSQLDatabaseChain._call\u001b[0;34m(self, inputs, run_manager)\u001b[0m\n\u001b[1;32m 139\u001b[0m intermediate_steps\u001b[38;5;241m.\u001b[39mappend(\n\u001b[1;32m 140\u001b[0m sql_cmd\n\u001b[1;32m 141\u001b[0m ) \u001b[38;5;66;03m# output: sql generation (no checker)\u001b[39;00m\n\u001b[1;32m 142\u001b[0m intermediate_steps\u001b[38;5;241m.\u001b[39mappend({\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msql_cmd\u001b[39m\u001b[38;5;124m\"\u001b[39m: sql_cmd}) \u001b[38;5;66;03m# input: sql exec\u001b[39;00m\n\u001b[0;32m--> 143\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdatabase\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[43msql_cmd\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 144\u001b[0m intermediate_steps\u001b[38;5;241m.\u001b[39mappend(\u001b[38;5;28mstr\u001b[39m(result)) \u001b[38;5;66;03m# output: sql exec\u001b[39;00m\n\u001b[1;32m 145\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/langchain/utilities/sql_database.py:414\u001b[0m, in \u001b[0;36mSQLDatabase.run\u001b[0;34m(self, command, fetch)\u001b[0m\n\u001b[1;32m 408\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrun\u001b[39m(\u001b[38;5;28mself\u001b[39m, command: \u001b[38;5;28mstr\u001b[39m, fetch: \u001b[38;5;28mstr\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mall\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28mstr\u001b[39m:\n\u001b[1;32m 409\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Execute a SQL command and return a string representing the results.\u001b[39;00m\n\u001b[1;32m 410\u001b[0m \n\u001b[1;32m 411\u001b[0m \u001b[38;5;124;03m If the statement returns rows, a string of the results is returned.\u001b[39;00m\n\u001b[1;32m 412\u001b[0m \u001b[38;5;124;03m If the statement returns no rows, an empty string is returned.\u001b[39;00m\n\u001b[1;32m 413\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 414\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_execute\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcommand\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfetch\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 415\u001b[0m \u001b[38;5;66;03m# Convert columns values to string to avoid issues with sqlalchemy\u001b[39;00m\n\u001b[1;32m 416\u001b[0m \u001b[38;5;66;03m# truncating text\u001b[39;00m\n\u001b[1;32m 417\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m result:\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/langchain/utilities/sql_database.py:397\u001b[0m, in \u001b[0;36mSQLDatabase._execute\u001b[0;34m(self, command, fetch)\u001b[0m\n\u001b[1;32m 395\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m: \u001b[38;5;66;03m# postgresql and compatible dialects\u001b[39;00m\n\u001b[1;32m 396\u001b[0m connection\u001b[38;5;241m.\u001b[39mexec_driver_sql(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSET search_path TO \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_schema\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m--> 397\u001b[0m cursor \u001b[38;5;241m=\u001b[39m \u001b[43mconnection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtext\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcommand\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 398\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m cursor\u001b[38;5;241m.\u001b[39mreturns_rows:\n\u001b[1;32m 399\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m fetch \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mall\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/sqlalchemy/engine/base.py:1422\u001b[0m, in \u001b[0;36mConnection.execute\u001b[0;34m(self, statement, parameters, execution_options)\u001b[0m\n\u001b[1;32m 1420\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc\u001b[38;5;241m.\u001b[39mObjectNotExecutableError(statement) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01merr\u001b[39;00m\n\u001b[1;32m 1421\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 1422\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mmeth\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1423\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1424\u001b[0m \u001b[43m \u001b[49m\u001b[43mdistilled_parameters\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1425\u001b[0m \u001b[43m \u001b[49m\u001b[43mexecution_options\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mNO_OPTIONS\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1426\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/sqlalchemy/sql/elements.py:514\u001b[0m, in \u001b[0;36mClauseElement._execute_on_connection\u001b[0;34m(self, connection, distilled_params, execution_options)\u001b[0m\n\u001b[1;32m 512\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m TYPE_CHECKING:\n\u001b[1;32m 513\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28mself\u001b[39m, Executable)\n\u001b[0;32m--> 514\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mconnection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_execute_clauseelement\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 515\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdistilled_params\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mexecution_options\u001b[49m\n\u001b[1;32m 516\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 517\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 518\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc\u001b[38;5;241m.\u001b[39mObjectNotExecutableError(\u001b[38;5;28mself\u001b[39m)\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/sqlalchemy/engine/base.py:1644\u001b[0m, in \u001b[0;36mConnection._execute_clauseelement\u001b[0;34m(self, elem, distilled_parameters, execution_options)\u001b[0m\n\u001b[1;32m 1632\u001b[0m compiled_cache: Optional[CompiledCacheType] \u001b[38;5;241m=\u001b[39m execution_options\u001b[38;5;241m.\u001b[39mget(\n\u001b[1;32m 1633\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcompiled_cache\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mengine\u001b[38;5;241m.\u001b[39m_compiled_cache\n\u001b[1;32m 1634\u001b[0m )\n\u001b[1;32m 1636\u001b[0m compiled_sql, extracted_params, cache_hit \u001b[38;5;241m=\u001b[39m elem\u001b[38;5;241m.\u001b[39m_compile_w_cache(\n\u001b[1;32m 1637\u001b[0m dialect\u001b[38;5;241m=\u001b[39mdialect,\n\u001b[1;32m 1638\u001b[0m compiled_cache\u001b[38;5;241m=\u001b[39mcompiled_cache,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1642\u001b[0m linting\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdialect\u001b[38;5;241m.\u001b[39mcompiler_linting \u001b[38;5;241m|\u001b[39m compiler\u001b[38;5;241m.\u001b[39mWARN_LINTING,\n\u001b[1;32m 1643\u001b[0m )\n\u001b[0;32m-> 1644\u001b[0m ret \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_execute_context\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1645\u001b[0m \u001b[43m \u001b[49m\u001b[43mdialect\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1646\u001b[0m \u001b[43m \u001b[49m\u001b[43mdialect\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecution_ctx_cls\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_init_compiled\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1647\u001b[0m \u001b[43m \u001b[49m\u001b[43mcompiled_sql\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1648\u001b[0m \u001b[43m \u001b[49m\u001b[43mdistilled_parameters\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1649\u001b[0m \u001b[43m \u001b[49m\u001b[43mexecution_options\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1650\u001b[0m \u001b[43m \u001b[49m\u001b[43mcompiled_sql\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1651\u001b[0m \u001b[43m \u001b[49m\u001b[43mdistilled_parameters\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1652\u001b[0m \u001b[43m \u001b[49m\u001b[43melem\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1653\u001b[0m \u001b[43m \u001b[49m\u001b[43mextracted_params\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1654\u001b[0m \u001b[43m \u001b[49m\u001b[43mcache_hit\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcache_hit\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1655\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1656\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m has_events:\n\u001b[1;32m 1657\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdispatch\u001b[38;5;241m.\u001b[39mafter_execute(\n\u001b[1;32m 1658\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 1659\u001b[0m elem,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1663\u001b[0m ret,\n\u001b[1;32m 1664\u001b[0m )\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/sqlalchemy/engine/base.py:1850\u001b[0m, in \u001b[0;36mConnection._execute_context\u001b[0;34m(self, dialect, constructor, statement, parameters, execution_options, *args, **kw)\u001b[0m\n\u001b[1;32m 1848\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_exec_insertmany_context(dialect, context)\n\u001b[1;32m 1849\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 1850\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_exec_single_context\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1851\u001b[0m \u001b[43m \u001b[49m\u001b[43mdialect\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcontext\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstatement\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparameters\u001b[49m\n\u001b[1;32m 1852\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/sqlalchemy/engine/base.py:1990\u001b[0m, in \u001b[0;36mConnection._exec_single_context\u001b[0;34m(self, dialect, context, statement, parameters)\u001b[0m\n\u001b[1;32m 1987\u001b[0m result \u001b[38;5;241m=\u001b[39m context\u001b[38;5;241m.\u001b[39m_setup_result_proxy()\n\u001b[1;32m 1989\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m-> 1990\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_handle_dbapi_exception\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1991\u001b[0m \u001b[43m \u001b[49m\u001b[43me\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstr_statement\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43meffective_parameters\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcursor\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcontext\u001b[49m\n\u001b[1;32m 1992\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1994\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/sqlalchemy/engine/base.py:2357\u001b[0m, in \u001b[0;36mConnection._handle_dbapi_exception\u001b[0;34m(self, e, statement, parameters, cursor, context, is_sub_exec)\u001b[0m\n\u001b[1;32m 2355\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m should_wrap:\n\u001b[1;32m 2356\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m sqlalchemy_exception \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m-> 2357\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m sqlalchemy_exception\u001b[38;5;241m.\u001b[39mwith_traceback(exc_info[\u001b[38;5;241m2\u001b[39m]) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01me\u001b[39;00m\n\u001b[1;32m 2358\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 2359\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m exc_info[\u001b[38;5;241m1\u001b[39m] \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/sqlalchemy/engine/base.py:1971\u001b[0m, in \u001b[0;36mConnection._exec_single_context\u001b[0;34m(self, dialect, context, statement, parameters)\u001b[0m\n\u001b[1;32m 1969\u001b[0m \u001b[38;5;28;01mbreak\u001b[39;00m\n\u001b[1;32m 1970\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m evt_handled:\n\u001b[0;32m-> 1971\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdialect\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdo_execute\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1972\u001b[0m \u001b[43m \u001b[49m\u001b[43mcursor\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstr_statement\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43meffective_parameters\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcontext\u001b[49m\n\u001b[1;32m 1973\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1975\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_has_events \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mengine\u001b[38;5;241m.\u001b[39m_has_events:\n\u001b[1;32m 1976\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdispatch\u001b[38;5;241m.\u001b[39mafter_cursor_execute(\n\u001b[1;32m 1977\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 1978\u001b[0m cursor,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1982\u001b[0m context\u001b[38;5;241m.\u001b[39mexecutemany,\n\u001b[1;32m 1983\u001b[0m )\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/sqlalchemy/engine/default.py:919\u001b[0m, in \u001b[0;36mDefaultDialect.do_execute\u001b[0;34m(self, cursor, statement, parameters, context)\u001b[0m\n\u001b[1;32m 918\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdo_execute\u001b[39m(\u001b[38;5;28mself\u001b[39m, cursor, statement, parameters, context\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[0;32m--> 919\u001b[0m \u001b[43mcursor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstatement\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparameters\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/pymysql/cursors.py:153\u001b[0m, in \u001b[0;36mCursor.execute\u001b[0;34m(self, query, args)\u001b[0m\n\u001b[1;32m 149\u001b[0m \u001b[38;5;28;01mpass\u001b[39;00m\n\u001b[1;32m 151\u001b[0m query \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmogrify(query, args)\n\u001b[0;32m--> 153\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_query\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquery\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 154\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_executed \u001b[38;5;241m=\u001b[39m query\n\u001b[1;32m 155\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/pymysql/cursors.py:322\u001b[0m, in \u001b[0;36mCursor._query\u001b[0;34m(self, q)\u001b[0m\n\u001b[1;32m 320\u001b[0m conn \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_db()\n\u001b[1;32m 321\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_clear_result()\n\u001b[0;32m--> 322\u001b[0m \u001b[43mconn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mquery\u001b[49m\u001b[43m(\u001b[49m\u001b[43mq\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 323\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_do_get_result()\n\u001b[1;32m 324\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mrowcount\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/pymysql/connections.py:558\u001b[0m, in \u001b[0;36mConnection.query\u001b[0;34m(self, sql, unbuffered)\u001b[0m\n\u001b[1;32m 556\u001b[0m sql \u001b[38;5;241m=\u001b[39m sql\u001b[38;5;241m.\u001b[39mencode(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mencoding, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msurrogateescape\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 557\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_execute_command(COMMAND\u001b[38;5;241m.\u001b[39mCOM_QUERY, sql)\n\u001b[0;32m--> 558\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_affected_rows \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_read_query_result\u001b[49m\u001b[43m(\u001b[49m\u001b[43munbuffered\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43munbuffered\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 559\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_affected_rows\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/pymysql/connections.py:822\u001b[0m, in \u001b[0;36mConnection._read_query_result\u001b[0;34m(self, unbuffered)\u001b[0m\n\u001b[1;32m 820\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 821\u001b[0m result \u001b[38;5;241m=\u001b[39m MySQLResult(\u001b[38;5;28mself\u001b[39m)\n\u001b[0;32m--> 822\u001b[0m \u001b[43mresult\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 823\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_result \u001b[38;5;241m=\u001b[39m result\n\u001b[1;32m 824\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m result\u001b[38;5;241m.\u001b[39mserver_status \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/pymysql/connections.py:1200\u001b[0m, in \u001b[0;36mMySQLResult.read\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1198\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mread\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m 1199\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m-> 1200\u001b[0m first_packet \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_read_packet\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1202\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m first_packet\u001b[38;5;241m.\u001b[39mis_ok_packet():\n\u001b[1;32m 1203\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_read_ok_packet(first_packet)\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/pymysql/connections.py:772\u001b[0m, in \u001b[0;36mConnection._read_packet\u001b[0;34m(self, packet_type)\u001b[0m\n\u001b[1;32m 770\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_result \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_result\u001b[38;5;241m.\u001b[39munbuffered_active \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m 771\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_result\u001b[38;5;241m.\u001b[39munbuffered_active \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[0;32m--> 772\u001b[0m \u001b[43mpacket\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mraise_for_error\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 773\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m packet\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/pymysql/protocol.py:221\u001b[0m, in \u001b[0;36mMysqlPacket.raise_for_error\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 219\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m DEBUG:\n\u001b[1;32m 220\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124merrno =\u001b[39m\u001b[38;5;124m\"\u001b[39m, errno)\n\u001b[0;32m--> 221\u001b[0m \u001b[43merr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mraise_mysql_exception\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_data\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/lib/python3.11/site-packages/pymysql/err.py:143\u001b[0m, in \u001b[0;36mraise_mysql_exception\u001b[0;34m(data)\u001b[0m\n\u001b[1;32m 141\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m errorclass \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 142\u001b[0m errorclass \u001b[38;5;241m=\u001b[39m InternalError \u001b[38;5;28;01mif\u001b[39;00m errno \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m1000\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m OperationalError\n\u001b[0;32m--> 143\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m errorclass(errno, errval)\n", "\u001b[0;31mOperationalError\u001b[0m: (pymysql.err.OperationalError) (1054, \"Unknown column 'discounts.start_date' in 'where clause'\")\n[SQL: SELECT SUM(price - price * pct_discount) FROM t_shirts JOIN discounts ON t_shirts.t_shirt_id = discounts.t_shirt_id WHERE brand = 'Levi' AND CURDATE() BETWEEN discounts.start_date AND discounts.end_date]\n(Background on this error at: https://sqlalche.me/e/20/e3q8)" ] } ], "source": [ "\n", "qs3 = db_chain.run(\"if we have to see all my levi's t-shirts today with discounts applied. how much revenue our store will generate (post discounts)?\")" ] }, { "cell_type": "code", "execution_count": 21, "id": "1842f69c", "metadata": {}, "outputs": [], "source": [ "sql_code = \"\"\"\n", "\n", "SELECT sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) AS total_revenue \n", "FROM (select sum(price*stock_quantity) AS total_amount, t_shirt_id FROM t_shirts WHERE brand = \"Levi\" \n", "GROUP BY t_shirt_id) a LEFT JOIN discounts ON a.t_shirt_id=discounts.t_shirt_id\n", "\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 22, "id": "0f5e3472", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n", "\n", "\n", "SELECT sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) AS total_revenue \n", "FROM (select sum(price*stock_quantity) AS total_amount, t_shirt_id FROM t_shirts WHERE brand = \"Levi\" \n", "GROUP BY t_shirt_id) a LEFT JOIN discounts ON a.t_shirt_id=discounts.t_shirt_id\n", "\n", "\n", "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) AS total_revenue \n", "FROM (select sum(price*stock_quantity) AS total_amount, t_shirt_id FROM t_shirts WHERE brand = \"Levi\" \n", "GROUP BY t_shirt_id) a LEFT JOIN discounts ON a.t_shirt_id=discounts.t_shirt_id\u001b[0m\n", "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('26011.000000'),)]\u001b[0m\n", "Answer:\u001b[32;1m\u001b[1;3m26011\u001b[0m\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] } ], "source": [ "qs3 = db_chain.run(sql_code)" ] }, { "cell_type": "code", "execution_count": 23, "id": "8af1b5d8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'26011'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "qs3" ] }, { "cell_type": "code", "execution_count": 24, "id": "47679dee", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n", "how many white color levi's t-shirts we have available?\n", "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT stock_quantity FROM t_shirts WHERE brand = 'Levi' AND color = 'White'\u001b[0m\n", "SQLResult: \u001b[33;1m\u001b[1;3m[(40,), (85,), (36,)]\u001b[0m\n", "Answer:\u001b[32;1m\u001b[1;3m85\u001b[0m\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] } ], "source": [ "qs4 = db_chain.run(\"how many white color levi's t-shirts we have available?\")" ] }, { "cell_type": "code", "execution_count": 25, "id": "a83abf09", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n", "SELECT sum(stock_quantity) FROM t-shirts WHERE brand = 'Levi' AND color = 'White'\n", "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT sum(stock_quantity) FROM t_shirts WHERE brand = 'Levi' AND color = 'White'\u001b[0m\n", "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('161'),)]\u001b[0m\n", "Answer:\u001b[32;1m\u001b[1;3m161\u001b[0m\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] } ], "source": [ "qs4 = db_chain.run(\"SELECT sum(stock_quantity) FROM t-shirts WHERE brand = 'Levi' AND color = 'White'\")" ] }, { "cell_type": "code", "execution_count": 26, "id": "02310a6d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'161'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "qs4" ] }, { "cell_type": "code", "execution_count": 27, "id": "a25adb91", "metadata": {}, "outputs": [], "source": [ "fewshots = [\n", " {\n", " 'Question':\"how many t-shirts do we have for Nike in extra small and white color?\",\n", " 'SQLQuery':\"SELECT stock_quantity FROM t_shirts WHERE brand = 'Nike' AND color = 'White' AND size = 'XS'\",\n", " 'SQLResult':\"Result of the SQL query\",\n", " 'Answer': qs1\n", " },\n", " {\n", " 'Question':\"how much is the price of the inventory for all small size t-shirts?\",\n", " 'SQLQuery':\"SELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'\",\n", " 'SQLResult':\"Result of the SQL query\",\n", " 'Answer': qs2\n", " \n", " },\n", " {\n", " 'Question':\"if we have to see all my levi's t-shirts today with discounts applied. how much revenue our store will generate (post discounts)?\",\n", " 'SQLQuery':\"\"\"SELECT sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) AS total_revenue FROM (select sum(price*stock_quantity) AS total_amount, t_shirt_id FROM t_shirts WHERE brand = \"Levi\" GROUP BY t_shirt_id) a LEFT JOIN discounts ON a.t_shirt_id = discounts.t_shirt_id\"\"\",\n", " 'SQLResult':\"Result of the SQL query\",\n", " 'Answer': qs3\n", " \n", " },\n", " {\n", " 'Question':\"how many white color levi's t-shirts we have available?\",\n", " 'SQLQuery':\"SELECT sum(stock_quantity) FROM t-shirts WHERE brand = 'Levi' AND color = 'White'\",\n", " 'SQLResult':\"Result of the SQL query\",\n", " 'Answer': qs4\n", " } \n", "]" ] }, { "cell_type": "code", "execution_count": 28, "id": "e7dcad87", "metadata": {}, "outputs": [], "source": [ "from langchain.embeddings import HuggingFaceEmbeddings" ] }, { "cell_type": "code", "execution_count": 29, "id": "1ee2a866", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: sentence-transformers in /opt/homebrew/lib/python3.11/site-packages (2.6.1)\n", "Requirement already satisfied: transformers<5.0.0,>=4.32.0 in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (4.38.2)\n", "Requirement already satisfied: tqdm in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (4.66.2)\n", "Requirement already satisfied: torch>=1.11.0 in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (2.2.1)\n", "Requirement already satisfied: numpy in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (1.24.2)\n", "Requirement already satisfied: scikit-learn in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (1.4.1.post1)\n", "Requirement already satisfied: scipy in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (1.12.0)\n", "Requirement already satisfied: huggingface-hub>=0.15.1 in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (0.21.4)\n", "Requirement already satisfied: Pillow in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (10.2.0)\n", "Requirement already satisfied: filelock in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.15.1->sentence-transformers) (3.13.1)\n", "Requirement already satisfied: fsspec>=2023.5.0 in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.15.1->sentence-transformers) (2024.2.0)\n", "Requirement already satisfied: requests in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.15.1->sentence-transformers) (2.31.0)\n", "Requirement already satisfied: pyyaml>=5.1 in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.15.1->sentence-transformers) (6.0.1)\n", "Requirement already satisfied: typing-extensions>=3.7.4.3 in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.15.1->sentence-transformers) (4.10.0)\n", "Requirement already satisfied: packaging>=20.9 in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.15.1->sentence-transformers) (23.2)\n", "Requirement already satisfied: sympy in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers) (1.12)\n", "Requirement already satisfied: networkx in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers) (3.2.1)\n", "Requirement already satisfied: jinja2 in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers) (3.1.3)\n", "Requirement already satisfied: regex!=2019.12.17 in /opt/homebrew/lib/python3.11/site-packages (from transformers<5.0.0,>=4.32.0->sentence-transformers) (2023.12.25)\n", "Requirement already satisfied: tokenizers<0.19,>=0.14 in /opt/homebrew/lib/python3.11/site-packages (from transformers<5.0.0,>=4.32.0->sentence-transformers) (0.15.2)\n", "Requirement already satisfied: safetensors>=0.4.1 in /opt/homebrew/lib/python3.11/site-packages (from transformers<5.0.0,>=4.32.0->sentence-transformers) (0.4.2)\n", "Requirement already satisfied: joblib>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from scikit-learn->sentence-transformers) (1.3.2)\n", "Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/homebrew/lib/python3.11/site-packages (from scikit-learn->sentence-transformers) (3.3.0)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /opt/homebrew/lib/python3.11/site-packages (from jinja2->torch>=1.11.0->sentence-transformers) (2.1.5)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/lib/python3.11/site-packages (from requests->huggingface-hub>=0.15.1->sentence-transformers) (3.3.2)\n", "Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/lib/python3.11/site-packages (from requests->huggingface-hub>=0.15.1->sentence-transformers) (3.6)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/homebrew/lib/python3.11/site-packages (from requests->huggingface-hub>=0.15.1->sentence-transformers) (2.2.1)\n", "Requirement already satisfied: certifi>=2017.4.17 in /opt/homebrew/lib/python3.11/site-packages (from requests->huggingface-hub>=0.15.1->sentence-transformers) (2024.2.2)\n", "Requirement already satisfied: mpmath>=0.19 in /opt/homebrew/lib/python3.11/site-packages (from sympy->torch>=1.11.0->sentence-transformers) (1.3.0)\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install sentence-transformers" ] }, { "cell_type": "code", "execution_count": 30, "id": "9b32825e", "metadata": {}, "outputs": [], "source": [ "embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-V2')" ] }, { "cell_type": "code", "execution_count": 31, "id": "f6d36cdc", "metadata": {}, "outputs": [], "source": [ "to_vectorize = [\" \".join(example.values())for example in fewshots]" ] }, { "cell_type": "code", "execution_count": 32, "id": "d62ffe97", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"how many t-shirts do we have for Nike in extra small and white color? SELECT stock_quantity FROM t_shirts WHERE brand = 'Nike' AND color = 'White' AND size = 'XS' Result of the SQL query 0\"" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "to_vectorize[0]" ] }, { "cell_type": "code", "execution_count": 33, "id": "20ce3e8a", "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: chromadb in /opt/homebrew/lib/python3.11/site-packages (0.4.24)\n", "Requirement already satisfied: build>=1.0.3 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.1.1)\n", "Requirement already satisfied: requests>=2.28 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (2.31.0)\n", "Requirement already satisfied: pydantic>=1.9 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (2.6.4)\n", "Requirement already satisfied: chroma-hnswlib==0.7.3 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.7.3)\n", "Requirement already satisfied: fastapi>=0.95.2 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.110.1)\n", "Requirement already satisfied: uvicorn>=0.18.3 in /opt/homebrew/lib/python3.11/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.24.0.post1)\n", "Requirement already satisfied: numpy>=1.22.5 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.24.2)\n", "Requirement already satisfied: posthog>=2.4.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (3.5.0)\n", "Requirement already satisfied: typing-extensions>=4.5.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.10.0)\n", "Requirement already satisfied: pulsar-client>=3.1.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (3.4.0)\n", "Requirement already satisfied: onnxruntime>=1.14.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.17.1)\n", "Requirement already satisfied: opentelemetry-api>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.24.0)\n", "Requirement already satisfied: opentelemetry-exporter-otlp-proto-grpc>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.24.0)\n", "Requirement already satisfied: opentelemetry-instrumentation-fastapi>=0.41b0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.45b0)\n", "Requirement already satisfied: opentelemetry-sdk>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.24.0)\n", "Requirement already satisfied: tokenizers>=0.13.2 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.15.2)\n", "Requirement already satisfied: pypika>=0.48.9 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.48.9)\n", "Requirement already satisfied: tqdm>=4.65.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.66.2)\n", "Requirement already satisfied: overrides>=7.3.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (7.7.0)\n", "Requirement already satisfied: importlib-resources in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (6.4.0)\n", "Requirement already satisfied: grpcio>=1.58.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.62.1)\n", "Requirement already satisfied: bcrypt>=4.0.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.1.2)\n", "Requirement already satisfied: typer>=0.9.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.12.3)\n", "Requirement already satisfied: kubernetes>=28.1.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (29.0.0)\n", "Requirement already satisfied: tenacity>=8.2.3 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (8.2.3)\n", "Requirement already satisfied: PyYAML>=6.0.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (6.0.1)\n", "Requirement already satisfied: mmh3>=4.0.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.1.0)\n", "Requirement already satisfied: orjson>=3.9.12 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (3.9.15)\n", "Requirement already satisfied: packaging>=19.0 in /opt/homebrew/lib/python3.11/site-packages (from build>=1.0.3->chromadb) (23.2)\n", "Requirement already satisfied: pyproject_hooks in /opt/homebrew/lib/python3.11/site-packages (from build>=1.0.3->chromadb) (1.0.0)\n", "Requirement already satisfied: starlette<0.38.0,>=0.37.2 in /opt/homebrew/lib/python3.11/site-packages (from fastapi>=0.95.2->chromadb) (0.37.2)\n", "Requirement already satisfied: certifi>=14.05.14 in /opt/homebrew/lib/python3.11/site-packages (from kubernetes>=28.1.0->chromadb) (2024.2.2)\n", "Requirement already satisfied: six>=1.9.0 in /opt/homebrew/lib/python3.11/site-packages (from kubernetes>=28.1.0->chromadb) (1.16.0)\n", "Requirement already satisfied: python-dateutil>=2.5.3 in /opt/homebrew/lib/python3.11/site-packages (from kubernetes>=28.1.0->chromadb) (2.9.0.post0)\n", "Requirement already satisfied: google-auth>=1.0.1 in /opt/homebrew/lib/python3.11/site-packages (from kubernetes>=28.1.0->chromadb) (2.29.0)\n", "Requirement already satisfied: websocket-client!=0.40.0,!=0.41.*,!=0.42.*,>=0.32.0 in /opt/homebrew/lib/python3.11/site-packages (from kubernetes>=28.1.0->chromadb) (1.7.0)\n", "Requirement already satisfied: requests-oauthlib in /opt/homebrew/lib/python3.11/site-packages (from kubernetes>=28.1.0->chromadb) (2.0.0)\n", "Requirement already satisfied: oauthlib>=3.2.2 in /opt/homebrew/lib/python3.11/site-packages (from kubernetes>=28.1.0->chromadb) (3.2.2)\n", "Requirement already satisfied: urllib3>=1.24.2 in /opt/homebrew/lib/python3.11/site-packages (from kubernetes>=28.1.0->chromadb) (2.2.1)\n", "Requirement already satisfied: coloredlogs in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (15.0.1)\n", "Requirement already satisfied: flatbuffers in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (24.3.7)\n", "Requirement already satisfied: protobuf in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (4.25.3)\n", "Requirement already satisfied: sympy in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (1.12)\n", "Requirement already satisfied: deprecated>=1.2.6 in /opt/homebrew/lib/python3.11/site-packages (from opentelemetry-api>=1.2.0->chromadb) (1.2.14)\n", "Requirement already satisfied: importlib-metadata<=7.0,>=6.0 in /opt/homebrew/lib/python3.11/site-packages (from opentelemetry-api>=1.2.0->chromadb) (7.0.0)\n", "Requirement already satisfied: googleapis-common-protos~=1.52 in /opt/homebrew/lib/python3.11/site-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb) (1.63.0)\n", "Requirement already satisfied: opentelemetry-exporter-otlp-proto-common==1.24.0 in /opt/homebrew/lib/python3.11/site-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb) (1.24.0)\n", "Requirement already satisfied: opentelemetry-proto==1.24.0 in /opt/homebrew/lib/python3.11/site-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb) (1.24.0)\n", "Requirement already satisfied: opentelemetry-instrumentation-asgi==0.45b0 in /opt/homebrew/lib/python3.11/site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (0.45b0)\n", "Requirement already satisfied: opentelemetry-instrumentation==0.45b0 in /opt/homebrew/lib/python3.11/site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (0.45b0)\n", "Requirement already satisfied: opentelemetry-semantic-conventions==0.45b0 in /opt/homebrew/lib/python3.11/site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (0.45b0)\n", "Requirement already satisfied: opentelemetry-util-http==0.45b0 in /opt/homebrew/lib/python3.11/site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (0.45b0)\n", "Requirement already satisfied: setuptools>=16.0 in /opt/homebrew/lib/python3.11/site-packages (from opentelemetry-instrumentation==0.45b0->opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (69.2.0)\n", "Requirement already satisfied: wrapt<2.0.0,>=1.0.0 in /opt/homebrew/lib/python3.11/site-packages (from opentelemetry-instrumentation==0.45b0->opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (1.16.0)\n", "Requirement already satisfied: asgiref~=3.0 in /opt/homebrew/lib/python3.11/site-packages (from opentelemetry-instrumentation-asgi==0.45b0->opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (3.8.1)\n", "Requirement already satisfied: monotonic>=1.5 in /opt/homebrew/lib/python3.11/site-packages (from posthog>=2.4.0->chromadb) (1.6)\n", "Requirement already satisfied: backoff>=1.10.0 in /opt/homebrew/lib/python3.11/site-packages (from posthog>=2.4.0->chromadb) (2.2.1)\n", "Requirement already satisfied: annotated-types>=0.4.0 in /opt/homebrew/lib/python3.11/site-packages (from pydantic>=1.9->chromadb) (0.6.0)\n", "Requirement already satisfied: pydantic-core==2.16.3 in /opt/homebrew/lib/python3.11/site-packages (from pydantic>=1.9->chromadb) (2.16.3)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.28->chromadb) (3.3.2)\n", "Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.28->chromadb) (3.6)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: huggingface_hub<1.0,>=0.16.4 in /opt/homebrew/lib/python3.11/site-packages (from tokenizers>=0.13.2->chromadb) (0.21.4)\n", "Requirement already satisfied: click>=8.0.0 in /opt/homebrew/lib/python3.11/site-packages (from typer>=0.9.0->chromadb) (8.1.7)\n", "Requirement already satisfied: shellingham>=1.3.0 in /opt/homebrew/lib/python3.11/site-packages (from typer>=0.9.0->chromadb) (1.5.4)\n", "Requirement already satisfied: rich>=10.11.0 in /opt/homebrew/lib/python3.11/site-packages (from typer>=0.9.0->chromadb) (13.7.1)\n", "Requirement already satisfied: h11>=0.8 in /opt/homebrew/lib/python3.11/site-packages (from uvicorn>=0.18.3->uvicorn[standard]>=0.18.3->chromadb) (0.14.0)\n", "Requirement already satisfied: httptools>=0.5.0 in /opt/homebrew/lib/python3.11/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.6.1)\n", "Requirement already satisfied: python-dotenv>=0.13 in /opt/homebrew/lib/python3.11/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (1.0.0)\n", "Requirement already satisfied: uvloop!=0.15.0,!=0.15.1,>=0.14.0 in /opt/homebrew/lib/python3.11/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.19.0)\n", "Requirement already satisfied: watchfiles>=0.13 in /opt/homebrew/lib/python3.11/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.21.0)\n", "Requirement already satisfied: websockets>=10.4 in /opt/homebrew/lib/python3.11/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (12.0)\n", "Requirement already satisfied: cachetools<6.0,>=2.0.0 in /opt/homebrew/lib/python3.11/site-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (5.3.3)\n", "Requirement already satisfied: pyasn1-modules>=0.2.1 in /opt/homebrew/lib/python3.11/site-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (0.4.0)\n", "Requirement already satisfied: rsa<5,>=3.1.4 in /opt/homebrew/lib/python3.11/site-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (4.9)\n", "Requirement already satisfied: filelock in /opt/homebrew/lib/python3.11/site-packages (from huggingface_hub<1.0,>=0.16.4->tokenizers>=0.13.2->chromadb) (3.13.1)\n", "Requirement already satisfied: fsspec>=2023.5.0 in /opt/homebrew/lib/python3.11/site-packages (from huggingface_hub<1.0,>=0.16.4->tokenizers>=0.13.2->chromadb) (2024.2.0)\n", "Requirement already satisfied: zipp>=0.5 in /opt/homebrew/lib/python3.11/site-packages (from importlib-metadata<=7.0,>=6.0->opentelemetry-api>=1.2.0->chromadb) (3.18.1)\n", "Requirement already satisfied: markdown-it-py>=2.2.0 in /opt/homebrew/lib/python3.11/site-packages (from rich>=10.11.0->typer>=0.9.0->chromadb) (3.0.0)\n", "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /opt/homebrew/lib/python3.11/site-packages (from rich>=10.11.0->typer>=0.9.0->chromadb) (2.17.2)\n", "Requirement already satisfied: anyio<5,>=3.4.0 in /opt/homebrew/lib/python3.11/site-packages (from starlette<0.38.0,>=0.37.2->fastapi>=0.95.2->chromadb) (4.3.0)\n", "Requirement already satisfied: humanfriendly>=9.1 in /opt/homebrew/lib/python3.11/site-packages (from coloredlogs->onnxruntime>=1.14.1->chromadb) (10.0)\n", "Requirement already satisfied: mpmath>=0.19 in /opt/homebrew/lib/python3.11/site-packages (from sympy->onnxruntime>=1.14.1->chromadb) (1.3.0)\n", "Requirement already satisfied: sniffio>=1.1 in /opt/homebrew/lib/python3.11/site-packages (from anyio<5,>=3.4.0->starlette<0.38.0,>=0.37.2->fastapi>=0.95.2->chromadb) (1.3.1)\n", "Requirement already satisfied: mdurl~=0.1 in /opt/homebrew/lib/python3.11/site-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer>=0.9.0->chromadb) (0.1.2)\n", "Requirement already satisfied: pyasn1<0.7.0,>=0.4.6 in /opt/homebrew/lib/python3.11/site-packages (from pyasn1-modules>=0.2.1->google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (0.6.0)\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install chromadb" ] }, { "cell_type": "code", "execution_count": 34, "id": "40d77eac", "metadata": {}, "outputs": [], "source": [ "from langchain.vectorstores import FAISS\n", "\n", "vectorstore = FAISS.from_texts(to_vectorize,embedding=embeddings,metadatas=fewshots)" ] }, { "cell_type": "code", "execution_count": 35, "id": "db1b58e4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'Question': \"how many white color levi's t-shirts we have available?\",\n", " 'SQLQuery': \"SELECT sum(stock_quantity) FROM t-shirts WHERE brand = 'Levi' AND color = 'White'\",\n", " 'SQLResult': 'Result of the SQL query',\n", " 'Answer': '161'},\n", " {'Question': 'how many t-shirts do we have for Nike in extra small and white color?',\n", " 'SQLQuery': \"SELECT stock_quantity FROM t_shirts WHERE brand = 'Nike' AND color = 'White' AND size = 'XS'\",\n", " 'SQLResult': 'Result of the SQL query',\n", " 'Answer': '0'}]" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.prompts import SemanticSimilarityExampleSelector\n", "\n", "example_selector = SemanticSimilarityExampleSelector(\n", " vectorstore=vectorstore,\n", " k=2\n", ")\n", "example_selector.select_examples({\"Question\": \"How many Addidas T shirts I have left in my store?\"})" ] }, { "cell_type": "code", "execution_count": 36, "id": "20ac12c1", "metadata": {}, "outputs": [], "source": [ "from langchain.chains.sql_database.prompt import PROMPT_SUFFIX,_mysql_prompt" ] }, { "cell_type": "code", "execution_count": 37, "id": "b9a6a53f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "You are a MySQL expert. Given an input question, first create a syntactically correct MySQL query to run, then look at the results of the query and return the answer to the input question.\n", "Unless the user specifies in the question a specific number of examples to obtain, query for at most {top_k} results using the LIMIT clause as per MySQL. You can order the results to return the most informative data in the database.\n", "Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in backticks (`) to denote them as delimited identifiers.\n", "Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table.\n", "Pay attention to use CURDATE() function to get the current date, if the question involves \"today\".\n", "\n", "Use the following format:\n", "\n", "Question: Question here\n", "SQLQuery: SQL Query to run\n", "SQLResult: Result of the SQLQuery\n", "Answer: Final answer here\n", "\n", "\n", "Only use the following tables:\n", "{table_info}\n", "\n", "Question: {input}\n" ] }, { "data": { "text/plain": [ "(None, None)" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(_mysql_prompt),print(PROMPT_SUFFIX)" ] }, { "cell_type": "code", "execution_count": 38, "id": "26a9b9b0", "metadata": {}, "outputs": [], "source": [ "from langchain.prompts.prompt import PromptTemplate\n", "\n", "example_prompt = PromptTemplate(\n", " input_variables=[\"Question\", \"SQLQuery\", \"SQLResult\",\"Answer\",],\n", " template=\"\\nQuestion: {Question}\\nSQLQuery: {SQLQuery}\\nSQLResult: {SQLResult}\\nAnswer: {Answer}\",\n", ")" ] }, { "cell_type": "code", "execution_count": 39, "id": "a341c8df", "metadata": {}, "outputs": [], "source": [ "from langchain.prompts import FewShotPromptTemplate\n", "\n", "few_shot_prompt = FewShotPromptTemplate(\n", " example_selector = example_selector,\n", " example_prompt = example_prompt,\n", " prefix = _mysql_prompt,\n", " suffix = PROMPT_SUFFIX,\n", " input_variables = [\"input\",\"table_info\",\"top_k\"]\n", ")" ] }, { "cell_type": "code", "execution_count": 40, "id": "d6e1cad5", "metadata": {}, "outputs": [], "source": [ "new_chain = SQLDatabaseChain.from_llm(llm,db,verbose=True,prompt = few_shot_prompt)" ] }, { "cell_type": "code", "execution_count": 41, "id": "a130f9f9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n", "How many white color Levi's t shirts we have available and it should be either extra small or small?Even black would be fine\n", "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT sum(stock_quantity) FROM t_shirts WHERE brand = 'Levi' AND (color = 'White' OR color = 'Black') AND (size = 'XS' OR size = 'S')\u001b[0m\n", "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('207'),)]\u001b[0m\n", "Answer:\u001b[32;1m\u001b[1;3m207\u001b[0m\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] } ], "source": [ "ans = new_chain.run(\"How many white color Levi's t shirts we have available and it should be either extra small or small?Even black would be fine\")" ] }, { "cell_type": "code", "execution_count": 42, "id": "e4bd6489", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'207'" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ans" ] }, { "cell_type": "code", "execution_count": null, "id": "dbe9e984", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.8" } }, "nbformat": 4, "nbformat_minor": 5 }