|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# CrateDB\n", |
| 8 | + "\n", |
| 9 | + "This notebook demonstrates how to load documents from a [CrateDB] database,\n", |
| 10 | + "using the [SQLAlchemy] document loader.\n", |
| 11 | + "\n", |
| 12 | + "It loads the result of a database query with one document per row.\n", |
| 13 | + "\n", |
| 14 | + "[CrateDB]: https://github.com/crate/crate\n", |
| 15 | + "[SQLAlchemy]: https://www.sqlalchemy.org/" |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "markdown", |
| 20 | + "source": [ |
| 21 | + "## Prerequisites" |
| 22 | + ], |
| 23 | + "metadata": { |
| 24 | + "collapsed": false |
| 25 | + } |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": 40, |
| 30 | + "metadata": { |
| 31 | + "tags": [] |
| 32 | + }, |
| 33 | + "outputs": [], |
| 34 | + "source": [ |
| 35 | + "#!pip install langchain crash 'crate[sqlalchemy]'" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "markdown", |
| 40 | + "source": [ |
| 41 | + "Populate database." |
| 42 | + ], |
| 43 | + "metadata": { |
| 44 | + "collapsed": false |
| 45 | + } |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": 41, |
| 50 | + "metadata": { |
| 51 | + "tags": [] |
| 52 | + }, |
| 53 | + "outputs": [ |
| 54 | + { |
| 55 | + "name": "stdout", |
| 56 | + "output_type": "stream", |
| 57 | + "text": [ |
| 58 | + "\u001B[32mCONNECT OK\r\n", |
| 59 | + "\u001B[0m\u001B[32mPSQL OK, 1 row affected (0.001 sec)\r\n", |
| 60 | + "\u001B[0m\u001B[32mDELETE OK, 30 rows affected (0.008 sec)\r\n", |
| 61 | + "\u001B[0m\u001B[32mINSERT OK, 30 rows affected (0.011 sec)\r\n", |
| 62 | + "\u001B[0m\u001B[0m\u001B[32mCONNECT OK\r\n", |
| 63 | + "\u001B[0m\u001B[32mREFRESH OK, 1 row affected (0.001 sec)\r\n", |
| 64 | + "\u001B[0m\u001B[0m" |
| 65 | + ] |
| 66 | + } |
| 67 | + ], |
| 68 | + "source": [ |
| 69 | + "!crash < ./example_data/mlb_teams_2012.sql\n", |
| 70 | + "!crash --command \"REFRESH TABLE mlb_teams_2012;\"" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "markdown", |
| 75 | + "source": [ |
| 76 | + "## Usage" |
| 77 | + ], |
| 78 | + "metadata": { |
| 79 | + "collapsed": false |
| 80 | + } |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "code", |
| 84 | + "execution_count": 42, |
| 85 | + "metadata": { |
| 86 | + "tags": [] |
| 87 | + }, |
| 88 | + "outputs": [], |
| 89 | + "source": [ |
| 90 | + "from langchain.document_loaders.cratedb import CrateDBLoader\n", |
| 91 | + "from pprint import pprint\n", |
| 92 | + "\n", |
| 93 | + "CONNECTION_STRING = \"crate://crate@localhost/\"\n", |
| 94 | + "\n", |
| 95 | + "loader = CrateDBLoader(\n", |
| 96 | + " 'SELECT * FROM mlb_teams_2012 ORDER BY \"Team\" LIMIT 5;',\n", |
| 97 | + " url=CONNECTION_STRING,\n", |
| 98 | + ")\n", |
| 99 | + "documents = loader.load()" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": 43, |
| 105 | + "metadata": { |
| 106 | + "tags": [] |
| 107 | + }, |
| 108 | + "outputs": [ |
| 109 | + { |
| 110 | + "name": "stdout", |
| 111 | + "output_type": "stream", |
| 112 | + "text": [ |
| 113 | + "[Document(page_content='Team: Angels\\nPayroll (millions): 154.49\\nWins: 89', metadata={}),\n", |
| 114 | + " Document(page_content='Team: Astros\\nPayroll (millions): 60.65\\nWins: 55', metadata={}),\n", |
| 115 | + " Document(page_content='Team: Athletics\\nPayroll (millions): 55.37\\nWins: 94', metadata={}),\n", |
| 116 | + " Document(page_content='Team: Blue Jays\\nPayroll (millions): 75.48\\nWins: 73', metadata={}),\n", |
| 117 | + " Document(page_content='Team: Braves\\nPayroll (millions): 83.31\\nWins: 94', metadata={})]\n" |
| 118 | + ] |
| 119 | + } |
| 120 | + ], |
| 121 | + "source": [ |
| 122 | + "pprint(documents)" |
| 123 | + ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "cell_type": "markdown", |
| 127 | + "metadata": {}, |
| 128 | + "source": [ |
| 129 | + "## Specifying Which Columns are Content vs Metadata" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "code", |
| 134 | + "execution_count": 44, |
| 135 | + "metadata": {}, |
| 136 | + "outputs": [], |
| 137 | + "source": [ |
| 138 | + "loader = CrateDBLoader(\n", |
| 139 | + " 'SELECT * FROM mlb_teams_2012 ORDER BY \"Team\" LIMIT 5;',\n", |
| 140 | + " url=CONNECTION_STRING,\n", |
| 141 | + " page_content_columns=[\"Team\"],\n", |
| 142 | + " metadata_columns=[\"Payroll (millions)\"],\n", |
| 143 | + ")\n", |
| 144 | + "documents = loader.load()" |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "code", |
| 149 | + "execution_count": 45, |
| 150 | + "metadata": {}, |
| 151 | + "outputs": [ |
| 152 | + { |
| 153 | + "name": "stdout", |
| 154 | + "output_type": "stream", |
| 155 | + "text": [ |
| 156 | + "[Document(page_content='Team: Angels', metadata={'Payroll (millions)': 154.49}),\n", |
| 157 | + " Document(page_content='Team: Astros', metadata={'Payroll (millions)': 60.65}),\n", |
| 158 | + " Document(page_content='Team: Athletics', metadata={'Payroll (millions)': 55.37}),\n", |
| 159 | + " Document(page_content='Team: Blue Jays', metadata={'Payroll (millions)': 75.48}),\n", |
| 160 | + " Document(page_content='Team: Braves', metadata={'Payroll (millions)': 83.31})]\n" |
| 161 | + ] |
| 162 | + } |
| 163 | + ], |
| 164 | + "source": [ |
| 165 | + "pprint(documents)" |
| 166 | + ] |
| 167 | + }, |
| 168 | + { |
| 169 | + "cell_type": "markdown", |
| 170 | + "metadata": {}, |
| 171 | + "source": [ |
| 172 | + "## Adding Source to Metadata" |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "code", |
| 177 | + "execution_count": 46, |
| 178 | + "metadata": {}, |
| 179 | + "outputs": [], |
| 180 | + "source": [ |
| 181 | + "loader = CrateDBLoader(\n", |
| 182 | + " 'SELECT * FROM mlb_teams_2012 ORDER BY \"Team\" LIMIT 5;',\n", |
| 183 | + " url=CONNECTION_STRING,\n", |
| 184 | + " source_columns=[\"Team\"],\n", |
| 185 | + ")\n", |
| 186 | + "documents = loader.load()" |
| 187 | + ] |
| 188 | + }, |
| 189 | + { |
| 190 | + "cell_type": "code", |
| 191 | + "execution_count": 47, |
| 192 | + "metadata": {}, |
| 193 | + "outputs": [ |
| 194 | + { |
| 195 | + "name": "stdout", |
| 196 | + "output_type": "stream", |
| 197 | + "text": [ |
| 198 | + "[Document(page_content='Team: Angels\\nPayroll (millions): 154.49\\nWins: 89', metadata={'source': 'Angels'}),\n", |
| 199 | + " Document(page_content='Team: Astros\\nPayroll (millions): 60.65\\nWins: 55', metadata={'source': 'Astros'}),\n", |
| 200 | + " Document(page_content='Team: Athletics\\nPayroll (millions): 55.37\\nWins: 94', metadata={'source': 'Athletics'}),\n", |
| 201 | + " Document(page_content='Team: Blue Jays\\nPayroll (millions): 75.48\\nWins: 73', metadata={'source': 'Blue Jays'}),\n", |
| 202 | + " Document(page_content='Team: Braves\\nPayroll (millions): 83.31\\nWins: 94', metadata={'source': 'Braves'})]\n" |
| 203 | + ] |
| 204 | + } |
| 205 | + ], |
| 206 | + "source": [ |
| 207 | + "pprint(documents)" |
| 208 | + ] |
| 209 | + } |
| 210 | + ], |
| 211 | + "metadata": { |
| 212 | + "kernelspec": { |
| 213 | + "display_name": "Python 3 (ipykernel)", |
| 214 | + "language": "python", |
| 215 | + "name": "python3" |
| 216 | + }, |
| 217 | + "language_info": { |
| 218 | + "codemirror_mode": { |
| 219 | + "name": "ipython", |
| 220 | + "version": 3 |
| 221 | + }, |
| 222 | + "file_extension": ".py", |
| 223 | + "mimetype": "text/x-python", |
| 224 | + "name": "python", |
| 225 | + "nbconvert_exporter": "python", |
| 226 | + "pygments_lexer": "ipython3", |
| 227 | + "version": "3.10.6" |
| 228 | + } |
| 229 | + }, |
| 230 | + "nbformat": 4, |
| 231 | + "nbformat_minor": 4 |
| 232 | +} |
0 commit comments