MongoDB Vector Search Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys if you're a web developer are looking for a new Vector database offering I would highly recommend mongodbs Vector database and the reason is it's super simple to set up and so in this video I'm going to talk a little bit about the background of vector databases I'm going to talk about how to set up mongodb since there's not a lot of tutorials online we're also going to go into the pricing as well because I know people who are sensitive about that so let me share my screen so what exactly is a vector embedding a vector embedding is basically just a mathematical representation of a piece of data and so on open ai's website we're going to look at this example in which a piece of text can we run through their embedding model and be turned into a series of floating Point numbers and to be exact the model we're going to use turns it into a series of 1536 different floating Point numbers and we could think of these floating Point numbers as a representation of these words and by the way it doesn't have to be just words this can also apply to images and audio but for what we're doing we're just going to stick with text and so let me show you the intuition behind how this works so let's go to these slides here apologies for the quality it's from a Chinese research paper from the 1990s so in this image we have an x-coordinate we have a y coordinate and a z coordinate right so we're in three-dimensional space for a vector embedding using open ai's encoder it would be like this except instead of three dimensions it would be 1536 different dimensions so it would be multi-dimensional space and to measure the similarity between vectors we have this Vector here Vector zero and Vector one and we can measure their similarity by three different ways technically morbid we're going to stick with the most common three you can either use a euclidean distance which is the distance between the ends you can either use cosine which is the angle between the vectors or you can use the dot product of the vectors and so this applies not just a three-dimensional space but to multi-dimensional space as well and so in multi-dimensional space we're going to be using the cosine which is the angle between the vectors so let's go into the code and show you how we actually set things up in mongodb okay so let's now go over the code the first thing you're going to need to do is to find your schema and here I have a document upload schema and then the schema I have a title description field name upload date and the important part embedding is an array of numbers so basically is an array of floating Point numbers and you can actually call this whatever you want I call it embedding just to make it clear in this case and just create your schema like this like this is the important part and then I'm going to show you how to create the index for the embedding in mongodb so what you're going to want to do is already have a mongodb atlas account and I'm not going to go over how to set that up but it's there's plenty of tutorials online but once you have your mongodb Atlas account you're going to want to go to search and then let's see go to atlas search and I already have the index created here for the embeddings but what you want what you're going to want to do is create index go to Json editor click next and then go to where your collection is in this case it's AI automation uploaded documents for me it's going to be whatever you whatever your schema is that you've created for your for your collection and I already have it listed as default you're going to want to do the same it says duplicate index name because I already have one but we're going to need to copy the Json from the mongodb tutorial here that they have and I'll put this link in the chat for you but it's also available on their website and basically all this is doing is it's creating an index on the field and we need to change this to embedding by the way because this needs to match what we have in our schema or those two things need to match and so we're basically creating that index on embedding Dimensions 1536 which is the number of dimensions of the open AI embedded grades similarity cosine and if you hover over it it'll tell you the three different options that you can use again euclidean cosine and Dot product and k k nearest neighbor Vector as the type and after you have that set up and again I'll post the link to this in the chat in the description you're going to want to click next and yeah and once you've clicked next it'll take a couple of minutes to for the index to be created but it should be good from there I'm not going to create this because I already have one set up like that okay so so once you've created the index you're ready you're pretty much ready and I'm going to walk you through the code how to create the embedding and then retrieve it so let's go to embedding here and so I've set up an endpoint already and I'm going to walk you through the code of how this works and how you would do this in your code base but basically we're going to need to we're going to need to give some text to create an embedding on and in this case what I'm doing is I want to give my AI knowledge on current events specifically what's happening with Twitter so chat cpt's cut update is 2021 so we want to give it current information so that's what I'm going to do I'm going to give information about Elon Musk changing the name to Twitter to X which happened very recently usually last month from this recording so what I'm doing here is I'm scraping this website and getting all the text from it and the scraper is located here it's just a regular Puppeteer scraper you can get this code from T all this does is it scrapes a web page and gets the text and returns it and then with the return text I'm calling open AI to create an embedding based on the text and that create embedding function is here I'm just using open ai's node.js wrapper and creating the embedding here open AI create embedding I'm using the Ada model which you should too it's their best performing one at the moment and then I'm just passing in the text from the web page as the input and here I'm getting the embedding from the response and I'm just returning it so that's all that is here it's just calling open AI to create an embedding with the Ada model based on the scraped text and so once we get the embedding what we're going to want to do is upload it to mongodb we're going to upload the raw text as well as the embedding and we're going to save it and once we say that we should get a response to that everything's been the document has been uploaded successfully so let's run this function and see what happens so let's just First grab this URL and Postman already have this route set up API embedding document and I'll put this here and let's click Send and so it's going to open up the web browser it's going to scrape The Hollywood Reporter it's going to take the text create an embedding with the text and then put it in mongodb okay document uploads successfully you can see that this is all the text in the description that it's scraped and we can scroll down and this is the embedding that was created from the text and we can see that this was successful by going to mongodb and going to collections for me and then going to upload a documents and then it will be here I believe yeah Elon Musk says Twitter and here we have the embedding which is array of floating Point numbers of length 1536 okay that's that's awesome so that was creating the embedding and then uploading it now what we want to do is we want to ask a question for it to retrieve the closest embedding based on the cosine and then use that to give us a response so let me walk you through that here this is the query embedding route that I've set up what it takes in a question you could ask and then based on the question it turns that question which is text into an embedding using open AI That's the same create embedding function that we looked at earlier it just turns my question that's a text and turns into a betting and here is sort of the difficult part I know the difficult part about I'll post this as well so you can copy and paste it but what this is doing is it is taking our embedding that we just created and looking in the database for other bettings that we have and comparing them and returning the closest ones so first is going through mongodb we have this Pipeline and it's searching for the nearest neighbors if it's going to return the five closest ones in the path embedding and this is going to need to be changed to what you have as your path like if this was embedding two you need to change it to embedding two or whatever so let me just go back and then we're going to return the description and the score the score is a measure of how closely related the embeddings are the higher the better if it's like point if it's like above 0.9 that's that's really good that's a good match like the highest you can get is one that means it's the perfect exact match and we're returning these documents here these are the five similar documents we're going to console log it and here I want to get the closest document possible the one with the highest score so that's what I'm doing here console logging it and then here I'm turning this into a prompt to pass into open AI so I'm saying based on this context and the text from the embedding with the highest score answer my question so I'm going to run this and it should make a little bit more sense to you and this is just calling open AI with the prompt here so let me run this let's go to console.log some things and we'll walk through the console.logs to make it clear so we have this set up here and the query we're asking is what did Elon Musk change the name of Twitter to again church if you're just not going to have the answer to this because it's cut off date is 2021 so let's click Send actually let's make sure that the server is running which it should be of course yeah okay so this looks sent okay Elon must change the name of Twitter to X that's perfect so let's see how it got that so first similar documents it's going through mongodb it's comparing the vector embeddings and it's returning the five closest ones and we can see this is the first result that was returned and it's a lot of text it's gonna have the highest score 0.92 now the rest of them though are unrelated it's still going to return the five closest one so there's four underneath it that are unrelated still going to return those two so we don't really want those and you could tell the scores are lower for these 0.86 0.862 so that's all stuff that we don't need right but then here highest score dock which is basically the one with the highest score this is what we want right and so all of this text here we're passing in as context here so it's like based on this context all the information scraped then what to do you want to change the what the Elon must change the name of Twitter to and then the answer so yeah hopefully that makes sense it's a little bit complicated I know but let me know if you have any questions in the comments below I'm happy to answer them and in terms of pricing we should talk about that too mongodb has a free tier with the atlas so it's good to just get up and running let me see if I had the pricing page pulled up yeah as a free tier I mean I think this will get you pretty far to see if you know anybody wants to buy your product to scale though mongodb is like a bit expensive like the cheapest dedicated instance that was 57 per month and then you have a serverless option but you know it's got a bunch of it's got a bunch of hidden pricing kind of so you're going to want to look at that so basically in conclusion if you want to just set up one database that also has your vector embeddings it's like super simple to set up and you can get started for free I would recommend this I think it's very very convenient the other option is PG Vector with postgres that's also pretty convenient but if you have a nosql database you want to stick with nosql I think this is the way to go unless if you're scaling then you might want to look at probably dedicated databases again let me know if you have any questions let me know in the comments what you think and yeah happy to happy to help thanks bye
Info
Channel: Tosh Velaga
Views: 4,977
Rating: undefined out of 5
Keywords:
Id: yMdEsZOBJhI
Channel Id: undefined
Length: 14min 13sec (853 seconds)
Published: Wed Aug 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.