In this lesson, you'll create a simple chat interface to interact with the Mistral models in chat with an external document. Let's try it out in this lab. Let's create two simple chat interfaces for interacting with the models and chatting with a document. The first one is a basic chat UI where you can send a message. For example. Write an email to schedule an appointment with my CS professor to discuss research opportunities. By the way, right now I just want to show you the end result. We'll walk you through the code in detail in a little bit. Okay, now you can see this detailed email requesting for appointment to discuss research opportunities to a professor. The second chat bot is where we can upload a document, for example, The Batch newsletter, and ask the questions about this document. What are the ways that I can reduce emissions in agriculture? Based on The Batch newsletter, we get a good output. In this lesson, we'll take a look at how we can create these two chat interfaces so that you can create your own chat apps with Mistral Models. Okay, let's get started. Let's first import needed packages. The new package we have here is panel, which is an open source data exploration and web app framework. We import panel as pn. We need to run this line pn dot extension to load customized JavaScript and CSS extensions for panel applications. Here we have a Mistral function we have seen before. Note that we have these two additional arguments user and chat interface. That's not using the function, but we need it to include here for the UI. Now let's simply run these four lines of code and we get this chat interface. We can interact with the model. Pretty cool right? Okay. Let's take a closer look at the code. We define a chat interface widget with pn.chat.ChatInterface. This widget handles all the UI and the logic of the chatbot. Then we define how the system responds in the callback function, which is the Mistral function we defined above. That we can define the callback username as Mistral, to indicate responses from the Mistral model. Right here. As you can see here, with only a few lines of code, you can get a similar chat interface to interact with our model. Next, let's see how we can create a chat interface to chat with an external document. We have seen this code before. We get a newsletter from The Batch. We save the text of the newsletter as the TXT file. Okay, now let's copy and paste some the RAG code just for a short recap. First of all, in the prompt we have the context information which is the retrieved text chunks based on the user query. And then we have the user query. And we're trying to get an answer. We have a function to get the text embeddings using this tools. embedding model. Another function to run the Mistral. model. We recommend using this to a large for the best performance for RAG tasks. The main function is this answer question function. We start with the text we get from the file we upload. We'll talk about the file input widget in a little bit. We split the document into chunks. Load the text chunks into a vector database, creating beddings for a question. Retrieve similar chunks from the vector database and finally generate response based on the retrieved relevant text chunks. And here are the code for the chat interface. In this chat interface, we first need to define a file input widget for us to upload files. Let's take a look at this widget file input, where we can choose a file and upload a file like that. Then we need to define the chat interface widget, where we define how the system responds in the callback function, which is the answer question function we just defined. We can then arrange the file input widget in the header. As you can see here, the first thing we see is the file input widget with some text descriptions. We can optionally start the chat interface with a system message, so that users know what to expect from this chatbot. As you can see, here is the system message we defined. Now let's give it another try. We upload the batch.txt file. We ask a question about this file. And we get an answer from Mistral Large. So that's the code for this simple chat UI. As an exercise, feel free to try different articles from The Batch, by changing the URL here, and then try to upload a different article and ask questions about this article and see how Mistral Large response. In the next lesson, I will conclude the course and share some thoughts about the next steps. See you in the next lesson.