|
5 | 5 | "id": "e8cba0b6", |
6 | 6 | "metadata": {}, |
7 | 7 | "source": [ |
8 | | - "<a href=\"https://colab.research.google.com/github/meta-llama/llama-cookbook/blob/main/end-to-end-use-cases/coding/text2sql/quickstart/quickstart.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a> \n", |
9 | | - "\n", |
10 | 8 | "## Quick Demo of Text2SQL Using Llama 3.3\n", |
11 | 9 | "\n", |
12 | 10 | "This demo shows how to use Llama 3.3 to answer questions about a SQLite DB. \n", |
|
26 | 24 | }, |
27 | 25 | { |
28 | 26 | "cell_type": "code", |
29 | | - "execution_count": 2, |
| 27 | + "execution_count": null, |
30 | 28 | "id": "fa4562d3", |
31 | 29 | "metadata": {}, |
32 | 30 | "outputs": [], |
|
65 | 63 | }, |
66 | 64 | { |
67 | 65 | "cell_type": "code", |
68 | | - "execution_count": 4, |
| 66 | + "execution_count": null, |
69 | 67 | "id": "3bb99f39-cd7a-4db6-91dd-02f3bf80347c", |
70 | 68 | "metadata": {}, |
71 | 69 | "outputs": [], |
|
81 | 79 | }, |
82 | 80 | { |
83 | 81 | "cell_type": "code", |
84 | | - "execution_count": 5, |
| 82 | + "execution_count": null, |
85 | 83 | "id": "8d793ce7-324b-4861-926c-54973d7c9b43", |
86 | 84 | "metadata": {}, |
87 | | - "outputs": [ |
88 | | - { |
89 | | - "name": "stdout", |
90 | | - "output_type": "stream", |
91 | | - "text": [ |
92 | | - "Based on the table schema below, write a SQL query that would answer the user's question; just return the SQL query and nothing else.\n", |
93 | | - "\n", |
94 | | - "Scheme:\n", |
95 | | - "\n", |
96 | | - "CREATE TABLE nba_roster (\n", |
97 | | - "\t\"Team\" TEXT, \n", |
98 | | - "\t\"NAME\" TEXT, \n", |
99 | | - "\t\"Jersey\" TEXT, \n", |
100 | | - "\t\"POS\" TEXT, \n", |
101 | | - "\t\"AGE\" INTEGER, \n", |
102 | | - "\t\"HT\" TEXT, \n", |
103 | | - "\t\"WT\" TEXT, \n", |
104 | | - "\t\"COLLEGE\" TEXT, \n", |
105 | | - "\t\"SALARY\" TEXT\n", |
106 | | - ")\n", |
107 | | - "\n", |
108 | | - "Question: What team is Stephen Curry on?\n", |
109 | | - "\n", |
110 | | - "SQL Query:\n" |
111 | | - ] |
112 | | - } |
113 | | - ], |
| 85 | + "outputs": [], |
114 | 86 | "source": [ |
115 | 87 | "question = \"What team is Stephen Curry on?\"\n", |
116 | 88 | "prompt = f\"\"\"Based on the table schema below, write a SQL query that would answer the user's question; just return the SQL query and nothing else.\n", |
|
127 | 99 | }, |
128 | 100 | { |
129 | 101 | "cell_type": "code", |
130 | | - "execution_count": 6, |
| 102 | + "execution_count": null, |
131 | 103 | "id": "70776558", |
132 | 104 | "metadata": {}, |
133 | | - "outputs": [ |
134 | | - { |
135 | | - "name": "stdout", |
136 | | - "output_type": "stream", |
137 | | - "text": [ |
138 | | - "SELECT Team FROM nba_roster WHERE NAME = 'Stephen Curry'\n" |
139 | | - ] |
140 | | - } |
141 | | - ], |
| 105 | + "outputs": [], |
142 | 106 | "source": [ |
143 | 107 | "answer = llm.invoke(prompt).content\n", |
144 | 108 | "print(answer)" |
|
154 | 118 | }, |
155 | 119 | { |
156 | 120 | "cell_type": "code", |
157 | | - "execution_count": 7, |
| 121 | + "execution_count": null, |
158 | 122 | "id": "62472ce6-794b-4a61-b88c-a1e031e28e4e", |
159 | 123 | "metadata": {}, |
160 | | - "outputs": [ |
161 | | - { |
162 | | - "data": { |
163 | | - "text/plain": [ |
164 | | - "\"[('Golden State Warriors',)]\"" |
165 | | - ] |
166 | | - }, |
167 | | - "execution_count": 7, |
168 | | - "metadata": {}, |
169 | | - "output_type": "execute_result" |
170 | | - } |
171 | | - ], |
| 124 | + "outputs": [], |
172 | 125 | "source": [ |
173 | 126 | "# note this is a dangerous operation and for demo purpose only; in production app you'll need to safe-guard any DB operation\n", |
174 | 127 | "result = db.run(answer)\n", |
|
177 | 130 | }, |
178 | 131 | { |
179 | 132 | "cell_type": "code", |
180 | | - "execution_count": 8, |
| 133 | + "execution_count": null, |
181 | 134 | "id": "39ed4bc3", |
182 | 135 | "metadata": {}, |
183 | | - "outputs": [ |
184 | | - { |
185 | | - "name": "stdout", |
186 | | - "output_type": "stream", |
187 | | - "text": [ |
188 | | - "I don't have enough information to determine whose salary you are referring to. Could you please provide more context or specify the person you are asking about?\n" |
189 | | - ] |
190 | | - } |
191 | | - ], |
| 136 | + "outputs": [], |
192 | 137 | "source": [ |
193 | 138 | "# how about a follow up question\n", |
194 | 139 | "follow_up = \"What's his salary?\"\n", |
|
205 | 150 | }, |
206 | 151 | { |
207 | 152 | "cell_type": "code", |
208 | | - "execution_count": 9, |
| 153 | + "execution_count": null, |
209 | 154 | "id": "0c305278-29d2-4e88-9b3d-ad67c94ce0f2", |
210 | 155 | "metadata": {}, |
211 | | - "outputs": [ |
212 | | - { |
213 | | - "name": "stdout", |
214 | | - "output_type": "stream", |
215 | | - "text": [ |
216 | | - "Based on the table schema, question, SQL query, and SQL response below, write a new SQL response; be concise, just output the SQL response.\n", |
217 | | - "\n", |
218 | | - "Scheme:\n", |
219 | | - "\n", |
220 | | - "CREATE TABLE nba_roster (\n", |
221 | | - "\t\"Team\" TEXT, \n", |
222 | | - "\t\"NAME\" TEXT, \n", |
223 | | - "\t\"Jersey\" TEXT, \n", |
224 | | - "\t\"POS\" TEXT, \n", |
225 | | - "\t\"AGE\" INTEGER, \n", |
226 | | - "\t\"HT\" TEXT, \n", |
227 | | - "\t\"WT\" TEXT, \n", |
228 | | - "\t\"COLLEGE\" TEXT, \n", |
229 | | - "\t\"SALARY\" TEXT\n", |
230 | | - ")\n", |
231 | | - "\n", |
232 | | - "Question: What's his salary?\n", |
233 | | - "SQL Query: What team is Stephen Curry on?\n", |
234 | | - "SQL Result: [('Golden State Warriors',)]\n", |
235 | | - "\n", |
236 | | - "New SQL Response:\n", |
237 | | - "\n" |
238 | | - ] |
239 | | - } |
240 | | - ], |
| 156 | + "outputs": [], |
241 | 157 | "source": [ |
242 | 158 | "prompt = f\"\"\"Based on the table schema, question, SQL query, and SQL response below, write a new SQL response; be concise, just output the SQL response.\n", |
243 | 159 | "\n", |
|
255 | 171 | }, |
256 | 172 | { |
257 | 173 | "cell_type": "code", |
258 | | - "execution_count": 10, |
| 174 | + "execution_count": null, |
259 | 175 | "id": "03739b96-e607-4fa9-bc5c-df118198dc7f", |
260 | 176 | "metadata": {}, |
261 | | - "outputs": [ |
262 | | - { |
263 | | - "name": "stdout", |
264 | | - "output_type": "stream", |
265 | | - "text": [ |
266 | | - "SELECT SALARY FROM nba_roster WHERE NAME = \"Stephen Curry\"\n" |
267 | | - ] |
268 | | - } |
269 | | - ], |
| 177 | + "outputs": [], |
270 | 178 | "source": [ |
271 | 179 | "new_answer = llm.invoke(prompt).content\n", |
272 | 180 | "print(new_answer)" |
|
282 | 190 | }, |
283 | 191 | { |
284 | 192 | "cell_type": "code", |
285 | | - "execution_count": 11, |
| 193 | + "execution_count": null, |
286 | 194 | "id": "6ecfca53-be7e-4668-bad1-5ca7571817d7", |
287 | 195 | "metadata": {}, |
288 | | - "outputs": [ |
289 | | - { |
290 | | - "data": { |
291 | | - "text/plain": [ |
292 | | - "\"[('$51,915,615',)]\"" |
293 | | - ] |
294 | | - }, |
295 | | - "execution_count": 11, |
296 | | - "metadata": {}, |
297 | | - "output_type": "execute_result" |
298 | | - } |
299 | | - ], |
| 196 | + "outputs": [], |
300 | 197 | "source": [ |
301 | 198 | "db.run(new_answer)" |
302 | 199 | ] |
303 | | - }, |
304 | | - { |
305 | | - "cell_type": "code", |
306 | | - "execution_count": null, |
307 | | - "id": "9d79bbb1-e91d-4b56-b6ef-98c94ff414d0", |
308 | | - "metadata": {}, |
309 | | - "outputs": [], |
310 | | - "source": [] |
311 | 200 | } |
312 | 201 | ], |
313 | 202 | "metadata": { |
|
331 | 220 | "pygments_lexer": "ipython3", |
332 | 221 | "version": "3.10.14" |
333 | 222 | } |
334 | | - } |
| 223 | + }, |
| 224 | + "nbformat": 4, |
| 225 | + "nbformat_minor": 5 |
335 | 226 | } |
0 commit comments