Building a second brain with AI
Transcript
[00:00:00] Today, I'm going to show you something a little more advanced than usual. I'm going to build my second brain with AI.
So what I have here is a collection of all the bookmarks, articles, tweets, everything that I have ever read. And I have stored all of this in a database and connected it to AI. So when I ask AI or a custom GPD, any question, it will go look into my database and come back with the right answer.
And I'm going to show you how to build this from scratch.
So I'm going to go and ask tell me about thousand true fans. So when I asked the GPT, it goes and looks for all the information related to thousand true fans.
And it summarizes exactly what I had in my notes. And when I look into the sources, you can see what articles that it referred to
so what AI did was to summarize all that information and answered my question but not from an external knowledge, but from my stored database. So let's try asking another question. I have a presentation tomorrow. Tell me 10 [00:01:00] tips about storytelling.
Pretty cool, right? See, these are all the notes that I had collected related to storytelling I see these incredibly useful, especially when I'm starting to make more notes and read more books.
This is going to be a center point for all my information.
So let's get started.
For this project, unlike all the other projects, I am going to go to Vercel templates and in this particular case, I want to start an AI template. And the framework that I want to use is, Nextjs. So I have a bunch of templates here, and this template that I am specifically interested in is the one with Langchain.
So langchain is like this library that sits on top of all these LLMs that makes it easy to interact with them.
If you see here, there are a bunch of. features that Lanchain provides, agents, retrieval, retrieval agents. But the one that we are interested in today is retrieval.
If you scroll here, this is some data that is pre filled in this form. And when you upload it, it should typically go Store in the database. And then when you ask [00:02:00] questions from this data, it should answer you just like what I showed you in the demo,
Once I've opened the repository, what I need to do is first copy this and I can go into my terminal, and I can just say git clone, and paste this project. And along with it, I also want to give a name for my project. In this case, I'm going to call it second brain.
I have just opened Cursor, and within Cursor I will go drop my folder, the second Brain
Now within cursor, start a new terminal and now I can say yarn install.
The next thing I need to do is run the project yarn dev
basically what you saw in the demo is now on the localhost. So if I go into retrieval, you can see that I have the retrieval working here. But then when I go and try to upload, it will throw me an error because We haven't configured our database
so we have a folder called app. you just have to know this once to know what is where. So when you ask cursor to do [00:03:00] things, you can tag that file and ask it to do certain things
so within app, you have a place called API and within API, you have a chat and you have a retrieval. And within chat, you have retrieval, which is the one that we are mainly interested in and everything else we don't need. We have all these retrieval agents, structure output, we don't need all of that.
The only thing we are concerned about is retrieval. So we can actually go ahead and delete those things. So we can go and delete agents. Retrieval agents. Structure output. We need retrieval ingest. So ingestion is to upload the files into the database. So we're going to keep that. We are also going to delete this retrieval agents. We don't need structured output again. We don't need this top header on the top. This is something that I know is inside the layout. And if I go into the layout, I can see this navbar component. I don't need the navbar component either.
Now what we're going to do [00:04:00] is create an account in super base. You can do that by going to super base. com. I am going to log in into my dashboard. And within dashboard, I can open a new project. I will first select a project name.
So I'm going to say a second brain and you can create any password you like. I'm in EU, so I'm just going to select EU. So once I've selected a new project. You can see that I have a project URL. So I'm going to copy this URL and I am going to go into env. example. These are environment variables. This is the place where you store all your API keys and all your URLs and all of that, which gets read in the application.
When I go here, you can see that there are these example keys that are pasted here for us to refer to. So if you see there are two keys here, so I am just going to uncomment them and I will paste my super base URL over here.
So now when you scroll down, you will see this button called view API settings. Let's go [00:05:00] into that settings. And then you see this, a project API keys, and you will see a thing called secret.
I want to copy this. And I am going to go back into my, env. example file and go paste this Secret over here. I am going to go save it and I will rename this into env. So example is just for them to show us what should this look like,
and the last thing we need to do is to enter our OpenAI API key. I'm going to paste that key and then I will hit save.
There is one last step, which is to create a table where we will be storing our information.
To do that, you can see the information right inside this GitHub project. So if you scroll down, retrieval is the project that we need to do, which is just to retrieve. information from the database. And you can see that for super base, follow these instructions. So I am going to go into the super base instructions that takes me to the Langchain [00:06:00] documentation. And within, if I scroll down, I can see that there is this whole command to set up a PG vector database.
So once I've copied this, I will go back to my super base and open SQL editor, and I will paste whatever I copied from there.
And when I run it, it just says success, no rows return. And what that will do is when I go into my table editor, I, it will create this table called documents. And within documents, you can see that it's a table. Think of it like a Google sheets table, except there are no contents over here.
This is a one time configuration. I know this is a lot of new things if you're new to supabase, but once you set this up. There's not much you need to do
So right away, if I go back into my example, if I tap on Upload, what it will do is Upload whatever is contents are here into supabase. And I get a success message that says uploaded. If I go into my super base, you can see that all this data [00:07:00] has been uploaded.
And you can see there's like over 127 records of data that got uploaded. And that is because all this content is not stored in one entry. It's transformed into different chunks and stored in different entries. So that makes it easier for the large language model to fetch the right kind of information.
I will explain to you a little more technical details of this a little later, for now, you just have to understand that all this information is now stored in the database. Now I can also go and delete all the items in the table. So I can go back into my SQL editor and there is an AI right within super base.
And then I can also create a new query and I will say, create a new snippet. And I will say, write an. SQL query to delete all records in my table.
So I'm just going to go insert this code. If I run this code, it says it's a destructive operation, but that's okay. Let's delete it. And then once we go into our table editor, you see there's nothing over here.
Now [00:08:00] we will open our cursor at this point.
Okay. First thing I want to do is instead of uploading this random text that got prefilled here., I want to upload my own documents.
So what I can do right now is I can go, opened my project and within the main root folder, I have this folder called public, I will create a new folder called data , and within data, I will paste a couple of Markdown files.
So I have these three Markdown files, one are these highlights from Ego is the enemy, the book that I read. And this is an article from FS blog and then this is another from Julian Shapiro,
Now what I'm going to do is, as I told you, you just need to understand which file is what. We only need chat retrieval route. Then we need retrieval ingest. The ingest is to upload the documents to the database. The retrieval is to retrieve the documents from the database.
This retrieval page is our front end, which is what you [00:09:00] see on this page. This is the most simplest application and this is the sample data that gets loaded into this form.
I think it would help if you download this file and just go through it in your own time a little bit. But now, once you've gotten a hang of what all files are inside, all you need to do is whenever you ask AI something, you just have to tag those files.
So here in my composer, I'm going to go tag both the routes for the retrieval for the ingest. And I am also going to tag my chat window, and I'm going to say instead of the form, create a button that says upload files. And when I click on the button upload all the files inside data folder. vectorize them and store them in supabase
as you can see, it created a folder called upload files. And let me go and click on it
So it looks like it gave me a success message. It says files uploaded and vectorized successfully. I [00:10:00] can go into my database and now I have all these files in my database. So let me go into my data and you can see ego is the enemy. And the first, if I open the first item over here, I can see that there's just this much data, which is until this highlights 13.
And then if I go into the second one, I can see that there's another chunk of data. So it just basically chunked all this data into different rows and it created all of these rows in my database.
Okay, so now if I go back into my application and ask it questions around,
explain about writing from ego is the enemy.
It's giving me these funny answers because the prompt is written that way. And we can go fix that because when we go into root. ts is this file.
This is the prompt that is actually influencing the way AI is talking to us. It says you are an energetic talking puppy named Dana. We don't need that. We need to say [00:11:00] that you are an assistant who answers questions related to the user's uploaded documents. That's it. Don't use puns. Now that we have that, we can go and ask it the questions that we want to ask again. So let's go and copy this question again. See, it goes and says, according to the concept, the importance of being humble and open to feedback and writing is emphasized as a way to avoid chasing one's own ego and instead focusing on improving, refining one's work.
From this text, it inferred from three different texts, and it summarized and answered my question that I just asked.
I can go into AI and I can say, let me open a new one, remove the form to upload text. I just want to retain the upload files,
Button. As you see, that's gone as well.
Now there is one last part. The. We haven't done. The problem is when you upload files every single time, it will upload a new batch of vectorized files but it will not check if these files had already existed.
So I [00:12:00] took care of that doing the same function. I went to command I and would again do tag both root and I would also tag chat window and say, when I click on upload files, check first. if those files have already been ingested. Because it created a new file right now, it created a file called upload and vectorize let me tag that upload and vectorize. And so check if those files have already been ingested. And if so, do not Upload them to database again, if not upload all the files inside the data folder. And when I say that it should take care of the duplication problem.
Okay. No errors. So I can just accept it. So right now let's go and see the database. So I have in total 32 records. So now if I go and say upload, it should ideally not increase, right? so it says three files were already ingested and skipped.
Pretty cool. So if I go here, it again stays at 32 records. So that's [00:13:00] pretty much it. Let me go back into here and I can say,
tell me five interesting things about ego is the enemy. And when I ask that, what it will do is just pull out all the notes related to ego as the enemy, and then just gives me all the notes that I have collected. The more data you add, the better it gets. And of course you can go a little deeper into how vectorizing works, but that goes beyond the point of this . I just wanted to show you what is possible and with very little coding.
And finally, I want to leave you with what's happening behind the scenes. So when you have all these Markdown files, what it does is it creates all these chunks and that's the chunks that you saw in the database. And with these chunks, it first vectorizes them.
So vectorizes is to create in this numerical format that makes it easy for the retrieval and it stores these vectors into the database. And within the database, based on the question that we [00:14:00] ask, because the question is also vectorized and using this vector and the vectors stored in the database, it pulls the relevant vectors.
What it does is it does similarity matching. So it can, based on the question that you ask, it understands what's the right vector that we need to pull based on similarity. And then it creates all these chunks again. And all these chunks are then fed into ChatGPT with our LLM model. And then it uses those information chunks, and uses the question that we asked, and then returns the answer back to us.
So that's what happens behind the scenes. So I know this is a bit advanced for some of you. But if you like this video, and if you want to know more about how I did this and little more details right in the comments and I will make another video explaining this in more detail.
Thank you for watching.