From the course: Advanced Python Projects: Build AI Applications

Introduction to API communication with Python - Python Tutorial

From the course: Advanced Python Projects: Build AI Applications

Introduction to API communication with Python

- [Instructor] All right. Now we're going to build the frontend for the AI application that we built in the previous chapter. In the last lesson, we saw how to build the backend and how it worked. Now, before we get coding, I'll give you a quick sneak peek of how the frontend looks. So here's how it looks. Here you have a browse files button, click on that, and then now we're going to load the same employee manual from a fictitious company that we created to test our application. Click open, excellent, it's uploaded. Now let's ask questions. "Hi, what is the employee leave policy?" Excellent, so it says, "Although it doesn't specifically provide employee leave policy, it does mention if an employee requires an extended absence due to medical or personal reasons, they should consult HR." So that's exactly what we wanted it to do. We want it to be able to upload a document or A PDF and be able to ask questions regarding the document and obtain intelligent answers. So I specifically asked this question about employee leave policy because I knew that there is no employee leave policy that was explicitly mentioned in the document. So I wanted to see how the chat bot would react to a question like this, which wasn't directly in the document, and it did a pretty good job of telling me that there was no specific employee leave policy, but at the same time, provided additional guidance on how to go about obtaining a leave. There we go. That's a quick demo of our frontend. Great, now let's close our windows and go back to our code file. So first we're going to say PIP install Streamlit. After Streamlit is installed, we're going to import request and JSON modules. These are commonly used for making HTTP request and handling JSON data. Next, go to the ports tab, and over here port, 8000 contains our backend. So expand this and you'll see the entire address. Click on the copy local address option over here, and then paste it over in the backend URL. Make sure that we don't have a slash after .dev, if you have a slash, make sure you delete it. So what we're going to do is we're going to send a user input to a chat API and return the response. At the end we'll get a tuple containing the response answer and the updated session_id. So let's see. Now we're going to create the API endpoint URL for the chat by appending the /chat to the backend URL. After that, we print the input parameters for debugging purposes. Then we're going to prepare the payload for API requests. Here we check if the session ID is provided. If not, we create a payload, JSON string, with only user_input and data_source. If session_id is provided, we include it in the payload. Next, we define the headers for the API request, specifying that the client accept JSON and that the content type of the request is JSON. Next, we use the request module to make a post request to the chat API with the specified URL header and payload. After that, we print the JSON response from the API for debugging purposes. Lastly, we check off the HTTP status code of the responses 200, which means it's successful. If yes, return the response answer and the update session_id from the JSON response. And to summarize, this code essentially defines a function chat to communicate with the chat API, sending the user input and the data, and receiving a response, along with the updated session identifier. We also add debugging statements to print input parameters and API responses for troubleshooting.

Contents