LLMOPS 03: Building API with FastAPI & Swagger Testing | API Development in LLMOPS Project

Sunny Savita · Intermediate ·☁️ DevOps & Cloud ·9mo ago

Key Takeaways

Builds a production-ready GenAI Document Chat System using FastAPI, Swagger, and LLMOPS

Full Transcript

So welcome everyone. Hope you guys are doing absolutely fine. In the previous video we basically reached till the uh working of our main modules like document chatting modules and uh we go through the whole rag pipeline and how to implement it in a modular fashion. But in this video we are going to do some uh interesting stuff. We are going to connect the working modules the chat ingesttor the retriever the data generation and all to a fast API to a API so that we can get it into a front end. Uh so this is a very this is also a very crucial step for a productions application because API is necessary and it should be uh it is a mandatory layer to have in every application. Hope you guys are excited to learn about how to integrate fast API in an end toend project of an LLM of application and let's move on to the today's session agenda. Uh we are going to cover these four things in in this session. The first is get we are we are going to get the overview of fast API how fast API works why it is uh why it is used and what are the syntax and what are the basic working. Uh the second is the overview of pyic. What is pyntic and why we use pyntic in fast API? This is some common questions and we are going to uh debunk it and the third is we are going to finally implement the fast in our project. As you guys will know the how what is fast and pyic. You guys will be ready that how to integrate it into a project and then we are going to just test the integration using the swagger UI with the fast API provides by default and which will going to interesting for you. Uh so let's get started uh with the first thing. Let's see uh what is the overview of fast API. Let's see uh from the top that what is a fast API. So to understand what is fast API uh so let's go over to the uh fast API documentation and let's understand it in a very quick manner. So yeah here so so here is the documentation of fast API and we are just going to get a overview using the examples they have given. So here you can see that here is a quick example that how to use fast API. So what they have done they basically imported fast API from the module fast API itself dependency basically package basically and they are initializing the fast API class they get and storing it in the app variable and just using the app variable to perform some AP operations. So let's understand what's actually happening here and how it will work in a uh application uh using a flow which I have created for you. It will be it is a good flow and you will get a clear understanding that how uh it will work. So let's go uh into the flow. So as you guys see in the documentation this was the code uh which was being presented like uh there was a this fast API imported and uh we have initialized it and saved it uh into a app name variable. Now what we are doing we are basically using this decorator uh by using this decorator we are writing header it app and we are using that get. Uh so in API there is different different methods uh like get, post and others also. For get means we are going to get something uh from the server uh at the particular location at a particular route. Here this is the home route. This is the base route. Okay. And for post what is post? Post means sending some data from the user to the server at a particular route. So if there was written like app.post then uh this will be used to send some data to the server. But here get is written so we get some response from the server. Okay. Uh so let's see how this code is working in fast API. So for example you are a user and you send a request that okay uh I want I have sent a user at the home route at the SL route. So what fast API will see okay someone sends a request at the fast at the okay someone send a request at the home route. Uh so what I need to do I will run this root function because it is written below it. It is written just below it below the decorator. So fast API will automatically run this function. Why it will run this function? because a user has specifically called at the home route. That's why fast API will run this function. The uh fast API will run this root function and then this root function will run and what root function is running and returning the root function is returning a JSON object with a key message and some value of it. So this root function will return this JSON object to the fast API uh app and the fast API app will get this uh returned JSON and it will give us a API response with the value. So this is how the fast API works in a very simple manner. This can be complex but the overall but will be safe. Someone the user will call uh the user will request at a particular route. Uh the underlying function present inside the route will run by the fast API and the returning value will be sent to the response uh of the request which was the calculation happens in the function. So this is the basic overview a simple overview which you will need to understand to integrate fast into a project. Uh so the next thing in our session agenda is getting the overview of identic. So what is a identic? Basically identic is basically a way of defining uh the types of the data you are using like uh you can easily define uh the types or the you can easily apply checks that okay my data should be in this format and it should follow this structure and if uh somewhere in the application this structure is not getting followed uh I will get some validation errors and it also helps us in getting the suggestions like uh writing the suggestions and all. So you will get a good idea after I tell you uh in the code also. So this is a quick understanding. Now let's go to the pying documentation also and see how it works. So uh here is the pyic documentation and why to use pyic why they are telling to use pyic because pyic validates against is uh pentic used to validates basically schema like you we can basically validate the schema of the data we are using. Uh so why to use pyic with fast api. Uh so let's see that I will explain you with this uh simple example uh that if we are using without parenting this is a simple code which we have already looked uh so what is happening here uh the user is creating a post request we are basically configuring our code for a post request for the item route basically and this if a user get creates a post request at autom item route this function will run but as you can see this function is accepting an argument which is item and we just we defined that this item will be a dictionary Okay. And we are returning this item. But here there is no structure uh there is no representation that what is the underlying structure of the dictionary. What if I want to define that okay this item should have this two keys for example the title and the description. I want that the uh create item uh should have should accept item item JSON dictionary but it should must it must have these two keys like the title and the description. How to do it? We don't have the way to do it. uh so but with pyic we can do that. So let's see how to do it. So firstly for using pentic uh we need to import this base model. What is the base model? Basically base model is the uh way that they have given us to use the pentic functions and we how to use this base model how to use pentic with it. So you to use pentic and to define the uh schema types we need to create a class with a type name. So here we have created a class with a type item. So this is a type of this. So here we are defining a pyic type whose name is item and we are inheriting this class with the base model. So this inheritance with the base models tells that okay this class is a pentic type class and it will use to define uh the data structure using pentic. So this is the syntax and here we write basically that name string price float and is offered boole. So why it is written that? So it is basically saying that if uh a data entity is uh given a pentic type item then it must have this item attribute item uh key which is which will be of string type. It must have a price uh key which will be of float type and it must have a is offer key which will be of boolean type and why I have written equal to false. This is a default value. Like for example, if someone uh has mentioned his offer but uh has not given a default uh value, so the default value will be placed uh here as false. So we can do the same for other uh keys also here like we can write like price uh float and equal to 100. So the default value of the price will be 100. So like that. So I hope you understand that how to define pyic and uh how to uh define the pentic model and what is base model. But how to use it with fast API? So here is a very simple example like previous without pentic what we are doing we are just defining that okay this create item will get a argument item which will be a dictionary type but we have we defined that this will be a dictionary type but with pentic we have tell that okay this create item function will get a argument which is called item but it will be of type item and here capital I is here item so this is a pentic type pyntic model which we are enforcing on this item argument so this item argument will follow this uh item pentic model. So this item argument will mandatorily have this name, price and is offer. And if we do not pass uh the item uh argument in this format uh the pyic will return us validation error. So this is how the how this is the power of pyic that we can uh define uh the uh type checking at that much depth. Okay. So this is the power and this is how we use it with fast API. So this is the how we use it in the function. But for what if you want a structured output from the fast API you want to define. So this is how you structure a route response like uh if someone like here we see in the fast API that there is a response right there's a response but we want to add a type checking here that this response must have this four keys and the key type will be this. So how to do it? So pyic also allows us to do it and fast also integrate the pent for it. So this is pretty simple. So how to do it? For example, here we have created a pyic model which is user. It's all inheriting with base model. That's how we know that it is pyic model. And then it must have a id uh key and it uh it will be a its value will be a integer and it must have a name key. It it value will be of string type and the default value will be johnu and it must have a friend key and it uh it will be it value will be of list and of integer a list of inteious. If you pass if in the friends we pass a list of string the identity will give us a validation error. So this is the power of identity like at this at this step we can define the uh schema uh schema type using the identity and we can validate it. So uh and the default value is empty. So how to use it in the response we are just uh we have to just define that response model equals to user equals to user means this user identic model. So when we get the response for uh what is happening here we are just configuring that if someone uh uses a get uses a get uh someone postes a get request at this route uh user/ this dynamic variable user id uh then this function will run and this must uh return a response return uh a JSON object which is uh which follows this uh user identic model and if this uh response from this function if this returning value from this function do not follow the pyic model which is uh enforced here we will get some validation error. So this is the power of pyic and this is how to use it uh for the basically function manipulation uh and type checking and for also responses like how to use pentic in the response of the fast API. So I hope you get what is pyic and this must pyic is okay for now to understand how to use pentic with fast api. Uh so let's move on to the main part of this session which is integration of the fast API in project. Now you know what is fast API, how it works, how to write the syntax and what is pyantic and why we use it. You are pretty ready you are pretty much ready to integrate fast API in our project. So before uh so let's move on to the ID uh of our project and let's understand first that where we are going to integrate fast API and what routes we are going to create. Okay. So let's go ahead and uh so this is the uh back back implementation like the implementation we did in the previous session. Uh we have coded the multi-doc chat. We have coded the multi-docad functionality uh in a modular fashion and we have done that. So now we are going to implement the fast API. The first thing we need to do is we need to install some dependencies like for fast API. Uh so for fast API we will need these three things. So for fast API we will need uh a fast API firstly which will be I will uh release the version here. I want to use this version and for some for the uh if you don't want any error in your running and if you don't want any uh messiness of package uh conflicts please use this exact same version which I'm using. Okay. So the last is Ginger template. So I will tell you why we are installing this uon and ginger template in a bit. Please be with me and understand. So this is the three packages additional we need to install to run fast API in a Python project. Uh so we have written this but we have not installed it. So in this project we are using uh UV. So we need to install it using UV. So let me quickly open my terminal and uh here the uh environment is already activated. But uh in UV the environment is handled by default. Uh so we do not need to activate it manually. We just need to uh write the code. Don't need to activate it. That is a good part of UV. So what is the command we need to write? We need to write UV add minus R requirements txt. And now the UV will see that okay this extra packages are required in this. So I will install them. So uh the UV has done that and now to sync it. So this uh now to sync it the requirements in our UV.lock lock uh we need to use this UVC and the uh additional packages will be synced in the inside the UV lock so that we can use it anywhere in the uh production environment okay and the production environment will be done fine now we have seen it now what the next step is we are we are ready with our dependencies and we have installed it all also now the second thing is we need to start coding uh so we need to do it inside this main py okay main py so the current code is that but firstly uh we need uh to import some variables. So the variables we need to import is firstly let's import this basic things here. Yeah. So this is the uh basic things where we are uh need to import first like we are importing annotation then path then dictionary then list from typing and we are importing OS also if we need to implement something. Now we need to import this fast related things. So what we are importing here fast API provides us this fast API uh class which we've initialized for the app variable I have told you this file is basically some for file processing this upload file will uh handle the upload files from the user this HTTP exception is required to send the uh responses uh HTTP exceptions like some errors we got so we send the HTTP exception this request is the is used to handle the user request okay and the other is the HTML responses we are going to use HTML client okay HTML as a front end. So this HTML responses is required because we are going to serve our HTML from this. And the other is static files. Why we need this static file? I will tell you in a bit. Uh the course middleware course middleware is basically to ensure that we do not get any cross origin errors like uh we will allow access fast API from uh from every origin and the other is ginger to template. I will tell you what why we need this ginger to template in static files in a bit. Please be with me. And the base model I have already told this is for to use to tell that okay this class is uh you this class is for defining pentic model. Okay. And now let's uh import some modules from our previous implementation. Okay. So we are importing this chat in which is from data injection which is for data injection. And we are importing this conversational rack which will be responsible for data retrieval and generation from using the and the human message and AI message will be from the lchain as we as you guys already know. And this document portal exception we are importing to raise some exceptions and trace back them. Uh so which all this implementation we have already seen in the previous session. So if you have any doubt that why am I importing this chat injust what is actually happening inside the chat injust. I have already told all these things in details in the previous session. So go watch the second part and then come here to understand okay uh and you will get a pretty clear understanding and how these things are will be working. And now let's move ahead. We have imported all the necessary things. Now let's do the first we are going to initialize the fast API here. Okay, fast API class we got fast API object we are initializing on which we are going to configure the routes uh code and functions and we are giving this fast API object a title of multi chat and we are using uh specifying a simple version. This can be uh this you can specify according to these two things. No hard coding required. Now we are allowing the course basically we are writing this code to ensure that uh this our fast API can be accessed from any origin. Okay, this is the uh code uh if you are integrating a front end with your fast API. So you should uh write this code. Okay, and uh now let's configure some things like for the static and template. So let me write a code here. Yeah. So now we have this static and template. And what is this static and what is this template here? Static is like if you have coded in front end or some uh JavaScript framework like React and all there is public thing right the public is you will get the file as it is and to use it. So static is similar like if you place any file in the static in static we generally place some uh CSS or HTML file or some front end related file. So uh in static folder in static directory uh the file which are going to be present it will be served as it is inside the fast API responses. Okay. So we use static for that. So uh we need to configure where we are want we where where the fast API will search for this static directory. So in the next line we are doing the same like we are defining this base directory using the path and getting the uh using this base directory and then we are just defining okay in this base directory uh just create a directory with the name of static and then uh use this directory for all the static files. Okay, what is template in fast API and what is this ginger to template and why we are using it. So our plan our overall plan is that we are going to create a HTML uh front end HTML CSS based front end but we we know that HTML is static but we what if you want to render some uh uh but what if you want to render some dynamic variables inside the static HTML. So this ginger to template helps us to do that. So the ginger to template is basically for that and you can study the syntax of that and we are going to also look the syntax of ginger to template in the upcoming session uh when we are going to implement the UI. Okay. Uh so this ginger to template u I hope you get what is this and what we are going we are just also defining this ginger to uh this template directory path in a template directory variable and then we are using this app of fast API and telling that okay mount this uh static mount this static route or the static files at this uh static directory we have configured. Okay. So this is how to configure the static files for uh in the fast API. Uh you just have to write that okay mount is static here and uh at this route and this static file will be provided by uh the fast API itself. Okay. And we are just initializing it and we are target we are telling that okay the directory of the static files will be the static directory and the name will be static. And the next thing is template. We are defining a template here which we are going to use in fast API to add dynamic variables inside the HTML to render the dynamic responses from the fast API inside the static HTML. So we have just used this ginger to template class to initialize the object and we are configuring that the ginger 2 template will present inside this template directory. So I hope this code is clear. Now let's move ahead and let's create a session. So why I am creating this session? This session will be related to the chat history which we have uh in the uh in the multi-dog chat. Why chat history is important? chat is important to reference our llm reference our chatbot from the previous conversation. So for that we are writing this code. We are defining this session uh variable which will be have this type of dictionary and it the dictionary will have key of type string and the value of time list and the list will be of dictionary. So this is how to read this and we are initializing with the empty dictionary. Okay. So I hope this uh session is clear. Okay. So this configuration for now is done. uh now we will create some uh helping routers uh so that we are now uh no now we will create some parenting models for our fast API uh code. So let's go ahead and create some identic models. So where we are going to create this identity models we have already defined this model directory and in this we here is the code space for writing identic models. So the first identic model I want to create is this uh upload response. upload responses. Uh the upload files we are going to create up upload the files we are going to upload will be uploaded inside this fast API and will be processed uh in uh through the back end but it will provided by the fast API. So we need to uh add a type check here using pent model. So this is upload response identity model and it will have a session ID mandatorily. It will have a index key mandatorily and it will be a boolean type and it will have a message uh key which will be of string type or it can be none also and the by default value is none. So why we are using this or operator here it just tell that this message key can have a value of string or it can be null. Okay, I can also write here list like here list is not important but I can write here. So what it will do? It will just tell uh the pent will understand that okay the uh underlying uh module the underlying code the underlying data struct data entity should have a message key but it can be of string type and it can be of list type like that. Okay. So, but here we are using black. Okay. So, this is the first identic model I want to use. The other two are simple. Uh this is the other two. So, wait a minute. Yeah. Yeah. So, this is two. The the first is chat request. Uh this is the parenting model. I want to type check when some when we are configuring the chat route and we are requesting uh the chat. Okay. We are requesting the uh chat input from the user and this is a chat response. This is the identicing model. You can also customize this. You can also add many more structure guidance here like structure keys here. But uh for for the simplicity for now I have just keep this. Okay. Basically this two keys and their the one key and their type. So these three identic models we are going to use in a fast integration. So now let's move on to create the fast API routes which are required to integrate uh the fast API with our project in a proper manner. Okay. So our pyic model uh is done. We have installed the dependencies. We have imported all the necessary variables. We have configured some uh directory related settings like we have configured the gener template. We have configured the static directory and we have created a session uh variable uh for our chat history of our chat model. Okay. So the other thing we have defined we have defined the identity models. Now what we are going to do we are going to define the routes at which route the user will going to uh send the request and we are going to send appropriate respon. So for understanding what routes we are going to create I have created a simple flowchart for you to get a good understanding. Uh so here is a flowchart like fast API route we are going to create this four routes. The first is health the home route and the upload route and the chat. So in the health health is basically a simple route which is uh run by fast API to check if the fast API is running good or not. This is for simple purpose of checking the health that okay the fast API is functional or not. The home route is used to we are using it to render our front. So how we are going to uh use to render our front end using that we are going to use the two things we have studied. the static and the ginger. We are going to see the details of it in the uh next video a bit more. Uh in this session we are going to just create a basic route home route which will serve a simple HTML but we will use the ginger ginger to template and all in the second uh uh uh next and the other two routes in which we are going to focus today is upload and chat. This upload route will going to process our data injection. When the user will uh send requests for this upload routes, the data injection will be performed. uh and this chat route will be responsible for performing data retrieval and data. So let's go one by one and let's implement this routes. Okay. So firstly we going to focus the health rout. So let's move on this main py and let's code this health route. So the health route code is pretty much simple. It will just say that okay uh let me yeah no I don't want that for now. So the health route is pretty much simple. We are just saying that okay if someone if a user request get request at this health route / health then we are going to run this function and this is a way that defining that this function will return a dictionary uh which will have a key of string and a value of so this function is just returning we are implementing a type check here uh of the return type and we are returning that okay status is okay so if this health route is run successfully we are get this uh return status okay so this is how we uh implement this health route okay Now what the next thing is we are going to render this front end at a home run. So for now we are just going to configure it using a simple uh file okay like HTML response I am configuring it. I'm saying okay dev home function I will run here and I will say request equals to request and uh this function will uh this get a request uh this get a request object uh of pass API and this will uh uh give a output in a HTML response uh object. Okay. So I will return a template here. I will return a template here. Um return a template. Yeah, template. And the template response should be HTML index and request will be request like uh all the so we are writing this code uh so that the fast API will know that okay if someone uh puts a get request at the home route uh we are going to uh response we are going to do the response in this HTML response way and we are going to get the response in HTML we are just telling that okay uh render this HTML CSS present inside this uh templates directory we have so what we have in index html which is the uh fast will render we don't have anything so we just write a simple uh HTML code here which says hello world for now and we will configure it a bit more in the next session okay uh so let's go and let's go back and what is this request equals to request we are just uh sending uh uh the access of this request variable we have here in this fast API inside this index html and we will access it using the ginga to template syntax in the next so for now this is done our home route is done and now let's move on to Next route which is upload. Uh so let's code that. Yeah. Here. So as you can see this upload route we are getting some problem that okay upload response is not different. So where we get this upload response? We get this upload response on the fast itself. So we need to uh so as you can see we are getting upload response is not defined. What was the upload response? Upload response was a pentic model which we have created and we are saying that okay response model which we uploaded upload response. So this uh output of the uh uh upload uh API upload route will be uh valid validated using this upload response identity model. So we just need to import it. So we need to import it using here we need to type okay from uh multi.sh domodels yes domodels domodels we need to import import upload response. So the upload response is done. Now we are getting getting this error at fast API file adapter. Uh so this is a uh basically a upload file processor. The user will upload a file like a raw file like PDF file, docx file. We need to process it in some way so that the fast API can use it and uh the uh use it and process in such a way so that we can process it process entire bracket. So for that we need to write uh the code inside the uh where we need to write we need to write inside the utils and document oper here we need to write. So we will just write the file adapter. Here we need to import this upload file uh from fast API itself. Uh so I will uh from this from fast API I will import upload. Yes. So we have this upload file. So what this fast API add file adapter is doing when we are when we initialize it it will create a self variable which is uh which contains the uploaded file itself and it will also create a name self variable which will create which will get the file name. Okay. And if the uploaded file do not have a file name, it will just use it by default file. Okay. And we are we also have a method here which is get buffer. This method will just get the uh file uh file we have the user has uploaded and get the and return the uh uploaded file into a buffer format like into a bytes format. As you can see we are returning a bytes here. Uh so the raw file the user has uploaded and we are getting the buffer like the bite raw bite code of the uploaded files using this get buffer meth. So let's see how to use it inside this. we have used inside the fast API. Firstly, we need to import it. So how we are going to import it? From multi set utils document ops yes correct and fast API router. Okay. The same uh module code we have created. So how we are using and now let's understand what actually happening when a user uh creates a post required as upload file. Uh so we have uh we have giving a type check here that okay this response from this uh post route upload file should be validated using this upload response identity model. And now at this route this upload function will run. This will accept a files uh keyword files argument which will be of type list and it will be a list of uploaded files. What is uploaded file here? Uploaded files is a pass API uh class uh which is which will be appended to all the uploaded files from the user and we are just uh using this file keyword here. Okay. And the uploaded response uh and this function will give us output in uploaded response class object. Okay. Uh so uh now let's see the implementation of this function. If no file is present, we are going to raise HTTP exception that okay 404 no file is found. Uh and if if the file is found what we are going to do we are going to create a wrapped wrapped files variable inside. What is it? It is a list of all the files the user has uploaded but it will be processed using the fast API file adapter. And what the fast API file adapter will do at the installation we have seen that it will uh just give us access of a method of get buffer and it will store the file name and the raw file. So this will uh we know we will get the list of all the raw uploaded files in the form of the fast API file adapter way. We have now what we are doing we are going to uh use the initialize this chat injector which we have already seen. What it will do it will just get the uh it will for now it will just initialize the uh sessions and configure the model embedding model and then it will uh set all the directories and session uh sessions according to uh the initialized session. Okay. And now we are also getting the session id here access of session ID here so that we can use it later when we are chatting uh with the uh document. So this session id is also stored. And now what we are doing uh we are uh using this uh inester we are using this build retriever class and we are passing the all the uploaded files inside the uh in the wrapped files manner. Okay. So this build retriever what it will do it will take the uploaded files and it will uh build the retriever like it will ingest all the uploaded files in the embedding forms inside the vector store and it will provide us the vectors. Okay like save load split embed and write inside the file index. Okay, this is done and now we are just initializing a empty statistry uh which we have initialized above. We are just configuring that okay uh this uh session ID uh this session uh key basically uh what we are saying here is that uh for this uh the key here is session id like the uh key here is session id and uh for that for this particular session id we are creating this part u uh this uh list empty list okay so what what I'm saying here this session is doing this session is doing let understand it we're using this flowchart here what we are saying that Okay. Uh this is the like this is a session. Okay. Session what is session? Session is basically a empty dictionary like okay this is a empty dictionary. What it will have? It will have a key. Okay. And it will have a key of type string and it will have empty dictionary. So what we have done here that in this code what we have done we have just said that okay uh keep the key as a session ID. What what I have done? So what uh whatever the session ID will be will be stored like this. Okay. X Y Z will be the session ID and the empty list for now it will be stored. So this is the initialization of a session and all the chat history will be stored inside the dictionary. How we will see further in the session. So this is done. Now we are just returning this upload response and all the session ID will be stored the index two and message this. So as you can see this we are following the pentic uh model uh structure here. Now this uh upload route is also done. Now what we have to do the next thing we have to do is create the the next thing we have to do is create the chat route. So this chat route will be responsible for data interval and data generation. So let's code it. Yeah. Yeah. So we are getting again getting some import errors like chat response. So what is chat response here? Chat response is a response model. It means it's a pentic model. So we need to import it like uh here we have imported somewhere. Yeah. Uploaded response. So here we areing chat response. Chat response. Right. So I think the model this is chat response is chat request is remaining. So we need to import chat request also. Yeah. So we don't have any import error. Now let's understand what uh will happen if someone creates a post request at this chat route. So what will happen? We are just telling that okay follow this chat response identic model for for all the responses. If there will there there is some mismatch we will show uh we will show validation error. Now we are running this chat function. chat function is getting the request in the form of chat request and it is uh giving it will give us the output in the form of chat response out class output. Okay. Now we are initializing the session id using the request dot session id and we have to pass the session ID inside the request. It means uh now we have uh this message. What is this message? Message is basically we are getting from the request itself. Now if there is no session ID and all we will just throw an error and then what we are doing we are if there is no message we are also throwing an error. Now we are basically building the rag application here and uh loading the retriever here. So what we are doing we are initializing conversational rack this session id this will basically configure the llm model this will configure the vector store uh vector store and this will configure all the session directories uh for the functioning of the uh project and then we have this index path basically this fires index that it is telling that okay create this fire index uh the vector store uh inside this uh if not present create this and if present use it from the fire index inside this particular session id and then we are using this load retrieval from fs. So if this if inside this uh fs index directory at this particular session a vector store is present we are going to load the particular vector. Okay. And now what we are going to do we have just uh get the dictionary the particular uh get the particular sessions uh chat history inside this simple uh keyword uh simple uh variable. And how to do it? How we do it? We reference the session uh dictionary which we have created. we say that okay get the get the uh list of the particular session. So we have already get that and we are just storing the particular list of the particular session inside this simple variable. Now we are just creating this LC history also with the empty uh chain. Then now in this simple we are just fetching that okay uh this this is the role then content and if the role is user we are going to store this LC history uh with the name of like append human message in it and if the role is assistant we are going to append the uh data uh in the form of AI message inside this LC history list. Okay. And uh finally what we are going to do we are going to invoke the our whole uh conversational rag like data retrieval data uh generation uh data generation uh lang chain pipeline uh using this rag dot invoke uh the working of this in rag.invoke Invoke we have already seen in the previous session. You can also understand uh by going to the previous session. Uh this invoke method what will get it will get it will uh just get the message here. Uh it will just get the message and the chat history which is the LC chat history for now. Okay. And now uh what it will happen now it will invoke and it will give us the answer. So what we are doing we are just uh updating our chat history so that we get the uh we are we get updated chat history every time. So we are just uh appending all the uh like the answer we got from the assistant and the input we have given as a user. So this is the code for that and in this session we are just restoring that at the particular session. Okay. So this is just refreshing the update updating the history of our uh chatbot and at last we are uh giving the response to the fast API. Okay, that the whole data uh retrieval and data generation is done and here is the answer pass it to the user. Okay, in the form of chat response and if there is any error, we will raise a document exception error and HTTP exception error depends on the uh thing we got. Okay, and now this is the code of the chat module. So we have successfully coded all the required routes uh inside our uh uh project and using all these routes we are going to query uh our uh module using APIs. Okay. And we are going to uh connect this API to a very good pretty clean front end. And the uh our whole end to end application will be completed in a local. Uh now as we have configured all the routes inside our fast a fast API we need uh we need to route a a small code of uicon uh to be able to run it. So let's do that and let's test it using the swagger UI uh in a bit. Okay. So let's go ahead and I just need to uh define a pretty simple thing here like uh I will just remove this main function here and I will remove it from here and I will just import the UV here and then I will say that okay uh uon import u and now I will write like uvicon dot run what I want to run I want to run this main file please run this main pyth and use this app variable as a API response. Okay, as a API base. So use this app variable and I'm telling that okay ser this uh fast API inside this host of inside this host of uh local okay and the post port must be and the port must be equal to integer of get env. So this is why I'm writing this. I'm writing this because if someone has uh defined a environment defined the port variable inside our env file so we will get that from here and we will set the port and if someone has not defined that we will use just by default 8,000 done. So the code of our uh API converation and the routes configuration and how to run is done and why we have imported u uicon here. Uicon is a way to is used to run our fast API code. Uh so let's go to the terminal and let's check that if if our working fast APIs are working fine or not. So we need to just deactivate the deactivated environment. Uh because we are using u so it will handle on itself. Okay. And now I just need to write the code. Okay. So here's the code. What we have to do? We have to tell that okay uon please run this main. py for me and inside that use app uh app variable uh to serve it as a API and we are just using this reload flag. So for example if someone uh makes some changes and some things changes the app will automatically reload. Now let's run it and see so it's saying that application starter complete and this is the route on which our application is running. Uh so as you guys can see as you guys can see uh we are getting uh at the home route we are getting at the root route and we are getting hello world over here. Why we getting hello world here? Because in the uh in the root route in the root route we are saying that okay render this index html and in index html we have hello element. So that's why we are rendering hello. Okay. And now let's check the health route. Okay. So we are getting status. Okay. So mean health are also working. But uh I think this is not a good way that manually type this routes and check that. What if you have 100 routes in your application? You will have if you have a production grade application. How to do it? So fast API provides a very pretty clean way to do it by using this doc routes. So this doc routes will be automatically generated by the fast API. This is called swagger UI and it will contains all the summaries of the routes you will have all the details of the routes you will have and all the schemas we have implemented. So it will uh showing by default that this HTML valation will become by default by but we have created this chat request chat response like all this is already coming like upload response. So we can see and we can see that okay get route we can also try it out like here we can execute and we get okay this call request is run this request URL and we get a status okay so this is how we do it okay so now let's test our upload and uh our chat route uh using uh the swagger so we have what we have here in the request body we we required a file and this is a required so we need to require it so let's try it out and let's choose a file so for the sake of simplicity I will choose a simple file here in my desktop I have a simple file this aentic file This aenti file uh contains some information about agent. So let's open it and let's uh execute. Okay. And this will load. And you will also see the processing in our back end also in the terminal also. And it will say what it is saying. Okay. This curl request got performed and this is saying that okay index said true and our response run successfully. So this run successfully and if the agentic file is uploaded successfully. Uh if we uh if we uh we want to chat it. Okay. We want to chat it to document. So how to chat it? We we will chat it using this uh chat route. So in this chat route we will just type it out and this session id as we know that all our uh things are controlled using the session. Uh so the uploaded file is uploaded inside a particular session. Uh so we need to get this session. So we are getting this session from this session uh key in the response uh in the response of the uh upload route and we are just paste this session here. And we are going to type here that okay what is agentic a okay and now let's execute and it must go to the back end and it must pass the file and it must give us the answer as you can see that aent refers to the new paradigm like it is based on the uh uh file we have given the injection we have done of the file uh to prove that let me ask some unrelated question okay what is uh let me ask a pretty unrelated question that what is react okay what is react So let's see what it will do. Uh it must say I don't know uh if it is uh based on the uh data injection we have done. So we can see that okay it is saying I don't know. Uh so this is how the swagger UI works. This is how you can test it. In this you can also see all the configuration you have performed that okay in the 200 we have successfully found that 42 is a validation error. Validation error. Why we getting validation error? Because it is automatically set up by the pyic because we are using pyic. If we haven't used pyic here we will not get this pretty clean validation error. So let's go and what what is remaining in our session agenda. So we have done till the fourth step like we have also tested our fast API using this swagger UI and our today's session agenda is done. I hope you know what is fast API now what is pentic how to use them how why we use pentic with fast API and how to integrate the fast API into our project and how to test it using this UI. So in the next session what we will do we will uh configure uh we will create a front end using HTML and CSS and we will connect that front end with our fast API and the uh front end will show all these things like uh the uh we have the functionality of uploading file and the uh chatting with the documents uh in a critically chat interface. Uh so in the next session we will do uh we will do that until that uh just the device what I have done here just go through the code you will find the code in the video description and you will find the resources also uh so go through them and I will meet you in the next session the fourth session of the LLM of series and we will code the UI there. Okay. So, have a nice day and goodbye.

