From the course: Level up LLM applications development with LangChain and OpenAI
Querying as a retriever
From the course: Level up LLM applications development with LangChain and OpenAI
Querying as a retriever
- [Instructor] Now we want to see how to query the vector store as a retriever. So we're going to see how to do it with this example. So we're going to compute this line, very simple. We can just invoke here this method as retriever directly on the vector store. And so the next step will be to return a list of documents. So we're going to call directly on the retriever, this method, which is invoke, and also pass as a parameter, the same query. And so we can also use the same optional parameter, which is top_k=1, and which will allow to return the top most relevant result based on a given query. So it's true that here the data source is very small, but you could imagine to have a large volume of data sets and it can be interesting to here specify a limited number of the top most results to return based on the relevancy. And so next we're going to return the first of the list and allow to read the page content only. Alright, so we're going to try that with Python main.py. Alright, perfect. So now we can see that we have the same answer for both queries. And so next what we could do is also define a chain. So we're going to use the LCL syntax to compose a chain and combine components. And that's going to be like an extension of the basic example that we had seen before. And we're going to add one additional components. here in order to provide with a context to give additional information to the language model. So let's copy these lines. We're going to add it to our project. Let's go back. So here, so what you need to understand is that we're going to use "context" and "question". If you go back up, this is where we define the templates. And here you notice that we use two input variables, "context" and "question". And they're like placeholders, that's going to be replaced with actual content in order to then structure and define a prompt. And we're going to use that to send instructions to the language model. First we're going to define the prompt, and then send the instructions to the next component, which is the model. And finally allow to convert the generated output into a string format. So we're going to use the same example, and notice here that we are using this class, which is RunnablePassthrough, which will allow, as the name suggests to pass data through. We're going to see how it works in a moment. We also use string OutputParser, which is an output parser, that allow to parse and then format the outputs by the previous component, in that case from the language model. So let's just make sure that we add these two lines to our projects. I'm going to add also Open AI to the scope, because I will need to initialize the language model. Actually, let's name it model, just like the example. And we're going to make a few adjustments. Here, we want to pass the retriever as a value for the context, and this will correspond to the retrieved documents. After we're going to use RunnablePassthrough, to allow to pass data through. We're going to see how it works in a moment. Then we're going to define a prompt to send instructions to the model, like this. And finally generate an output in a string format. Well, let's see it in action. We're going to run and invoke the chain. So that's going to be with invoke. And then we're going to pass query as an input. And here the thing is how it works, line 46 is that we pass query that corresponds to this question. We pass query to the method invoke, and that's going to be passed through thanks to this class. So this is very convenient, because we're going to be able to get the value of the query by using this class, which is passed through with this class. So then we're going to allow to put that in a response and print it. And I'm just going to put these two lines in a comment, line 33 and line 27, because I just want to see the results of this chain after it is invoked. Now let's try that. Alright, and now we can see the answer from the language model, which is that Harrison worked at Kensho, which is the results of running this chain. So what we've done is to use the context, which was provided by the retriever, that corresponds to the retrieve documents. We used this class to get the user query. So finally what we could do is to structure the prompts with context and questions. So that was an augmented prompt that we sent as instructions to the model. And finally, return the output in a string format. So this was a basic example, with the goal to augment the query prompt, with specific and relevant documents, provided as a context, thanks to the retriever.