Skip to content

Commit 2bfde78

Browse files
authored
Merge pull request #7 from mportdata/codex/notebook-parameter-cleanup
[codex] Clean up pruning notebook example
2 parents 2c9ddcb + 9920602 commit 2bfde78

3 files changed

Lines changed: 46 additions & 23 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ print(f"Aggregated from {len(logical_plan.sample_plans)} sample plans.")
8181

8282
### Database connector
8383

84+
**Note:** currently only supports `bigquery`, support for all other popular database types coming soon.
85+
8486
Use `DatabaseConnector` to inspect a supported database and return a typed schema.
8587

8688
```python

‎examples/dual-pathway-pruning-open-ai-bigquery-example.ipynb‎

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,37 @@
6060
"cell_type": "markdown",
6161
"id": "5",
6262
"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": {},
6387
"source": [
6488
"## Step 1: Create a Logical Plan"
6589
]
6690
},
6791
{
6892
"cell_type": "markdown",
69-
"id": "6",
93+
"id": "8",
7094
"metadata": {},
7195
"source": [
7296
"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,27 +101,23 @@
77101
{
78102
"cell_type": "code",
79103
"execution_count": null,
80-
"id": "7",
104+
"id": "9",
81105
"metadata": {},
82106
"outputs": [],
83107
"source": [
84108
"from piglets import LogicalPlanner\n",
85109
"\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",
90110
"\n",
91111
"logical_planner = LogicalPlanner(\"gpt-5.2\")\n",
92112
"logical_plan = logical_planner.plan(\n",
93-
" natural_language_query=question,\n",
113+
" natural_language_query=QUESTION,\n",
94114
" num_samples=3,\n",
95115
")"
96116
]
97117
},
98118
{
99119
"cell_type": "markdown",
100-
"id": "8",
120+
"id": "10",
101121
"metadata": {},
102122
"source": [
103123
"Below we can inspect the sample plans that were first created and the aggregate plan that was created form them:"
@@ -106,7 +126,7 @@
106126
{
107127
"cell_type": "code",
108128
"execution_count": null,
109-
"id": "9",
129+
"id": "11",
110130
"metadata": {},
111131
"outputs": [],
112132
"source": [
@@ -134,15 +154,15 @@
134154
},
135155
{
136156
"cell_type": "markdown",
137-
"id": "10",
157+
"id": "12",
138158
"metadata": {},
139159
"source": [
140160
"## Step 2: Conenct to BigQuery and retrieve the Database Schema"
141161
]
142162
},
143163
{
144164
"cell_type": "markdown",
145-
"id": "11",
165+
"id": "13",
146166
"metadata": {},
147167
"source": [
148168
"Next we connect to BigQuery. The dataset `stack_overflow` was created by cloning the public stack overflow dataset.\n",
@@ -152,23 +172,23 @@
152172
{
153173
"cell_type": "code",
154174
"execution_count": null,
155-
"id": "12",
175+
"id": "14",
156176
"metadata": {},
157177
"outputs": [],
158178
"source": [
159179
"from piglets import DatabaseConnector\n",
160180
"\n",
161181
"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",
164184
")\n",
165185
"database = database_connector.get_database_schema()"
166186
]
167187
},
168188
{
169189
"cell_type": "code",
170190
"execution_count": null,
171-
"id": "13",
191+
"id": "15",
172192
"metadata": {},
173193
"outputs": [],
174194
"source": [
@@ -188,21 +208,21 @@
188208
},
189209
{
190210
"cell_type": "markdown",
191-
"id": "14",
211+
"id": "16",
192212
"metadata": {},
193213
"source": [
194214
"## Use Dual-Pathway Pruning to Reduce Potemtial Schema"
195215
]
196216
},
197217
{
198218
"cell_type": "markdown",
199-
"id": "15",
219+
"id": "17",
200220
"metadata": {},
201221
"source": [
202222
"Above we have retrieved a large schema, much of which won't be relevant to our natural language question.\n",
203223
"The `piglets` `Pruner` uses dual-pathway pruning to reduce the size of this schema.\n",
204224
"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",
206226
"$$\n",
207227
"D_{\\text{pruned}} = (D \\setminus C_{\\text{del}}) \\cup C_{\\text{keep}}\n",
208228
"$$\n",
@@ -212,15 +232,15 @@
212232
{
213233
"cell_type": "code",
214234
"execution_count": null,
215-
"id": "16",
235+
"id": "18",
216236
"metadata": {},
217237
"outputs": [],
218238
"source": [
219239
"from piglets import Pruner\n",
220240
"\n",
221-
"pruner = Pruner(model_name=\"gpt-5.2\")\n",
241+
"pruner = Pruner(model_name=MODEL_NAME)\n",
222242
"pruned_database = pruner.dual_pathway_pruning(\n",
223-
" natural_language_query=question,\n",
243+
" natural_language_query=QUESTION,\n",
224244
" database=database,\n",
225245
" logical_plan=logical_plan,\n",
226246
")"
@@ -229,7 +249,7 @@
229249
{
230250
"cell_type": "code",
231251
"execution_count": null,
232-
"id": "17",
252+
"id": "19",
233253
"metadata": {},
234254
"outputs": [],
235255
"source": [
@@ -249,7 +269,7 @@
249269
},
250270
{
251271
"cell_type": "markdown",
252-
"id": "18",
272+
"id": "20",
253273
"metadata": {},
254274
"source": [
255275
"You should now see a reduced version of the original schema above."

‎src/piglets/database/database_connector.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from piglets.types import Column, Database, Table
66

77
class DatabaseConnector():
8+
# TODO: Research using ADBC connectors rather than an orm (https://github.com/apache/arrow-adbc)
89
"""Base class for database connectors."""
910
def __init__(self, database_type: str, database_name: str, gcp_project_id: str = None):
1011
if database_type == "bigquery":

0 commit comments

Comments
 (0)