|
60 | 60 | "cell_type": "markdown", |
61 | 61 | "id": "5", |
62 | 62 | "metadata": {}, |
| 63 | + "source": [ |
| 64 | + "## Set Your Model, Database and Natural Language Question" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "code", |
| 69 | + "execution_count": null, |
| 70 | + "id": "6", |
| 71 | + "metadata": {}, |
| 72 | + "outputs": [], |
| 73 | + "source": [ |
| 74 | + "MODEL_NAME = \"gpt-5.2\"\n", |
| 75 | + "DATABASE_TYPE = \"bigquery\"\n", |
| 76 | + "DATABASE_NAME = \"stack_overflow\"\n", |
| 77 | + "QUESTION = \"\"\"\n", |
| 78 | + " Which tags saw the largest increase in average answer score from 2022 to 2023, \n", |
| 79 | + " considering only questions with at least 5 answers?\n", |
| 80 | + "\"\"\"" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "markdown", |
| 85 | + "id": "7", |
| 86 | + "metadata": {}, |
63 | 87 | "source": [ |
64 | 88 | "## Step 1: Create a Logical Plan" |
65 | 89 | ] |
66 | 90 | }, |
67 | 91 | { |
68 | 92 | "cell_type": "markdown", |
69 | | - "id": "6", |
| 93 | + "id": "8", |
70 | 94 | "metadata": {}, |
71 | 95 | "source": [ |
72 | 96 | "We first have our model verbalise it's hypotheses. This step is schema agnostic and is therefore completed without any knowledge of the actual database. This allows the model to reason about possible approaches without the influence of naming conventions and ambiguous abbreviations in the database schema.\n", |
|
77 | 101 | { |
78 | 102 | "cell_type": "code", |
79 | 103 | "execution_count": null, |
80 | | - "id": "7", |
| 104 | + "id": "9", |
81 | 105 | "metadata": {}, |
82 | 106 | "outputs": [], |
83 | 107 | "source": [ |
84 | 108 | "from piglets import LogicalPlanner\n", |
85 | 109 | "\n", |
86 | | - "question = \"\"\"\n", |
87 | | - " Which tags saw the largest increase in average answer score from 2022 to 2023, \n", |
88 | | - " considering only questions with at least 5 answers?\n", |
89 | | - "\"\"\"\n", |
90 | 110 | "\n", |
91 | 111 | "logical_planner = LogicalPlanner(\"gpt-5.2\")\n", |
92 | 112 | "logical_plan = logical_planner.plan(\n", |
93 | | - " natural_language_query=question,\n", |
| 113 | + " natural_language_query=QUESTION,\n", |
94 | 114 | " num_samples=3,\n", |
95 | 115 | ")" |
96 | 116 | ] |
97 | 117 | }, |
98 | 118 | { |
99 | 119 | "cell_type": "markdown", |
100 | | - "id": "8", |
| 120 | + "id": "10", |
101 | 121 | "metadata": {}, |
102 | 122 | "source": [ |
103 | 123 | "Below we can inspect the sample plans that were first created and the aggregate plan that was created form them:" |
|
106 | 126 | { |
107 | 127 | "cell_type": "code", |
108 | 128 | "execution_count": null, |
109 | | - "id": "9", |
| 129 | + "id": "11", |
110 | 130 | "metadata": {}, |
111 | 131 | "outputs": [], |
112 | 132 | "source": [ |
|
134 | 154 | }, |
135 | 155 | { |
136 | 156 | "cell_type": "markdown", |
137 | | - "id": "10", |
| 157 | + "id": "12", |
138 | 158 | "metadata": {}, |
139 | 159 | "source": [ |
140 | 160 | "## Step 2: Conenct to BigQuery and retrieve the Database Schema" |
141 | 161 | ] |
142 | 162 | }, |
143 | 163 | { |
144 | 164 | "cell_type": "markdown", |
145 | | - "id": "11", |
| 165 | + "id": "13", |
146 | 166 | "metadata": {}, |
147 | 167 | "source": [ |
148 | 168 | "Next we connect to BigQuery. The dataset `stack_overflow` was created by cloning the public stack overflow dataset.\n", |
|
152 | 172 | { |
153 | 173 | "cell_type": "code", |
154 | 174 | "execution_count": null, |
155 | | - "id": "12", |
| 175 | + "id": "14", |
156 | 176 | "metadata": {}, |
157 | 177 | "outputs": [], |
158 | 178 | "source": [ |
159 | 179 | "from piglets import DatabaseConnector\n", |
160 | 180 | "\n", |
161 | 181 | "database_connector = DatabaseConnector(\n", |
162 | | - " database_type=\"bigquery\",\n", |
163 | | - " database_name=\"stack_overflow\",\n", |
| 182 | + " database_type=DATABASE_TYPE,\n", |
| 183 | + " database_name=DATABASE_NAME,\n", |
164 | 184 | ")\n", |
165 | 185 | "database = database_connector.get_database_schema()" |
166 | 186 | ] |
167 | 187 | }, |
168 | 188 | { |
169 | 189 | "cell_type": "code", |
170 | 190 | "execution_count": null, |
171 | | - "id": "13", |
| 191 | + "id": "15", |
172 | 192 | "metadata": {}, |
173 | 193 | "outputs": [], |
174 | 194 | "source": [ |
|
188 | 208 | }, |
189 | 209 | { |
190 | 210 | "cell_type": "markdown", |
191 | | - "id": "14", |
| 211 | + "id": "16", |
192 | 212 | "metadata": {}, |
193 | 213 | "source": [ |
194 | 214 | "## Use Dual-Pathway Pruning to Reduce Potemtial Schema" |
195 | 215 | ] |
196 | 216 | }, |
197 | 217 | { |
198 | 218 | "cell_type": "markdown", |
199 | | - "id": "15", |
| 219 | + "id": "17", |
200 | 220 | "metadata": {}, |
201 | 221 | "source": [ |
202 | 222 | "Above we have retrieved a large schema, much of which won't be relevant to our natural language question.\n", |
203 | 223 | "The `piglets` `Pruner` uses dual-pathway pruning to reduce the size of this schema.\n", |
204 | 224 | "Dual-pathway pruning first produces a `PreservationSet` and a `DeletionSet`, these are made up of potentially useful tables and columns and obviously useless tables and columns respectively. \n", |
205 | | - "We then take the union of all columns either not present in the `DeletionSet` or are present in the `PreservationSeet`. More formally:\n", |
| 225 | + "We then take the union of all columns either not present in the `DeletionSet` or are present in the `PreservationSet`. More formally:\n", |
206 | 226 | "$$\n", |
207 | 227 | "D_{\\text{pruned}} = (D \\setminus C_{\\text{del}}) \\cup C_{\\text{keep}}\n", |
208 | 228 | "$$\n", |
|
212 | 232 | { |
213 | 233 | "cell_type": "code", |
214 | 234 | "execution_count": null, |
215 | | - "id": "16", |
| 235 | + "id": "18", |
216 | 236 | "metadata": {}, |
217 | 237 | "outputs": [], |
218 | 238 | "source": [ |
219 | 239 | "from piglets import Pruner\n", |
220 | 240 | "\n", |
221 | | - "pruner = Pruner(model_name=\"gpt-5.2\")\n", |
| 241 | + "pruner = Pruner(model_name=MODEL_NAME)\n", |
222 | 242 | "pruned_database = pruner.dual_pathway_pruning(\n", |
223 | | - " natural_language_query=question,\n", |
| 243 | + " natural_language_query=QUESTION,\n", |
224 | 244 | " database=database,\n", |
225 | 245 | " logical_plan=logical_plan,\n", |
226 | 246 | ")" |
|
229 | 249 | { |
230 | 250 | "cell_type": "code", |
231 | 251 | "execution_count": null, |
232 | | - "id": "17", |
| 252 | + "id": "19", |
233 | 253 | "metadata": {}, |
234 | 254 | "outputs": [], |
235 | 255 | "source": [ |
|
249 | 269 | }, |
250 | 270 | { |
251 | 271 | "cell_type": "markdown", |
252 | | - "id": "18", |
| 272 | + "id": "20", |
253 | 273 | "metadata": {}, |
254 | 274 | "source": [ |
255 | 275 | "You should now see a reduced version of the original schema above." |
|
0 commit comments