{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0ef005ca", "metadata": {}, "outputs": [], "source": [ "from langchain.llms import GooglePalm\n", "\n", "api_key = 'AIzaSyDlXYnP2xNNa0DNa7dPN89u2L4IuAchEg4'\n", "\n", "llm = GooglePalm(google_api_key=api_key, temperature=0.2)" ] }, { "cell_type": "code", "execution_count": 2, "id": "737f32b2", "metadata": {}, "outputs": [], "source": [ "from langchain.utilities import SQLDatabase\n", "from langchain_experimental.sql import SQLDatabaseChain" ] }, { "cell_type": "code", "execution_count": 3, "id": "3ca26845", "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", ")DEFAULT CHARSET=utf8mb4 ENGINE=InnoDB COLLATE utf8mb4_0900_ai_ci\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", ")DEFAULT CHARSET=utf8mb4 ENGINE=InnoDB COLLATE utf8mb4_0900_ai_ci\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": [ "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", "\n", "print(db.table_info)" ] }, { "cell_type": "code", "execution_count": 4, "id": "cd2e9968", "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 left for nike in extra small size and white color?\n", "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT stock_quantity FROM t_shirts WHERE brand = 'Nike' 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;3m33\u001b[0m\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] } ], "source": [ "db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)\n", "qns1 = db_chain(\"How many t-shirts do we have left for nike in extra small size and white color?\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "dfa0c74e", "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": [ "qns2 = db_chain.run(\"How much is the price of the inventory for all small size t-shirts?\")" ] }, { "cell_type": "code", "execution_count": 6, "id": "c5d1feed", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n", "If we have to sell all the 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 * (1 - 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 * (1 - 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[6], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m qns3 \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 sell all the Levi’s 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 * (1 - 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": [ "qns3 = db_chain.run(\"If we have to sell all the Levi’s T-shirts today with discounts applied. How much revenue our store will generate (post discounts)?\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "e5785845", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n", "\n", "select sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) as total_revenue from\n", "(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", "SQLQuery:\u001b[32;1m\u001b[1;3mselect sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) as total_revenue from\n", "(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": [ "sql_code = \"\"\"\n", "select sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) as total_revenue from\n", "(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", "qns3 = db_chain.run(sql_code)" ] }, { "cell_type": "code", "execution_count": 8, "id": "acdad0f9", "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 brand = 'Levi'\n", "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT SUM(price * stock_quantity) FROM t_shirts WHERE brand = 'Levi'\u001b[0m\n", "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('26011'),)]\u001b[0m\n", "Answer:\u001b[32;1m\u001b[1;3m26011\u001b[0m\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] } ], "source": [ "qns4 = db_chain.run(\"SELECT SUM(price * stock_quantity) FROM t_shirts WHERE brand = 'Levi'\")" ] }, { "cell_type": "code", "execution_count": 9, "id": "caf32b2a", "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": [ "qns5 = db_chain.run(\"How many white color Levi's t shirts we have available?\")" ] }, { "cell_type": "code", "execution_count": 39, "id": "4eefd089", "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': qns1\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': qns2\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': qns3\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': qns4\n", " } \n", "]" ] }, { "cell_type": "code", "execution_count": 11, "id": "e5a7ae98", "metadata": {}, "outputs": [], "source": [ "from langchain.prompts import SemanticSimilarityExampleSelector\n", "from langchain.embeddings import HuggingFaceEmbeddings\n", "from langchain.vectorstores import Chroma\n", "\n", "embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')\n", "\n", "# Ensure that only strings are joined\n", "to_vectorize = []\n", "for example in few_shots:\n", " if isinstance(example, str):\n", " to_vectorize.append(example)\n", " elif isinstance(example, dict):\n", " values = example.values()\n", " for value in values:\n", " if isinstance(value, str):\n", " to_vectorize.append(value)\n", "\n", "# Now, to_vectorize should only contain strings\n", "to_vectorize = [\" \".join(example) for example in to_vectorize]\n", "\n", "# Proceed with the rest of your code...\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "b249bc2a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['H o w m a n y t - s h i r t s d o w e h a v e l e f t f o r N i k e i n X S s i z e a n d w h i t e c o l o r ?',\n", " \"S E L E C T s u m ( s t o c k _ q u a n t i t y ) F R O M t _ s h i r t s W H E R E b r a n d = ' N i k e ' A N D c o l o r = ' W h i t e ' A N D s i z e = ' X S '\",\n", " 'R e s u l t o f t h e S Q L q u e r y',\n", " 'H o w m u c h i s t h e t o t a l p r i c e o f t h e i n v e n t o r y f o r a l l S - s i z e t - s h i r t s ?',\n", " \"S E L E C T S U M ( p r i c e * s t o c k _ q u a n t i t y ) F R O M t _ s h i r t s W H E R E s i z e = ' S '\",\n", " 'R e s u l t o f t h e S Q L q u e r y',\n", " '3 7 1',\n", " 'I f w e h a v e t o s e l l a l l t h e L e v i ’ s T - s h i r t s t o d a y w i t h d i s c o u n t s a p p l i e d . H o w m u c h r e v e n u e o u r s t o r e w i l l g e n e r a t e ( p o s t d i s c o u n t s ) ?',\n", " \"S E L E C T s u m ( a . t o t a l _ a m o u n t * ( ( 1 0 0 - C O A L E S C E ( d i s c o u n t s . p c t _ d i s c o u n t , 0 ) ) / 1 0 0 ) ) a s t o t a l _ r e v e n u e f r o m \\n ( s e l e c t s u m ( p r i c e * s t o c k _ q u a n t i t y ) a s t o t a l _ a m o u n t , t _ s h i r t _ i d f r o m t _ s h i r t s w h e r e b r a n d = ' L e v i ' \\n g r o u p b y t _ s h i r t _ i d ) a l e f t j o i n d i s c o u n t s o n a . t _ s h i r t _ i d = d i s c o u n t s . t _ s h i r t _ i d \\n \",\n", " 'R e s u l t o f t h e S Q L q u e r y',\n", " '2 6 0 1 1',\n", " 'I f w e h a v e t o s e l l a l l t h e L e v i ’ s T - s h i r t s t o d a y . H o w m u c h r e v e n u e o u r s t o r e w i l l g e n e r a t e w i t h o u t d i s c o u n t ?',\n", " \"S E L E C T S U M ( p r i c e * s t o c k _ q u a n t i t y ) F R O M t _ s h i r t s W H E R E b r a n d = ' L e v i '\",\n", " 'R e s u l t o f t h e S Q L q u e r y',\n", " '2 6 0 1 1',\n", " \"H o w m a n y w h i t e c o l o r L e v i ' s s h i r t I h a v e ?\",\n", " \"S E L E C T s u m ( s t o c k _ q u a n t i t y ) F R O M t _ s h i r t s W H E R E b r a n d = ' L e v i ' A N D c o l o r = ' W h i t e '\",\n", " 'R e s u l t o f t h e S Q L q u e r y',\n", " '8 5']" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "to_vectorize" ] }, { "cell_type": "code", "execution_count": 13, "id": "2671b7ce", "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 -U sentence-transformers\n" ] }, { "cell_type": "code", "execution_count": 14, "id": "2823d198", "metadata": {}, "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) (3.19.6)\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", "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" ] }, { "name": "stdout", "output_type": "stream", "text": [ "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": 30, "id": "cf07ca37", "metadata": {}, "outputs": [], "source": [ "from langchain.embeddings import HuggingFaceEmbeddings\n", "\n", "embeddings = HuggingFaceEmbeddings(model_name = \"sentence-transformers/all-MiniLM-L6-v2\")\n", "\n", "e = embeddings.embed_query(\"How many white color Levi's shirt I have?\")" ] }, { "cell_type": "code", "execution_count": 31, "id": "efba95a6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0.0036103595048189163, 0.07093444466590881]" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "e[:2]" ] }, { "cell_type": "code", "execution_count": 36, "id": "15a1d1c5", "metadata": {}, "outputs": [], "source": [ "from langchain.embeddings import HuggingFaceEmbeddings" ] }, { "cell_type": "code", "execution_count": 33, "id": "9739547c", "metadata": {}, "outputs": [], "source": [ "embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-V2')" ] }, { "cell_type": "code", "execution_count": 41, "id": "01a2ab5c", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "sequence item 3: expected str instance, dict found", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[41], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m vectorize \u001b[38;5;241m=\u001b[39m \u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m \u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mjoin\u001b[49m\u001b[43m(\u001b[49m\u001b[43mexample\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalues\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mexample\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mfewshots\u001b[49m\u001b[43m]\u001b[49m\n", "Cell \u001b[0;32mIn[41], line 1\u001b[0m, in \u001b[0;36m\u001b[0;34m(.0)\u001b[0m\n\u001b[0;32m----> 1\u001b[0m vectorize \u001b[38;5;241m=\u001b[39m [\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m \u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mjoin\u001b[49m\u001b[43m(\u001b[49m\u001b[43mexample\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalues\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;28;01mfor\u001b[39;00m example \u001b[38;5;129;01min\u001b[39;00m fewshots]\n", "\u001b[0;31mTypeError\u001b[0m: sequence item 3: expected str instance, dict found" ] } ], "source": [ "vectorize = [\" \".join(example.values())for example in fewshots]" ] }, { "cell_type": "code", "execution_count": null, "id": "4489463e", "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 }