Original Description

Welcome to the Complete LLMOPS Project Series! In this End-to-End Advanced RAG Project, we’ll build a production-ready GenAI Document Chat System step by step. From setup → RAG pipeline → FastAPI integration → testing → deployment on AWS ECS (Fargate) — everything is covered. Series Syllabus 1️⃣ Setup & Installation – UV setup, project structure, requirements, Jupyter integration 2️⃣ RAG Implementation – Core concepts, logger, config, Pydantic models, multi-doc chat, advanced RAG (token counter, memory, evals, MMR) 3️⃣ FastAPI Integration – Overview, adding APIs, testing with Swagger UI 4️⃣ UI Integration – HTML & CSS basics, connect frontend with backend 5️⃣ Testing with Pytest – Unit & integration tests, fixtures, mocking, test cases for multi-doc chat 6️⃣ Deployment (AWS ECS Fargate) – CI/CD pipeline, GitHub Actions, workflow integration, cloud deployment LangChain – RAG pipeline DataStax AstraDB– Vector store Python – Backend + AI logic FastAPI – API layer Pytest – Testing framework AWS ECS (Fargate) – Cloud deployment Streamlit/HTML/CSS – UI Project Github: https://github.com/yashprogrammer/LLMOps_series.git Mentor Profile: https://www.linkedin.com/in/yash-patil-ux/ #AI #GenerativeAI #GenAI #LLMOPS #LangChain #LangGraph #AIagents #AgenticAI #StructuredOutput #AIautomation #RAG #AdvancedRAG #FAISS #LlamaIndex #LCEL #Python #Chatbot #OpenAI #GPT #Gemini #Google #FastAPI #AWS #CICD #AIProjects Don't miss out; learn with me! P.S. Don't forget to like and subscribe for more AI content! End-to-End-Langgraph-Project: https://github.com/sunnysavita10/doctor-appoitment-multiagent Multimodel RAG Playlist: https://www.youtube.com/watch?v=7CXJWnHI05w&list=PLQxDHpeGU14D6dm0rmAXhdLeLYlX2zk7p&pp=gAQBiAQB RAG detailed Playlist: https://www.youtube.com/watch?v=wTVTkOb3SZc&list=PLQxDHpeGU14Blorx3Ps1eZJ4XvKET1_vx&pp=gAQBiAQB GenAI Foundation Playlist: https://www.youtube.com/watch?v=ajWheP8ZD70&list=PLQxDHpeGU14D7NiPgqxC9qhKkx4jMQcDk&pp=gAQBiAQB Connect with me on
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Related Reads

Up next
AWS, Azure, GCP: The One Thing Every Business Gets Wrong
AI Daily
Watch →