From the course: Building a Personalized Chatbot with OpenAI and LangChain
Building the LangChain architecture
From the course: Building a Personalized Chatbot with OpenAI and LangChain
Building the LangChain architecture
- [Instructor] So now that we know the basic fundamental OpenAI API architecture, we can start to use LangChain, and build our fully fledged chat bot. So let's start off by, first of all, importing the modules that we'll need. We'll need the langchain_openai module, as well as the langchain module. OpenAI is the class that will be required in order to initialize the OpenAI language model, and PromptTemplate is used for creating a template for our prompt. After that, let's go ahead and open up the website_text.txt file, which is the file that has all of the extracted text from our landonhotel.com website. Then we read all of these contents into the prompt variable. Now, we create our full prompt. This is the hotel assistant template variable, and first of all, we're given all of the text from our website_text file, and then we also give the chat bot some guidelines they have to stick to. In this case, I've said that it is the hotel manager of Landon Hotel, and I've given it a name. After that, I've also said that any queries that are not related to the Landon Hotel, it can simply ignore. So now, we can go ahead and use the PromptTemplate class, and create our whole object. The input variables parameter specifies that the template expects one input and one input only, and this variable is named question, which will be the user's query. Also the template that we're using for our prompt will be the hotel assistant template, which includes both sides of our prompt. After that, we go ahead and initialize the OpenAI language model. In my case, I'll be using the GPT 3.5 turbo instruct model. However, you can use any other for your application. I've also set the temperature parameter to zero, which means that the model will generate more deterministic and conservative responses instead of trying to be creative. After that, we go ahead and create the llm chain, and this is the llm chain object, which is created by combining the hotel assistant prompt template and the llm, which is our OpenAI language model. The vertical bar operator is used to create the chain, and it allows the prompt template to be passed onto the language model. After that, we go ahead and create a new function. This function defines the query_llm, and this takes in the question as input, and it prints out the response that is generated by our llm chain. The invoke method is called on by the llm chain with a dictionary containing the user's question. After that, we add in a loop based approach in order to allow the user to ask as many questions as they want to the chat bot. So now, let's have a go at running this, and see how it all works. So first of all, I'll go ahead and say hi. We got a response, hello, welcome to Landon Hotel, how can I assist you? I'll go ahead and ask them what their name is. As you can see, it says, I am Mr. Landon, the hotel manager of Landon Hotel, and then we can go ahead and ask some more specific questions that would've been included in our text file. For example, we could say, where is the hotel? It says it's located in the heart of London's West end neighborhood. We can ask, what time do I check in? And the response is 3:00 PM, which if we have a look at the website, matches up with what it says over there. So as you can see, the chat bot is correctly replying to our answers based on all the context that we've given it, but now what happens if we ask it a question where it is not in the context? For example, what if I say, what is LangChain? And as you can see, we get a response, I can't assist you with that, sorry. So as you can see, we have sort of restricted our chat bot to only answer questions relating to the knowledge that we have given it, and not answer questions from outside of that knowledge. That was it for this video, that's basically our chat bot made. Now, the only thing that is left to do is to create the user interface and the front end of the chat bot to make it into a simple web application.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.