From the course: OpenAI API for Python Developers

Run chains: Knowledge retrieval and content generation

From the course: OpenAI API for Python Developers

Run chains: Knowledge retrieval and content generation

- [Instructor] Previously, we have successfully loaded and split documents into chunks and also created embeddings in order to load them into a vector store, a chrome a vector store. So we have also seen an example together as to how to search by similarity with this example. So next we want to be able to also allow the language models to retrieve relevant information. So you're going to see here, back up that now we have a more detailed and extensive template in order to give specific instructions to the custom AI assistant. So here we're going to say that you will only answer questions based on the knowledge that you are trained on, and you're going to see this extra input variable. And this is to say, to indicate that we're going to give context to the language model. So that's going to be based on the question and the context, the knowledge retrieved that the language model is going to be able to provide with an extended response. So let's go back here. You can see that we've got here this template. So we're going to replace here line 46 with templates. All right, so we're going to train this custom AI assistant with this system message. And in the human message prompt, you're going to see that you've got this input variables. So we're going to provide a question, a user input, a user query, and also give context. And this is going to correspond to the question that we ask. So that's going to be here, the user query. So what we want is to see an example by chaining all these actions. So just like we did in the first place. And also allow, so this time we're going to add another component. We want to allow the language model to retrieve custom information. So let's remove that. And right here you've got questions. So we're going to add another piece. This will be context and actually that's going to correspond to this. And that's going to correspond to the relevant information, which is returned after research by similarity right here. So we made a test earlier. So whatever is returned as a response after submitting the user query, it is going to be passed as a value for the context. And for the user input, it's going to be actually user query. And here it's going to be chat from template. So let me replace that. And for this one, I'm going to call it chat model. You're going to find it up here. So let me replace that to chat model. Here we go. So let's try that and we're going to make just another change. Actually right here, we're going to just return the results and we're going to save these to context. (keyboard clicks) Okay. And then this time, let's replace with this frequently asked questions from the ABC Shoes Company, which is a completely fictitious company. So I'm going to replace this document with FAQ, and that is ABC. Alright, so let's try that. We're going to run this. So first we're going to have the chat prompt template, which is going to take those two values as an input. So the user query and the context. Then the output, meaning the list of messages is going to be sent, submitted to the chat model as an input. And so the output of the language model is going to be then parsed by using this component, string parser. In order to get the results, we're going to print the message, which is, that corresponds to the response of the chatbot. So let's try that. All right, so now we can read the response from the language model, which is that we do ship to Europe. International shipping costs vary by destination starting at $15. So we're going to see if this corresponds to what we have here on the custom knowledge that we have provided to here. So let's try another question. Maybe if we ask shipping cost. Okay, here we go. So now you could find the information, it's about $15, but you could see that actually it was much faster than me. I couldn't retrieve it. So imagine if you have much bigger documents. We're going to try another questions about the return policy. So I'm going to ask that I want to return, or let me go back. Actually I need to replace it right here because otherwise it's going to be the same answer. Let me clear that. So I'm going to ask another question about, I want to return a pair of shoes. We're going to see what is the outputs. Alright, so now it says that, sure, I can assist you with that. ABC Shoes accept returns within 30 days of purchase. And it's going to continue with more detailed information. And if we go back, you're going to see that you're going to have the exact same information which is provided by this document. Excellent. So we've been able to successfully add this component, which is to give context to the language model in order to allow the AI assistant to retrieve custom knowledge. And that would be nice to interact with this custom AI assistant by using a user interface. So we're going to do that next. We're going to create a new user interface for this custom AI assistant with stream leads.

Contents