Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Coding Querying

00:00 In the previous lesson, you created your index. The next stage of your project is querying. This is where you prompt the model and then get an answer. You’re going to need some sort of model to do that, and you’re going to need a query engine in this particular case.

00:17 Now, to make sure that the query engine has access to your policy document, you’ll need to somehow link this query engine to the index that you just created.

00:27 And this, my friend, leads us to a line of code that, in my opinion, is a thing of beauty. Because built into the index object is a method called as_query_engine(), and that takes care of everything.

00:42 So you don’t need to worry about calling an LLM API and then feeding the index into that somehow. No, there is just this line of code. On line 14, if you type query_engine equals index. and then as_query_engine, and then don’t forget the parentheses.

01:06 So you call the as_query_engine() method on the index. So the query engine, by definition, understands the context that was created in the index, and that, I think, is very clever.

01:20 Okay, so the next thing then is to request user input. All that we’re going to do is just create a simple prompt. I’m just going to copy-paste that just to save some time. So on line 17,

01:32 user_input equals input(), and then a prompt which is “Ask a question about our company policy.” The final step then is to use the query engine. You’re going to capture a response.

01:46 So on line 20, type response.

01:50 And from the query engine,

01:53 call the query() method. So .query, open parentheses, and then enter the user input here. So this is how you feed the user input into the query engine.

02:05 Now here is a thing that confused me, to be honest. So the query engine will use the user input and the context. That’s the idea. So it will have extra context such that it can interpret or provide an answer to what the user is asking.

02:20 But all I’m feeding it here is the user input. So how does my query engine, how does it know the context? Well, this is the beauty, as I explained before. This is what’s happening in line 14.

02:34 The query engine is created on the index, if you like. So it is created by calling a method as_query_engine() on the index. So therefore, the query engine, by definition, will know the context.

02:49 Okay, so the last thing to then do is to just print the response.

02:55 And there you have it. So that’s you done coding for now. In the next lesson, we’re just going to play around with this code a little bit, ask it some questions, and see what it comes back with.

Become a Member to join the conversation.