|
58 | 58 | },
|
59 | 59 | {
|
60 | 60 | "cell_type": "code",
|
61 |
| - "execution_count": null, |
| 61 | + "execution_count": 3, |
62 | 62 | "id": "a8fc8f23",
|
63 | 63 | "metadata": {},
|
64 | 64 | "outputs": [],
|
|
242 | 242 | "db_chain.run(\"What are some example tracks by composer Johann Sebastian Bach?\")"
|
243 | 243 | ]
|
244 | 244 | },
|
| 245 | + { |
| 246 | + "cell_type": "markdown", |
| 247 | + "id": "bcc5e936", |
| 248 | + "metadata": {}, |
| 249 | + "source": [ |
| 250 | + "## Adding first row of each table\n", |
| 251 | + "Sometimes, the format of the data is not obvious and it is optimal to include the first row of the table in the prompt to allow the LLM to understand the data before providing a final query. Here we will use this feature to let the LLM know that artists are saved with their full names." |
| 252 | + ] |
| 253 | + }, |
| 254 | + { |
| 255 | + "cell_type": "code", |
| 256 | + "execution_count": 11, |
| 257 | + "id": "9a22ee47", |
| 258 | + "metadata": {}, |
| 259 | + "outputs": [], |
| 260 | + "source": [ |
| 261 | + "db = SQLDatabase.from_uri(\n", |
| 262 | + " \"sqlite:///../../../../notebooks/Chinook.db\", \n", |
| 263 | + " include_tables=['Track'], # we include only one table to save tokens in the prompt :)\n", |
| 264 | + " sample_row_in_table_info=True)" |
| 265 | + ] |
| 266 | + }, |
| 267 | + { |
| 268 | + "cell_type": "code", |
| 269 | + "execution_count": 12, |
| 270 | + "id": "bcb7a489", |
| 271 | + "metadata": {}, |
| 272 | + "outputs": [], |
| 273 | + "source": [ |
| 274 | + "db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)" |
| 275 | + ] |
| 276 | + }, |
| 277 | + { |
| 278 | + "cell_type": "code", |
| 279 | + "execution_count": 13, |
| 280 | + "id": "81e05d82", |
| 281 | + "metadata": {}, |
| 282 | + "outputs": [ |
| 283 | + { |
| 284 | + "name": "stdout", |
| 285 | + "output_type": "stream", |
| 286 | + "text": [ |
| 287 | + "\n", |
| 288 | + "\n", |
| 289 | + "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n", |
| 290 | + "What are some example tracks by Bach? \n", |
| 291 | + "SQLQuery:Table 'Track' has columns: TrackId (INTEGER), Name (NVARCHAR(200)), AlbumId (INTEGER), MediaTypeId (INTEGER), GenreId (INTEGER), Composer (NVARCHAR(220)), Milliseconds (INTEGER), Bytes (INTEGER), UnitPrice (NUMERIC(10, 2)). Here is an example row for this table (long strings are truncated): ['1', 'For Those About To Rock (We Salute You)', '1', '1', '1', 'Angus Young, Malcolm Young, Brian Johnson', '343719', '11170334', '0.99'].\n", |
| 292 | + "\u001b[32;1m\u001b[1;3m SELECT TrackId, Name, Composer FROM Track WHERE Composer LIKE '%Bach%' ORDER BY Name LIMIT 5;\u001b[0m\n", |
| 293 | + "SQLResult: \u001b[33;1m\u001b[1;3m[(1709, 'American Woman', 'B. Cummings/G. Peterson/M.J. Kale/R. Bachman'), (3408, 'Aria Mit 30 Veränderungen, BWV 988 \"Goldberg Variations\": Aria', 'Johann Sebastian Bach'), (3433, 'Concerto No.2 in F Major, BWV1047, I. Allegro', 'Johann Sebastian Bach'), (3407, 'Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace', 'Johann Sebastian Bach'), (3490, 'Partita in E Major, BWV 1006A: I. Prelude', 'Johann Sebastian Bach')]\u001b[0m\n", |
| 294 | + "Answer:\u001b[32;1m\u001b[1;3m Some example tracks by Bach are 'American Woman', 'Aria Mit 30 Veränderungen, BWV 988 \"Goldberg Variations\": Aria', 'Concerto No.2 in F Major, BWV1047, I. Allegro', 'Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace', and 'Partita in E Major, BWV 1006A: I. Prelude'.\u001b[0m\n", |
| 295 | + "\u001b[1m> Finished chain.\u001b[0m\n" |
| 296 | + ] |
| 297 | + }, |
| 298 | + { |
| 299 | + "data": { |
| 300 | + "text/plain": [ |
| 301 | + "' Some example tracks by Bach are \\'American Woman\\', \\'Aria Mit 30 Veränderungen, BWV 988 \"Goldberg Variations\": Aria\\', \\'Concerto No.2 in F Major, BWV1047, I. Allegro\\', \\'Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace\\', and \\'Partita in E Major, BWV 1006A: I. Prelude\\'.'" |
| 302 | + ] |
| 303 | + }, |
| 304 | + "execution_count": 13, |
| 305 | + "metadata": {}, |
| 306 | + "output_type": "execute_result" |
| 307 | + } |
| 308 | + ], |
| 309 | + "source": [ |
| 310 | + "db_chain.run(\"What are some example tracks by Bach?\")" |
| 311 | + ] |
| 312 | + }, |
245 | 313 | {
|
246 | 314 | "cell_type": "markdown",
|
247 | 315 | "id": "c12ae15a",
|
|
319 | 387 | "source": [
|
320 | 388 | "chain.run(\"How many employees are also customers?\")"
|
321 | 389 | ]
|
322 |
| - }, |
323 |
| - { |
324 |
| - "cell_type": "code", |
325 |
| - "execution_count": null, |
326 |
| - "id": "b2998b03", |
327 |
| - "metadata": {}, |
328 |
| - "outputs": [], |
329 |
| - "source": [] |
330 | 390 | }
|
331 | 391 | ],
|
332 | 392 | "metadata": {
|
|
345 | 405 | "name": "python",
|
346 | 406 | "nbconvert_exporter": "python",
|
347 | 407 | "pygments_lexer": "ipython3",
|
348 |
| - "version": "3.10.9" |
| 408 | + "version": "3.8.16" |
349 | 409 | }
|
350 | 410 | },
|
351 | 411 | "nbformat": 4,
|
|
0 commit comments