Creating the Cortex Search service
1. Creating the Cortex Search service
Welcome back. In the last video, you got everything set up in your notebooks and are now ready to create your own Cortex search service. This video will be very hands-on. Take a moment and pause the video if you need to log back into your Snowflake account and get your notebook open. Now that we have our data ready, we can create the Cortex search service. Here, I'm using the fully qualified name for clarity, but this is not strictly necessary. We'll point the search service to the table we created in the last video, storing our chunks. You'll see this in the first part of the SQL query and in the select query at the end. You'll also set the warehouse and the target lag, and the target lag sets the refresh schedule and tells the service how often to check and update the index. Last, we'll choose the embedding model, Snowflake Arctic Embed Large version 2.0. The embedding model is a really important ingredient to performance search, and the Arctic Embed 2.0 model is benchmarked to perform well for RAG use cases. However, this is another good opportunity for experimentation. Try this on your own with different embedding models and see how the performance changes. Once that's all done, we run the command and the Cortex search service will be built. Remember that when creating a search service, the search index is also created. This can result in the search service creation statement taking a long time to complete on larger datasets, so keep an eye on this. Once we have our Cortex search service created, we can move on to using it. In this next step, you'll create the Cortex search service, use it, and check to see how it performs. Here, we'll import some additional libraries we'll need. We'll import OS. From Snowflake core, you'll import root. From typing, you'll import the list. And from Snowpark, you import session. Then we'll create our Python class to set up our retriever. This is not strictly necessary, but it's a good way to keep things organized. Now you define the class, CortexSearchRetriever, and add two parameters for the user to set, their Snowpark session and the limit to retrieve. The class will just have one method, retrieve. In retrieve, we'll initialize the search service with the database, schema, and service name. We're fixing these, but you might want to let these be dynamic. Your call. Then you can call the service with .search using the query, column name, and limit to retrieve. And then we'll use list comprehension to return a list of chunks. In the next cell, we'll put this in action. We initialize the retriever with the session and limit to retrieve. Then we'll use it with a query. Well done. We got a lot built in this video. In this video, we covered how to create and call a CortexSearchService, and you continued your hands-on practice. This is how we'll retrieve relevant chunks from our knowledge base, our PDFs, and let an LLM answer questions using this information. So now we'll move on to the generation step of RAG. I'll see you there.2. Let's practice!
Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.