Skip to content

Commit 86b4b2a

Browse files
authored
Merge pull request #20 from matsonj/jm-part-4-ai
adding ai
2 parents 192eb22 + 6739b7e commit 86b4b2a

1 file changed

Lines changed: 38 additions & 44 deletions

File tree

4_data-in-the-cloud.md

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,6 @@ kernelspec:
1818

1919
# 4. Collaborating with data in the Cloud
2020

21-
To start off, install the latest version of `duckdb` and `magic-duckdb` to run this notebook.
22-
23-
```{code-cell}
24-
!pip install --upgrade duckdb magic-duckdb -q
25-
%load_ext magic_duckdb
26-
```
27-
28-
We're also going to create a helper variable `IN_COLAB` to see if we're running Google Colab. This will come in handy later.
29-
30-
```{code-cell}
31-
try:
32-
import google.colab
33-
IN_COLAB = True
34-
except:
35-
IN_COLAB = False
36-
```
37-
3821
This cell downloads the answers for the exercises.
3922

4023
```{code-cell}
@@ -57,27 +40,16 @@ However, this will throw an error! You actually need to specify your authenticat
5740

5841
To do so, you can [copy your token](https://app.motherduck.com/token-request?appName=Jupyter) from Motherduck and add it to your notebook "Secrets".
5942

60-
If you are using Google Colab, you can click on the "Secrets" tab and add a new "token" secret there. See how to do that in the screenshot below.
61-
62-
<img src="https://github.com/motherduckdb/sql-tutorial/blob/main/notebooks/Colab_Secret.png?raw=true" width=400>
63-
6443
Now, you can get your token from the secrets manager and load it into an environment variable. After this, you can connect to MotherDuck without any extra authentication steps!
6544

66-
```{code-cell}
67-
import os
45+
BUT if you want to use a Marimo SQL Cell, this will *simply work*:
6846

69-
if IN_COLAB:
70-
from google.colab import userdata
71-
os.environ["motherduck_token"] = userdata.get('token')
7247
```
73-
74-
If you're running in a Jupyter Notebook elsewhere, you can uncomment and run the following, and paste your token in the input field:
75-
76-
```{code-cell}
77-
# import getpass
78-
# os.environ["motherduck_token"] = getpass.getpass(prompt='Password: ', stream=None)
48+
ATTACH 'md:'
7949
```
8050

51+
Then you can click-through the authentication steps and get your token into your Marimo session.
52+
8153
```{admonition} Exercise 4.01
8254
Create a connection to MotherDuck and show all tables in your `sample_data` database. You can use the `SHOW TABLES` command that is documented [here](https://duckdb.org/docs/guides/meta/list_tables.html).
8355
```
@@ -95,7 +67,6 @@ To query the data, you'll want to fully specify the table name with the followin
9567
For example, you can run the below cell to get the service requests between March 27th and 31st of 2022:
9668

9769
```{code-cell}
98-
%%dql -co con
9970
SELECT
10071
created_date, agency_name, complaint_type,
10172
descriptor, incident_address, resolution_description
@@ -122,14 +93,12 @@ Now, let's try to load some data from a data source into MotherDuck. HuggingFace
12293
To query a HuggingFace dataset, you can run:
12394

12495
```{code-cell}
125-
%%dql -co con
12696
SELECT * FROM read_parquet('hf://datasets/datonic/threatened_animal_species/data/threatened_animal_species.parquet');
12797
```
12898

12999
Before we create a new table with this data, let's first swap to a different database. You can do so by creating a new DuckDB connection, or by changing the database with the `USE` statement. For example, to connect to your default database, `my_db`, run:
130100

131101
```{code-cell}
132-
%%dql -co con
133102
USE my_db;
134103
```
135104

@@ -156,12 +125,9 @@ Now, we have two tables that we can join together and share with our colleagues!
156125
Let's inspect them and take a look at the columns we have available.
157126

158127
```{code-cell}
159-
%%dql -co con
160128
DESCRIBE animals;
161129
```
162130

163-
```{code-cell}
164-
%%dql -co con
165131
DESCRIBE duckdb_ducks;
166132
```
167133
@@ -178,7 +144,6 @@ Create a new table called `duckdb_species` that joins the `duckdb_ducks` and `an
178144
To share your database, you can run:
179145

180146
```{code-cell}
181-
%%dql -co con -o df
182147
CREATE SHARE duck_share FROM my_db (ACCESS UNRESTRICTED);
183148
```
184149

@@ -200,20 +165,17 @@ ATTACH '<share_url>';
200165
For example, to load the [Mosaic example datasets](https://github.com/motherduckdb/wasm-client/tree/main), run
201166

202167
```{code-cell}
203-
%%dql -co con
204168
ATTACH 'md:_share/mosaic_examples/b01cfda8-239e-4148-a228-054b94cdc3b4';
205169
```
206170

207171
You can then inspect the database and query the data like so:
208172

209173
```{code-cell}
210-
%%dql -co con
211174
USE mosaic_examples;
212175
SHOW TABLES;
213176
```
214177

215178
```{code-cell}
216-
%%dql -co con
217179
SELECT * FROM seattle_weather;
218180
```
219181

@@ -226,14 +188,46 @@ Attach the share you received from your neighbor and inspect the tables.
226188
To detach a database someone shared with you, make sure it's not selected, and run `DETACH`:
227189

228190
```{code-cell}
229-
%%dql -co con
230191
USE my_db;
231192
DETACH mosaic_examples;
232193
```
233194

234195
To drop the share you created, simply run:
235196

236197
```{code-cell}
237-
%%dql -co con
238198
DROP SHARE duck_share;
239199
```
200+
201+
## How do we fit AI into this?
202+
203+
MotherDuck contains [a set of useful AI functions](https://motherduck.com/docs/category/sql-assistant/) that you can use interrogate your data.
204+
205+
A particularly useful one is `PRAGMA prompt_query('<natural language question>')` - which we can use interrogate our datasets. Recall the exercise from part 3 - getting the bird with the maximum wing length? Lets do this with a bit of AI in MotherDuck.
206+
207+
The first step is that it must understand the data, so we can simply create a table using CTAS from our local file:
208+
209+
```sql
210+
CREATE OR REPLACE TABLE birds AS
211+
FROM 'birds.csv'
212+
```
213+
214+
Then we can ask a question about it.
215+
216+
```sql
217+
PRAGMA prompt_query('which bird has the largest wing length?')
218+
```
219+
220+
This *should* return the right answer. But how can we validate it? Lets use `CALL prompt_sql()` to do so!
221+
222+
```sql
223+
CALL prompt_sql('which bird has the largest wing length?')
224+
```
225+
226+
This will return the SQL query that is associated to this question, which can then be inspected and run by the user!
227+
228+
## Further Reading
229+
230+
We have written extensively about using AI with SQL. Hopefully these links will help you understand how you can better these types of capabilities into your own workflow.
231+
- [Writing Flawless SQL in Cursor](https://motherduck.com/blog/vibe-coding-sql-cursor/)
232+
- [Using the MotherDuck MCP for Fast Pipeline Dev](https://motherduck.com/blog/faster-data-pipelines-with-mcp-duckdb-ai/)
233+
- [NLP inside of your database with `PROMPT()`](https://motherduck.com/blog/llm-data-pipelines-prompt-motherduck-dbt/)

0 commit comments

Comments
 (0)