From the course: Vector Databases in Practice: Deep Dive

Recommendations with RAG

From the course: Vector Databases in Practice: Deep Dive

Recommendations with RAG

- [Instructor] Okay, at last, we've come to what, at least for me, might be the most exciting feature of this app. We're going to add a recommendation feature to this app using retrieval augmented generation or RAG. Here's a reminder of our requirements. The app's going to recommend movies to watch to our user based on two inputs, which are already shown here. One is the search criteria or the kind of movie that they would want to watch, and the other is some context or occasion. So what's needed then is for us to formulate a query in our code. Firstly, what collection should the generative query be based on? We could use the movie or the synopsis collection. For me, I think it'll be better to base it on the synopsis. Each synopsis contains richer details of the movie so it'll make more sense in my view for these recommendations to be based on those. And then what search type do we use? Well, I guess the user could type in anything in their query. Could be something general, like a genre, but it also could be something like a specific title. So I would prefer a robust search method that can search by meaning, but also respect these exact matches when they occur. And to me, that sounds like a job for a hybrid search. So let's start to construct our query. Here's our hybrid search on synopsis, and the query is just passed on from the user input. And then how would we prompt the model? First, we want an overall recommendation, so that's a job for our group task prompt. And as for the exact prompt, well, it's not an exact science, but I find that generally the more precise and specific prompts work well. Here's my attempt at it. The users looking to watch something types of movies for some occasion. Provide a movie recommendation based on the provided movie synopsis. Note that this is an F string. So these names in braces, search string and occasion will be replaced by whatever the user inputs into our interface. We'll have the large language model base the answer on three objects. And since this is a query on synopsis, let's also retrieve the movie data. So this is a reverse of how we've used references before where we've fetched synopsis based on movie data. This time we'll use the reference property for movie to fetch details of the reference movie object from the synopsis. So all that's left to do is to pass the results back to the user. Let's start with the recommendation. So you'll recall this was a group task, meaning there will be one generated answer for all the objects combined. So what we can do is to display the generated text by fetching it as an attribute like so. And then we can get details of each movie, like we've done before. We'll access the objects attribute to get the list of movies, and from there we'll get rid of the placeholder data. And here's a movie title. Remember that this is again, fetched via the reference so we'll dig into the reference object and get the property of the movie object within the reference and the ID. And let's get the movie description as well. And they have the same variable names as before, so we don't need to change how they are displayed on the screen. So if we save this and refresh our app, we should have a fully functioning web app that's connected to a vector database. Let's try something. Here's a recommendation for a thriller that's for any occasion. What if I change the query to look for a thriller that might be suitable for watching with children? Look at that. It's providing two different recommendations based on the context, even though it's actually grabbing the same objects from the database. In other words, our app can now find relevant parts of your data and process it using AI or a language model, taking into account specific inputs from our user based on their needs. And this is only a relatively simple application. Think about the possibilities for your own complex data and needs. As we've mentioned before, this transformative capability is really exciting. As long as you have the right data, you can find them with a vector database and then transform it using these AI models. In the next chapter, let's talk a little bit more about exactly that, more complex real world data and some of the techniques that deal with them as you import them into a vector database.

Contents