- [Instructor] So with that, 2
I'm going to jump over here into Colab 3
and we're going to get started building with the API. 4
So the use case for today is building a podcast search. 5
So I personally listen to a ton of podcasts. 6
It's very unstructured data. 7
I'm often listening while I'm like doing the dishes 8
or driving my car, 9
and so I might hear something, 10
I won't get a chance to write it down 11
and I always want to refer back to something that I learned. 12
So today's use case will be taking a bunch of podcasts 13
and making a searchable database 14
so we can retrieve information about what we learned. 15
To do this, we're going to be using the File Search tool 16
in the Gemini API. 17
This is a built-in tool that imports, chunks, 18
and indexes your data 19
so that that information can be retrieved 20
based on a user's prompt. 21
And then it's provided as context for the model prompt, 22
which allows the model to basically give more accurate 23
and relevant answers. 24
So if you're thinking, 25
well, this sounds a whole lot like RAG, you are correct. 26
The File Search tool is essentially the managed RAG tool 27
within the Gemini API. 28
So that's what we're going to build today. 29
You will need to set up your API key. 30
I'm going to show you how to do this in Colab 31
just in case that's the environment you're using. 32
But if you're using something else, 33
you can set the API key as an environment variable. 34
In Colab, we do this as a Colab secret. 35
So you just create a new secret here 36
and you can call it Gemini API key 37
and paste in the value of your key. 38
And then we can run the cell here, 39
which imports user data from Google Colab, 40
and that allows us to extract our API key. 41
So again, this is just specific to Colab. 42
If you're doing this elsewhere, 43
just make sure you set your API key 44
as an environment variable. 45
But once we've done that, 46
we can then import the Google Gen AI SDK, 47
and we can go ahead and create our client 48
and pass in the API key. 49
And again, don't hard-code this. 50
If you're doing this in another environment, 51
you'll just want to pass this as an environment variable. 52
But now we have our client set up 53
and we are ready to get started. 54
So before we go into the more complicated podcast example, 55
we're just going to start off using the File Search tool 56
with a single document 57
just to get some familiarity with how this tool works. 58
So in order to use the File Search tool, 59
we will go through three steps. 60
The first thing we'll do 61
is we will create a File Search store 62
and then we will upload a file to the File Search store 63
and then we can ask a question. 64
So I'm going to get started 65
by importing a couple libraries that I'll need. 66
And then the next thing we'll do 67
is create our File Search store. 68
So the search store is essentially the database 69
that has all of the information from our files. 70
So it's our persistent container for these embeddings 71
that the semantic search will operate on at inference time. 72
So to create this, we are going to call 73
file_search_stores.create on our client. 74
And remember, we set up our client earlier up here, 75
that was our Google Gen AI client up here. 76
And we will need to give our search store a name. 77
I'm just calling it my example store, 78
but feel free to try out anything else you like. 79
And then the next thing we'll do 80
is we need to upload a file. 81
So I need to get a file uploaded into my Colab runtime. 82
I'm just going to drag this file 83
and upload it right here to my Colab environment. 84
And once I've done that, 85
we can take a look at this file really quickly. 86
This just has information 87
about different baked goods at a bakery. 88
You can see the name of the item 89
and then the description, ingredients, and the macros. 90
So let's jump back into Colab. 91
Now that I've got this file uploaded, 92
I can upload the file to my File Search store. 93
So that is calling upload_to_file_search_store. 94
And you'll notice that I am passing in 95
not only the name of the file we just uploaded, 96
that was our bake-shop.txt file, 97
but also the specific File Search store that we created, 98
and that's through store.name. 99
So let's take a look at what this looks like. 100
So if we call store.name, 101
we'll see that there is a unique identifier 102
created for our File Search store, 103
and that is right here. 104
And so this is just the identifier 105
so that our client knows which of our File Search stores 106
to upload this file to, 107
because you might have multiple different ones depending on, 108
you know, all the different use cases you're working on. 109
So now that we've done that and our file has been uploaded, 110
we can go ahead and ask the model a question. 111
So I have a prompt here, which is: 112
what item can I eat on the menu if I'm gluten-free? 113
And in the next cell, we are going to pass that prompt 114
to the model right here. 115
And we'll do this with the generate_content method. 116
So if you've used the API before, 117
you might know that generate_content 118
is our main text generation method. 119
And so we can pass in the name of the model we're using, 120
that is Gemini 3 Pro preview. 121
And let's go ahead and execute the cell. 122
Just to note, 123
in addition to passing the prompt and the model, 124
I have also passed in the File Search tool. 125
So to do that, we create this GenerateContentConfig 126
and we pass in tools. 127
And the tool we're passing in is the File Search tool. 128
And then you'll notice again, 129
we pass in the specific name of our store. 130
Just in case you have multiple stores 131
associated with this client, 132
you just need to specify the exact store. 133
And so now that we've done this, 134
we can take a look at the response. 135
So we'll say response.text, 136
and you'll see that the model says, 137
based on the bake shop menu provided, 138
there's one item explicitly labeled as gluten-free, 139
blah, blah, blah. 140
So it gives us an answer to the query that we asked. 141
And just to make this extra clear, 142
this whole block of text right here 143
that I've highlighted in blue, 144
this is the configuration of the tool. 145
So if we were to just call the model like this 146
without the tool in there, 147
it wouldn't have access to this bake shop file. 148
So if we said, what item can I eat on the menu 149
if I'm gluten-free, 150
it wouldn't know what we're talking about. 151
Something else we can do 152
is we can take a look at the metadata if we want to. 153
So the response includes not just an answer to our question, 154
but also citations that specify which part of the documents 155
were used to actually generate the answer. 156
So if you look at the grounding metadata, 157
you'll see these chunks of data 158
which show in the text file what was being used. 159
And you'll also be able to see the actual text 160
that was generated by the model 161
and the response like this right here 162
that is associated with those grounded chunks. 163
So that gives us an example of how to do this 164
with one single document. 165
But of course, we could have just uploaded this document 166
directly into the model's prompt. 167
It probably would've been a whole lot faster. 168
We didn't need to go through all the effort 169
of actually creating this File Search store 170
just for one single document. 171
But creating the File Search store is really useful 172
when we have tons and tons of documents 173
and we don't know where the answer is exactly. 174
And so that is a perfect kind of setup 175
for our podcast search example.