gokulp06 commited on
Commit
28ebfe4
·
verified ·
1 Parent(s): 05e38d3

Upload 12 files

Browse files
Files changed (12) hide show
  1. Eureka.ipynb +1045 -0
  2. Main-Copy1.ipynb +1045 -0
  3. Main.ipynb +1045 -0
  4. README.md +67 -13
  5. Test.py +65 -0
  6. Untitled.ipynb +0 -0
  7. Untitled1.ipynb +665 -0
  8. few_shots.py +26 -0
  9. langchain_helper (1).py +65 -0
  10. langchain_helper.py +43 -0
  11. main.py +19 -0
  12. requirements.txt +11 -0
Eureka.ipynb ADDED
@@ -0,0 +1,1045 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 2,
6
+ "id": "5b6ddc1d",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "#pip install langchain"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": 3,
16
+ "id": "428de9cd",
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "#pip install --upgrade pip"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": 4,
26
+ "id": "c6963d71",
27
+ "metadata": {},
28
+ "outputs": [],
29
+ "source": [
30
+ "#pip install --upgrade setuptools"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "execution_count": 5,
36
+ "id": "f107f9f5",
37
+ "metadata": {},
38
+ "outputs": [],
39
+ "source": [
40
+ "from langchain.llms import GooglePalm"
41
+ ]
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "execution_count": 6,
46
+ "id": "0cf73123",
47
+ "metadata": {},
48
+ "outputs": [],
49
+ "source": [
50
+ "#pip install google-generativeai"
51
+ ]
52
+ },
53
+ {
54
+ "cell_type": "code",
55
+ "execution_count": 7,
56
+ "id": "03866604",
57
+ "metadata": {},
58
+ "outputs": [],
59
+ "source": [
60
+ "import google.generativeai as genai"
61
+ ]
62
+ },
63
+ {
64
+ "cell_type": "code",
65
+ "execution_count": 8,
66
+ "id": "2a11da2a",
67
+ "metadata": {},
68
+ "outputs": [
69
+ {
70
+ "name": "stdout",
71
+ "output_type": "stream",
72
+ "text": [
73
+ "**Verse 1**\n",
74
+ "America, America,\n",
75
+ "Land of the free, home of the brave.\n",
76
+ "You're a beacon of hope,\n",
77
+ "For all the world to see.\n",
78
+ "**Chorus**\n",
79
+ "We're proud to be Americans,\n",
80
+ "We'll fight for our freedom,\n",
81
+ "We'll stand together,\n",
82
+ "As one nation, under God.\n",
83
+ "**Verse 2**\n",
84
+ "We're a melting pot,\n",
85
+ "Of cultures and races.\n",
86
+ "We're all different,\n",
87
+ "But we're all united.\n",
88
+ "**Chorus**\n",
89
+ "We're proud to be Americans,\n",
90
+ "We'll fight for our freedom,\n",
91
+ "We'll stand together,\n",
92
+ "As one nation, under God.\n",
93
+ "**Bridge**\n",
94
+ "Through thick and thin,\n",
95
+ "We'll always stick together.\n",
96
+ "We're Americans,\n",
97
+ "And we're proud.\n",
98
+ "**Chorus**\n",
99
+ "We're proud to be Americans,\n",
100
+ "We'll fight for our freedom,\n",
101
+ "We'll stand together,\n",
102
+ "As one nation, under God.\n"
103
+ ]
104
+ }
105
+ ],
106
+ "source": [
107
+ "api_key = \"AIzaSyBAsa1XcZysgK7DKo9Bs-9MNfixaRR0ocw\"\n",
108
+ "llm = GooglePalm(google_api_key=api_key, temperature=0.2)\n",
109
+ "poem = llm(\"write a song on America in 5 lines\")\n",
110
+ "print(poem)"
111
+ ]
112
+ },
113
+ {
114
+ "cell_type": "code",
115
+ "execution_count": 9,
116
+ "id": "09a438b5",
117
+ "metadata": {},
118
+ "outputs": [],
119
+ "source": [
120
+ "#pip install pymysql"
121
+ ]
122
+ },
123
+ {
124
+ "cell_type": "code",
125
+ "execution_count": 10,
126
+ "id": "74b4e2ef",
127
+ "metadata": {},
128
+ "outputs": [],
129
+ "source": [
130
+ "#pip install mysql-connector-python\n"
131
+ ]
132
+ },
133
+ {
134
+ "cell_type": "code",
135
+ "execution_count": 11,
136
+ "id": "d0ff0a6a",
137
+ "metadata": {},
138
+ "outputs": [],
139
+ "source": [
140
+ "#pip install mysqlclient\n"
141
+ ]
142
+ },
143
+ {
144
+ "cell_type": "code",
145
+ "execution_count": 12,
146
+ "id": "ff9174a2",
147
+ "metadata": {},
148
+ "outputs": [],
149
+ "source": [
150
+ "#pip install psycopg2"
151
+ ]
152
+ },
153
+ {
154
+ "cell_type": "code",
155
+ "execution_count": 13,
156
+ "id": "bf08a5a6",
157
+ "metadata": {},
158
+ "outputs": [
159
+ {
160
+ "name": "stdout",
161
+ "output_type": "stream",
162
+ "text": [
163
+ "\n",
164
+ "CREATE TABLE discounts (\n",
165
+ "\tdiscount_id INTEGER NOT NULL AUTO_INCREMENT, \n",
166
+ "\tt_shirt_id INTEGER NOT NULL, \n",
167
+ "\tpct_discount DECIMAL(5, 2), \n",
168
+ "\tPRIMARY KEY (discount_id), \n",
169
+ "\tCONSTRAINT discounts_ibfk_1 FOREIGN KEY(t_shirt_id) REFERENCES t_shirts (t_shirt_id), \n",
170
+ "\tCONSTRAINT discounts_chk_1 CHECK ((`pct_discount` between 0 and 100))\n",
171
+ ")COLLATE utf8mb4_0900_ai_ci ENGINE=InnoDB DEFAULT CHARSET=utf8mb4\n",
172
+ "\n",
173
+ "/*\n",
174
+ "3 rows from discounts table:\n",
175
+ "discount_id\tt_shirt_id\tpct_discount\n",
176
+ "\n",
177
+ "*/\n",
178
+ "\n",
179
+ "\n",
180
+ "CREATE TABLE t_shirts (\n",
181
+ "\tt_shirt_id INTEGER NOT NULL AUTO_INCREMENT, \n",
182
+ "\tbrand ENUM('Van Huesen','Levi','Nike','Adidas') NOT NULL, \n",
183
+ "\tcolor ENUM('Red','Blue','Black','White') NOT NULL, \n",
184
+ "\tsize ENUM('XS','S','M','L','XL') NOT NULL, \n",
185
+ "\tprice INTEGER, \n",
186
+ "\tstock_quantity INTEGER NOT NULL, \n",
187
+ "\tPRIMARY KEY (t_shirt_id), \n",
188
+ "\tCONSTRAINT t_shirts_chk_1 CHECK ((`price` between 10 and 50))\n",
189
+ ")COLLATE utf8mb4_0900_ai_ci ENGINE=InnoDB DEFAULT CHARSET=utf8mb4\n",
190
+ "\n",
191
+ "/*\n",
192
+ "3 rows from t_shirts table:\n",
193
+ "t_shirt_id\tbrand\tcolor\tsize\tprice\tstock_quantity\n",
194
+ "1\tLevi\tWhite\tL\t33\t85\n",
195
+ "2\tLevi\tBlack\tL\t30\t64\n",
196
+ "4\tNike\tWhite\tL\t45\t33\n",
197
+ "*/\n"
198
+ ]
199
+ }
200
+ ],
201
+ "source": [
202
+ "from langchain.utilities import SQLDatabase\n",
203
+ "\n",
204
+ "db_user = \"root\"\n",
205
+ "db_password = \"root\"\n",
206
+ "db_host = \"localhost\"\n",
207
+ "db_name = \"atliq_tshirts\"\n",
208
+ "\n",
209
+ "db = SQLDatabase.from_uri(f\"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}\",sample_rows_in_table_info=3)\n",
210
+ "print(db.table_info)"
211
+ ]
212
+ },
213
+ {
214
+ "cell_type": "code",
215
+ "execution_count": 14,
216
+ "id": "4bad6deb",
217
+ "metadata": {},
218
+ "outputs": [
219
+ {
220
+ "name": "stdout",
221
+ "output_type": "stream",
222
+ "text": [
223
+ "Requirement already satisfied: langchain-experimental in /opt/homebrew/lib/python3.11/site-packages (0.0.25)\n",
224
+ "Requirement already satisfied: langchain>=0.0.239 in /opt/homebrew/lib/python3.11/site-packages (from langchain-experimental) (0.0.284)\n",
225
+ "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",
226
+ "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",
227
+ "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",
228
+ "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",
229
+ "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",
230
+ "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",
231
+ "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",
232
+ "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",
233
+ "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",
234
+ "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",
235
+ "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",
236
+ "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",
237
+ "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",
238
+ "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",
239
+ "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",
240
+ "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",
241
+ "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",
242
+ "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",
243
+ "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",
244
+ "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",
245
+ "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",
246
+ "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",
247
+ "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",
248
+ "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",
249
+ "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",
250
+ "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",
251
+ "Note: you may need to restart the kernel to use updated packages.\n"
252
+ ]
253
+ }
254
+ ],
255
+ "source": [
256
+ "pip install langchain-experimental"
257
+ ]
258
+ },
259
+ {
260
+ "cell_type": "code",
261
+ "execution_count": 15,
262
+ "id": "23d042fa",
263
+ "metadata": {},
264
+ "outputs": [],
265
+ "source": [
266
+ "from langchain_experimental.sql import SQLDatabaseChain\n",
267
+ "\n",
268
+ "db_chain = SQLDatabaseChain.from_llm(llm,db,verbose=True)"
269
+ ]
270
+ },
271
+ {
272
+ "cell_type": "code",
273
+ "execution_count": 16,
274
+ "id": "ac08c264",
275
+ "metadata": {},
276
+ "outputs": [
277
+ {
278
+ "name": "stdout",
279
+ "output_type": "stream",
280
+ "text": [
281
+ "\n",
282
+ "\n",
283
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
284
+ "how many t-shirts do we have for Levi in extra small and white color\n",
285
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT stock_quantity FROM t_shirts WHERE brand = 'Levi' AND color = 'White' AND size = 'XS'\u001b[0m\n",
286
+ "SQLResult: \u001b[33;1m\u001b[1;3m\u001b[0m\n",
287
+ "Answer:\u001b[32;1m\u001b[1;3m0\u001b[0m\n",
288
+ "\u001b[1m> Finished chain.\u001b[0m\n"
289
+ ]
290
+ }
291
+ ],
292
+ "source": [
293
+ "qs1 = db_chain.run(\"how many t-shirts do we have for Levi in extra small and white color\")"
294
+ ]
295
+ },
296
+ {
297
+ "cell_type": "code",
298
+ "execution_count": 17,
299
+ "id": "3cf99da1",
300
+ "metadata": {},
301
+ "outputs": [
302
+ {
303
+ "name": "stdout",
304
+ "output_type": "stream",
305
+ "text": [
306
+ "\n",
307
+ "\n",
308
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
309
+ "how much is the price of the inventory for all small size t-shirts?\n",
310
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT SUM(price) FROM t_shirts WHERE size = 'S'\u001b[0m\n",
311
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('371'),)]\u001b[0m\n",
312
+ "Answer:\u001b[32;1m\u001b[1;3m371\u001b[0m\n",
313
+ "\u001b[1m> Finished chain.\u001b[0m\n"
314
+ ]
315
+ }
316
+ ],
317
+ "source": [
318
+ "qs2 = db_chain.run(\"how much is the price of the inventory for all small size t-shirts?\")"
319
+ ]
320
+ },
321
+ {
322
+ "cell_type": "code",
323
+ "execution_count": 18,
324
+ "id": "7944d470",
325
+ "metadata": {},
326
+ "outputs": [
327
+ {
328
+ "name": "stdout",
329
+ "output_type": "stream",
330
+ "text": [
331
+ "\n",
332
+ "\n",
333
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
334
+ "SELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'\n",
335
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'\u001b[0m\n",
336
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('20416'),)]\u001b[0m\n",
337
+ "Answer:\u001b[32;1m\u001b[1;3m20416\u001b[0m\n",
338
+ "\u001b[1m> Finished chain.\u001b[0m\n"
339
+ ]
340
+ }
341
+ ],
342
+ "source": [
343
+ "qs2 = db_chain.run(\"SELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'\")"
344
+ ]
345
+ },
346
+ {
347
+ "cell_type": "code",
348
+ "execution_count": 19,
349
+ "id": "2105b4e3",
350
+ "metadata": {},
351
+ "outputs": [
352
+ {
353
+ "data": {
354
+ "text/plain": [
355
+ "'20416'"
356
+ ]
357
+ },
358
+ "execution_count": 19,
359
+ "metadata": {},
360
+ "output_type": "execute_result"
361
+ }
362
+ ],
363
+ "source": [
364
+ "qs2"
365
+ ]
366
+ },
367
+ {
368
+ "cell_type": "code",
369
+ "execution_count": 20,
370
+ "id": "083c6c80",
371
+ "metadata": {},
372
+ "outputs": [
373
+ {
374
+ "name": "stdout",
375
+ "output_type": "stream",
376
+ "text": [
377
+ "\n",
378
+ "\n",
379
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
380
+ "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",
381
+ "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"
382
+ ]
383
+ },
384
+ {
385
+ "ename": "OperationalError",
386
+ "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)",
387
+ "output_type": "error",
388
+ "traceback": [
389
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
390
+ "\u001b[0;31mOperationalError\u001b[0m Traceback (most recent call last)",
391
+ "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",
392
+ "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",
393
+ "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",
394
+ "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",
395
+ "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",
396
+ "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",
397
+ "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",
398
+ "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",
399
+ "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",
400
+ "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",
401
+ "\u001b[0;31mOperationalError\u001b[0m: (1054, \"Unknown column 'discounts.start_date' in 'where clause'\")",
402
+ "\nThe above exception was the direct cause of the following exception:\n",
403
+ "\u001b[0;31mOperationalError\u001b[0m Traceback (most recent call last)",
404
+ "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",
405
+ "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",
406
+ "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",
407
+ "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",
408
+ "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",
409
+ "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",
410
+ "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",
411
+ "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",
412
+ "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",
413
+ "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",
414
+ "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",
415
+ "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",
416
+ "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",
417
+ "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",
418
+ "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",
419
+ "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",
420
+ "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",
421
+ "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",
422
+ "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",
423
+ "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",
424
+ "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",
425
+ "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",
426
+ "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",
427
+ "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",
428
+ "\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)"
429
+ ]
430
+ }
431
+ ],
432
+ "source": [
433
+ "\n",
434
+ "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)?\")"
435
+ ]
436
+ },
437
+ {
438
+ "cell_type": "code",
439
+ "execution_count": 21,
440
+ "id": "1842f69c",
441
+ "metadata": {},
442
+ "outputs": [],
443
+ "source": [
444
+ "sql_code = \"\"\"\n",
445
+ "\n",
446
+ "SELECT sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) AS total_revenue \n",
447
+ "FROM (select sum(price*stock_quantity) AS total_amount, t_shirt_id FROM t_shirts WHERE brand = \"Levi\" \n",
448
+ "GROUP BY t_shirt_id) a LEFT JOIN discounts ON a.t_shirt_id=discounts.t_shirt_id\n",
449
+ "\n",
450
+ "\"\"\""
451
+ ]
452
+ },
453
+ {
454
+ "cell_type": "code",
455
+ "execution_count": 22,
456
+ "id": "0f5e3472",
457
+ "metadata": {},
458
+ "outputs": [
459
+ {
460
+ "name": "stdout",
461
+ "output_type": "stream",
462
+ "text": [
463
+ "\n",
464
+ "\n",
465
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
466
+ "\n",
467
+ "\n",
468
+ "SELECT sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) AS total_revenue \n",
469
+ "FROM (select sum(price*stock_quantity) AS total_amount, t_shirt_id FROM t_shirts WHERE brand = \"Levi\" \n",
470
+ "GROUP BY t_shirt_id) a LEFT JOIN discounts ON a.t_shirt_id=discounts.t_shirt_id\n",
471
+ "\n",
472
+ "\n",
473
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) AS total_revenue \n",
474
+ "FROM (select sum(price*stock_quantity) AS total_amount, t_shirt_id FROM t_shirts WHERE brand = \"Levi\" \n",
475
+ "GROUP BY t_shirt_id) a LEFT JOIN discounts ON a.t_shirt_id=discounts.t_shirt_id\u001b[0m\n",
476
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('26011.000000'),)]\u001b[0m\n",
477
+ "Answer:\u001b[32;1m\u001b[1;3m26011\u001b[0m\n",
478
+ "\u001b[1m> Finished chain.\u001b[0m\n"
479
+ ]
480
+ }
481
+ ],
482
+ "source": [
483
+ "qs3 = db_chain.run(sql_code)"
484
+ ]
485
+ },
486
+ {
487
+ "cell_type": "code",
488
+ "execution_count": 23,
489
+ "id": "8af1b5d8",
490
+ "metadata": {},
491
+ "outputs": [
492
+ {
493
+ "data": {
494
+ "text/plain": [
495
+ "'26011'"
496
+ ]
497
+ },
498
+ "execution_count": 23,
499
+ "metadata": {},
500
+ "output_type": "execute_result"
501
+ }
502
+ ],
503
+ "source": [
504
+ "qs3"
505
+ ]
506
+ },
507
+ {
508
+ "cell_type": "code",
509
+ "execution_count": 24,
510
+ "id": "47679dee",
511
+ "metadata": {},
512
+ "outputs": [
513
+ {
514
+ "name": "stdout",
515
+ "output_type": "stream",
516
+ "text": [
517
+ "\n",
518
+ "\n",
519
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
520
+ "how many white color levi's t-shirts we have available?\n",
521
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT stock_quantity FROM t_shirts WHERE brand = 'Levi' AND color = 'White'\u001b[0m\n",
522
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(40,), (85,), (36,)]\u001b[0m\n",
523
+ "Answer:\u001b[32;1m\u001b[1;3m85\u001b[0m\n",
524
+ "\u001b[1m> Finished chain.\u001b[0m\n"
525
+ ]
526
+ }
527
+ ],
528
+ "source": [
529
+ "qs4 = db_chain.run(\"how many white color levi's t-shirts we have available?\")"
530
+ ]
531
+ },
532
+ {
533
+ "cell_type": "code",
534
+ "execution_count": 25,
535
+ "id": "a83abf09",
536
+ "metadata": {},
537
+ "outputs": [
538
+ {
539
+ "name": "stdout",
540
+ "output_type": "stream",
541
+ "text": [
542
+ "\n",
543
+ "\n",
544
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
545
+ "SELECT sum(stock_quantity) FROM t-shirts WHERE brand = 'Levi' AND color = 'White'\n",
546
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT sum(stock_quantity) FROM t_shirts WHERE brand = 'Levi' AND color = 'White'\u001b[0m\n",
547
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('161'),)]\u001b[0m\n",
548
+ "Answer:\u001b[32;1m\u001b[1;3m161\u001b[0m\n",
549
+ "\u001b[1m> Finished chain.\u001b[0m\n"
550
+ ]
551
+ }
552
+ ],
553
+ "source": [
554
+ "qs4 = db_chain.run(\"SELECT sum(stock_quantity) FROM t-shirts WHERE brand = 'Levi' AND color = 'White'\")"
555
+ ]
556
+ },
557
+ {
558
+ "cell_type": "code",
559
+ "execution_count": 26,
560
+ "id": "02310a6d",
561
+ "metadata": {},
562
+ "outputs": [
563
+ {
564
+ "data": {
565
+ "text/plain": [
566
+ "'161'"
567
+ ]
568
+ },
569
+ "execution_count": 26,
570
+ "metadata": {},
571
+ "output_type": "execute_result"
572
+ }
573
+ ],
574
+ "source": [
575
+ "qs4"
576
+ ]
577
+ },
578
+ {
579
+ "cell_type": "code",
580
+ "execution_count": 27,
581
+ "id": "a25adb91",
582
+ "metadata": {},
583
+ "outputs": [],
584
+ "source": [
585
+ "fewshots = [\n",
586
+ " {\n",
587
+ " 'Question':\"how many t-shirts do we have for Nike in extra small and white color?\",\n",
588
+ " 'SQLQuery':\"SELECT stock_quantity FROM t_shirts WHERE brand = 'Nike' AND color = 'White' AND size = 'XS'\",\n",
589
+ " 'SQLResult':\"Result of the SQL query\",\n",
590
+ " 'Answer': qs1\n",
591
+ " },\n",
592
+ " {\n",
593
+ " 'Question':\"how much is the price of the inventory for all small size t-shirts?\",\n",
594
+ " 'SQLQuery':\"SELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'\",\n",
595
+ " 'SQLResult':\"Result of the SQL query\",\n",
596
+ " 'Answer': qs2\n",
597
+ " \n",
598
+ " },\n",
599
+ " {\n",
600
+ " '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",
601
+ " '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",
602
+ " 'SQLResult':\"Result of the SQL query\",\n",
603
+ " 'Answer': qs3\n",
604
+ " \n",
605
+ " },\n",
606
+ " {\n",
607
+ " 'Question':\"how many white color levi's t-shirts we have available?\",\n",
608
+ " 'SQLQuery':\"SELECT sum(stock_quantity) FROM t-shirts WHERE brand = 'Levi' AND color = 'White'\",\n",
609
+ " 'SQLResult':\"Result of the SQL query\",\n",
610
+ " 'Answer': qs4\n",
611
+ " } \n",
612
+ "]"
613
+ ]
614
+ },
615
+ {
616
+ "cell_type": "code",
617
+ "execution_count": 28,
618
+ "id": "e7dcad87",
619
+ "metadata": {},
620
+ "outputs": [],
621
+ "source": [
622
+ "from langchain.embeddings import HuggingFaceEmbeddings"
623
+ ]
624
+ },
625
+ {
626
+ "cell_type": "code",
627
+ "execution_count": 29,
628
+ "id": "1ee2a866",
629
+ "metadata": {},
630
+ "outputs": [
631
+ {
632
+ "name": "stdout",
633
+ "output_type": "stream",
634
+ "text": [
635
+ "Requirement already satisfied: sentence-transformers in /opt/homebrew/lib/python3.11/site-packages (2.6.1)\n",
636
+ "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",
637
+ "Requirement already satisfied: tqdm in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (4.66.2)\n",
638
+ "Requirement already satisfied: torch>=1.11.0 in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (2.2.1)\n",
639
+ "Requirement already satisfied: numpy in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (1.24.2)\n",
640
+ "Requirement already satisfied: scikit-learn in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (1.4.1.post1)\n",
641
+ "Requirement already satisfied: scipy in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (1.12.0)\n",
642
+ "Requirement already satisfied: huggingface-hub>=0.15.1 in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (0.21.4)\n",
643
+ "Requirement already satisfied: Pillow in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (10.2.0)\n",
644
+ "Requirement already satisfied: filelock in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.15.1->sentence-transformers) (3.13.1)\n",
645
+ "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",
646
+ "Requirement already satisfied: requests in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.15.1->sentence-transformers) (2.31.0)\n",
647
+ "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",
648
+ "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",
649
+ "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",
650
+ "Requirement already satisfied: sympy in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers) (1.12)\n",
651
+ "Requirement already satisfied: networkx in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers) (3.2.1)\n",
652
+ "Requirement already satisfied: jinja2 in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers) (3.1.3)\n",
653
+ "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",
654
+ "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",
655
+ "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",
656
+ "Requirement already satisfied: joblib>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from scikit-learn->sentence-transformers) (1.3.2)\n",
657
+ "Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/homebrew/lib/python3.11/site-packages (from scikit-learn->sentence-transformers) (3.3.0)\n",
658
+ "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",
659
+ "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",
660
+ "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",
661
+ "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",
662
+ "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",
663
+ "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",
664
+ "Note: you may need to restart the kernel to use updated packages.\n"
665
+ ]
666
+ }
667
+ ],
668
+ "source": [
669
+ "pip install sentence-transformers"
670
+ ]
671
+ },
672
+ {
673
+ "cell_type": "code",
674
+ "execution_count": 30,
675
+ "id": "9b32825e",
676
+ "metadata": {},
677
+ "outputs": [],
678
+ "source": [
679
+ "embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-V2')"
680
+ ]
681
+ },
682
+ {
683
+ "cell_type": "code",
684
+ "execution_count": 31,
685
+ "id": "f6d36cdc",
686
+ "metadata": {},
687
+ "outputs": [],
688
+ "source": [
689
+ "to_vectorize = [\" \".join(example.values())for example in fewshots]"
690
+ ]
691
+ },
692
+ {
693
+ "cell_type": "code",
694
+ "execution_count": 32,
695
+ "id": "d62ffe97",
696
+ "metadata": {},
697
+ "outputs": [
698
+ {
699
+ "data": {
700
+ "text/plain": [
701
+ "\"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\""
702
+ ]
703
+ },
704
+ "execution_count": 32,
705
+ "metadata": {},
706
+ "output_type": "execute_result"
707
+ }
708
+ ],
709
+ "source": [
710
+ "to_vectorize[0]"
711
+ ]
712
+ },
713
+ {
714
+ "cell_type": "code",
715
+ "execution_count": 33,
716
+ "id": "20ce3e8a",
717
+ "metadata": {
718
+ "scrolled": false
719
+ },
720
+ "outputs": [
721
+ {
722
+ "name": "stdout",
723
+ "output_type": "stream",
724
+ "text": [
725
+ "Requirement already satisfied: chromadb in /opt/homebrew/lib/python3.11/site-packages (0.4.24)\n",
726
+ "Requirement already satisfied: build>=1.0.3 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.1.1)\n",
727
+ "Requirement already satisfied: requests>=2.28 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (2.31.0)\n",
728
+ "Requirement already satisfied: pydantic>=1.9 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (2.6.4)\n",
729
+ "Requirement already satisfied: chroma-hnswlib==0.7.3 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.7.3)\n",
730
+ "Requirement already satisfied: fastapi>=0.95.2 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.110.1)\n",
731
+ "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",
732
+ "Requirement already satisfied: numpy>=1.22.5 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.24.2)\n",
733
+ "Requirement already satisfied: posthog>=2.4.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (3.5.0)\n",
734
+ "Requirement already satisfied: typing-extensions>=4.5.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.10.0)\n",
735
+ "Requirement already satisfied: pulsar-client>=3.1.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (3.4.0)\n",
736
+ "Requirement already satisfied: onnxruntime>=1.14.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.17.1)\n",
737
+ "Requirement already satisfied: opentelemetry-api>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.24.0)\n",
738
+ "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",
739
+ "Requirement already satisfied: opentelemetry-instrumentation-fastapi>=0.41b0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.45b0)\n",
740
+ "Requirement already satisfied: opentelemetry-sdk>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.24.0)\n",
741
+ "Requirement already satisfied: tokenizers>=0.13.2 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.15.2)\n",
742
+ "Requirement already satisfied: pypika>=0.48.9 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.48.9)\n",
743
+ "Requirement already satisfied: tqdm>=4.65.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.66.2)\n",
744
+ "Requirement already satisfied: overrides>=7.3.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (7.7.0)\n",
745
+ "Requirement already satisfied: importlib-resources in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (6.4.0)\n",
746
+ "Requirement already satisfied: grpcio>=1.58.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.62.1)\n",
747
+ "Requirement already satisfied: bcrypt>=4.0.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.1.2)\n",
748
+ "Requirement already satisfied: typer>=0.9.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.12.3)\n",
749
+ "Requirement already satisfied: kubernetes>=28.1.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (29.0.0)\n",
750
+ "Requirement already satisfied: tenacity>=8.2.3 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (8.2.3)\n",
751
+ "Requirement already satisfied: PyYAML>=6.0.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (6.0.1)\n",
752
+ "Requirement already satisfied: mmh3>=4.0.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.1.0)\n",
753
+ "Requirement already satisfied: orjson>=3.9.12 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (3.9.15)\n",
754
+ "Requirement already satisfied: packaging>=19.0 in /opt/homebrew/lib/python3.11/site-packages (from build>=1.0.3->chromadb) (23.2)\n",
755
+ "Requirement already satisfied: pyproject_hooks in /opt/homebrew/lib/python3.11/site-packages (from build>=1.0.3->chromadb) (1.0.0)\n",
756
+ "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",
757
+ "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",
758
+ "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",
759
+ "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",
760
+ "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",
761
+ "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",
762
+ "Requirement already satisfied: requests-oauthlib in /opt/homebrew/lib/python3.11/site-packages (from kubernetes>=28.1.0->chromadb) (2.0.0)\n",
763
+ "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",
764
+ "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",
765
+ "Requirement already satisfied: coloredlogs in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (15.0.1)\n",
766
+ "Requirement already satisfied: flatbuffers in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (24.3.7)\n",
767
+ "Requirement already satisfied: protobuf in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (4.25.3)\n",
768
+ "Requirement already satisfied: sympy in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (1.12)\n",
769
+ "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",
770
+ "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",
771
+ "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",
772
+ "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",
773
+ "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",
774
+ "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",
775
+ "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",
776
+ "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",
777
+ "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",
778
+ "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",
779
+ "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",
780
+ "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",
781
+ "Requirement already satisfied: monotonic>=1.5 in /opt/homebrew/lib/python3.11/site-packages (from posthog>=2.4.0->chromadb) (1.6)\n",
782
+ "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",
783
+ "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",
784
+ "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",
785
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.28->chromadb) (3.3.2)\n",
786
+ "Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.28->chromadb) (3.6)\n"
787
+ ]
788
+ },
789
+ {
790
+ "name": "stdout",
791
+ "output_type": "stream",
792
+ "text": [
793
+ "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",
794
+ "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",
795
+ "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",
796
+ "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",
797
+ "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",
798
+ "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",
799
+ "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",
800
+ "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",
801
+ "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",
802
+ "Requirement already satisfied: websockets>=10.4 in /opt/homebrew/lib/python3.11/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (12.0)\n",
803
+ "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",
804
+ "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",
805
+ "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",
806
+ "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",
807
+ "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",
808
+ "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",
809
+ "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",
810
+ "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",
811
+ "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",
812
+ "Requirement already satisfied: humanfriendly>=9.1 in /opt/homebrew/lib/python3.11/site-packages (from coloredlogs->onnxruntime>=1.14.1->chromadb) (10.0)\n",
813
+ "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",
814
+ "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",
815
+ "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",
816
+ "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",
817
+ "Note: you may need to restart the kernel to use updated packages.\n"
818
+ ]
819
+ }
820
+ ],
821
+ "source": [
822
+ "pip install chromadb"
823
+ ]
824
+ },
825
+ {
826
+ "cell_type": "code",
827
+ "execution_count": 34,
828
+ "id": "40d77eac",
829
+ "metadata": {},
830
+ "outputs": [],
831
+ "source": [
832
+ "from langchain.vectorstores import FAISS\n",
833
+ "\n",
834
+ "vectorstore = FAISS.from_texts(to_vectorize,embedding=embeddings,metadatas=fewshots)"
835
+ ]
836
+ },
837
+ {
838
+ "cell_type": "code",
839
+ "execution_count": 35,
840
+ "id": "db1b58e4",
841
+ "metadata": {},
842
+ "outputs": [
843
+ {
844
+ "data": {
845
+ "text/plain": [
846
+ "[{'Question': \"how many white color levi's t-shirts we have available?\",\n",
847
+ " 'SQLQuery': \"SELECT sum(stock_quantity) FROM t-shirts WHERE brand = 'Levi' AND color = 'White'\",\n",
848
+ " 'SQLResult': 'Result of the SQL query',\n",
849
+ " 'Answer': '161'},\n",
850
+ " {'Question': 'how many t-shirts do we have for Nike in extra small and white color?',\n",
851
+ " 'SQLQuery': \"SELECT stock_quantity FROM t_shirts WHERE brand = 'Nike' AND color = 'White' AND size = 'XS'\",\n",
852
+ " 'SQLResult': 'Result of the SQL query',\n",
853
+ " 'Answer': '0'}]"
854
+ ]
855
+ },
856
+ "execution_count": 35,
857
+ "metadata": {},
858
+ "output_type": "execute_result"
859
+ }
860
+ ],
861
+ "source": [
862
+ "from langchain.prompts import SemanticSimilarityExampleSelector\n",
863
+ "\n",
864
+ "example_selector = SemanticSimilarityExampleSelector(\n",
865
+ " vectorstore=vectorstore,\n",
866
+ " k=2\n",
867
+ ")\n",
868
+ "example_selector.select_examples({\"Question\": \"How many Addidas T shirts I have left in my store?\"})"
869
+ ]
870
+ },
871
+ {
872
+ "cell_type": "code",
873
+ "execution_count": 36,
874
+ "id": "20ac12c1",
875
+ "metadata": {},
876
+ "outputs": [],
877
+ "source": [
878
+ "from langchain.chains.sql_database.prompt import PROMPT_SUFFIX,_mysql_prompt"
879
+ ]
880
+ },
881
+ {
882
+ "cell_type": "code",
883
+ "execution_count": 37,
884
+ "id": "b9a6a53f",
885
+ "metadata": {},
886
+ "outputs": [
887
+ {
888
+ "name": "stdout",
889
+ "output_type": "stream",
890
+ "text": [
891
+ "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",
892
+ "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",
893
+ "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",
894
+ "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",
895
+ "Pay attention to use CURDATE() function to get the current date, if the question involves \"today\".\n",
896
+ "\n",
897
+ "Use the following format:\n",
898
+ "\n",
899
+ "Question: Question here\n",
900
+ "SQLQuery: SQL Query to run\n",
901
+ "SQLResult: Result of the SQLQuery\n",
902
+ "Answer: Final answer here\n",
903
+ "\n",
904
+ "\n",
905
+ "Only use the following tables:\n",
906
+ "{table_info}\n",
907
+ "\n",
908
+ "Question: {input}\n"
909
+ ]
910
+ },
911
+ {
912
+ "data": {
913
+ "text/plain": [
914
+ "(None, None)"
915
+ ]
916
+ },
917
+ "execution_count": 37,
918
+ "metadata": {},
919
+ "output_type": "execute_result"
920
+ }
921
+ ],
922
+ "source": [
923
+ "print(_mysql_prompt),print(PROMPT_SUFFIX)"
924
+ ]
925
+ },
926
+ {
927
+ "cell_type": "code",
928
+ "execution_count": 38,
929
+ "id": "26a9b9b0",
930
+ "metadata": {},
931
+ "outputs": [],
932
+ "source": [
933
+ "from langchain.prompts.prompt import PromptTemplate\n",
934
+ "\n",
935
+ "example_prompt = PromptTemplate(\n",
936
+ " input_variables=[\"Question\", \"SQLQuery\", \"SQLResult\",\"Answer\",],\n",
937
+ " template=\"\\nQuestion: {Question}\\nSQLQuery: {SQLQuery}\\nSQLResult: {SQLResult}\\nAnswer: {Answer}\",\n",
938
+ ")"
939
+ ]
940
+ },
941
+ {
942
+ "cell_type": "code",
943
+ "execution_count": 39,
944
+ "id": "a341c8df",
945
+ "metadata": {},
946
+ "outputs": [],
947
+ "source": [
948
+ "from langchain.prompts import FewShotPromptTemplate\n",
949
+ "\n",
950
+ "few_shot_prompt = FewShotPromptTemplate(\n",
951
+ " example_selector = example_selector,\n",
952
+ " example_prompt = example_prompt,\n",
953
+ " prefix = _mysql_prompt,\n",
954
+ " suffix = PROMPT_SUFFIX,\n",
955
+ " input_variables = [\"input\",\"table_info\",\"top_k\"]\n",
956
+ ")"
957
+ ]
958
+ },
959
+ {
960
+ "cell_type": "code",
961
+ "execution_count": 40,
962
+ "id": "d6e1cad5",
963
+ "metadata": {},
964
+ "outputs": [],
965
+ "source": [
966
+ "new_chain = SQLDatabaseChain.from_llm(llm,db,verbose=True,prompt = few_shot_prompt)"
967
+ ]
968
+ },
969
+ {
970
+ "cell_type": "code",
971
+ "execution_count": 41,
972
+ "id": "a130f9f9",
973
+ "metadata": {},
974
+ "outputs": [
975
+ {
976
+ "name": "stdout",
977
+ "output_type": "stream",
978
+ "text": [
979
+ "\n",
980
+ "\n",
981
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
982
+ "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",
983
+ "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",
984
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('207'),)]\u001b[0m\n",
985
+ "Answer:\u001b[32;1m\u001b[1;3m207\u001b[0m\n",
986
+ "\u001b[1m> Finished chain.\u001b[0m\n"
987
+ ]
988
+ }
989
+ ],
990
+ "source": [
991
+ "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\")"
992
+ ]
993
+ },
994
+ {
995
+ "cell_type": "code",
996
+ "execution_count": 42,
997
+ "id": "e4bd6489",
998
+ "metadata": {},
999
+ "outputs": [
1000
+ {
1001
+ "data": {
1002
+ "text/plain": [
1003
+ "'207'"
1004
+ ]
1005
+ },
1006
+ "execution_count": 42,
1007
+ "metadata": {},
1008
+ "output_type": "execute_result"
1009
+ }
1010
+ ],
1011
+ "source": [
1012
+ "ans"
1013
+ ]
1014
+ },
1015
+ {
1016
+ "cell_type": "code",
1017
+ "execution_count": null,
1018
+ "id": "dbe9e984",
1019
+ "metadata": {},
1020
+ "outputs": [],
1021
+ "source": []
1022
+ }
1023
+ ],
1024
+ "metadata": {
1025
+ "kernelspec": {
1026
+ "display_name": "Python 3 (ipykernel)",
1027
+ "language": "python",
1028
+ "name": "python3"
1029
+ },
1030
+ "language_info": {
1031
+ "codemirror_mode": {
1032
+ "name": "ipython",
1033
+ "version": 3
1034
+ },
1035
+ "file_extension": ".py",
1036
+ "mimetype": "text/x-python",
1037
+ "name": "python",
1038
+ "nbconvert_exporter": "python",
1039
+ "pygments_lexer": "ipython3",
1040
+ "version": "3.11.8"
1041
+ }
1042
+ },
1043
+ "nbformat": 4,
1044
+ "nbformat_minor": 5
1045
+ }
Main-Copy1.ipynb ADDED
@@ -0,0 +1,1045 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 2,
6
+ "id": "5b6ddc1d",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "#pip install langchain"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": 3,
16
+ "id": "428de9cd",
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "#pip install --upgrade pip"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": 4,
26
+ "id": "c6963d71",
27
+ "metadata": {},
28
+ "outputs": [],
29
+ "source": [
30
+ "#pip install --upgrade setuptools"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "execution_count": 5,
36
+ "id": "f107f9f5",
37
+ "metadata": {},
38
+ "outputs": [],
39
+ "source": [
40
+ "from langchain.llms import GooglePalm"
41
+ ]
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "execution_count": 6,
46
+ "id": "0cf73123",
47
+ "metadata": {},
48
+ "outputs": [],
49
+ "source": [
50
+ "#pip install google-generativeai"
51
+ ]
52
+ },
53
+ {
54
+ "cell_type": "code",
55
+ "execution_count": 7,
56
+ "id": "03866604",
57
+ "metadata": {},
58
+ "outputs": [],
59
+ "source": [
60
+ "import google.generativeai as genai"
61
+ ]
62
+ },
63
+ {
64
+ "cell_type": "code",
65
+ "execution_count": 8,
66
+ "id": "2a11da2a",
67
+ "metadata": {},
68
+ "outputs": [
69
+ {
70
+ "name": "stdout",
71
+ "output_type": "stream",
72
+ "text": [
73
+ "**Verse 1**\n",
74
+ "America, America,\n",
75
+ "Land of the free, home of the brave.\n",
76
+ "You're a beacon of hope,\n",
77
+ "For all the world to see.\n",
78
+ "**Chorus**\n",
79
+ "We're proud to be Americans,\n",
80
+ "We'll fight for our freedom,\n",
81
+ "We'll stand together,\n",
82
+ "As one nation, under God.\n",
83
+ "**Verse 2**\n",
84
+ "We're a melting pot,\n",
85
+ "Of cultures and races.\n",
86
+ "We're all different,\n",
87
+ "But we're all united.\n",
88
+ "**Chorus**\n",
89
+ "We're proud to be Americans,\n",
90
+ "We'll fight for our freedom,\n",
91
+ "We'll stand together,\n",
92
+ "As one nation, under God.\n",
93
+ "**Bridge**\n",
94
+ "Through thick and thin,\n",
95
+ "We'll always stick together.\n",
96
+ "We're Americans,\n",
97
+ "And we're proud.\n",
98
+ "**Chorus**\n",
99
+ "We're proud to be Americans,\n",
100
+ "We'll fight for our freedom,\n",
101
+ "We'll stand together,\n",
102
+ "As one nation, under God.\n"
103
+ ]
104
+ }
105
+ ],
106
+ "source": [
107
+ "api_key = \"AIzaSyBAsa1XcZysgK7DKo9Bs-9MNfixaRR0ocw\"\n",
108
+ "llm = GooglePalm(google_api_key=api_key, temperature=0.2)\n",
109
+ "poem = llm(\"write a song on America in 5 lines\")\n",
110
+ "print(poem)"
111
+ ]
112
+ },
113
+ {
114
+ "cell_type": "code",
115
+ "execution_count": 9,
116
+ "id": "09a438b5",
117
+ "metadata": {},
118
+ "outputs": [],
119
+ "source": [
120
+ "#pip install pymysql"
121
+ ]
122
+ },
123
+ {
124
+ "cell_type": "code",
125
+ "execution_count": 10,
126
+ "id": "74b4e2ef",
127
+ "metadata": {},
128
+ "outputs": [],
129
+ "source": [
130
+ "#pip install mysql-connector-python\n"
131
+ ]
132
+ },
133
+ {
134
+ "cell_type": "code",
135
+ "execution_count": 11,
136
+ "id": "d0ff0a6a",
137
+ "metadata": {},
138
+ "outputs": [],
139
+ "source": [
140
+ "#pip install mysqlclient\n"
141
+ ]
142
+ },
143
+ {
144
+ "cell_type": "code",
145
+ "execution_count": 12,
146
+ "id": "ff9174a2",
147
+ "metadata": {},
148
+ "outputs": [],
149
+ "source": [
150
+ "#pip install psycopg2"
151
+ ]
152
+ },
153
+ {
154
+ "cell_type": "code",
155
+ "execution_count": 13,
156
+ "id": "bf08a5a6",
157
+ "metadata": {},
158
+ "outputs": [
159
+ {
160
+ "name": "stdout",
161
+ "output_type": "stream",
162
+ "text": [
163
+ "\n",
164
+ "CREATE TABLE discounts (\n",
165
+ "\tdiscount_id INTEGER NOT NULL AUTO_INCREMENT, \n",
166
+ "\tt_shirt_id INTEGER NOT NULL, \n",
167
+ "\tpct_discount DECIMAL(5, 2), \n",
168
+ "\tPRIMARY KEY (discount_id), \n",
169
+ "\tCONSTRAINT discounts_ibfk_1 FOREIGN KEY(t_shirt_id) REFERENCES t_shirts (t_shirt_id), \n",
170
+ "\tCONSTRAINT discounts_chk_1 CHECK ((`pct_discount` between 0 and 100))\n",
171
+ ")COLLATE utf8mb4_0900_ai_ci ENGINE=InnoDB DEFAULT CHARSET=utf8mb4\n",
172
+ "\n",
173
+ "/*\n",
174
+ "3 rows from discounts table:\n",
175
+ "discount_id\tt_shirt_id\tpct_discount\n",
176
+ "\n",
177
+ "*/\n",
178
+ "\n",
179
+ "\n",
180
+ "CREATE TABLE t_shirts (\n",
181
+ "\tt_shirt_id INTEGER NOT NULL AUTO_INCREMENT, \n",
182
+ "\tbrand ENUM('Van Huesen','Levi','Nike','Adidas') NOT NULL, \n",
183
+ "\tcolor ENUM('Red','Blue','Black','White') NOT NULL, \n",
184
+ "\tsize ENUM('XS','S','M','L','XL') NOT NULL, \n",
185
+ "\tprice INTEGER, \n",
186
+ "\tstock_quantity INTEGER NOT NULL, \n",
187
+ "\tPRIMARY KEY (t_shirt_id), \n",
188
+ "\tCONSTRAINT t_shirts_chk_1 CHECK ((`price` between 10 and 50))\n",
189
+ ")COLLATE utf8mb4_0900_ai_ci ENGINE=InnoDB DEFAULT CHARSET=utf8mb4\n",
190
+ "\n",
191
+ "/*\n",
192
+ "3 rows from t_shirts table:\n",
193
+ "t_shirt_id\tbrand\tcolor\tsize\tprice\tstock_quantity\n",
194
+ "1\tLevi\tWhite\tL\t33\t85\n",
195
+ "2\tLevi\tBlack\tL\t30\t64\n",
196
+ "4\tNike\tWhite\tL\t45\t33\n",
197
+ "*/\n"
198
+ ]
199
+ }
200
+ ],
201
+ "source": [
202
+ "from langchain.utilities import SQLDatabase\n",
203
+ "\n",
204
+ "db_user = \"root\"\n",
205
+ "db_password = \"root\"\n",
206
+ "db_host = \"localhost\"\n",
207
+ "db_name = \"atliq_tshirts\"\n",
208
+ "\n",
209
+ "db = SQLDatabase.from_uri(f\"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}\",sample_rows_in_table_info=3)\n",
210
+ "print(db.table_info)"
211
+ ]
212
+ },
213
+ {
214
+ "cell_type": "code",
215
+ "execution_count": 14,
216
+ "id": "4bad6deb",
217
+ "metadata": {},
218
+ "outputs": [
219
+ {
220
+ "name": "stdout",
221
+ "output_type": "stream",
222
+ "text": [
223
+ "Requirement already satisfied: langchain-experimental in /opt/homebrew/lib/python3.11/site-packages (0.0.25)\n",
224
+ "Requirement already satisfied: langchain>=0.0.239 in /opt/homebrew/lib/python3.11/site-packages (from langchain-experimental) (0.0.284)\n",
225
+ "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",
226
+ "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",
227
+ "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",
228
+ "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",
229
+ "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",
230
+ "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",
231
+ "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",
232
+ "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",
233
+ "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",
234
+ "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",
235
+ "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",
236
+ "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",
237
+ "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",
238
+ "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",
239
+ "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",
240
+ "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",
241
+ "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",
242
+ "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",
243
+ "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",
244
+ "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",
245
+ "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",
246
+ "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",
247
+ "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",
248
+ "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",
249
+ "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",
250
+ "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",
251
+ "Note: you may need to restart the kernel to use updated packages.\n"
252
+ ]
253
+ }
254
+ ],
255
+ "source": [
256
+ "pip install langchain-experimental"
257
+ ]
258
+ },
259
+ {
260
+ "cell_type": "code",
261
+ "execution_count": 15,
262
+ "id": "23d042fa",
263
+ "metadata": {},
264
+ "outputs": [],
265
+ "source": [
266
+ "from langchain_experimental.sql import SQLDatabaseChain\n",
267
+ "\n",
268
+ "db_chain = SQLDatabaseChain.from_llm(llm,db,verbose=True)"
269
+ ]
270
+ },
271
+ {
272
+ "cell_type": "code",
273
+ "execution_count": 16,
274
+ "id": "ac08c264",
275
+ "metadata": {},
276
+ "outputs": [
277
+ {
278
+ "name": "stdout",
279
+ "output_type": "stream",
280
+ "text": [
281
+ "\n",
282
+ "\n",
283
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
284
+ "how many t-shirts do we have for Levi in extra small and white color\n",
285
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT stock_quantity FROM t_shirts WHERE brand = 'Levi' AND color = 'White' AND size = 'XS'\u001b[0m\n",
286
+ "SQLResult: \u001b[33;1m\u001b[1;3m\u001b[0m\n",
287
+ "Answer:\u001b[32;1m\u001b[1;3m0\u001b[0m\n",
288
+ "\u001b[1m> Finished chain.\u001b[0m\n"
289
+ ]
290
+ }
291
+ ],
292
+ "source": [
293
+ "qs1 = db_chain.run(\"how many t-shirts do we have for Levi in extra small and white color\")"
294
+ ]
295
+ },
296
+ {
297
+ "cell_type": "code",
298
+ "execution_count": 17,
299
+ "id": "3cf99da1",
300
+ "metadata": {},
301
+ "outputs": [
302
+ {
303
+ "name": "stdout",
304
+ "output_type": "stream",
305
+ "text": [
306
+ "\n",
307
+ "\n",
308
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
309
+ "how much is the price of the inventory for all small size t-shirts?\n",
310
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT SUM(price) FROM t_shirts WHERE size = 'S'\u001b[0m\n",
311
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('371'),)]\u001b[0m\n",
312
+ "Answer:\u001b[32;1m\u001b[1;3m371\u001b[0m\n",
313
+ "\u001b[1m> Finished chain.\u001b[0m\n"
314
+ ]
315
+ }
316
+ ],
317
+ "source": [
318
+ "qs2 = db_chain.run(\"how much is the price of the inventory for all small size t-shirts?\")"
319
+ ]
320
+ },
321
+ {
322
+ "cell_type": "code",
323
+ "execution_count": 18,
324
+ "id": "7944d470",
325
+ "metadata": {},
326
+ "outputs": [
327
+ {
328
+ "name": "stdout",
329
+ "output_type": "stream",
330
+ "text": [
331
+ "\n",
332
+ "\n",
333
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
334
+ "SELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'\n",
335
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'\u001b[0m\n",
336
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('20416'),)]\u001b[0m\n",
337
+ "Answer:\u001b[32;1m\u001b[1;3m20416\u001b[0m\n",
338
+ "\u001b[1m> Finished chain.\u001b[0m\n"
339
+ ]
340
+ }
341
+ ],
342
+ "source": [
343
+ "qs2 = db_chain.run(\"SELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'\")"
344
+ ]
345
+ },
346
+ {
347
+ "cell_type": "code",
348
+ "execution_count": 19,
349
+ "id": "2105b4e3",
350
+ "metadata": {},
351
+ "outputs": [
352
+ {
353
+ "data": {
354
+ "text/plain": [
355
+ "'20416'"
356
+ ]
357
+ },
358
+ "execution_count": 19,
359
+ "metadata": {},
360
+ "output_type": "execute_result"
361
+ }
362
+ ],
363
+ "source": [
364
+ "qs2"
365
+ ]
366
+ },
367
+ {
368
+ "cell_type": "code",
369
+ "execution_count": 20,
370
+ "id": "083c6c80",
371
+ "metadata": {},
372
+ "outputs": [
373
+ {
374
+ "name": "stdout",
375
+ "output_type": "stream",
376
+ "text": [
377
+ "\n",
378
+ "\n",
379
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
380
+ "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",
381
+ "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"
382
+ ]
383
+ },
384
+ {
385
+ "ename": "OperationalError",
386
+ "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)",
387
+ "output_type": "error",
388
+ "traceback": [
389
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
390
+ "\u001b[0;31mOperationalError\u001b[0m Traceback (most recent call last)",
391
+ "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",
392
+ "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",
393
+ "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",
394
+ "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",
395
+ "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",
396
+ "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",
397
+ "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",
398
+ "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",
399
+ "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",
400
+ "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",
401
+ "\u001b[0;31mOperationalError\u001b[0m: (1054, \"Unknown column 'discounts.start_date' in 'where clause'\")",
402
+ "\nThe above exception was the direct cause of the following exception:\n",
403
+ "\u001b[0;31mOperationalError\u001b[0m Traceback (most recent call last)",
404
+ "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",
405
+ "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",
406
+ "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",
407
+ "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",
408
+ "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",
409
+ "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",
410
+ "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",
411
+ "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",
412
+ "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",
413
+ "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",
414
+ "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",
415
+ "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",
416
+ "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",
417
+ "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",
418
+ "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",
419
+ "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",
420
+ "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",
421
+ "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",
422
+ "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",
423
+ "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",
424
+ "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",
425
+ "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",
426
+ "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",
427
+ "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",
428
+ "\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)"
429
+ ]
430
+ }
431
+ ],
432
+ "source": [
433
+ "\n",
434
+ "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)?\")"
435
+ ]
436
+ },
437
+ {
438
+ "cell_type": "code",
439
+ "execution_count": 21,
440
+ "id": "1842f69c",
441
+ "metadata": {},
442
+ "outputs": [],
443
+ "source": [
444
+ "sql_code = \"\"\"\n",
445
+ "\n",
446
+ "SELECT sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) AS total_revenue \n",
447
+ "FROM (select sum(price*stock_quantity) AS total_amount, t_shirt_id FROM t_shirts WHERE brand = \"Levi\" \n",
448
+ "GROUP BY t_shirt_id) a LEFT JOIN discounts ON a.t_shirt_id=discounts.t_shirt_id\n",
449
+ "\n",
450
+ "\"\"\""
451
+ ]
452
+ },
453
+ {
454
+ "cell_type": "code",
455
+ "execution_count": 22,
456
+ "id": "0f5e3472",
457
+ "metadata": {},
458
+ "outputs": [
459
+ {
460
+ "name": "stdout",
461
+ "output_type": "stream",
462
+ "text": [
463
+ "\n",
464
+ "\n",
465
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
466
+ "\n",
467
+ "\n",
468
+ "SELECT sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) AS total_revenue \n",
469
+ "FROM (select sum(price*stock_quantity) AS total_amount, t_shirt_id FROM t_shirts WHERE brand = \"Levi\" \n",
470
+ "GROUP BY t_shirt_id) a LEFT JOIN discounts ON a.t_shirt_id=discounts.t_shirt_id\n",
471
+ "\n",
472
+ "\n",
473
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) AS total_revenue \n",
474
+ "FROM (select sum(price*stock_quantity) AS total_amount, t_shirt_id FROM t_shirts WHERE brand = \"Levi\" \n",
475
+ "GROUP BY t_shirt_id) a LEFT JOIN discounts ON a.t_shirt_id=discounts.t_shirt_id\u001b[0m\n",
476
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('26011.000000'),)]\u001b[0m\n",
477
+ "Answer:\u001b[32;1m\u001b[1;3m26011\u001b[0m\n",
478
+ "\u001b[1m> Finished chain.\u001b[0m\n"
479
+ ]
480
+ }
481
+ ],
482
+ "source": [
483
+ "qs3 = db_chain.run(sql_code)"
484
+ ]
485
+ },
486
+ {
487
+ "cell_type": "code",
488
+ "execution_count": 23,
489
+ "id": "8af1b5d8",
490
+ "metadata": {},
491
+ "outputs": [
492
+ {
493
+ "data": {
494
+ "text/plain": [
495
+ "'26011'"
496
+ ]
497
+ },
498
+ "execution_count": 23,
499
+ "metadata": {},
500
+ "output_type": "execute_result"
501
+ }
502
+ ],
503
+ "source": [
504
+ "qs3"
505
+ ]
506
+ },
507
+ {
508
+ "cell_type": "code",
509
+ "execution_count": 24,
510
+ "id": "47679dee",
511
+ "metadata": {},
512
+ "outputs": [
513
+ {
514
+ "name": "stdout",
515
+ "output_type": "stream",
516
+ "text": [
517
+ "\n",
518
+ "\n",
519
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
520
+ "how many white color levi's t-shirts we have available?\n",
521
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT stock_quantity FROM t_shirts WHERE brand = 'Levi' AND color = 'White'\u001b[0m\n",
522
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(40,), (85,), (36,)]\u001b[0m\n",
523
+ "Answer:\u001b[32;1m\u001b[1;3m85\u001b[0m\n",
524
+ "\u001b[1m> Finished chain.\u001b[0m\n"
525
+ ]
526
+ }
527
+ ],
528
+ "source": [
529
+ "qs4 = db_chain.run(\"how many white color levi's t-shirts we have available?\")"
530
+ ]
531
+ },
532
+ {
533
+ "cell_type": "code",
534
+ "execution_count": 25,
535
+ "id": "a83abf09",
536
+ "metadata": {},
537
+ "outputs": [
538
+ {
539
+ "name": "stdout",
540
+ "output_type": "stream",
541
+ "text": [
542
+ "\n",
543
+ "\n",
544
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
545
+ "SELECT sum(stock_quantity) FROM t-shirts WHERE brand = 'Levi' AND color = 'White'\n",
546
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT sum(stock_quantity) FROM t_shirts WHERE brand = 'Levi' AND color = 'White'\u001b[0m\n",
547
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('161'),)]\u001b[0m\n",
548
+ "Answer:\u001b[32;1m\u001b[1;3m161\u001b[0m\n",
549
+ "\u001b[1m> Finished chain.\u001b[0m\n"
550
+ ]
551
+ }
552
+ ],
553
+ "source": [
554
+ "qs4 = db_chain.run(\"SELECT sum(stock_quantity) FROM t-shirts WHERE brand = 'Levi' AND color = 'White'\")"
555
+ ]
556
+ },
557
+ {
558
+ "cell_type": "code",
559
+ "execution_count": 26,
560
+ "id": "02310a6d",
561
+ "metadata": {},
562
+ "outputs": [
563
+ {
564
+ "data": {
565
+ "text/plain": [
566
+ "'161'"
567
+ ]
568
+ },
569
+ "execution_count": 26,
570
+ "metadata": {},
571
+ "output_type": "execute_result"
572
+ }
573
+ ],
574
+ "source": [
575
+ "qs4"
576
+ ]
577
+ },
578
+ {
579
+ "cell_type": "code",
580
+ "execution_count": 27,
581
+ "id": "a25adb91",
582
+ "metadata": {},
583
+ "outputs": [],
584
+ "source": [
585
+ "fewshots = [\n",
586
+ " {\n",
587
+ " 'Question':\"how many t-shirts do we have for Nike in extra small and white color?\",\n",
588
+ " 'SQLQuery':\"SELECT stock_quantity FROM t_shirts WHERE brand = 'Nike' AND color = 'White' AND size = 'XS'\",\n",
589
+ " 'SQLResult':\"Result of the SQL query\",\n",
590
+ " 'Answer': qs1\n",
591
+ " },\n",
592
+ " {\n",
593
+ " 'Question':\"how much is the price of the inventory for all small size t-shirts?\",\n",
594
+ " 'SQLQuery':\"SELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'\",\n",
595
+ " 'SQLResult':\"Result of the SQL query\",\n",
596
+ " 'Answer': qs2\n",
597
+ " \n",
598
+ " },\n",
599
+ " {\n",
600
+ " '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",
601
+ " '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",
602
+ " 'SQLResult':\"Result of the SQL query\",\n",
603
+ " 'Answer': qs3\n",
604
+ " \n",
605
+ " },\n",
606
+ " {\n",
607
+ " 'Question':\"how many white color levi's t-shirts we have available?\",\n",
608
+ " 'SQLQuery':\"SELECT sum(stock_quantity) FROM t-shirts WHERE brand = 'Levi' AND color = 'White'\",\n",
609
+ " 'SQLResult':\"Result of the SQL query\",\n",
610
+ " 'Answer': qs4\n",
611
+ " } \n",
612
+ "]"
613
+ ]
614
+ },
615
+ {
616
+ "cell_type": "code",
617
+ "execution_count": 28,
618
+ "id": "e7dcad87",
619
+ "metadata": {},
620
+ "outputs": [],
621
+ "source": [
622
+ "from langchain.embeddings import HuggingFaceEmbeddings"
623
+ ]
624
+ },
625
+ {
626
+ "cell_type": "code",
627
+ "execution_count": 29,
628
+ "id": "1ee2a866",
629
+ "metadata": {},
630
+ "outputs": [
631
+ {
632
+ "name": "stdout",
633
+ "output_type": "stream",
634
+ "text": [
635
+ "Requirement already satisfied: sentence-transformers in /opt/homebrew/lib/python3.11/site-packages (2.6.1)\n",
636
+ "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",
637
+ "Requirement already satisfied: tqdm in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (4.66.2)\n",
638
+ "Requirement already satisfied: torch>=1.11.0 in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (2.2.1)\n",
639
+ "Requirement already satisfied: numpy in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (1.24.2)\n",
640
+ "Requirement already satisfied: scikit-learn in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (1.4.1.post1)\n",
641
+ "Requirement already satisfied: scipy in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (1.12.0)\n",
642
+ "Requirement already satisfied: huggingface-hub>=0.15.1 in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (0.21.4)\n",
643
+ "Requirement already satisfied: Pillow in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (10.2.0)\n",
644
+ "Requirement already satisfied: filelock in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.15.1->sentence-transformers) (3.13.1)\n",
645
+ "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",
646
+ "Requirement already satisfied: requests in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.15.1->sentence-transformers) (2.31.0)\n",
647
+ "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",
648
+ "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",
649
+ "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",
650
+ "Requirement already satisfied: sympy in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers) (1.12)\n",
651
+ "Requirement already satisfied: networkx in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers) (3.2.1)\n",
652
+ "Requirement already satisfied: jinja2 in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers) (3.1.3)\n",
653
+ "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",
654
+ "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",
655
+ "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",
656
+ "Requirement already satisfied: joblib>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from scikit-learn->sentence-transformers) (1.3.2)\n",
657
+ "Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/homebrew/lib/python3.11/site-packages (from scikit-learn->sentence-transformers) (3.3.0)\n",
658
+ "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",
659
+ "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",
660
+ "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",
661
+ "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",
662
+ "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",
663
+ "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",
664
+ "Note: you may need to restart the kernel to use updated packages.\n"
665
+ ]
666
+ }
667
+ ],
668
+ "source": [
669
+ "pip install sentence-transformers"
670
+ ]
671
+ },
672
+ {
673
+ "cell_type": "code",
674
+ "execution_count": 30,
675
+ "id": "9b32825e",
676
+ "metadata": {},
677
+ "outputs": [],
678
+ "source": [
679
+ "embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-V2')"
680
+ ]
681
+ },
682
+ {
683
+ "cell_type": "code",
684
+ "execution_count": 31,
685
+ "id": "f6d36cdc",
686
+ "metadata": {},
687
+ "outputs": [],
688
+ "source": [
689
+ "to_vectorize = [\" \".join(example.values())for example in fewshots]"
690
+ ]
691
+ },
692
+ {
693
+ "cell_type": "code",
694
+ "execution_count": 32,
695
+ "id": "d62ffe97",
696
+ "metadata": {},
697
+ "outputs": [
698
+ {
699
+ "data": {
700
+ "text/plain": [
701
+ "\"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\""
702
+ ]
703
+ },
704
+ "execution_count": 32,
705
+ "metadata": {},
706
+ "output_type": "execute_result"
707
+ }
708
+ ],
709
+ "source": [
710
+ "to_vectorize[0]"
711
+ ]
712
+ },
713
+ {
714
+ "cell_type": "code",
715
+ "execution_count": 33,
716
+ "id": "20ce3e8a",
717
+ "metadata": {
718
+ "scrolled": false
719
+ },
720
+ "outputs": [
721
+ {
722
+ "name": "stdout",
723
+ "output_type": "stream",
724
+ "text": [
725
+ "Requirement already satisfied: chromadb in /opt/homebrew/lib/python3.11/site-packages (0.4.24)\n",
726
+ "Requirement already satisfied: build>=1.0.3 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.1.1)\n",
727
+ "Requirement already satisfied: requests>=2.28 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (2.31.0)\n",
728
+ "Requirement already satisfied: pydantic>=1.9 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (2.6.4)\n",
729
+ "Requirement already satisfied: chroma-hnswlib==0.7.3 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.7.3)\n",
730
+ "Requirement already satisfied: fastapi>=0.95.2 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.110.1)\n",
731
+ "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",
732
+ "Requirement already satisfied: numpy>=1.22.5 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.24.2)\n",
733
+ "Requirement already satisfied: posthog>=2.4.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (3.5.0)\n",
734
+ "Requirement already satisfied: typing-extensions>=4.5.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.10.0)\n",
735
+ "Requirement already satisfied: pulsar-client>=3.1.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (3.4.0)\n",
736
+ "Requirement already satisfied: onnxruntime>=1.14.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.17.1)\n",
737
+ "Requirement already satisfied: opentelemetry-api>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.24.0)\n",
738
+ "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",
739
+ "Requirement already satisfied: opentelemetry-instrumentation-fastapi>=0.41b0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.45b0)\n",
740
+ "Requirement already satisfied: opentelemetry-sdk>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.24.0)\n",
741
+ "Requirement already satisfied: tokenizers>=0.13.2 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.15.2)\n",
742
+ "Requirement already satisfied: pypika>=0.48.9 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.48.9)\n",
743
+ "Requirement already satisfied: tqdm>=4.65.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.66.2)\n",
744
+ "Requirement already satisfied: overrides>=7.3.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (7.7.0)\n",
745
+ "Requirement already satisfied: importlib-resources in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (6.4.0)\n",
746
+ "Requirement already satisfied: grpcio>=1.58.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.62.1)\n",
747
+ "Requirement already satisfied: bcrypt>=4.0.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.1.2)\n",
748
+ "Requirement already satisfied: typer>=0.9.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.12.3)\n",
749
+ "Requirement already satisfied: kubernetes>=28.1.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (29.0.0)\n",
750
+ "Requirement already satisfied: tenacity>=8.2.3 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (8.2.3)\n",
751
+ "Requirement already satisfied: PyYAML>=6.0.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (6.0.1)\n",
752
+ "Requirement already satisfied: mmh3>=4.0.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.1.0)\n",
753
+ "Requirement already satisfied: orjson>=3.9.12 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (3.9.15)\n",
754
+ "Requirement already satisfied: packaging>=19.0 in /opt/homebrew/lib/python3.11/site-packages (from build>=1.0.3->chromadb) (23.2)\n",
755
+ "Requirement already satisfied: pyproject_hooks in /opt/homebrew/lib/python3.11/site-packages (from build>=1.0.3->chromadb) (1.0.0)\n",
756
+ "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",
757
+ "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",
758
+ "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",
759
+ "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",
760
+ "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",
761
+ "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",
762
+ "Requirement already satisfied: requests-oauthlib in /opt/homebrew/lib/python3.11/site-packages (from kubernetes>=28.1.0->chromadb) (2.0.0)\n",
763
+ "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",
764
+ "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",
765
+ "Requirement already satisfied: coloredlogs in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (15.0.1)\n",
766
+ "Requirement already satisfied: flatbuffers in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (24.3.7)\n",
767
+ "Requirement already satisfied: protobuf in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (4.25.3)\n",
768
+ "Requirement already satisfied: sympy in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (1.12)\n",
769
+ "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",
770
+ "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",
771
+ "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",
772
+ "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",
773
+ "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",
774
+ "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",
775
+ "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",
776
+ "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",
777
+ "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",
778
+ "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",
779
+ "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",
780
+ "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",
781
+ "Requirement already satisfied: monotonic>=1.5 in /opt/homebrew/lib/python3.11/site-packages (from posthog>=2.4.0->chromadb) (1.6)\n",
782
+ "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",
783
+ "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",
784
+ "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",
785
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.28->chromadb) (3.3.2)\n",
786
+ "Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.28->chromadb) (3.6)\n"
787
+ ]
788
+ },
789
+ {
790
+ "name": "stdout",
791
+ "output_type": "stream",
792
+ "text": [
793
+ "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",
794
+ "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",
795
+ "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",
796
+ "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",
797
+ "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",
798
+ "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",
799
+ "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",
800
+ "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",
801
+ "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",
802
+ "Requirement already satisfied: websockets>=10.4 in /opt/homebrew/lib/python3.11/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (12.0)\n",
803
+ "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",
804
+ "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",
805
+ "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",
806
+ "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",
807
+ "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",
808
+ "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",
809
+ "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",
810
+ "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",
811
+ "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",
812
+ "Requirement already satisfied: humanfriendly>=9.1 in /opt/homebrew/lib/python3.11/site-packages (from coloredlogs->onnxruntime>=1.14.1->chromadb) (10.0)\n",
813
+ "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",
814
+ "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",
815
+ "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",
816
+ "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",
817
+ "Note: you may need to restart the kernel to use updated packages.\n"
818
+ ]
819
+ }
820
+ ],
821
+ "source": [
822
+ "pip install chromadb"
823
+ ]
824
+ },
825
+ {
826
+ "cell_type": "code",
827
+ "execution_count": 34,
828
+ "id": "40d77eac",
829
+ "metadata": {},
830
+ "outputs": [],
831
+ "source": [
832
+ "from langchain.vectorstores import FAISS\n",
833
+ "\n",
834
+ "vectorstore = FAISS.from_texts(to_vectorize,embedding=embeddings,metadatas=fewshots)"
835
+ ]
836
+ },
837
+ {
838
+ "cell_type": "code",
839
+ "execution_count": 35,
840
+ "id": "db1b58e4",
841
+ "metadata": {},
842
+ "outputs": [
843
+ {
844
+ "data": {
845
+ "text/plain": [
846
+ "[{'Question': \"how many white color levi's t-shirts we have available?\",\n",
847
+ " 'SQLQuery': \"SELECT sum(stock_quantity) FROM t-shirts WHERE brand = 'Levi' AND color = 'White'\",\n",
848
+ " 'SQLResult': 'Result of the SQL query',\n",
849
+ " 'Answer': '161'},\n",
850
+ " {'Question': 'how many t-shirts do we have for Nike in extra small and white color?',\n",
851
+ " 'SQLQuery': \"SELECT stock_quantity FROM t_shirts WHERE brand = 'Nike' AND color = 'White' AND size = 'XS'\",\n",
852
+ " 'SQLResult': 'Result of the SQL query',\n",
853
+ " 'Answer': '0'}]"
854
+ ]
855
+ },
856
+ "execution_count": 35,
857
+ "metadata": {},
858
+ "output_type": "execute_result"
859
+ }
860
+ ],
861
+ "source": [
862
+ "from langchain.prompts import SemanticSimilarityExampleSelector\n",
863
+ "\n",
864
+ "example_selector = SemanticSimilarityExampleSelector(\n",
865
+ " vectorstore=vectorstore,\n",
866
+ " k=2\n",
867
+ ")\n",
868
+ "example_selector.select_examples({\"Question\": \"How many Addidas T shirts I have left in my store?\"})"
869
+ ]
870
+ },
871
+ {
872
+ "cell_type": "code",
873
+ "execution_count": 36,
874
+ "id": "20ac12c1",
875
+ "metadata": {},
876
+ "outputs": [],
877
+ "source": [
878
+ "from langchain.chains.sql_database.prompt import PROMPT_SUFFIX,_mysql_prompt"
879
+ ]
880
+ },
881
+ {
882
+ "cell_type": "code",
883
+ "execution_count": 37,
884
+ "id": "b9a6a53f",
885
+ "metadata": {},
886
+ "outputs": [
887
+ {
888
+ "name": "stdout",
889
+ "output_type": "stream",
890
+ "text": [
891
+ "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",
892
+ "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",
893
+ "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",
894
+ "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",
895
+ "Pay attention to use CURDATE() function to get the current date, if the question involves \"today\".\n",
896
+ "\n",
897
+ "Use the following format:\n",
898
+ "\n",
899
+ "Question: Question here\n",
900
+ "SQLQuery: SQL Query to run\n",
901
+ "SQLResult: Result of the SQLQuery\n",
902
+ "Answer: Final answer here\n",
903
+ "\n",
904
+ "\n",
905
+ "Only use the following tables:\n",
906
+ "{table_info}\n",
907
+ "\n",
908
+ "Question: {input}\n"
909
+ ]
910
+ },
911
+ {
912
+ "data": {
913
+ "text/plain": [
914
+ "(None, None)"
915
+ ]
916
+ },
917
+ "execution_count": 37,
918
+ "metadata": {},
919
+ "output_type": "execute_result"
920
+ }
921
+ ],
922
+ "source": [
923
+ "print(_mysql_prompt),print(PROMPT_SUFFIX)"
924
+ ]
925
+ },
926
+ {
927
+ "cell_type": "code",
928
+ "execution_count": 38,
929
+ "id": "26a9b9b0",
930
+ "metadata": {},
931
+ "outputs": [],
932
+ "source": [
933
+ "from langchain.prompts.prompt import PromptTemplate\n",
934
+ "\n",
935
+ "example_prompt = PromptTemplate(\n",
936
+ " input_variables=[\"Question\", \"SQLQuery\", \"SQLResult\",\"Answer\",],\n",
937
+ " template=\"\\nQuestion: {Question}\\nSQLQuery: {SQLQuery}\\nSQLResult: {SQLResult}\\nAnswer: {Answer}\",\n",
938
+ ")"
939
+ ]
940
+ },
941
+ {
942
+ "cell_type": "code",
943
+ "execution_count": 39,
944
+ "id": "a341c8df",
945
+ "metadata": {},
946
+ "outputs": [],
947
+ "source": [
948
+ "from langchain.prompts import FewShotPromptTemplate\n",
949
+ "\n",
950
+ "few_shot_prompt = FewShotPromptTemplate(\n",
951
+ " example_selector = example_selector,\n",
952
+ " example_prompt = example_prompt,\n",
953
+ " prefix = _mysql_prompt,\n",
954
+ " suffix = PROMPT_SUFFIX,\n",
955
+ " input_variables = [\"input\",\"table_info\",\"top_k\"]\n",
956
+ ")"
957
+ ]
958
+ },
959
+ {
960
+ "cell_type": "code",
961
+ "execution_count": 40,
962
+ "id": "d6e1cad5",
963
+ "metadata": {},
964
+ "outputs": [],
965
+ "source": [
966
+ "new_chain = SQLDatabaseChain.from_llm(llm,db,verbose=True,prompt = few_shot_prompt)"
967
+ ]
968
+ },
969
+ {
970
+ "cell_type": "code",
971
+ "execution_count": 41,
972
+ "id": "a130f9f9",
973
+ "metadata": {},
974
+ "outputs": [
975
+ {
976
+ "name": "stdout",
977
+ "output_type": "stream",
978
+ "text": [
979
+ "\n",
980
+ "\n",
981
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
982
+ "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",
983
+ "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",
984
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('207'),)]\u001b[0m\n",
985
+ "Answer:\u001b[32;1m\u001b[1;3m207\u001b[0m\n",
986
+ "\u001b[1m> Finished chain.\u001b[0m\n"
987
+ ]
988
+ }
989
+ ],
990
+ "source": [
991
+ "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\")"
992
+ ]
993
+ },
994
+ {
995
+ "cell_type": "code",
996
+ "execution_count": 42,
997
+ "id": "e4bd6489",
998
+ "metadata": {},
999
+ "outputs": [
1000
+ {
1001
+ "data": {
1002
+ "text/plain": [
1003
+ "'207'"
1004
+ ]
1005
+ },
1006
+ "execution_count": 42,
1007
+ "metadata": {},
1008
+ "output_type": "execute_result"
1009
+ }
1010
+ ],
1011
+ "source": [
1012
+ "ans"
1013
+ ]
1014
+ },
1015
+ {
1016
+ "cell_type": "code",
1017
+ "execution_count": null,
1018
+ "id": "dbe9e984",
1019
+ "metadata": {},
1020
+ "outputs": [],
1021
+ "source": []
1022
+ }
1023
+ ],
1024
+ "metadata": {
1025
+ "kernelspec": {
1026
+ "display_name": "Python 3 (ipykernel)",
1027
+ "language": "python",
1028
+ "name": "python3"
1029
+ },
1030
+ "language_info": {
1031
+ "codemirror_mode": {
1032
+ "name": "ipython",
1033
+ "version": 3
1034
+ },
1035
+ "file_extension": ".py",
1036
+ "mimetype": "text/x-python",
1037
+ "name": "python",
1038
+ "nbconvert_exporter": "python",
1039
+ "pygments_lexer": "ipython3",
1040
+ "version": "3.11.8"
1041
+ }
1042
+ },
1043
+ "nbformat": 4,
1044
+ "nbformat_minor": 5
1045
+ }
Main.ipynb ADDED
@@ -0,0 +1,1045 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 2,
6
+ "id": "5b6ddc1d",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "#pip install langchain"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": 3,
16
+ "id": "428de9cd",
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "#pip install --upgrade pip"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": 4,
26
+ "id": "c6963d71",
27
+ "metadata": {},
28
+ "outputs": [],
29
+ "source": [
30
+ "#pip install --upgrade setuptools"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "execution_count": 5,
36
+ "id": "f107f9f5",
37
+ "metadata": {},
38
+ "outputs": [],
39
+ "source": [
40
+ "from langchain.llms import GooglePalm"
41
+ ]
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "execution_count": 6,
46
+ "id": "0cf73123",
47
+ "metadata": {},
48
+ "outputs": [],
49
+ "source": [
50
+ "#pip install google-generativeai"
51
+ ]
52
+ },
53
+ {
54
+ "cell_type": "code",
55
+ "execution_count": 7,
56
+ "id": "03866604",
57
+ "metadata": {},
58
+ "outputs": [],
59
+ "source": [
60
+ "import google.generativeai as genai"
61
+ ]
62
+ },
63
+ {
64
+ "cell_type": "code",
65
+ "execution_count": 8,
66
+ "id": "2a11da2a",
67
+ "metadata": {},
68
+ "outputs": [
69
+ {
70
+ "name": "stdout",
71
+ "output_type": "stream",
72
+ "text": [
73
+ "**Verse 1**\n",
74
+ "America, America,\n",
75
+ "Land of the free, home of the brave.\n",
76
+ "You're a beacon of hope,\n",
77
+ "For all the world to see.\n",
78
+ "**Chorus**\n",
79
+ "We're proud to be Americans,\n",
80
+ "We'll fight for our freedom,\n",
81
+ "We'll stand together,\n",
82
+ "As one nation, under God.\n",
83
+ "**Verse 2**\n",
84
+ "We're a melting pot,\n",
85
+ "Of cultures and races.\n",
86
+ "We're all different,\n",
87
+ "But we're all united.\n",
88
+ "**Chorus**\n",
89
+ "We're proud to be Americans,\n",
90
+ "We'll fight for our freedom,\n",
91
+ "We'll stand together,\n",
92
+ "As one nation, under God.\n",
93
+ "**Bridge**\n",
94
+ "Through thick and thin,\n",
95
+ "We'll always stick together.\n",
96
+ "We're Americans,\n",
97
+ "And we're proud.\n",
98
+ "**Chorus**\n",
99
+ "We're proud to be Americans,\n",
100
+ "We'll fight for our freedom,\n",
101
+ "We'll stand together,\n",
102
+ "As one nation, under God.\n"
103
+ ]
104
+ }
105
+ ],
106
+ "source": [
107
+ "api_key = \"AIzaSyBAsa1XcZysgK7DKo9Bs-9MNfixaRR0ocw\"\n",
108
+ "llm = GooglePalm(google_api_key=api_key, temperature=0.2)\n",
109
+ "poem = llm(\"write a song on America in 5 lines\")\n",
110
+ "print(poem)"
111
+ ]
112
+ },
113
+ {
114
+ "cell_type": "code",
115
+ "execution_count": 9,
116
+ "id": "09a438b5",
117
+ "metadata": {},
118
+ "outputs": [],
119
+ "source": [
120
+ "#pip install pymysql"
121
+ ]
122
+ },
123
+ {
124
+ "cell_type": "code",
125
+ "execution_count": 10,
126
+ "id": "74b4e2ef",
127
+ "metadata": {},
128
+ "outputs": [],
129
+ "source": [
130
+ "#pip install mysql-connector-python\n"
131
+ ]
132
+ },
133
+ {
134
+ "cell_type": "code",
135
+ "execution_count": 11,
136
+ "id": "d0ff0a6a",
137
+ "metadata": {},
138
+ "outputs": [],
139
+ "source": [
140
+ "#pip install mysqlclient\n"
141
+ ]
142
+ },
143
+ {
144
+ "cell_type": "code",
145
+ "execution_count": 12,
146
+ "id": "ff9174a2",
147
+ "metadata": {},
148
+ "outputs": [],
149
+ "source": [
150
+ "#pip install psycopg2"
151
+ ]
152
+ },
153
+ {
154
+ "cell_type": "code",
155
+ "execution_count": 13,
156
+ "id": "bf08a5a6",
157
+ "metadata": {},
158
+ "outputs": [
159
+ {
160
+ "name": "stdout",
161
+ "output_type": "stream",
162
+ "text": [
163
+ "\n",
164
+ "CREATE TABLE discounts (\n",
165
+ "\tdiscount_id INTEGER NOT NULL AUTO_INCREMENT, \n",
166
+ "\tt_shirt_id INTEGER NOT NULL, \n",
167
+ "\tpct_discount DECIMAL(5, 2), \n",
168
+ "\tPRIMARY KEY (discount_id), \n",
169
+ "\tCONSTRAINT discounts_ibfk_1 FOREIGN KEY(t_shirt_id) REFERENCES t_shirts (t_shirt_id), \n",
170
+ "\tCONSTRAINT discounts_chk_1 CHECK ((`pct_discount` between 0 and 100))\n",
171
+ ")COLLATE utf8mb4_0900_ai_ci ENGINE=InnoDB DEFAULT CHARSET=utf8mb4\n",
172
+ "\n",
173
+ "/*\n",
174
+ "3 rows from discounts table:\n",
175
+ "discount_id\tt_shirt_id\tpct_discount\n",
176
+ "\n",
177
+ "*/\n",
178
+ "\n",
179
+ "\n",
180
+ "CREATE TABLE t_shirts (\n",
181
+ "\tt_shirt_id INTEGER NOT NULL AUTO_INCREMENT, \n",
182
+ "\tbrand ENUM('Van Huesen','Levi','Nike','Adidas') NOT NULL, \n",
183
+ "\tcolor ENUM('Red','Blue','Black','White') NOT NULL, \n",
184
+ "\tsize ENUM('XS','S','M','L','XL') NOT NULL, \n",
185
+ "\tprice INTEGER, \n",
186
+ "\tstock_quantity INTEGER NOT NULL, \n",
187
+ "\tPRIMARY KEY (t_shirt_id), \n",
188
+ "\tCONSTRAINT t_shirts_chk_1 CHECK ((`price` between 10 and 50))\n",
189
+ ")COLLATE utf8mb4_0900_ai_ci ENGINE=InnoDB DEFAULT CHARSET=utf8mb4\n",
190
+ "\n",
191
+ "/*\n",
192
+ "3 rows from t_shirts table:\n",
193
+ "t_shirt_id\tbrand\tcolor\tsize\tprice\tstock_quantity\n",
194
+ "1\tLevi\tWhite\tL\t33\t85\n",
195
+ "2\tLevi\tBlack\tL\t30\t64\n",
196
+ "4\tNike\tWhite\tL\t45\t33\n",
197
+ "*/\n"
198
+ ]
199
+ }
200
+ ],
201
+ "source": [
202
+ "from langchain.utilities import SQLDatabase\n",
203
+ "\n",
204
+ "db_user = \"root\"\n",
205
+ "db_password = \"root\"\n",
206
+ "db_host = \"localhost\"\n",
207
+ "db_name = \"atliq_tshirts\"\n",
208
+ "\n",
209
+ "db = SQLDatabase.from_uri(f\"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}\",sample_rows_in_table_info=3)\n",
210
+ "print(db.table_info)"
211
+ ]
212
+ },
213
+ {
214
+ "cell_type": "code",
215
+ "execution_count": 14,
216
+ "id": "4bad6deb",
217
+ "metadata": {},
218
+ "outputs": [
219
+ {
220
+ "name": "stdout",
221
+ "output_type": "stream",
222
+ "text": [
223
+ "Requirement already satisfied: langchain-experimental in /opt/homebrew/lib/python3.11/site-packages (0.0.25)\n",
224
+ "Requirement already satisfied: langchain>=0.0.239 in /opt/homebrew/lib/python3.11/site-packages (from langchain-experimental) (0.0.284)\n",
225
+ "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",
226
+ "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",
227
+ "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",
228
+ "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",
229
+ "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",
230
+ "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",
231
+ "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",
232
+ "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",
233
+ "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",
234
+ "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",
235
+ "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",
236
+ "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",
237
+ "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",
238
+ "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",
239
+ "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",
240
+ "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",
241
+ "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",
242
+ "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",
243
+ "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",
244
+ "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",
245
+ "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",
246
+ "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",
247
+ "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",
248
+ "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",
249
+ "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",
250
+ "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",
251
+ "Note: you may need to restart the kernel to use updated packages.\n"
252
+ ]
253
+ }
254
+ ],
255
+ "source": [
256
+ "pip install langchain-experimental"
257
+ ]
258
+ },
259
+ {
260
+ "cell_type": "code",
261
+ "execution_count": 15,
262
+ "id": "23d042fa",
263
+ "metadata": {},
264
+ "outputs": [],
265
+ "source": [
266
+ "from langchain_experimental.sql import SQLDatabaseChain\n",
267
+ "\n",
268
+ "db_chain = SQLDatabaseChain.from_llm(llm,db,verbose=True)"
269
+ ]
270
+ },
271
+ {
272
+ "cell_type": "code",
273
+ "execution_count": 16,
274
+ "id": "ac08c264",
275
+ "metadata": {},
276
+ "outputs": [
277
+ {
278
+ "name": "stdout",
279
+ "output_type": "stream",
280
+ "text": [
281
+ "\n",
282
+ "\n",
283
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
284
+ "how many t-shirts do we have for Levi in extra small and white color\n",
285
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT stock_quantity FROM t_shirts WHERE brand = 'Levi' AND color = 'White' AND size = 'XS'\u001b[0m\n",
286
+ "SQLResult: \u001b[33;1m\u001b[1;3m\u001b[0m\n",
287
+ "Answer:\u001b[32;1m\u001b[1;3m0\u001b[0m\n",
288
+ "\u001b[1m> Finished chain.\u001b[0m\n"
289
+ ]
290
+ }
291
+ ],
292
+ "source": [
293
+ "qs1 = db_chain.run(\"how many t-shirts do we have for Levi in extra small and white color\")"
294
+ ]
295
+ },
296
+ {
297
+ "cell_type": "code",
298
+ "execution_count": 17,
299
+ "id": "3cf99da1",
300
+ "metadata": {},
301
+ "outputs": [
302
+ {
303
+ "name": "stdout",
304
+ "output_type": "stream",
305
+ "text": [
306
+ "\n",
307
+ "\n",
308
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
309
+ "how much is the price of the inventory for all small size t-shirts?\n",
310
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT SUM(price) FROM t_shirts WHERE size = 'S'\u001b[0m\n",
311
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('371'),)]\u001b[0m\n",
312
+ "Answer:\u001b[32;1m\u001b[1;3m371\u001b[0m\n",
313
+ "\u001b[1m> Finished chain.\u001b[0m\n"
314
+ ]
315
+ }
316
+ ],
317
+ "source": [
318
+ "qs2 = db_chain.run(\"how much is the price of the inventory for all small size t-shirts?\")"
319
+ ]
320
+ },
321
+ {
322
+ "cell_type": "code",
323
+ "execution_count": 18,
324
+ "id": "7944d470",
325
+ "metadata": {},
326
+ "outputs": [
327
+ {
328
+ "name": "stdout",
329
+ "output_type": "stream",
330
+ "text": [
331
+ "\n",
332
+ "\n",
333
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
334
+ "SELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'\n",
335
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'\u001b[0m\n",
336
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('20416'),)]\u001b[0m\n",
337
+ "Answer:\u001b[32;1m\u001b[1;3m20416\u001b[0m\n",
338
+ "\u001b[1m> Finished chain.\u001b[0m\n"
339
+ ]
340
+ }
341
+ ],
342
+ "source": [
343
+ "qs2 = db_chain.run(\"SELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'\")"
344
+ ]
345
+ },
346
+ {
347
+ "cell_type": "code",
348
+ "execution_count": 19,
349
+ "id": "2105b4e3",
350
+ "metadata": {},
351
+ "outputs": [
352
+ {
353
+ "data": {
354
+ "text/plain": [
355
+ "'20416'"
356
+ ]
357
+ },
358
+ "execution_count": 19,
359
+ "metadata": {},
360
+ "output_type": "execute_result"
361
+ }
362
+ ],
363
+ "source": [
364
+ "qs2"
365
+ ]
366
+ },
367
+ {
368
+ "cell_type": "code",
369
+ "execution_count": 20,
370
+ "id": "083c6c80",
371
+ "metadata": {},
372
+ "outputs": [
373
+ {
374
+ "name": "stdout",
375
+ "output_type": "stream",
376
+ "text": [
377
+ "\n",
378
+ "\n",
379
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
380
+ "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",
381
+ "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"
382
+ ]
383
+ },
384
+ {
385
+ "ename": "OperationalError",
386
+ "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)",
387
+ "output_type": "error",
388
+ "traceback": [
389
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
390
+ "\u001b[0;31mOperationalError\u001b[0m Traceback (most recent call last)",
391
+ "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",
392
+ "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",
393
+ "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",
394
+ "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",
395
+ "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",
396
+ "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",
397
+ "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",
398
+ "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",
399
+ "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",
400
+ "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",
401
+ "\u001b[0;31mOperationalError\u001b[0m: (1054, \"Unknown column 'discounts.start_date' in 'where clause'\")",
402
+ "\nThe above exception was the direct cause of the following exception:\n",
403
+ "\u001b[0;31mOperationalError\u001b[0m Traceback (most recent call last)",
404
+ "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",
405
+ "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",
406
+ "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",
407
+ "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",
408
+ "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",
409
+ "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",
410
+ "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",
411
+ "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",
412
+ "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",
413
+ "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",
414
+ "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",
415
+ "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",
416
+ "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",
417
+ "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",
418
+ "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",
419
+ "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",
420
+ "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",
421
+ "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",
422
+ "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",
423
+ "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",
424
+ "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",
425
+ "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",
426
+ "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",
427
+ "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",
428
+ "\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)"
429
+ ]
430
+ }
431
+ ],
432
+ "source": [
433
+ "\n",
434
+ "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)?\")"
435
+ ]
436
+ },
437
+ {
438
+ "cell_type": "code",
439
+ "execution_count": 21,
440
+ "id": "1842f69c",
441
+ "metadata": {},
442
+ "outputs": [],
443
+ "source": [
444
+ "sql_code = \"\"\"\n",
445
+ "\n",
446
+ "SELECT sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) AS total_revenue \n",
447
+ "FROM (select sum(price*stock_quantity) AS total_amount, t_shirt_id FROM t_shirts WHERE brand = \"Levi\" \n",
448
+ "GROUP BY t_shirt_id) a LEFT JOIN discounts ON a.t_shirt_id=discounts.t_shirt_id\n",
449
+ "\n",
450
+ "\"\"\""
451
+ ]
452
+ },
453
+ {
454
+ "cell_type": "code",
455
+ "execution_count": 22,
456
+ "id": "0f5e3472",
457
+ "metadata": {},
458
+ "outputs": [
459
+ {
460
+ "name": "stdout",
461
+ "output_type": "stream",
462
+ "text": [
463
+ "\n",
464
+ "\n",
465
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
466
+ "\n",
467
+ "\n",
468
+ "SELECT sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) AS total_revenue \n",
469
+ "FROM (select sum(price*stock_quantity) AS total_amount, t_shirt_id FROM t_shirts WHERE brand = \"Levi\" \n",
470
+ "GROUP BY t_shirt_id) a LEFT JOIN discounts ON a.t_shirt_id=discounts.t_shirt_id\n",
471
+ "\n",
472
+ "\n",
473
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) AS total_revenue \n",
474
+ "FROM (select sum(price*stock_quantity) AS total_amount, t_shirt_id FROM t_shirts WHERE brand = \"Levi\" \n",
475
+ "GROUP BY t_shirt_id) a LEFT JOIN discounts ON a.t_shirt_id=discounts.t_shirt_id\u001b[0m\n",
476
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('26011.000000'),)]\u001b[0m\n",
477
+ "Answer:\u001b[32;1m\u001b[1;3m26011\u001b[0m\n",
478
+ "\u001b[1m> Finished chain.\u001b[0m\n"
479
+ ]
480
+ }
481
+ ],
482
+ "source": [
483
+ "qs3 = db_chain.run(sql_code)"
484
+ ]
485
+ },
486
+ {
487
+ "cell_type": "code",
488
+ "execution_count": 23,
489
+ "id": "8af1b5d8",
490
+ "metadata": {},
491
+ "outputs": [
492
+ {
493
+ "data": {
494
+ "text/plain": [
495
+ "'26011'"
496
+ ]
497
+ },
498
+ "execution_count": 23,
499
+ "metadata": {},
500
+ "output_type": "execute_result"
501
+ }
502
+ ],
503
+ "source": [
504
+ "qs3"
505
+ ]
506
+ },
507
+ {
508
+ "cell_type": "code",
509
+ "execution_count": 24,
510
+ "id": "47679dee",
511
+ "metadata": {},
512
+ "outputs": [
513
+ {
514
+ "name": "stdout",
515
+ "output_type": "stream",
516
+ "text": [
517
+ "\n",
518
+ "\n",
519
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
520
+ "how many white color levi's t-shirts we have available?\n",
521
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT stock_quantity FROM t_shirts WHERE brand = 'Levi' AND color = 'White'\u001b[0m\n",
522
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(40,), (85,), (36,)]\u001b[0m\n",
523
+ "Answer:\u001b[32;1m\u001b[1;3m85\u001b[0m\n",
524
+ "\u001b[1m> Finished chain.\u001b[0m\n"
525
+ ]
526
+ }
527
+ ],
528
+ "source": [
529
+ "qs4 = db_chain.run(\"how many white color levi's t-shirts we have available?\")"
530
+ ]
531
+ },
532
+ {
533
+ "cell_type": "code",
534
+ "execution_count": 25,
535
+ "id": "a83abf09",
536
+ "metadata": {},
537
+ "outputs": [
538
+ {
539
+ "name": "stdout",
540
+ "output_type": "stream",
541
+ "text": [
542
+ "\n",
543
+ "\n",
544
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
545
+ "SELECT sum(stock_quantity) FROM t-shirts WHERE brand = 'Levi' AND color = 'White'\n",
546
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT sum(stock_quantity) FROM t_shirts WHERE brand = 'Levi' AND color = 'White'\u001b[0m\n",
547
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('161'),)]\u001b[0m\n",
548
+ "Answer:\u001b[32;1m\u001b[1;3m161\u001b[0m\n",
549
+ "\u001b[1m> Finished chain.\u001b[0m\n"
550
+ ]
551
+ }
552
+ ],
553
+ "source": [
554
+ "qs4 = db_chain.run(\"SELECT sum(stock_quantity) FROM t-shirts WHERE brand = 'Levi' AND color = 'White'\")"
555
+ ]
556
+ },
557
+ {
558
+ "cell_type": "code",
559
+ "execution_count": 26,
560
+ "id": "02310a6d",
561
+ "metadata": {},
562
+ "outputs": [
563
+ {
564
+ "data": {
565
+ "text/plain": [
566
+ "'161'"
567
+ ]
568
+ },
569
+ "execution_count": 26,
570
+ "metadata": {},
571
+ "output_type": "execute_result"
572
+ }
573
+ ],
574
+ "source": [
575
+ "qs4"
576
+ ]
577
+ },
578
+ {
579
+ "cell_type": "code",
580
+ "execution_count": 27,
581
+ "id": "a25adb91",
582
+ "metadata": {},
583
+ "outputs": [],
584
+ "source": [
585
+ "fewshots = [\n",
586
+ " {\n",
587
+ " 'Question':\"how many t-shirts do we have for Nike in extra small and white color?\",\n",
588
+ " 'SQLQuery':\"SELECT stock_quantity FROM t_shirts WHERE brand = 'Nike' AND color = 'White' AND size = 'XS'\",\n",
589
+ " 'SQLResult':\"Result of the SQL query\",\n",
590
+ " 'Answer': qs1\n",
591
+ " },\n",
592
+ " {\n",
593
+ " 'Question':\"how much is the price of the inventory for all small size t-shirts?\",\n",
594
+ " 'SQLQuery':\"SELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'\",\n",
595
+ " 'SQLResult':\"Result of the SQL query\",\n",
596
+ " 'Answer': qs2\n",
597
+ " \n",
598
+ " },\n",
599
+ " {\n",
600
+ " '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",
601
+ " '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",
602
+ " 'SQLResult':\"Result of the SQL query\",\n",
603
+ " 'Answer': qs3\n",
604
+ " \n",
605
+ " },\n",
606
+ " {\n",
607
+ " 'Question':\"how many white color levi's t-shirts we have available?\",\n",
608
+ " 'SQLQuery':\"SELECT sum(stock_quantity) FROM t-shirts WHERE brand = 'Levi' AND color = 'White'\",\n",
609
+ " 'SQLResult':\"Result of the SQL query\",\n",
610
+ " 'Answer': qs4\n",
611
+ " } \n",
612
+ "]"
613
+ ]
614
+ },
615
+ {
616
+ "cell_type": "code",
617
+ "execution_count": 28,
618
+ "id": "e7dcad87",
619
+ "metadata": {},
620
+ "outputs": [],
621
+ "source": [
622
+ "from langchain.embeddings import HuggingFaceEmbeddings"
623
+ ]
624
+ },
625
+ {
626
+ "cell_type": "code",
627
+ "execution_count": 29,
628
+ "id": "1ee2a866",
629
+ "metadata": {},
630
+ "outputs": [
631
+ {
632
+ "name": "stdout",
633
+ "output_type": "stream",
634
+ "text": [
635
+ "Requirement already satisfied: sentence-transformers in /opt/homebrew/lib/python3.11/site-packages (2.6.1)\n",
636
+ "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",
637
+ "Requirement already satisfied: tqdm in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (4.66.2)\n",
638
+ "Requirement already satisfied: torch>=1.11.0 in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (2.2.1)\n",
639
+ "Requirement already satisfied: numpy in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (1.24.2)\n",
640
+ "Requirement already satisfied: scikit-learn in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (1.4.1.post1)\n",
641
+ "Requirement already satisfied: scipy in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (1.12.0)\n",
642
+ "Requirement already satisfied: huggingface-hub>=0.15.1 in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (0.21.4)\n",
643
+ "Requirement already satisfied: Pillow in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (10.2.0)\n",
644
+ "Requirement already satisfied: filelock in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.15.1->sentence-transformers) (3.13.1)\n",
645
+ "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",
646
+ "Requirement already satisfied: requests in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.15.1->sentence-transformers) (2.31.0)\n",
647
+ "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",
648
+ "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",
649
+ "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",
650
+ "Requirement already satisfied: sympy in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers) (1.12)\n",
651
+ "Requirement already satisfied: networkx in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers) (3.2.1)\n",
652
+ "Requirement already satisfied: jinja2 in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers) (3.1.3)\n",
653
+ "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",
654
+ "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",
655
+ "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",
656
+ "Requirement already satisfied: joblib>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from scikit-learn->sentence-transformers) (1.3.2)\n",
657
+ "Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/homebrew/lib/python3.11/site-packages (from scikit-learn->sentence-transformers) (3.3.0)\n",
658
+ "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",
659
+ "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",
660
+ "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",
661
+ "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",
662
+ "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",
663
+ "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",
664
+ "Note: you may need to restart the kernel to use updated packages.\n"
665
+ ]
666
+ }
667
+ ],
668
+ "source": [
669
+ "pip install sentence-transformers"
670
+ ]
671
+ },
672
+ {
673
+ "cell_type": "code",
674
+ "execution_count": 30,
675
+ "id": "9b32825e",
676
+ "metadata": {},
677
+ "outputs": [],
678
+ "source": [
679
+ "embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-V2')"
680
+ ]
681
+ },
682
+ {
683
+ "cell_type": "code",
684
+ "execution_count": 31,
685
+ "id": "f6d36cdc",
686
+ "metadata": {},
687
+ "outputs": [],
688
+ "source": [
689
+ "to_vectorize = [\" \".join(example.values())for example in fewshots]"
690
+ ]
691
+ },
692
+ {
693
+ "cell_type": "code",
694
+ "execution_count": 32,
695
+ "id": "d62ffe97",
696
+ "metadata": {},
697
+ "outputs": [
698
+ {
699
+ "data": {
700
+ "text/plain": [
701
+ "\"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\""
702
+ ]
703
+ },
704
+ "execution_count": 32,
705
+ "metadata": {},
706
+ "output_type": "execute_result"
707
+ }
708
+ ],
709
+ "source": [
710
+ "to_vectorize[0]"
711
+ ]
712
+ },
713
+ {
714
+ "cell_type": "code",
715
+ "execution_count": 33,
716
+ "id": "20ce3e8a",
717
+ "metadata": {
718
+ "scrolled": false
719
+ },
720
+ "outputs": [
721
+ {
722
+ "name": "stdout",
723
+ "output_type": "stream",
724
+ "text": [
725
+ "Requirement already satisfied: chromadb in /opt/homebrew/lib/python3.11/site-packages (0.4.24)\n",
726
+ "Requirement already satisfied: build>=1.0.3 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.1.1)\n",
727
+ "Requirement already satisfied: requests>=2.28 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (2.31.0)\n",
728
+ "Requirement already satisfied: pydantic>=1.9 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (2.6.4)\n",
729
+ "Requirement already satisfied: chroma-hnswlib==0.7.3 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.7.3)\n",
730
+ "Requirement already satisfied: fastapi>=0.95.2 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.110.1)\n",
731
+ "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",
732
+ "Requirement already satisfied: numpy>=1.22.5 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.24.2)\n",
733
+ "Requirement already satisfied: posthog>=2.4.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (3.5.0)\n",
734
+ "Requirement already satisfied: typing-extensions>=4.5.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.10.0)\n",
735
+ "Requirement already satisfied: pulsar-client>=3.1.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (3.4.0)\n",
736
+ "Requirement already satisfied: onnxruntime>=1.14.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.17.1)\n",
737
+ "Requirement already satisfied: opentelemetry-api>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.24.0)\n",
738
+ "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",
739
+ "Requirement already satisfied: opentelemetry-instrumentation-fastapi>=0.41b0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.45b0)\n",
740
+ "Requirement already satisfied: opentelemetry-sdk>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.24.0)\n",
741
+ "Requirement already satisfied: tokenizers>=0.13.2 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.15.2)\n",
742
+ "Requirement already satisfied: pypika>=0.48.9 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.48.9)\n",
743
+ "Requirement already satisfied: tqdm>=4.65.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.66.2)\n",
744
+ "Requirement already satisfied: overrides>=7.3.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (7.7.0)\n",
745
+ "Requirement already satisfied: importlib-resources in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (6.4.0)\n",
746
+ "Requirement already satisfied: grpcio>=1.58.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.62.1)\n",
747
+ "Requirement already satisfied: bcrypt>=4.0.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.1.2)\n",
748
+ "Requirement already satisfied: typer>=0.9.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.12.3)\n",
749
+ "Requirement already satisfied: kubernetes>=28.1.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (29.0.0)\n",
750
+ "Requirement already satisfied: tenacity>=8.2.3 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (8.2.3)\n",
751
+ "Requirement already satisfied: PyYAML>=6.0.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (6.0.1)\n",
752
+ "Requirement already satisfied: mmh3>=4.0.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.1.0)\n",
753
+ "Requirement already satisfied: orjson>=3.9.12 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (3.9.15)\n",
754
+ "Requirement already satisfied: packaging>=19.0 in /opt/homebrew/lib/python3.11/site-packages (from build>=1.0.3->chromadb) (23.2)\n",
755
+ "Requirement already satisfied: pyproject_hooks in /opt/homebrew/lib/python3.11/site-packages (from build>=1.0.3->chromadb) (1.0.0)\n",
756
+ "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",
757
+ "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",
758
+ "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",
759
+ "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",
760
+ "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",
761
+ "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",
762
+ "Requirement already satisfied: requests-oauthlib in /opt/homebrew/lib/python3.11/site-packages (from kubernetes>=28.1.0->chromadb) (2.0.0)\n",
763
+ "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",
764
+ "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",
765
+ "Requirement already satisfied: coloredlogs in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (15.0.1)\n",
766
+ "Requirement already satisfied: flatbuffers in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (24.3.7)\n",
767
+ "Requirement already satisfied: protobuf in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (4.25.3)\n",
768
+ "Requirement already satisfied: sympy in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (1.12)\n",
769
+ "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",
770
+ "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",
771
+ "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",
772
+ "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",
773
+ "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",
774
+ "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",
775
+ "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",
776
+ "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",
777
+ "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",
778
+ "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",
779
+ "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",
780
+ "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",
781
+ "Requirement already satisfied: monotonic>=1.5 in /opt/homebrew/lib/python3.11/site-packages (from posthog>=2.4.0->chromadb) (1.6)\n",
782
+ "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",
783
+ "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",
784
+ "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",
785
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.28->chromadb) (3.3.2)\n",
786
+ "Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.28->chromadb) (3.6)\n"
787
+ ]
788
+ },
789
+ {
790
+ "name": "stdout",
791
+ "output_type": "stream",
792
+ "text": [
793
+ "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",
794
+ "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",
795
+ "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",
796
+ "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",
797
+ "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",
798
+ "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",
799
+ "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",
800
+ "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",
801
+ "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",
802
+ "Requirement already satisfied: websockets>=10.4 in /opt/homebrew/lib/python3.11/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (12.0)\n",
803
+ "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",
804
+ "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",
805
+ "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",
806
+ "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",
807
+ "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",
808
+ "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",
809
+ "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",
810
+ "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",
811
+ "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",
812
+ "Requirement already satisfied: humanfriendly>=9.1 in /opt/homebrew/lib/python3.11/site-packages (from coloredlogs->onnxruntime>=1.14.1->chromadb) (10.0)\n",
813
+ "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",
814
+ "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",
815
+ "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",
816
+ "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",
817
+ "Note: you may need to restart the kernel to use updated packages.\n"
818
+ ]
819
+ }
820
+ ],
821
+ "source": [
822
+ "pip install chromadb"
823
+ ]
824
+ },
825
+ {
826
+ "cell_type": "code",
827
+ "execution_count": 34,
828
+ "id": "40d77eac",
829
+ "metadata": {},
830
+ "outputs": [],
831
+ "source": [
832
+ "from langchain.vectorstores import FAISS\n",
833
+ "\n",
834
+ "vectorstore = FAISS.from_texts(to_vectorize,embedding=embeddings,metadatas=fewshots)"
835
+ ]
836
+ },
837
+ {
838
+ "cell_type": "code",
839
+ "execution_count": 35,
840
+ "id": "db1b58e4",
841
+ "metadata": {},
842
+ "outputs": [
843
+ {
844
+ "data": {
845
+ "text/plain": [
846
+ "[{'Question': \"how many white color levi's t-shirts we have available?\",\n",
847
+ " 'SQLQuery': \"SELECT sum(stock_quantity) FROM t-shirts WHERE brand = 'Levi' AND color = 'White'\",\n",
848
+ " 'SQLResult': 'Result of the SQL query',\n",
849
+ " 'Answer': '161'},\n",
850
+ " {'Question': 'how many t-shirts do we have for Nike in extra small and white color?',\n",
851
+ " 'SQLQuery': \"SELECT stock_quantity FROM t_shirts WHERE brand = 'Nike' AND color = 'White' AND size = 'XS'\",\n",
852
+ " 'SQLResult': 'Result of the SQL query',\n",
853
+ " 'Answer': '0'}]"
854
+ ]
855
+ },
856
+ "execution_count": 35,
857
+ "metadata": {},
858
+ "output_type": "execute_result"
859
+ }
860
+ ],
861
+ "source": [
862
+ "from langchain.prompts import SemanticSimilarityExampleSelector\n",
863
+ "\n",
864
+ "example_selector = SemanticSimilarityExampleSelector(\n",
865
+ " vectorstore=vectorstore,\n",
866
+ " k=2\n",
867
+ ")\n",
868
+ "example_selector.select_examples({\"Question\": \"How many Addidas T shirts I have left in my store?\"})"
869
+ ]
870
+ },
871
+ {
872
+ "cell_type": "code",
873
+ "execution_count": 36,
874
+ "id": "20ac12c1",
875
+ "metadata": {},
876
+ "outputs": [],
877
+ "source": [
878
+ "from langchain.chains.sql_database.prompt import PROMPT_SUFFIX,_mysql_prompt"
879
+ ]
880
+ },
881
+ {
882
+ "cell_type": "code",
883
+ "execution_count": 37,
884
+ "id": "b9a6a53f",
885
+ "metadata": {},
886
+ "outputs": [
887
+ {
888
+ "name": "stdout",
889
+ "output_type": "stream",
890
+ "text": [
891
+ "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",
892
+ "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",
893
+ "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",
894
+ "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",
895
+ "Pay attention to use CURDATE() function to get the current date, if the question involves \"today\".\n",
896
+ "\n",
897
+ "Use the following format:\n",
898
+ "\n",
899
+ "Question: Question here\n",
900
+ "SQLQuery: SQL Query to run\n",
901
+ "SQLResult: Result of the SQLQuery\n",
902
+ "Answer: Final answer here\n",
903
+ "\n",
904
+ "\n",
905
+ "Only use the following tables:\n",
906
+ "{table_info}\n",
907
+ "\n",
908
+ "Question: {input}\n"
909
+ ]
910
+ },
911
+ {
912
+ "data": {
913
+ "text/plain": [
914
+ "(None, None)"
915
+ ]
916
+ },
917
+ "execution_count": 37,
918
+ "metadata": {},
919
+ "output_type": "execute_result"
920
+ }
921
+ ],
922
+ "source": [
923
+ "print(_mysql_prompt),print(PROMPT_SUFFIX)"
924
+ ]
925
+ },
926
+ {
927
+ "cell_type": "code",
928
+ "execution_count": 38,
929
+ "id": "26a9b9b0",
930
+ "metadata": {},
931
+ "outputs": [],
932
+ "source": [
933
+ "from langchain.prompts.prompt import PromptTemplate\n",
934
+ "\n",
935
+ "example_prompt = PromptTemplate(\n",
936
+ " input_variables=[\"Question\", \"SQLQuery\", \"SQLResult\",\"Answer\",],\n",
937
+ " template=\"\\nQuestion: {Question}\\nSQLQuery: {SQLQuery}\\nSQLResult: {SQLResult}\\nAnswer: {Answer}\",\n",
938
+ ")"
939
+ ]
940
+ },
941
+ {
942
+ "cell_type": "code",
943
+ "execution_count": 39,
944
+ "id": "a341c8df",
945
+ "metadata": {},
946
+ "outputs": [],
947
+ "source": [
948
+ "from langchain.prompts import FewShotPromptTemplate\n",
949
+ "\n",
950
+ "few_shot_prompt = FewShotPromptTemplate(\n",
951
+ " example_selector = example_selector,\n",
952
+ " example_prompt = example_prompt,\n",
953
+ " prefix = _mysql_prompt,\n",
954
+ " suffix = PROMPT_SUFFIX,\n",
955
+ " input_variables = [\"input\",\"table_info\",\"top_k\"]\n",
956
+ ")"
957
+ ]
958
+ },
959
+ {
960
+ "cell_type": "code",
961
+ "execution_count": 40,
962
+ "id": "d6e1cad5",
963
+ "metadata": {},
964
+ "outputs": [],
965
+ "source": [
966
+ "new_chain = SQLDatabaseChain.from_llm(llm,db,verbose=True,prompt = few_shot_prompt)"
967
+ ]
968
+ },
969
+ {
970
+ "cell_type": "code",
971
+ "execution_count": 41,
972
+ "id": "a130f9f9",
973
+ "metadata": {},
974
+ "outputs": [
975
+ {
976
+ "name": "stdout",
977
+ "output_type": "stream",
978
+ "text": [
979
+ "\n",
980
+ "\n",
981
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
982
+ "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",
983
+ "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",
984
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('207'),)]\u001b[0m\n",
985
+ "Answer:\u001b[32;1m\u001b[1;3m207\u001b[0m\n",
986
+ "\u001b[1m> Finished chain.\u001b[0m\n"
987
+ ]
988
+ }
989
+ ],
990
+ "source": [
991
+ "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\")"
992
+ ]
993
+ },
994
+ {
995
+ "cell_type": "code",
996
+ "execution_count": 42,
997
+ "id": "e4bd6489",
998
+ "metadata": {},
999
+ "outputs": [
1000
+ {
1001
+ "data": {
1002
+ "text/plain": [
1003
+ "'207'"
1004
+ ]
1005
+ },
1006
+ "execution_count": 42,
1007
+ "metadata": {},
1008
+ "output_type": "execute_result"
1009
+ }
1010
+ ],
1011
+ "source": [
1012
+ "ans"
1013
+ ]
1014
+ },
1015
+ {
1016
+ "cell_type": "code",
1017
+ "execution_count": null,
1018
+ "id": "dbe9e984",
1019
+ "metadata": {},
1020
+ "outputs": [],
1021
+ "source": []
1022
+ }
1023
+ ],
1024
+ "metadata": {
1025
+ "kernelspec": {
1026
+ "display_name": "Python 3 (ipykernel)",
1027
+ "language": "python",
1028
+ "name": "python3"
1029
+ },
1030
+ "language_info": {
1031
+ "codemirror_mode": {
1032
+ "name": "ipython",
1033
+ "version": 3
1034
+ },
1035
+ "file_extension": ".py",
1036
+ "mimetype": "text/x-python",
1037
+ "name": "python",
1038
+ "nbconvert_exporter": "python",
1039
+ "pygments_lexer": "ipython3",
1040
+ "version": "3.11.8"
1041
+ }
1042
+ },
1043
+ "nbformat": 4,
1044
+ "nbformat_minor": 5
1045
+ }
README.md CHANGED
@@ -1,13 +1,67 @@
1
- ---
2
- title: Staples Inventory
3
- emoji: 🦀
4
- colorFrom: pink
5
- colorTo: red
6
- sdk: streamlit
7
- sdk_version: 1.33.0
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Staples Inventry: Talk to a Database
3
+
4
+ This is an end to end LLM project based on Google Palm and Langchain. We are building a system that can talk to MySQL database.
5
+ User asks questions in a natural language and the system generates answers by converting those questions to an SQL query and
6
+ then executing that query on MySQL database.
7
+ Staples is an Inventory store where they maintain their inventory, sales and discounts data in MySQL database. A store manager
8
+ will may ask questions such as,
9
+ - How many Black colour gel do we have left in the stock?
10
+ - How much sales our store will generate if we can sell all Blue gels after applying discounts?
11
+ The system is intelligent enough to generate accurate queries for given question and execute them on MySQL database
12
+
13
+ ![](atliq_tees.png)
14
+
15
+ ## Project Highlights
16
+
17
+ - Staples is a Inventory store that sells pens , books and office supplies.
18
+ - Their inventory, sales and discounts data is stored in a MySQL database
19
+ - We will build an LLM based question and answer system that will use following,
20
+ - Google Palm LLM
21
+ - Hugging face embeddings
22
+ - Streamlit for UI
23
+ - Langchain framework
24
+ - Chromadb as a vector store
25
+ - Few shot learning
26
+ - In the UI, store manager will ask questions in a natural language and it will produce the answers
27
+
28
+
29
+ ## Installation
30
+
31
+ 1.Clone this repository to your local machine using:
32
+
33
+ ```bash
34
+ git clone https://github.com/codebasics/langchain.git
35
+ ```
36
+ 2.Navigate to the project directory:
37
+
38
+ ```bash
39
+ cd 4_sqldb_tshirts
40
+ ```
41
+ 3. Install the required dependencies using pip:
42
+
43
+ ```bash
44
+ pip install -r requirements.txt
45
+ ```
46
+ 4.Acquire an api key through makersuite.google.com and put it in .env file
47
+
48
+ ```bash
49
+ GOOGLE_API_KEY="your_api_key_here"
50
+ ```
51
+
52
+
53
+ ## Usage
54
+
55
+ 1. Run the Streamlit app by executing:
56
+ ```bash
57
+ streamlit run main.py
58
+
59
+ ```
60
+ 9
61
+ ## Project Structure
62
+
63
+ - main.py: The main Streamlit application script.
64
+ - langchain_helper.py: This has all the langchain code
65
+ - requirements.txt: A list of required Python packages for the project.
66
+ - few_shots.py: Contains few shot prompts
67
+ - .env: Configuration file for storing your Google API key.
Test.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.llms import GooglePalm
2
+ from langchain_community.utilities import SQLDatabase
3
+ from langchain_experimental.sql import SQLDatabaseChain
4
+ from langchain.prompts import SemanticSimilarityExampleSelector
5
+ from langchain_community.embeddings import HuggingFaceEmbeddings
6
+ from langchain_community.vectorstores import Chroma
7
+ from langchain.prompts import FewShotPromptTemplate
8
+ from langchain.chains.sql_database.prompt import PROMPT_SUFFIX, _mysql_prompt
9
+ from langchain.prompts.prompt import PromptTemplate
10
+
11
+ from few_shots import few_shots
12
+
13
+ import os
14
+ from dotenv import load_dotenv
15
+
16
+ load_dotenv()
17
+
18
+
19
+ def get_few_shot_db_chain():
20
+ db_user = "root"
21
+ db_password = "root"
22
+ db_host = "localhost"
23
+ db_name = "staples_inventory"
24
+
25
+ db = SQLDatabase.from_uri(f"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}",
26
+ sample_rows_in_table_info=3)
27
+ llm = GooglePalm(google_api_key=os.environ["GOOGLE_API_KEY"], temperature=0.1)
28
+
29
+ embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
30
+ to_vectorize = [" ".join(example.values()) for example in few_shots]
31
+ vectorstore = Chroma.from_texts(to_vectorize, embeddings, metadatas=few_shots)
32
+ example_selector = SemanticSimilarityExampleSelector(
33
+ vectorstore=vectorstore,
34
+ k=2,
35
+ )
36
+ mysql_prompt = """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.
37
+ 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.
38
+ 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.
39
+ 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.
40
+ Pay attention to use CURDATE() function to get the current date, if the question involves "today".
41
+
42
+ Use the following format:
43
+
44
+ Question: Question here
45
+ SQLQuery: Query to run with no pre-amble
46
+ SQLResult: Result of the SQLQuery
47
+ Answer: Final answer here
48
+
49
+ No pre-amble.
50
+ """
51
+
52
+ example_prompt = PromptTemplate(
53
+ input_variables=["Question", "SQLQuery", "SQLResult", "Answer", ],
54
+ template="\nQuestion: {Question}\nSQLQuery: {SQLQuery}\nSQLResult: {SQLResult}\nAnswer: {Answer}",
55
+ )
56
+
57
+ few_shot_prompt = FewShotPromptTemplate(
58
+ example_selector=example_selector,
59
+ example_prompt=example_prompt,
60
+ prefix=mysql_prompt,
61
+ suffix=PROMPT_SUFFIX,
62
+ input_variables=["input", "table_info", "top_k"], # These variables are used in the prefix and suffix
63
+ )
64
+ chain = SQLDatabaseChain.from_llm(llm, db, verbose=True, prompt=few_shot_prompt)
65
+ return chain
Untitled.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
Untitled1.ipynb ADDED
@@ -0,0 +1,665 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "0ef005ca",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "from langchain.llms import GooglePalm\n",
11
+ "\n",
12
+ "api_key = 'AIzaSyDlXYnP2xNNa0DNa7dPN89u2L4IuAchEg4'\n",
13
+ "\n",
14
+ "llm = GooglePalm(google_api_key=api_key, temperature=0.2)"
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": 2,
20
+ "id": "737f32b2",
21
+ "metadata": {},
22
+ "outputs": [],
23
+ "source": [
24
+ "from langchain.utilities import SQLDatabase\n",
25
+ "from langchain_experimental.sql import SQLDatabaseChain"
26
+ ]
27
+ },
28
+ {
29
+ "cell_type": "code",
30
+ "execution_count": 3,
31
+ "id": "3ca26845",
32
+ "metadata": {},
33
+ "outputs": [
34
+ {
35
+ "name": "stdout",
36
+ "output_type": "stream",
37
+ "text": [
38
+ "\n",
39
+ "CREATE TABLE discounts (\n",
40
+ "\tdiscount_id INTEGER NOT NULL AUTO_INCREMENT, \n",
41
+ "\tt_shirt_id INTEGER NOT NULL, \n",
42
+ "\tpct_discount DECIMAL(5, 2), \n",
43
+ "\tPRIMARY KEY (discount_id), \n",
44
+ "\tCONSTRAINT discounts_ibfk_1 FOREIGN KEY(t_shirt_id) REFERENCES t_shirts (t_shirt_id), \n",
45
+ "\tCONSTRAINT discounts_chk_1 CHECK ((`pct_discount` between 0 and 100))\n",
46
+ ")DEFAULT CHARSET=utf8mb4 ENGINE=InnoDB COLLATE utf8mb4_0900_ai_ci\n",
47
+ "\n",
48
+ "/*\n",
49
+ "3 rows from discounts table:\n",
50
+ "discount_id\tt_shirt_id\tpct_discount\n",
51
+ "\n",
52
+ "*/\n",
53
+ "\n",
54
+ "\n",
55
+ "CREATE TABLE t_shirts (\n",
56
+ "\tt_shirt_id INTEGER NOT NULL AUTO_INCREMENT, \n",
57
+ "\tbrand ENUM('Van Huesen','Levi','Nike','Adidas') NOT NULL, \n",
58
+ "\tcolor ENUM('Red','Blue','Black','White') NOT NULL, \n",
59
+ "\tsize ENUM('XS','S','M','L','XL') NOT NULL, \n",
60
+ "\tprice INTEGER, \n",
61
+ "\tstock_quantity INTEGER NOT NULL, \n",
62
+ "\tPRIMARY KEY (t_shirt_id), \n",
63
+ "\tCONSTRAINT t_shirts_chk_1 CHECK ((`price` between 10 and 50))\n",
64
+ ")DEFAULT CHARSET=utf8mb4 ENGINE=InnoDB COLLATE utf8mb4_0900_ai_ci\n",
65
+ "\n",
66
+ "/*\n",
67
+ "3 rows from t_shirts table:\n",
68
+ "t_shirt_id\tbrand\tcolor\tsize\tprice\tstock_quantity\n",
69
+ "1\tLevi\tWhite\tL\t33\t85\n",
70
+ "2\tLevi\tBlack\tL\t30\t64\n",
71
+ "4\tNike\tWhite\tL\t45\t33\n",
72
+ "*/\n"
73
+ ]
74
+ }
75
+ ],
76
+ "source": [
77
+ "db_user = \"root\"\n",
78
+ "db_password = \"root\"\n",
79
+ "db_host = \"localhost\"\n",
80
+ "db_name = \"atliq_tshirts\"\n",
81
+ "\n",
82
+ "db = SQLDatabase.from_uri(f\"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}\",sample_rows_in_table_info=3)\n",
83
+ "\n",
84
+ "print(db.table_info)"
85
+ ]
86
+ },
87
+ {
88
+ "cell_type": "code",
89
+ "execution_count": 4,
90
+ "id": "cd2e9968",
91
+ "metadata": {},
92
+ "outputs": [
93
+ {
94
+ "name": "stdout",
95
+ "output_type": "stream",
96
+ "text": [
97
+ "\n",
98
+ "\n",
99
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
100
+ "How many t-shirts do we have left for nike in extra small size and white color?\n",
101
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT stock_quantity FROM t_shirts WHERE brand = 'Nike' AND color = 'White' AND size = 'XS'\u001b[0m\n",
102
+ "SQLResult: \u001b[33;1m\u001b[1;3m\u001b[0m\n",
103
+ "Answer:\u001b[32;1m\u001b[1;3m33\u001b[0m\n",
104
+ "\u001b[1m> Finished chain.\u001b[0m\n"
105
+ ]
106
+ }
107
+ ],
108
+ "source": [
109
+ "db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)\n",
110
+ "qns1 = db_chain(\"How many t-shirts do we have left for nike in extra small size and white color?\")"
111
+ ]
112
+ },
113
+ {
114
+ "cell_type": "code",
115
+ "execution_count": 5,
116
+ "id": "dfa0c74e",
117
+ "metadata": {},
118
+ "outputs": [
119
+ {
120
+ "name": "stdout",
121
+ "output_type": "stream",
122
+ "text": [
123
+ "\n",
124
+ "\n",
125
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
126
+ "How much is the price of the inventory for all small size t-shirts?\n",
127
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT SUM(price) FROM t_shirts WHERE size = 'S'\u001b[0m\n",
128
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('371'),)]\u001b[0m\n",
129
+ "Answer:\u001b[32;1m\u001b[1;3m371\u001b[0m\n",
130
+ "\u001b[1m> Finished chain.\u001b[0m\n"
131
+ ]
132
+ }
133
+ ],
134
+ "source": [
135
+ "qns2 = db_chain.run(\"How much is the price of the inventory for all small size t-shirts?\")"
136
+ ]
137
+ },
138
+ {
139
+ "cell_type": "code",
140
+ "execution_count": 6,
141
+ "id": "c5d1feed",
142
+ "metadata": {},
143
+ "outputs": [
144
+ {
145
+ "name": "stdout",
146
+ "output_type": "stream",
147
+ "text": [
148
+ "\n",
149
+ "\n",
150
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
151
+ "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",
152
+ "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"
153
+ ]
154
+ },
155
+ {
156
+ "ename": "OperationalError",
157
+ "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)",
158
+ "output_type": "error",
159
+ "traceback": [
160
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
161
+ "\u001b[0;31mOperationalError\u001b[0m Traceback (most recent call last)",
162
+ "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",
163
+ "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",
164
+ "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",
165
+ "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",
166
+ "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",
167
+ "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",
168
+ "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",
169
+ "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",
170
+ "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",
171
+ "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",
172
+ "\u001b[0;31mOperationalError\u001b[0m: (1054, \"Unknown column 'discounts.start_date' in 'where clause'\")",
173
+ "\nThe above exception was the direct cause of the following exception:\n",
174
+ "\u001b[0;31mOperationalError\u001b[0m Traceback (most recent call last)",
175
+ "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",
176
+ "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",
177
+ "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",
178
+ "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",
179
+ "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",
180
+ "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",
181
+ "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",
182
+ "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",
183
+ "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",
184
+ "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",
185
+ "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",
186
+ "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",
187
+ "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",
188
+ "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",
189
+ "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",
190
+ "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",
191
+ "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",
192
+ "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",
193
+ "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",
194
+ "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",
195
+ "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",
196
+ "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",
197
+ "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",
198
+ "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",
199
+ "\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)"
200
+ ]
201
+ }
202
+ ],
203
+ "source": [
204
+ "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)?\")"
205
+ ]
206
+ },
207
+ {
208
+ "cell_type": "code",
209
+ "execution_count": 7,
210
+ "id": "e5785845",
211
+ "metadata": {},
212
+ "outputs": [
213
+ {
214
+ "name": "stdout",
215
+ "output_type": "stream",
216
+ "text": [
217
+ "\n",
218
+ "\n",
219
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
220
+ "\n",
221
+ "select sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) as total_revenue from\n",
222
+ "(select sum(price*stock_quantity) as total_amount, t_shirt_id from t_shirts where brand = 'Levi'\n",
223
+ "group by t_shirt_id) a left join discounts on a.t_shirt_id = discounts.t_shirt_id\n",
224
+ " \n",
225
+ "SQLQuery:\u001b[32;1m\u001b[1;3mselect sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) as total_revenue from\n",
226
+ "(select sum(price*stock_quantity) as total_amount, t_shirt_id from t_shirts where brand = 'Levi'\n",
227
+ "group by t_shirt_id) a left join discounts on a.t_shirt_id = discounts.t_shirt_id\u001b[0m\n",
228
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('26011.000000'),)]\u001b[0m\n",
229
+ "Answer:\u001b[32;1m\u001b[1;3m26011\u001b[0m\n",
230
+ "\u001b[1m> Finished chain.\u001b[0m\n"
231
+ ]
232
+ }
233
+ ],
234
+ "source": [
235
+ "sql_code = \"\"\"\n",
236
+ "select sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) as total_revenue from\n",
237
+ "(select sum(price*stock_quantity) as total_amount, t_shirt_id from t_shirts where brand = 'Levi'\n",
238
+ "group by t_shirt_id) a left join discounts on a.t_shirt_id = discounts.t_shirt_id\n",
239
+ " \"\"\"\n",
240
+ "\n",
241
+ "qns3 = db_chain.run(sql_code)"
242
+ ]
243
+ },
244
+ {
245
+ "cell_type": "code",
246
+ "execution_count": 8,
247
+ "id": "acdad0f9",
248
+ "metadata": {},
249
+ "outputs": [
250
+ {
251
+ "name": "stdout",
252
+ "output_type": "stream",
253
+ "text": [
254
+ "\n",
255
+ "\n",
256
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
257
+ "SELECT SUM(price * stock_quantity) FROM t_shirts WHERE brand = 'Levi'\n",
258
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT SUM(price * stock_quantity) FROM t_shirts WHERE brand = 'Levi'\u001b[0m\n",
259
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(Decimal('26011'),)]\u001b[0m\n",
260
+ "Answer:\u001b[32;1m\u001b[1;3m26011\u001b[0m\n",
261
+ "\u001b[1m> Finished chain.\u001b[0m\n"
262
+ ]
263
+ }
264
+ ],
265
+ "source": [
266
+ "qns4 = db_chain.run(\"SELECT SUM(price * stock_quantity) FROM t_shirts WHERE brand = 'Levi'\")"
267
+ ]
268
+ },
269
+ {
270
+ "cell_type": "code",
271
+ "execution_count": 9,
272
+ "id": "caf32b2a",
273
+ "metadata": {},
274
+ "outputs": [
275
+ {
276
+ "name": "stdout",
277
+ "output_type": "stream",
278
+ "text": [
279
+ "\n",
280
+ "\n",
281
+ "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
282
+ "How many white color Levi's t shirts we have available?\n",
283
+ "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT stock_quantity FROM t_shirts WHERE brand = 'Levi' AND color = 'White'\u001b[0m\n",
284
+ "SQLResult: \u001b[33;1m\u001b[1;3m[(40,), (85,), (36,)]\u001b[0m\n",
285
+ "Answer:\u001b[32;1m\u001b[1;3m85\u001b[0m\n",
286
+ "\u001b[1m> Finished chain.\u001b[0m\n"
287
+ ]
288
+ }
289
+ ],
290
+ "source": [
291
+ "qns5 = db_chain.run(\"How many white color Levi's t shirts we have available?\")"
292
+ ]
293
+ },
294
+ {
295
+ "cell_type": "code",
296
+ "execution_count": 39,
297
+ "id": "4eefd089",
298
+ "metadata": {},
299
+ "outputs": [],
300
+ "source": [
301
+ "fewshots = [\n",
302
+ " {\n",
303
+ " 'Question':\"how many t-shirts do we have for Nike in extra small and white color?\",\n",
304
+ " 'SQLQuery':\"SELECT stock_quantity FROM t_shirts WHERE brand = 'Nike' AND color = 'White' AND size = 'XS'\",\n",
305
+ " 'SQLResult':\"Result of the SQL query\",\n",
306
+ " 'Answer': qns1\n",
307
+ " },\n",
308
+ " {\n",
309
+ " 'Question':\"how much is the price of the inventory for all small size t-shirts?\",\n",
310
+ " 'SQLQuery':\"SELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'\",\n",
311
+ " 'SQLResult':\"Result of the SQL query\",\n",
312
+ " 'Answer': qns2\n",
313
+ " \n",
314
+ " },\n",
315
+ " {\n",
316
+ " '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",
317
+ " '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",
318
+ " 'SQLResult':\"Result of the SQL query\",\n",
319
+ " 'Answer': qns3\n",
320
+ " \n",
321
+ " },\n",
322
+ " {\n",
323
+ " 'Question':\"how many white color levi's t-shirts we have available?\",\n",
324
+ " 'SQLQuery':\"SELECT sum(stock_quantity) FROM t-shirts WHERE brand = 'Levi' AND color = 'White'\",\n",
325
+ " 'SQLResult':\"Result of the SQL query\",\n",
326
+ " 'Answer': qns4\n",
327
+ " } \n",
328
+ "]"
329
+ ]
330
+ },
331
+ {
332
+ "cell_type": "code",
333
+ "execution_count": 11,
334
+ "id": "e5a7ae98",
335
+ "metadata": {},
336
+ "outputs": [],
337
+ "source": [
338
+ "from langchain.prompts import SemanticSimilarityExampleSelector\n",
339
+ "from langchain.embeddings import HuggingFaceEmbeddings\n",
340
+ "from langchain.vectorstores import Chroma\n",
341
+ "\n",
342
+ "embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')\n",
343
+ "\n",
344
+ "# Ensure that only strings are joined\n",
345
+ "to_vectorize = []\n",
346
+ "for example in few_shots:\n",
347
+ " if isinstance(example, str):\n",
348
+ " to_vectorize.append(example)\n",
349
+ " elif isinstance(example, dict):\n",
350
+ " values = example.values()\n",
351
+ " for value in values:\n",
352
+ " if isinstance(value, str):\n",
353
+ " to_vectorize.append(value)\n",
354
+ "\n",
355
+ "# Now, to_vectorize should only contain strings\n",
356
+ "to_vectorize = [\" \".join(example) for example in to_vectorize]\n",
357
+ "\n",
358
+ "# Proceed with the rest of your code...\n"
359
+ ]
360
+ },
361
+ {
362
+ "cell_type": "code",
363
+ "execution_count": 12,
364
+ "id": "b249bc2a",
365
+ "metadata": {},
366
+ "outputs": [
367
+ {
368
+ "data": {
369
+ "text/plain": [
370
+ "['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",
371
+ " \"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",
372
+ " 'R e s u l t o f t h e S Q L q u e r y',\n",
373
+ " '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",
374
+ " \"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",
375
+ " 'R e s u l t o f t h e S Q L q u e r y',\n",
376
+ " '3 7 1',\n",
377
+ " '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",
378
+ " \"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",
379
+ " 'R e s u l t o f t h e S Q L q u e r y',\n",
380
+ " '2 6 0 1 1',\n",
381
+ " '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",
382
+ " \"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",
383
+ " 'R e s u l t o f t h e S Q L q u e r y',\n",
384
+ " '2 6 0 1 1',\n",
385
+ " \"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",
386
+ " \"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",
387
+ " 'R e s u l t o f t h e S Q L q u e r y',\n",
388
+ " '8 5']"
389
+ ]
390
+ },
391
+ "execution_count": 12,
392
+ "metadata": {},
393
+ "output_type": "execute_result"
394
+ }
395
+ ],
396
+ "source": [
397
+ "to_vectorize"
398
+ ]
399
+ },
400
+ {
401
+ "cell_type": "code",
402
+ "execution_count": 13,
403
+ "id": "2671b7ce",
404
+ "metadata": {},
405
+ "outputs": [
406
+ {
407
+ "name": "stdout",
408
+ "output_type": "stream",
409
+ "text": [
410
+ "Requirement already satisfied: sentence-transformers in /opt/homebrew/lib/python3.11/site-packages (2.6.1)\n",
411
+ "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",
412
+ "Requirement already satisfied: tqdm in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (4.66.2)\n",
413
+ "Requirement already satisfied: torch>=1.11.0 in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (2.2.1)\n",
414
+ "Requirement already satisfied: numpy in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (1.24.2)\n",
415
+ "Requirement already satisfied: scikit-learn in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (1.4.1.post1)\n",
416
+ "Requirement already satisfied: scipy in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (1.12.0)\n",
417
+ "Requirement already satisfied: huggingface-hub>=0.15.1 in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (0.21.4)\n",
418
+ "Requirement already satisfied: Pillow in /opt/homebrew/lib/python3.11/site-packages (from sentence-transformers) (10.2.0)\n",
419
+ "Requirement already satisfied: filelock in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.15.1->sentence-transformers) (3.13.1)\n",
420
+ "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",
421
+ "Requirement already satisfied: requests in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.15.1->sentence-transformers) (2.31.0)\n",
422
+ "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",
423
+ "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",
424
+ "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",
425
+ "Requirement already satisfied: sympy in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers) (1.12)\n",
426
+ "Requirement already satisfied: networkx in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers) (3.2.1)\n",
427
+ "Requirement already satisfied: jinja2 in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers) (3.1.3)\n",
428
+ "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",
429
+ "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",
430
+ "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",
431
+ "Requirement already satisfied: joblib>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from scikit-learn->sentence-transformers) (1.3.2)\n",
432
+ "Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/homebrew/lib/python3.11/site-packages (from scikit-learn->sentence-transformers) (3.3.0)\n",
433
+ "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",
434
+ "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",
435
+ "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",
436
+ "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",
437
+ "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",
438
+ "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",
439
+ "Note: you may need to restart the kernel to use updated packages.\n"
440
+ ]
441
+ }
442
+ ],
443
+ "source": [
444
+ "pip install -U sentence-transformers\n"
445
+ ]
446
+ },
447
+ {
448
+ "cell_type": "code",
449
+ "execution_count": 14,
450
+ "id": "2823d198",
451
+ "metadata": {},
452
+ "outputs": [
453
+ {
454
+ "name": "stdout",
455
+ "output_type": "stream",
456
+ "text": [
457
+ "Requirement already satisfied: chromadb in /opt/homebrew/lib/python3.11/site-packages (0.4.24)\n",
458
+ "Requirement already satisfied: build>=1.0.3 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.1.1)\n",
459
+ "Requirement already satisfied: requests>=2.28 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (2.31.0)\n",
460
+ "Requirement already satisfied: pydantic>=1.9 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (2.6.4)\n",
461
+ "Requirement already satisfied: chroma-hnswlib==0.7.3 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.7.3)\n",
462
+ "Requirement already satisfied: fastapi>=0.95.2 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.110.1)\n",
463
+ "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",
464
+ "Requirement already satisfied: numpy>=1.22.5 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.24.2)\n",
465
+ "Requirement already satisfied: posthog>=2.4.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (3.5.0)\n",
466
+ "Requirement already satisfied: typing-extensions>=4.5.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.10.0)\n",
467
+ "Requirement already satisfied: pulsar-client>=3.1.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (3.4.0)\n",
468
+ "Requirement already satisfied: onnxruntime>=1.14.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.17.1)\n",
469
+ "Requirement already satisfied: opentelemetry-api>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.24.0)\n",
470
+ "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",
471
+ "Requirement already satisfied: opentelemetry-instrumentation-fastapi>=0.41b0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.45b0)\n",
472
+ "Requirement already satisfied: opentelemetry-sdk>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.24.0)\n",
473
+ "Requirement already satisfied: tokenizers>=0.13.2 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.15.2)\n",
474
+ "Requirement already satisfied: pypika>=0.48.9 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.48.9)\n",
475
+ "Requirement already satisfied: tqdm>=4.65.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.66.2)\n",
476
+ "Requirement already satisfied: overrides>=7.3.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (7.7.0)\n",
477
+ "Requirement already satisfied: importlib-resources in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (6.4.0)\n",
478
+ "Requirement already satisfied: grpcio>=1.58.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (1.62.1)\n",
479
+ "Requirement already satisfied: bcrypt>=4.0.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.1.2)\n",
480
+ "Requirement already satisfied: typer>=0.9.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (0.12.3)\n",
481
+ "Requirement already satisfied: kubernetes>=28.1.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (29.0.0)\n",
482
+ "Requirement already satisfied: tenacity>=8.2.3 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (8.2.3)\n",
483
+ "Requirement already satisfied: PyYAML>=6.0.0 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (6.0.1)\n",
484
+ "Requirement already satisfied: mmh3>=4.0.1 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (4.1.0)\n",
485
+ "Requirement already satisfied: orjson>=3.9.12 in /opt/homebrew/lib/python3.11/site-packages (from chromadb) (3.9.15)\n",
486
+ "Requirement already satisfied: packaging>=19.0 in /opt/homebrew/lib/python3.11/site-packages (from build>=1.0.3->chromadb) (23.2)\n",
487
+ "Requirement already satisfied: pyproject_hooks in /opt/homebrew/lib/python3.11/site-packages (from build>=1.0.3->chromadb) (1.0.0)\n",
488
+ "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",
489
+ "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",
490
+ "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",
491
+ "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",
492
+ "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",
493
+ "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",
494
+ "Requirement already satisfied: requests-oauthlib in /opt/homebrew/lib/python3.11/site-packages (from kubernetes>=28.1.0->chromadb) (2.0.0)\n",
495
+ "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",
496
+ "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",
497
+ "Requirement already satisfied: coloredlogs in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (15.0.1)\n",
498
+ "Requirement already satisfied: flatbuffers in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (24.3.7)\n",
499
+ "Requirement already satisfied: protobuf in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (3.19.6)\n",
500
+ "Requirement already satisfied: sympy in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime>=1.14.1->chromadb) (1.12)\n",
501
+ "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",
502
+ "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",
503
+ "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",
504
+ "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",
505
+ "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",
506
+ "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",
507
+ "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",
508
+ "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",
509
+ "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",
510
+ "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",
511
+ "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",
512
+ "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",
513
+ "Requirement already satisfied: monotonic>=1.5 in /opt/homebrew/lib/python3.11/site-packages (from posthog>=2.4.0->chromadb) (1.6)\n",
514
+ "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",
515
+ "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",
516
+ "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",
517
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.28->chromadb) (3.3.2)\n",
518
+ "Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.28->chromadb) (3.6)\n",
519
+ "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",
520
+ "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",
521
+ "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",
522
+ "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",
523
+ "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",
524
+ "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",
525
+ "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",
526
+ "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",
527
+ "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",
528
+ "Requirement already satisfied: websockets>=10.4 in /opt/homebrew/lib/python3.11/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (12.0)\n"
529
+ ]
530
+ },
531
+ {
532
+ "name": "stdout",
533
+ "output_type": "stream",
534
+ "text": [
535
+ "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",
536
+ "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",
537
+ "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",
538
+ "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",
539
+ "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",
540
+ "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",
541
+ "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",
542
+ "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",
543
+ "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",
544
+ "Requirement already satisfied: humanfriendly>=9.1 in /opt/homebrew/lib/python3.11/site-packages (from coloredlogs->onnxruntime>=1.14.1->chromadb) (10.0)\n",
545
+ "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",
546
+ "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",
547
+ "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",
548
+ "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",
549
+ "Note: you may need to restart the kernel to use updated packages.\n"
550
+ ]
551
+ }
552
+ ],
553
+ "source": [
554
+ "pip install chromadb"
555
+ ]
556
+ },
557
+ {
558
+ "cell_type": "code",
559
+ "execution_count": 30,
560
+ "id": "cf07ca37",
561
+ "metadata": {},
562
+ "outputs": [],
563
+ "source": [
564
+ "from langchain.embeddings import HuggingFaceEmbeddings\n",
565
+ "\n",
566
+ "embeddings = HuggingFaceEmbeddings(model_name = \"sentence-transformers/all-MiniLM-L6-v2\")\n",
567
+ "\n",
568
+ "e = embeddings.embed_query(\"How many white color Levi's shirt I have?\")"
569
+ ]
570
+ },
571
+ {
572
+ "cell_type": "code",
573
+ "execution_count": 31,
574
+ "id": "efba95a6",
575
+ "metadata": {},
576
+ "outputs": [
577
+ {
578
+ "data": {
579
+ "text/plain": [
580
+ "[0.0036103595048189163, 0.07093444466590881]"
581
+ ]
582
+ },
583
+ "execution_count": 31,
584
+ "metadata": {},
585
+ "output_type": "execute_result"
586
+ }
587
+ ],
588
+ "source": [
589
+ "e[:2]"
590
+ ]
591
+ },
592
+ {
593
+ "cell_type": "code",
594
+ "execution_count": 36,
595
+ "id": "15a1d1c5",
596
+ "metadata": {},
597
+ "outputs": [],
598
+ "source": [
599
+ "from langchain.embeddings import HuggingFaceEmbeddings"
600
+ ]
601
+ },
602
+ {
603
+ "cell_type": "code",
604
+ "execution_count": 33,
605
+ "id": "9739547c",
606
+ "metadata": {},
607
+ "outputs": [],
608
+ "source": [
609
+ "embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-V2')"
610
+ ]
611
+ },
612
+ {
613
+ "cell_type": "code",
614
+ "execution_count": 41,
615
+ "id": "01a2ab5c",
616
+ "metadata": {},
617
+ "outputs": [
618
+ {
619
+ "ename": "TypeError",
620
+ "evalue": "sequence item 3: expected str instance, dict found",
621
+ "output_type": "error",
622
+ "traceback": [
623
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
624
+ "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
625
+ "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",
626
+ "Cell \u001b[0;32mIn[41], line 1\u001b[0m, in \u001b[0;36m<listcomp>\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",
627
+ "\u001b[0;31mTypeError\u001b[0m: sequence item 3: expected str instance, dict found"
628
+ ]
629
+ }
630
+ ],
631
+ "source": [
632
+ "vectorize = [\" \".join(example.values())for example in fewshots]"
633
+ ]
634
+ },
635
+ {
636
+ "cell_type": "code",
637
+ "execution_count": null,
638
+ "id": "4489463e",
639
+ "metadata": {},
640
+ "outputs": [],
641
+ "source": []
642
+ }
643
+ ],
644
+ "metadata": {
645
+ "kernelspec": {
646
+ "display_name": "Python 3 (ipykernel)",
647
+ "language": "python",
648
+ "name": "python3"
649
+ },
650
+ "language_info": {
651
+ "codemirror_mode": {
652
+ "name": "ipython",
653
+ "version": 3
654
+ },
655
+ "file_extension": ".py",
656
+ "mimetype": "text/x-python",
657
+ "name": "python",
658
+ "nbconvert_exporter": "python",
659
+ "pygments_lexer": "ipython3",
660
+ "version": "3.11.8"
661
+ }
662
+ },
663
+ "nbformat": 4,
664
+ "nbformat_minor": 5
665
+ }
few_shots.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ few_shots = [
2
+ {'Question': "How many gel pens do we have left in blue color?",
3
+ 'SQLQuery': "SELECT sum(stock_quantity) FROM pens WHERE type = 'Gel' AND color = 'Blue'",
4
+ 'SQLResult': "Result of the SQL query",
5
+ 'Answer': "85"},
6
+ {'Question': "How much is the total value of the inventory for all ballpoint pens?",
7
+ 'SQLQuery': "SELECT SUM(price*stock_quantity) FROM pens WHERE type = 'Ballpoint'",
8
+ 'SQLResult': "Result of the SQL query",
9
+ 'Answer': "3542"},
10
+ {'Question': "If we apply a 15% discount to all fountain pens, how much revenue would our store generate?",
11
+ 'SQLQuery': """SELECT SUM(price * stock_quantity * 0.85) FROM pens WHERE type = 'Fountain'""",
12
+ 'SQLResult': "Result of the SQL query",
13
+ 'Answer': "1632.45"},
14
+ {'Question': "How many books are there in the 'Science' genre?",
15
+ 'SQLQuery': "SELECT sum(stock_quantity) FROM books WHERE genre = 'Science'",
16
+ 'SQLResult': "Result of the SQL query",
17
+ 'Answer': "120"},
18
+ {'Question': "How much revenue will our store generate if we sell all biography books today at a 10% discount?",
19
+ 'SQLQuery': """SELECT SUM(price * stock_quantity * 0.90) FROM books WHERE genre = 'Biography'""",
20
+ 'SQLResult': "Result of the SQL query",
21
+ 'Answer': "2700"},
22
+ {'Question': "What is the total stock of rollerball pens in green color?",
23
+ 'SQLQuery': "SELECT sum(stock_quantity) FROM pens WHERE type = 'Rollerball' AND color = 'Green'",
24
+ 'SQLResult': "Result of the SQL query",
25
+ 'Answer': "50"}
26
+ ]
langchain_helper (1).py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.llms import GooglePalm
2
+ from langchain.community.utilities import SQLDatabase
3
+ from langchain_experimental.sql import SQLDatabaseChain
4
+ from langchain_community.embeddings import HuggingFaceEmbeddings
5
+ from langchain_community.vectorstores import Chroma
6
+ from langchain.prompts import SemanticSimilarityExampleSelector
7
+ from langchain.chains.sql_database.prompt import PROMPT_SUFFIX, _mysql_prompt
8
+ from langchain.prompts.prompt import PromptTemplate
9
+ from langchain.prompts import FewShotPromptTemplate
10
+
11
+ from few_shots import few_shots
12
+
13
+ import os
14
+ from dotenv import load_dotenv
15
+
16
+ load_dotenv() # take environment variables from .env (especially openai api key)
17
+
18
+
19
+ def get_few_shot_db_chain():
20
+ db_user = "root"
21
+ db_password = "root"
22
+ db_host = "localhost"
23
+ db_name = "atliq_tshirts"
24
+
25
+ db = SQLDatabase.from_uri(f"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}",
26
+ sample_rows_in_table_info=3)
27
+ llm = GooglePalm(google_api_key=os.environ["GOOGLE_API_KEY"], temperature=0.1)
28
+
29
+ embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
30
+ to_vectorize = [" ".join(example.values()) for example in few_shots]
31
+ vectorstore = Chroma.from_texts(to_vectorize, embeddings, metadatas=few_shots)
32
+ example_selector = SemanticSimilarityExampleSelector(
33
+ vectorstore=vectorstore,
34
+ k=2,
35
+ )
36
+ mysql_prompt = """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.
37
+ 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.
38
+ 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.
39
+ 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.
40
+ Pay attention to use CURDATE() function to get the current date, if the question involves "today".
41
+
42
+ Use the following format:
43
+
44
+ Question: Question here
45
+ SQLQuery: Query to run with no pre-amble
46
+ SQLResult: Result of the SQLQuery
47
+ Answer: Final answer here
48
+
49
+ No pre-amble.
50
+ """
51
+
52
+ example_prompt = PromptTemplate(
53
+ input_variables=["Question", "SQLQuery", "SQLResult", "Answer", ],
54
+ template="\nQuestion: {Question}\nSQLQuery: {SQLQuery}\nSQLResult: {SQLResult}\nAnswer: {Answer}",
55
+ )
56
+
57
+ few_shot_prompt = FewShotPromptTemplate(
58
+ example_selector=example_selector,
59
+ example_prompt=example_prompt,
60
+ prefix=mysql_prompt,
61
+ suffix=PROMPT_SUFFIX,
62
+ input_variables=["input", "table_info", "top_k"], # These variables are used in the prefix and suffix
63
+ )
64
+ chain = SQLDatabaseChain.from_llm(llm, db, verbose=True, prompt=few_shot_prompt)
65
+ return chain
langchain_helper.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.llms import GooglePalm
2
+ from langchain.utilities import SQLDatabase
3
+ from langchain_experimental.sql import SQLDatabaseChain
4
+ from langchain.embeddings import HuggingFaceEmbeddings
5
+ from langchain.vectorstores import Chroma
6
+ from langchain.prompts import SemanticSimilarityExampleSelector
7
+ from langchain.chains.sql_database.prompt import PROMPT_SUFFIX, _mysql_prompt
8
+ from langchain.prompts.prompt import PromptTemplate
9
+ from langchain.prompts import FewShotPromptTemplate
10
+ import os
11
+ from few_shot import fewshots
12
+ from dotenv import load_dotenv
13
+ load_dotenv()
14
+
15
+ def get_few_shot_db_chain():
16
+ llm = GooglePalm(google_api_key=os.environ["google_api_key"], temperature=0.2)
17
+ db_user = "root"
18
+ db_password = "Kautilya1414"
19
+ db_host = 'localhost'
20
+ db_name = 'atliq_tshirts'
21
+
22
+ uri = f"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}"
23
+ db = SQLDatabase.from_uri(uri)
24
+ embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-V2')
25
+ vectorize = [" ".join(example.values()) for example in fewshots]
26
+ vectorstore = Chroma.from_texts(vectorize, embeddings, metadatas=fewshots)
27
+ example_selector = SemanticSimilarityExampleSelector(vectorstore=vectorstore, k=2)
28
+
29
+ example_prompt = PromptTemplate(
30
+ input_variables=["Question", "SQLQuery", "SQLResult", "Answer"],
31
+ template="\nQuestion:{Question}\nSQLQuery:{SQLQuery}\nSQLResult:{SQLResult}\nAnswer:{Answer}"
32
+ )
33
+ few_shot_temp = FewShotPromptTemplate(example_selector=example_selector,
34
+ example_prompt=example_prompt,
35
+ prefix=_mysql_prompt,
36
+ suffix=PROMPT_SUFFIX,
37
+ input_variables=["input", "table_info", "top_k"])
38
+ new_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True, prompt=few_shot_temp)
39
+ return new_chain
40
+
41
+ if __name__ == "__main__":
42
+ new_chain = get_few_shot_db_chain()
43
+ print(new_chain.run("how many white color Levi t-shirts we have?"))
main.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from Test import get_few_shot_db_chain
3
+
4
+ st.image('/Users/gokul/Desktop/Believe/Projects/langchain/Main/images/Staples_logo_PNG5.png', caption='Business is Human')
5
+
6
+ question = st.text_input("Question: ")
7
+
8
+ if question:
9
+ chain = get_few_shot_db_chain()
10
+ response = chain.run(question)
11
+
12
+ st.header("Answer")
13
+ st.write(response)
14
+
15
+
16
+
17
+
18
+
19
+
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ langchain==0.0.284
2
+ python-dotenv==1.0.0
3
+ streamlit==1.22.0
4
+ tiktoken==0.4.0
5
+ faiss-cpu==1.7.4
6
+ protobuf~=3.19.0
7
+ langchain_experimental
8
+ mysql-connector-python
9
+ pymysql
10
+ sentence-transformers
11
+ chromadb