AI guardrails are safety mechanisms that control what goes into and comes out of AI agents, ensuring they only process safe inputs, perform approved actions, and return validated outputs. They are implemented as middleware that intercepts agent executions at key points. There are two main approaches: deterministic guardrails (rule-based with keyword matching and regex, fast and cost-effective but may miss nuanced violations) and model-based guardrails (using LLMs for semantic understanding, more robust but requiring LLM costs). Built-in guardrails include PII detection middleware (for protecting personal information like emails and credit cards using redaction, masking, or blocking strategies) and human-in-the-loop middleware (requiring human approval for sensitive tool calls). Custom guardrails can be created for input filtering (before agent) and output safety (after agent). Production-grade AI agents typically use layered combined guardrails that stack multiple safety mechanisms for comprehensive protection.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Guardrails Crash Course: Build Safe & Reliable AI Agents
Added:Hi guys, welcome to this crash course of guardrails for AI agents. In this video, we'll try to master the entire concept of guardrails for AI agents implementation.
In this video, I'm not going to discuss about the theoretical concept of guardrails. Along with that, I'm going to show you lots of practical example.
after completing all of the practical example even I'm going to show you how we can integrate this kinds of guards uh concept inside our actual AI edgers development. So if you're completely new to this uh guard concept this video is for you. So make sure you watch this video till the end and if you found this content useful please try to subscribe to my channel and hit the like and please try to share this with your friends and family. So guys uh you know that I started a aentki playlist on my YouTube channel. Okay. And this is a complete aenti playlist since uh I started uh this from the very introduction of agenti then I completed um u till the advanced part and if I show you the entire plan of my uh playlist guys. So this is my aenti course plan guys. I think I already showed you this course plan before. So here I divided these courses into multiple phases. So in the phase one I already covered the introduction to agentic AI. What is agentic AI? How AI agents work. Okay. Each and everything I have explained here. Then in uh phase two I already discussed about the asynchronous programming and pentic for the agents. Phase three I have discussed about the langen agents implementation like single and multi- aents implementation. Phase four I told you about the langraph. Okay. our first uh and uh core orchestration framework for the AI agent development. uh I already discussed about the langraph right I have given you the entire uh let's say understanding about the langraph lang graph components even we have also implemented some amazing project with the help of lang graph okay even I also showed you about this uh memory planning monitoring okay autonomous AI system in the phase five then um uh phase six guys I discussed about the deployment and production engineering there I told you how to do the CIC deployment okay how we can uh let's say add docker docker how we can uh let's say add different different cloud services databases okay each and everything I discussed in this particular part then uh phase seven guys I implemented some amazing project like uh agentic chatbot our uh custom own GPT then trip AI okay along with that I also implemented some amazing project with the help of model context protocol then uh um uh just recently I implemented another project called uh agent writer this is another multi- aent system we implemented with the help of langraph subgraph. Okay. So yeah, if you haven't checked those uh videos guys, please try to go ahead with my playlist. I will give the link in the description. From there you can check it out. Then phase 8 guys, I already completed uh this MCP concept model context protocol. As you can see uh MCP I have also covered.
After MCP I discussed about the subg graph, okay. And I also implemented some amazing project with the help of MCP as well as the subgraph. Now uh we'll start with phase 9 guys. from this video onward uh I already told you about like I will be covering this AI safety and evaluation part uh for our AI agents right and uh the first thing we'll be learning guys uh which is very important nowadays if you're implementing any kinds of AI agents which is nothing but AI safety we also call it as a guardrails and safety okay for the AI agents so now uh in this video guys I'm going to give you the entire understanding about the guardrails okay what guardrails is and why we have to learn that why we have to integrate inside our AI agents development along with the practical example even after completing all of the example after learning these guardrails I'm going to implement real world uh agenti system and there I'll try to integrate this kinds of guardrails concept so that your understanding would be more clear okay then uh whatever actually I planned for next guys I'll try to cover them one by one okay so yeah uh we'll be starting with the phase 9 so in this phase 9 we'll try to understand about this guards uh evaluation okay and this gu guards uh safety concept inside our AI agents. So guys in this video we'll try to understand uh these are the concept I have already noted all of the concept I'm going to cover in this video. So first of all I'm going to discuss about what is uh guardrails exactly and why uh do they matter. Then um gels has some approaches. Okay, we have to understand uh it it is following basically two approaches. One is the deterministic approach and one is model based approach. Okay, I'll discuss both of them. Then we'll be learning built-in PII we call it as personally uh identifiable informations. Okay, we call it as a PI detection middleware. Okay, I'm going to discuss everything. Don't worry about that. Okay, what is middle wire and all? I'm going to discuss each and everything. Then we'll be learning about the built-in human in the loop middleware. Okay, this is another guardrails uh let's say uh system. Okay, we we try to develop. Uh then we have custom before agents guardrail. Uh here we basically do the input filtering.
Then we have custom um [clears throat] after agent guardrails. Uh we use this particular uh guardrails for the output safety. Then we'll be covering the layered and combined guardrails. So basically uh we combine all of these guardrails. Okay. in one place and we call we create this layered guard system then at the last I'm going to show you one real world use case we'll be creating a healthcare chatbot and there we'll try to integrate this guil's concept there okay and here I have already added the reference documentation u you can see in the lang documentation only you'll be getting this guil's guide if you want to go through the documentation I have already given the link so for this particular guil's practical example guys I'm going to use lang mostly Okay, because langen is already having the built-in functionality for the address. That's why I'm going to use the langen here.
Okay, and if you want to integrate with langraph or any other framework, it's completely fine because at the end it supports for all the framework. Okay, it's it's kinds of independent things you can use inside your aen development.
But for this kinds of example, I'm going to use langen and python to develop all of the practical example for this particular video. So guys uh as you can see here I have added a uh added an image. So from this image I think you can understand what is this LLM guard is exactly. So see what happens whenever let's say we are giving any kinds of prompt to the agents right. So previously whatever agents we had created I was to pass this kinds of prompt directly to the agents workflow and agent workflow was executing and you know that agent workflow will be connected with some kinds of tools right uh some kinds of tools or some kinds of thirdarty services and based on that it will provide you some kinds of response okay but in guards actually what we do we add a additional layer here so uh instead of directly passing this kinds of input to the agents we basically do a guard check here okay so here basically will try to add a guardrail safety layer. So basically first of all this input will go to the guardrails uh guardrails check layer and it will try to check whether this kinds of uh let's say prompt or user input user is providing whether it is having any kinds of harmful content or let's say any kinds of abusive content or not. Okay, if it is having this kinds of let's say violation related content, violation related input, we'll try to block the request. Okay, and uh we not going to execute the workflow that time.
Okay. Otherwise if this user inputs is completely safe, okay, this is not harmful or any kinds of violation related input, we'll try to mark it as a safe and we'll execute the main workflow and workflow will give you some kinds of response. So this is the highle understanding of the guard. So basically I think you know that nowadays safety is important whenever you are creating any kinds of agentic system or any kinds of LM powered application. It's not like that. Let's say I'm giving an input to my uh agents how to make a bomb. Right?
So if uh my agent is replying to that particular prompt that means I can use this agent in a uh in a let's say any case of illegal kinds of task right I can make the bomb and I can sell to the market and that bomb would be utilizing okay uh to destroy some someone other's life right so we don't want that so that's why whenever we are creating our application we have to make sure that our application is completely safe it is violation protected it is harmful content protected okay it doesn't have any kinds of sexual or harassment related content so that it will not harm any kinds of human being. Right? So this is what we have to take care about the safety. Okay. I think you have already heard of these kinds of safety uh keywords right whenever you are working in the field of gener agents these kinds of terminology I think you have heard of right. So now we'll be learning the actual safety terminology with the help of this guil. Okay. And guards is super important. uh I will definitely suggest you if you are creating any kinds of agent application or any kinds of alm powered application try to integrate this guardrills okay safety inside the development okay so yeah this thing guys we'll try to first of all understand uh in detail okay uh uh I mean uh what are the other way actually we can uh implement this kinds of gradal system inside our AI agents that's why I already written all of this step I told you uh it has all actually two major Let's say implementation strategy. One is the determine stick and another one is the model based. Okay, we'll be learning all of them and uh there are some built-in actually functionalities are also available inside langen. Uh we'll try to see the built uh built-in pi then built-in human in the loop uh middle void. Okay. And uh so this middle word is a actually a function inside langen with the help of that we can implement this guard. Okay. inside our AI agents. Then we will be also learning how to build the custom uh custom guardrails uh before agent. Before agent means let's say whenever user is giving the prompt before uh uh going to the agents we'll try first of all do this guard check okay whether it is having any kinds of harmful content or not. If it is having we'll block the request okay otherwise we'll try to execute the workflow. So this is called before agent guardrails. Okay input filtering. Then you can also integrate like uh after guardrails. Okay after guard means output safety. Let's say whatever output your agents is giving you right whether it is good or not for the human being right we have to also add another layer here see this is optional you can add otherwise you can also ignore it's completely up to you this is completely your design philosophy but it's recommended to add at the in input position as well as the output position okay if you're adding that that means whatever agents are generating the response we are checking this particular content is completely fine for the human being or not Okay. So that's why we have to also learn after agent guardrails output safety implementation. Then we'll be also learning the layer that means combined guardrails. We'll try to combine all of the guardrails. Okay.
Layer by layer we'll be implementing this one. And mostly in production grade architecture production grade AI agents development we use this layered and combined guardrails. Okay.
Implementation. Then at the last I'm going to show you how to implement a healthcare chatbot with the help of these guardrails. Okay. Now as you can see guys here I have written the definition for these guardels like what is these guards exactly? As you can see gardles uh are safety mechanism that controls what goes into and comes out of an AI agents. This sit around your agent pipeline and ensure the agent only process processes safe appropriate inputs only performs approved actions only returns validate validated and compliant outputs. Okay. So yes uh this is what actually your guardrails uh tells about. Uh as you can see guardrails helps you to build safe compliant AI application by validating and filtering content at the key point in your agent's execution. There are implemented as a middleware. Okay I told you um in uh langen actually this thing are implemented as a middleware uh that uh intercepts executions. Okay. So we can add before the agent start that means we can do the input guardrails that means the input verification. We can even add after the uh like agent execution that means the output guardrails okay for the output check operation and you can also integrate these kinds of guardrails around means uh in between the models and tools.
Okay, so let's say whenever your agents will be using some kinds of tool, okay, that time you can also integrate this kinds of guardrail so that uh before using any kinds of let's say actionable tool, it will take the permission from the human. Okay, so that's why we will be learning this concept uh human in the loop middleware. Okay, so basically here agent will try to uh uh take the input from the human whether it will be using that particular tool or not to perform this task. So those who have already used the VS code AI agents I think you know that whenever we are generating any kinds of codes right uh and uh you will see that it will be using some kinds of third party tools and before using that tools it will uh it will take the permission from you right whether I will use that or uh generate that particular output or not if you approve that then it will try to use that tool then it will uh generate the output okay so this is another kinds of let's say uh guadrils guard safety they have added inside the agents uh with the help of this human in the loop concept. Okay, we're also learning this this kinds of concept as well. So here you can see some common use cases of this guardrails. Uh the first use case uh we call it as a PII leakage prevention. uh for an example let's say uh we are passing emails or credit card informations or let's say any kinds of password to the AI agents that time uh we have to use this PII leakage prevention okay so what it basically does it will basically reduct your emails it will let's say hide your credit card numbers even it will also hash your password okay so if you are not adding this kinds of layer so there is a possibility your personal information may leak Right? So that's why this is super important. Then prompt injection blocking. Uh so we use for the detecting uh let's say adversarial inputs. So whenever user is giving any kinds of prompt we are first of all checking uh whether this prompt is fine or not. Um if they're trying to inject any kinds of harmful prompt definitely that time we'll try to block the uh prompt request that time. Then harmful content filtering. So we block dangerous request okay uh from the user side. Then there is another one called business rule enforcement. So basically uh this example shows us the uh approval concept human in the loop concept. Let's say for an example uh you are implementing a financial agents and that has to use uh let's say uh payment right payment tool.
So uh instead of using uh directly using that particular tool first of all it will ask you for the approval if you uh uh let's say give the approve that time the operation would be executed right and if you reject that time this would be rejected then output quality validation so ensure responses meets the safety standard okay whenever we are getting any kinds of output from the AI agents we have to make sure this is completely safe right so that's why I have added another image here so as you can see uh what is AI guards AI guards is nothing It's a kinds of uh filtered layer we add uh before and after let's say uh uh executing our AI agents even you can also add in between also the in between means if uh whenever your agents is uh hitting any kinds of tool okay before that also you can add this guard layer so that uh whenever it is selecting any kinds of sensitive tool it will ask for the human permission and if you give the permission it will work and even you can also add before the agent execution okay let's Okay, user has given a prompt. First of all, it will go to the guardrails layer. Then it will try to filter the output input whether this input is fine for or not. It it doesn't have any kinds of harmful content. Then it will send to the agents. Okay. Then whatever agents is giving the response we are sending to uh to this uh guard layer again and we are checking whether the response is fine or not. Okay. whatever response we are getting if this response is fine we'll try to show to the user otherwise we'll try to um we'll try to tell the user this response is not good for you okay so that's how guys the entire guardrails uh concept looks like and we'll try to see all of these u all of this let's say uh concept uh in a practical development uh I believe this guard is more about practical so if you want to understand these guardrails uh in more depth so definitely you have to uh do the practical example. Uh by the practical example only we can understand this concept in detail. So let's try to start the practical development of these guardrails. Uh first of all we'll start with this uh deterministic and model based guardrails. Okay. Uh we'll try to understand what is deterministic and what is this model based guard guardrails first of all. Then we'll uh start the practical development for all of the guardrails we have added here. So guys whenever it comes to practical development of guardrails uh basically we have to follow these two approaches uh to develop the guardrails. One is the deterministic guardrails. So here we use the uh traditional rule based let's say system rule based actually condition. Uh here you can also use something called regex that means regular expression.
Okay. So rule-based reg x keyword matching explicit checks. Okay. So here instead of using any kinds of large lang based model we'll be using this rule based condition as well as the regics okay to identify whether a harmful content is available in the input or output or not. Okay so this is basically fast predictable and cost effective because here we are not using any kinds of large language model. We are not using any kinds of let's say uh any kinds of predictable model. That's why it is fast and cost effective. But here you can see it may miss this uh non- seat violations. Okay, there is a possibility uh it will uh it will it will not be more robust. Sometimes there is a possibility it will miss lots of violation related content. Okay. Now there is another one called model based guardrails. Okay. In model based guardrail we will be using this uh large link based model and uh some kinds of classifier for semantic understanding.
So in this model based guardrails we'll be using some kinds of large language model or any other let's say model and uh we'll try to ask the model uh just try to figure out whether uh user input or let's say whatever model output we are getting it is having any kinds of uh let's say inappropriate content or not for the human being. Okay, then my model will tell okay it has the inappropriate or not based on that we'll try to uh we'll try to add the guardrails layer.
Okay. And what is the best benefit you will be getting in the model based guardrails? Basically it can ces almost all kinds of issues. Okay. Almost all kinds of let's say violation related issues. But in the deterministic one it may miss. Okay there is a possibility it may miss because we are using rule-based one. Okay, for example, let's say I have given um this uh this let's say um this website uh I'll I'm going to hack today.
Okay, so here you can see we are using hack hack keyword and hack keyword is basically kinds of violation uh we can consider. So that time let's say this this regix will work completely fine for that. Okay, but let's say we are giving another prompt. Let's say this system I'm going to uh shut down today right so here shutdown it's not available inside the keyword that time this will fail okay but if I'm using the model based guard guards okay that time my model will automatically understand okay this hacking and shutdown both are kinds of similar right and that time it will block both request requests okay so that's why this model based guards is more effective than your deterministic guards okay but if you're creating any let's say a simple guard pipeline that time deterministic is fine because here you are basically saving the cost but if you're using the model based guards definitely you have to use some kinds of LLM or model and some kinds of cost would be definitely there but again if I want to make the robust robust let's say guard system I'll go with the model based guard one okay so let's try to see the practical development first of all of the deterministic then I'm going to show you the modelbased guard uh practical development as well so see uh this is very very easy If I'm talking about the deterministic guard, so here we'll be using some kinds of conditional and uh let's say regular expression kinds of let's say logic to identify whether input is having any kinds of violation related content or not. So for this example I already created one code code example. Let me show you. So this is the code example guide I I I have created. So here basically I created one function. The function name is deterministic guardrails. It basically takes an input that means a text and it returns a boolean output. Okay. Uh boolean output means it will return whether uh it is having the violation related content or not. Okay. So as you can see here we are only doing the traditional okay checking condition.
Basically we are adding some kinds of ban keywords in this list like hack, explicit, malware, okay bomb. These are the keywords and if this keyword keyword is available inside the user input we'll try to basically return uh this kinds of violation content is available otherwise we'll not return u the violation content is available okay now here I have taken some kinds of input in this list as you can see how do I hack into a database what is the capital of friends explain how malware spreads okay now what I'm going to do I'm going to just uh write a for loop uh over this particular list And I'll try to take all of this input one by one. I'm going to pass inside this function. And this function will return me whether it has to block or not. Okay. Now here I'm checking a condition if uh let's say uh this particular request is blocked. I'm going to uh let's say print block otherwise I'm going to um let's say print it is allowed. Okay. Now if I execute let me show you. See if I execute. See it is super fast and there is no model cost uh is involved here because I'm not using any kinds of large length based model.
And you can see the first input how do I hack into the database. So basically it has the uh violation that's why it is getting blocked. Now the second one what is the capital of friends again this doesn't have any kinds of violation that's why this is allowed. Then the third one explain the malware spreads.
As you can see this is also blocked because malware is also considered as a violation inside our uh dictionary.
Okay. So yes that's how this deterministic guards works and for the simple use case you can use that but I will recommend go with the model based guards. Okay now let's try to implement this model get model based guards as well. Now this is also like very simple here you only just need to use a large lang based model. Now you can use any kinds of large language model either you can use openi model either you can use uh gemini model gro provider it's completely up to you. So I already have uh collected this Google API key that means I'm going to use the Gemini model.
Okay. But if you have open API key, you can also use OpenAI model. Okay. It's completely up to you. And I think you know that how to generate this uh Google API key, right? That means the Gemini API key. So for this you have to visit one website called Google uh Google AI Studio.
Google AI studio. This is the platform.
Just open this platform and uh left hand side uh first of all click on get started and left hand side you will see the API key option just click on the API key copy this API key if you don't have just create a month and try to add add inside your environment variable okay this is what only you just need to do okay now the first thing guys what I'm going to do I'm going to load my environment variable so let's add the code so here I'm using this uh package to load my environment variable let's load that then After that I already created this uh code that means the function related my model best guards. Okay. So this is the function guys I created. As you can see first of all I'm importing this uh chat Google generative vi function and here I'm using Gemini 2.5 flash model and this is the temperature parameter I have given and I created a model object.
Okay. So what I can do maybe I can keep in a different cell so that I can use this model object later on. Okay. So yeah. So now this is the like model based approach guys. As you can see, I have created a function called model based guards and it is uh taking a user input and here I have given a prompt.
Okay. Uh the prompt is is the following user input safe to process reply with the safe or unsafe. Okay. I'm telling my model if it is safe just try to return safe otherwise just try to return unsafe. Okay. Now this input I'm giving to the model and we are invoking the model and whatever result we are getting we are getting uh getting the content and we're returning that. And again whatever test input we had previously that means these are the test input okay we are executing one by one here. So as you can see we're running a for loop and we are passing this input my to my model based girdles right now and we are checking the status if this status is like let's say safe um uh safe we'll be trying uh we'll we'll return like sorry if this status is like let's say unsafe we'll try to return unsafe otherwise we'll try to return safe. Okay, so this is the condition we are writing. Then we are printing the status here. Now let me check if I execute you can see modelbased guard demo unsafe. See the first one how do I hack into database this is unsafe that's why it is detecting as unsafe. Then this one what is the capital of French? This is also let's say safe. Uh you can see this is giving you safe. And the third one as you can see uh explain the uh explain how maloid spreads. Okay. Now uh see if you closely see this particular sentence actually this is not harmful content right explain how malware spreads right because I want to know like how malware spreads this is not a harmful let's say um content but if I'm talking about my deterministic approach right this one has considered as unsafe content right and it was getting blocked so this is not good right user user wants to know about how to malware spreads this is a good content good input But my agent is getting blocked this one. My uh let's say guard is getting blocked this one.
This is not good, right? That's why I told you model based guard is more good because it has the large language model and large language model is smart enough to understand whether this input is having any kinds of violation or not. So definitely this content doesn't have any kinds of violation that's why it is safe right now. Okay. So I think you have understood. All right. So yeah this is the your deterministic guards as well as the model based guards. Okay. Now let's try to understand the next section which is built-in guards. First of all we'll try to see the PII detection middleware.
Okay that means this one. So I think I showed you the uh topic right. So we have already understood what is guard and why it is required. Then two apples we have already completed the deterministic and model based one. Now let's try to work on the third section built-in PII that means personally identifiable information PI detection uh middle part.
Okay. And this is already integrated inside langen. Okay. If you are using langen from langchen only you can use that particular functionality. Okay. So in section three as you can see langen provides built-in middle pi middleware wire for detecting and handling personally identifiable information pi.
So whenever I'm talking about personally identifiable information it includes actually Gmail. So definitely whatever email information you are giving to the agents then credit card numbers any kinds of IP address any kinds of MAC address any kinds of URL. Okay. So these are the actually information I can consider this is my personally identifiable informations. Now what should be the strategies to handle that?
See for the email actually we'll be using something called redact. Okay. So whenever user is giving the email I'll be using redact. Okay. Reduct strategy.
So basically it will convert your email to the redact email that time reducted email. Okay. Then for the credit card we'll be using masking operation. Okay.
Masking means we'll try to hide some of the number and we'll only show the last number of that. This is called hiding.
So I think if you have already used some kinds of agents right before like chat GPT or Gemini you have seen they also gives the output like that. Okay. They will mask the uh personal uh let's say information then they will give you the last information of that. Right? then I'm talking about any kinds of let's say IP address or MAC address we can use the hashing for that or any kinds of password. So instead of showing you the actual let's say password or IP address and MAC address we'll be using hashing number for that. Then uh let's say for any kinds of uh URL or API right let's say I'm giving a secret API so instead of let's say uh showing the result we what we can do we can block that particular request that means it will raise an exception you can't provide any kinds of API here okay so yeah these are the strategies we'll be following to implement this uh built-in PII detection uh middleware okay inside langen now how to implement that let me show you so for is first of all let's import some necessary libraries. So I'm going to import some necessary libraries. So from langchen see here I'm creating a simple agents. I'll be creating a simple agents with the help of langchen and then I'm going to show you how we can integrate this pi middleware wire guard system. Okay. So that's why I'm importing this create agent. I think you know with the help of create agent we can uh create a basic agent inside langen. Then we are importing langen agents middleware wire and we are importing pi middleware wire.
And you can see this function is already built in inside langen. That's why we don't need to write separately this code. Then langen we are also importing the tool. Okay. Now let me import all of the library. Then after that here we'll be creating a custom tool. So here I'm going to create just a dummy tool. Just to show you my uh agent is working.
Okay. See here you can use any kinds of tool. Uh the way you are creating the agents uh what purpose you are creating the agents based on that you can create your own tool. And I think I already explained how to create your custom tools right how to integrate inside the agent each and everything I have already covered inside my playlist. So if you haven't checked that guys please try to check it and then you can continue with the session. So yeah this is the tool guys I have created custom tool. So here I'm not executing any kind of API here only what I'm doing whatever query I'm getting from the user I'm just returning that particular query with some additional information. Okay I'm using f string and here I've written customer record found for query and we are giving the query. Okay. So this is my customer.
Let's say I named it as customer lookup tools. Okay. Now let's create an agent here. So this is the agent guys I created. And inside that I have integrated this uh pi middleware wire uh greas. Okay. As you can see this is the agent I'm creating create agent function we are using. First of all we're defining the model. So here I want to use this Gemini 2.5 flash model and the provider is Google geni. If you're using openi that time use openi model. If you're using grock that time use grock provider or grock model. Okay. Then we are passing the tools because every agents will have the tools definitely.
So we are passing the tools and this tool we are giving the custom uh customer lookup tools. Then here it is already having a keyword or parameter called middleware wire. So inside middleware wire you have to give the pi middleware wire. Okay. The pi middleware wire we have imported. answer that first PI middle where we are giving the email uh email related guidance okay now whenever user will give any kinds of email that time the strategy I'm going to use called redact okay as I told you we'll be using this redact for the email okay so this email would be redacted that time and apply to input that means whatever input user will give in that particular input I'll try to apply this particular strategy then I have taken another let's say uh PII middleware wire for the credit card so if user is giving any kinds credit card number right so here I'm using this strategy is equal to mask that means in this credit card this uh this information will be masked okay and last information will be only uh shown up okay and we'll be applying to the input then the third one I have added pia middleware for the API key let's say user is giving any kinds of secret API key that time I'm using this strategy is equal to block and it will automatically detect with the help of this regular expression okay I think not every uh API key will have the sk parameter than some alphabetical number then some special character. So if if we found these kinds of things we'll try to block that particular uh input. Right?
So here we'll be raising the exception and we'll try to apply to the input.
Okay. Now at the last I'm printing agent with pi middleware created successfully.
Now let's define the agent. So as you can see guys my agent has created. Now let me invoke this agent and let me show you whether it is able to basically block our request or not. So first of all I'm going to give one email example and credit card example. So here is the testing code. So agent.invoke and here we are giving the message role is equal to user and this is the content. My email is john.doample.com and my card is this is my credit card number. Can you help me with that? Okay.
Now if I give this prompt to my agent and agent is already having this guardrails layer. Okay. As you can see these are these are my guardrails layer.
This is called uh pi middleware guardrails. Okay. And it is only working on email, red card and API keys. So the if this information is available inside the content that time it will uh apply the guardrails and whatever strategy I have applied it will basically um show you that particular strategy. So let me show you. So if I execute this prompt let me show you what is the agent response we are getting. So as you can see this is the agent response and here you can see I have located your customer record. how I uh can help you today.
Okay. Now, one thing I want to show you here. If I print the entire result, that means the user input. See, this user input is not uh uh the personal information is not visible here. As you can see, I can't see the personal information right now. As you can see, my email is reducted email. So, it is telling it's reducted email. This is but I have given my original email. Okay?
But it has converted to the reducted email. Then, this is my credit card number. You can see it has done this hash uh sorry masking operation that means all of the first numbers it has um let's say uh converted to the star and only the last number is visible I hope you got it okay so this is called actually p I middle word guils okay that's how this actually hides your personal informations okay now let me give you another example so what I'm going to do I'm going to give this example let's say this API key I'm passing an API key okay here is my API key and definitely you have to use try except block because at this uh API key option you have added strate is equal to block okay and block will only work whenever you are using exception handling so as you can see here I already have added block basically raise an exception so that's why uh in this example we are adding this particular execution in the triet block and if uh let's say it is blocking we'll raising the exception okay now see if I execute now see this is blocking this request because I'm trying to pass my API And it is telling um block as expected detected one instance of API key in the text content. Okay, that's why it is getting blocked. Okay, I hope you got it guys. So this is called actually PII middleware guardrails and you can integrate this inside your AI agents to prevent the personal information safety.
Okay, I hope you get it. Now let's try to uh understand this built-in guardrails the section four which is human in the loop middle part. Okay. Now let's say um in between in between the architecture uh you want to integrate the guardrails basically your LLM will be connecting with some tools right and whenever it is using the tools let's say whenever it is using any sensitive tools that time instead of using directly it will ask for the human permission and if user is giving the permission that time it will execute the tool otherwise it will not execute the tool okay so for this kinds of work we can use this uh we can use this particular concept called human in the loop middleware. Okay. And this is already integrated inside lang lang. We can easily implement with the help of lang. So as you can see it pauses agent execution before sensitive operation and waits for the human approval. Best for financial transaction. So whenever you are creating this kinds of agent for the financial transaction or any other sensitive let's information you are using there you can use this particular concept. Then sending emails to the external parties. uh deleting the product production data any operation with significant business impact. Okay, for this kinds of work this is completely best and the key requirement is you have to use checkpointer here for this state uh persistence across the interruption. Okay, so I already discussed about the checkpointer inside lang graph, right? Whenever we are creating the workflow and whenever we are compiling the workflow, we have to give the checkp pointer to save the state memory. Right? And here we use some kinds of database either you can use inmemory saver either you can use external database. So definitely whenever you you are using this human in the loop middle bar you have to use the checkp pointer that means the database instance. Okay, because with the help of that it will understand basically which trades is getting interrupt right what user actually doing this kinds of uh middle point okay we can differentiate the user that's why this this checkpo pointer is required because checkpointer will take the trade ID right and each and every user will have a different trade ID so we can differentiate with the help of this checkpo pointer and this is recommended okay if you go to the documentation you'll see that this is recommended now let me show you the practical example how we can implement this uh human in the loop middle part.
Okay, so the first thing we'll be importing all of the necessary libraries. So as you can see we are importing again the agents from the langen agents. Then we are importing one more function which is human in the loop middleware wire. Okay, this is already available inside lang agents middleware wire. Then we are also importing this uh uh uh memory inmemory saver for the persistence memory and it is available inside lang graph. Okay. And we already installed lang graph inside our requirement. So then uh we are importing this command because you know that to implement this human in the loop I have to use this command function because in the command only you give the uh permission let's say you want to write the appro right with the help of this command function you will give that particular permission to the agents with the help of human in the loop. So this concept I already discussed in my playlist guys. Okay whenever I discussed about the langraph that I told you about these are the concept. If you haven't checked that please try to check it.
Then we are importing the tools from the langen code. Now let me import all of them. Now guys here first of all I'm going to create some dummy tools for the agents. So these are the dummy tools guys I have created uh for the agents and again I'm not using any kinds of API here only just dummy tools like s web here I'm only returning the query whatever user is passing and we are using this tool decorator and send email and delete records. Okay so some sensitive tools uh we have added for the agents. Okay. Now let's try to define the agents. I will try to define our agents. So let me show you. So this is the agents guys we have created. We I'm calling as a age it agents that mean human in the loop agents. Okay. Now you can see we are using the create agent function. We are giving the model. We are passing all of the tools we have uh created here. Then we are using middleware is equal to human in the loop middleware right now. But previously we used pi middleware tool. Okay. So there is a difference. PII middleware tools uh basically helps us to uh let's say handle my personal information and this uh human in uh human in the loop middleware helps us to uh take the permission from the human. Okay, whenever it will execute any kinds of sensitive tool okay that's why we have to use that. Now here I have given some interruption condition let's say whenever it will send the email that time it will ask for the permission.
Okay, that's why I'm making it as true.
whenever it will try to delete any kinds of records in the database that time it will take the permission from me okay that is from the human that's why I made it as true but whenever it is doing web search operation that time this human permission is not required it is auto approved we are setting that okay so I hope you understood then we are giving the checkpoint is equal to in memory then we are printing this particular agent as created now let me execute now you can see this is created now I'll try to invoke this agent so here I already created a example So as you can see first of all I'm preparing a configuration configuration config and here we're giving the trade ID. So let's say this is my session 001 and we're invoking the agent that means my hit agents and this is the invoke we are doing and you can see this is the input we are passing. So ro is equal to user content send an email to tech company tech at the rate company team at the rate company.com about the Q4 results. Okay. And we're giving the configuration and we are printing the result. Now you will see that uh for this kinds of input definitely it has to use this tool, right? It has to use the send email tool and I already told whenever it will send the email that time it will take the permission from the human. Okay. Now see if I execute now your agent will tell see agent pause pause the execution and it is waiting for the human approval. As you can see send an uh this this particular um this is my human input and for this human approval is required. Now I'll give the human approval. Okay. So for this I'm going to use this code this code. Okay. Again I'm invoking my agent. Now I'm giving the command is equal to my approve. Okay that mean decision is equal to approved. Basically I'm giving the approval. Okay. And that's how in uh langraph guys whenever we are creating the ACITL we give this uh uh human in the loop approval. Okay, like that with help of this command um function we're giving the configuration again that means this is the user actually um uh actually the same user giving um giving the trade right because this user has executed this agent okay now whenever it will give the permission definitely this user should give the permission so that's why we're giving the same configuration again okay to resume the posit session now uh again we are printing the final result now let me show you so now if I execute now see this is got approved And this is the final response we are getting from the LLM. I can do that. What would you like to uh like like the email body to say?
Okay. Now basically it is using my tool and it is trying to send the email to the customer right now. Okay. Because we have given the approved permission.
Okay. I hope you got it.
All right. Now let me give give another example.
So this is the example. Let's say I give. So here I'm telling u delete all the records from the user table where active is equal to false.
Okay. So definitely for deleting any kinds of records I need the human permission. Now if I execute see it is it it will pause the execution. So as you can see delete the uh okay I have to uh I have to print the result.
Okay, print the result.
Now see it is paused and it is waiting for the human approval. Now I will give the approval.
So again we are invoking the LLM. Now we are giving the uh approval. Now basically here I'm not going to give the approval. Basically I'm going to give the rejection. Okay let's say I don't want to invoke this tool. Okay for the uh database. I don't want to delete the records in the database. And here I'm also telling the reason okay why I'm not uh giving the permission. So this is the reason I'm giving. This is too risky.
Needs DBA review. Okay. And we are giving the configuration.
Uh now let's try to execute.
So guys here I'm getting one error. Uh if you see the adder so basically I already exceeded my current quart of my Gemini API key because I was using the free plan. Uh so this is the issue. So what I can do instead of using Gemini maybe I can use the Grock model. Okay Grock provider. So how to get the Grock provider? So I think you know that you have to search for Grock playground or Gro API key. Just open the gro and uh you can create a new API key. I'm going to name it as let's say um safety no expiration and submit.
I'll copy this key and I'm going to add inside my involvement variable. Okay. So here what I can do maybe I can add after that. So whichever model actually uh will work for you just try to use that model. Okay. If you're using free model.
Yeah. So I'm going to add this key gro API key. Okay.
Done. Now what I can do I can maybe replace my Gemini model with the gro model. So simply you have to go up and uh here you can change the model definition.
So I'll just try to add the model here.
Okay. And here you have to import the chat gro and make sure you are you are uh installing one library called langen gro in the requirement.txt.
So you can add that langen gro. Okay. So simply open up your terminal and install that.
So this is my environment name cond activate angraph test.
Okay. Soonda env list copy this and cond activate lang graph test done now I'll just do p install r requirement txt done right now simply I'll just restart my kernel [music] and let's execute ute from the beginning I load my environment variable and uh see if I want to uh define the model. So inside chat gro you just need to define the model which model you want to use.
So here you will see that there are different different models are available inside playground. Okay. So I'm using this model. You can also use any other model if you want. Okay. Other model is also available. So I'm using openi gpt o ss 20 billion model. Now let me execute this.
Okay. I think one thing I have to add here called strip parameter. Now let me execute. Okay. Now it's working. Now simply I'll go below.
Um you can also uh change this model to model the model we have created right now.
The tool is not defined.
Now it worked. Now let me execute this line as well.
H. It's working. So, let me test this one. This is also blocking. Now, let me check this uh human in the loop.
[clears throat] I'll import.
Oh, sorry. I have to change this. Change this to the model.
Done. Now I'll execute this front end.
Now see this is pausing the request. Now I'll give it to the approve.
Now see it is working and it is sending to this email. Okay. Now every LLM response uh quality is different. Okay.
So previously I was using Gemini. Gemini response quality is different. Now GPT response quality is different. Okay, that's why the previous execution and the execution I'm doing right now uh it cannot be same. Okay, I think you are getting right. All of the models are different. That's why uh now I I can say this uh quality is good. Uh this uh uh uh gro provider model I'm using this is this quality is good. Now uh I'll execute this one. Delete all the records from the user table where active is equal to false. Now again it is pausing the request. Now uh see now what I do want I want to give the reject uh let's say uh reject let's say command instead of giving the approved. Now if I give the reject command now see here it is rejected the final response I'm ready to delete those records but just to do be sure you don't uh you uh do you want me to proceed with the removing all the rows from the user table. Okay, because I have rejected that that's why it is not uh deleting that particular uh let's say database. Okay, I hope you got it guys. This uh human in the loop one.
Okay, this human human in the loop middleware guardrails. Okay, now once it is done now I'll try to move to the next one which is my uh custom guardrails.
Okay, now we'll try to see the custom guardrails. First of all, we'll try to see the before agent hook. Okay, input filter. So before executing my agent so let me mean here let mean this architecture we'll try to see right now uh before executing the agent first of all we'll see how to add the guardrails okay and we'll write our custom guardrails here we are not going to use any kinds of predefined guardrails from blank chain okay so let's try to define the custom guards before exec agent hook okay input filter so for this we'll be creating a function before agent to validate our block request before any lm process processing begins. Best for keyword and content filtering. If you want to uh filter the keywords and content from the user input, you can do that. Authentication checks, rate limiting and blocking the specific categories of the request.
Okay. Now, let me show you the example.
Uh I'm going to show you the example. So here first of all I'm going to import some necessary libraries.
So here you can see I'm I have imported this any then agent uh middleware wire I'm importing agent middleware wire agent state and hook config okay so these are the functionality you need to create this custom uh custom guardrails then I'm also importing the runtime this is also required then uh we are uh importing the create agent object and tool let's import them now here I already created one um custom class. Okay, for this uh custom guardrils. So this is the custom class guys I have written. Uh I named it as content filter middleware.
Okay, this is my custom written uh class. As you can see I'm not using any kinds of predefined middleware wire and uh see if you want to create your custom let's say middleware wire or custom guardrails you have to inherit with the agent middleware. Okay, there is a agent middleware let's class inside agent.
Okay, middleware wire you have to inherit that. After inheriting I'm using the super class to get all of the uh property okay inside my this particular class and here I'm checking some band keyword okay whatever band keyword user will mention I'll check that I'll store inside this list then here I've written this function called before agent and here you have to use this decorator called hook config okay and inside that you have to pass this parameter can jump to end that means what will happen if it founds any kinds of let's say band keyword or let's say violation in the user input it will directly go to the end nodes okay and your agent uh will stop that time okay it will block that particular request so here uh first of all I'm checking whether the message is available or not if message is available I'm taking the human human message then I'm only taking the message content then I'm checking if uh keyword is available inside my content message content will try to block the request okay and I'll try to send a message to the user I cannot proceed with the request test containing the inappropriate content.
Please refresh your uh request. Okay, then I'm jumping to the end. So this is the custom uh custom let's say class I have created for my uh custom uh guardrails. Okay, custom middleware. Now let me test whether it's working or not.
So simply I'm going to uh just create a tool just dummy tool to execute one agent. So this is the dummy tool. I I used the search tool. Okay, here now this is my agent. I created and here also I'm going to use my gro model.
Okay, not Gemini model. Sorry, this should be model H. So here I'm giving the search tool and middleware. Now I I'm using this content filter middleware the middleware I have created right now my custom one.
Okay. Now here I'm giving some band keyword like hack, expose, expo it, malware, jailbreak, bypass. If this words are available so what will happen my uh input should be blocked at time okay now let me define that and if I execute and show you uh see this is my testing code I'm invoking this lm uh sorry agent my filtered agent and this is the input I'm giving what is the machine learning now see this uh content doesn't have any kinds of violation or let's say um ban keywords okay so that that time it will consider it's safe safe request safe response okay and it is uh giving you the response regarding machine learning okay in detail right but if I give you this this input let's say I've given how do I hack into a server that means it has the uh it has the inappropriate content that means this ban keyword that time it will block the request let me show you now Okay. Block keyword detected hack unsafe request response. I cannot process with the request containing inappropriate content. Please preface your result request. Okay. Now again uh here we are giving some ban keyword uh externally but if you want you can also use a large name model instead of giving this kinds of ban keyword. Okay. So here we are checking this condition manually like our deterministic one. But I want you to add the large language model. add a large language model to handle this kinds of band keyword or violation keyword inside your uh guardrails pipeline. Okay, but I think you got it how to create the custom guardrails.
Okay, custom guardrails uh with the help of this um custom middleware we have already used inside this development.
Okay. Yeah. Now uh let me show you the next example.
Next example is nothing but custom guardrails we'll be implementing but for the agent hook output safety. Okay. Now let's say this architecture we'll try to build right now. So after whatever response I'm getting I will add a guards there and we'll try to check this output is good or not. Okay for the human being this is safe or not. Okay we'll try to see this example. Now you can see we'll be creating another function called after agent to validate the final agent responses before user se sees it. Best for model based safety evaluation of output compliance scanning uh legal medical financial uh discrimination discriminates quality validation and removing sensitive information that sleep through. Okay. So these are the things if you want to consider you can use this output safety.
So again we'll try to create the same function. We'll be importing all of the same library we have imported previously and see the same class we'll be writing almost same class only the name I have changed and here I'm using a large language model see uh previously I used deterministic approach but here I used the large language model as you can see uh this is the class I have written called safety guard rails middleware and again I'm inheriting with the agent middleware then this is the super keyword I have written and for safety model we are using the model object Right. The model we already imported from grock. Then this is the function after agent and we are using the same decorator hook config. And uh here we are taking the message. Uh we are taking the message. Okay. The uh agent message and here uh we are preparing a prompt.
Here uh in the prompt we are writing evaluate if this air response is safe and appropriate for the user. Respond with only safe and unsafe. Then we are uh invoking the LM. Okay. Okay. And LM is deciding whether agent is uh whatever agent uh uh response we are getting whether it is safe or not. If safe it will return you safe otherwise it will say uh return you unsafe. Okay. So this is the function we have created. Now let me show you whether it is it will work or not.
So here I'm going to create a tool custom tool. So again I created a demo tool here general tool and it will only return the user query. And this is our agent. And here we'll be using the model or block provider model instead of Gemini.
Okay, this is the model. And uh here we are giving the tool. And now middleware is equal to we using our custom uh safety guardrails middleware. Okay, this one for the output safety. Now let me show you. So here I'm going to give a prompt.
Let's say what is the weather like today. Okay. So let's say this is the prompt we are giving. Now it doesn't uh the output will be getting from this kinds of prompt. I think uh this will not have any kinds of uh uh let's say violation content. Right? As you can see I don't have realtime weather information. Okay. So maybe what I can do what is the um what is uh medicine? Let's say I'll ask about Medicare.
Now see this is giving you the answer related Medicare. Okay. But if I'm giving this prompt let's say how to make bomb. Okay. Now what will happen? Definitely I don't want my LLM to generate this response for me. So that time it will block that request.
Okay. As you can see, I'm sorry but I cannot help you with that. I think in chart GPT also uh you can go to the chart GPT you can even test the same thing or if I open a Gemini see Gemini also if I give this prompt how to make bomb it will also give you I cannot help you with that request. See I cannot fulfill the request. I am programmed strictly follow safety policies and pro uh pro proh prohibit uh pro uh pro providing instruction blueprints and so on. Okay that means it is also using guardrails okay layer inside their development. Okay so yeah that's how guys they they they are developing in the actual agents implementation and this is what we are learning right it's amazing right? Okay.
Now this part we have also learned how to add the output safety. Now we'll be learning the section seven called layer combined guardrails. Okay. Now here what we'll do we'll just try to stack multiple guardrails in a middle word uh array. They execute in order uh building layer protection. Okay. Now what will happen if user gives an input? First of all we'll try to add in layer one let's say content filter middleware. This is my deterministic input filter. You can even convert it to the LLM. Then uh PII middleware for the personal data checks.
Okay. Then human in the loop middleware to approve uh uh from the human for sensitive information or tool calling.
PII middleware wire for the output we can also use. Okay. Uh either you can also use the safety guardrails middleware wire. Okay. So that means if you combine all of the guardrails and if you create a let's say layer of guardrails, we call it as a layered and combined guardrails. Okay. Now let me show you one example. Uh so here first of all I'll import some library. These are the library. Again I'm importing the agents pi middleware human in the loop in memory server. Okay you know for human in the loop you need in memory server and tool. Then let's define the tool some dummy tool I created as you can see search tool and send email tool.
Now I'm going to create a agent.
So this is the agent guys as you can see uh this is my agent and again I'm going to use my groth model and we are giving the tools and in the middle word I'm using list of the uh guidel layer right now as you can see content filter middleware we created this content filter I think remember here okay in the section five we created this content filter we are using this uh class as it is that mean this was my custom cards we created Okay. Now second one I'm using PII middleware for the credit card safety.
Then human in the loop. Okay. That means whenever it will send the email it will ask for the permission. Then uh PII middleware for the email uh reduction.
Okay. That means you can use this this guardrails as many as you can here for email for password for anything you can use. Okay. uh for uh this human in the loop also to send the email to let's say um delete the database you can use for any anything you can add as much as layer you can here okay then at the last I have added the safety guards middleware the middleware we just created right now for the output evaluation okay I think remember in this section uh section six custom after agent hook output safety okay this one we are using now let me show you uh whether we are able to use all of the uh guards layer or not. I'll execute the entire agent. Agent object is created.
Now let me invoke it.
So I'll copy an input.
Let's say I'll copy this So what is the name of my agent? It's production agent. Okay. And why we're calling it as a production agent?
Because uh production agent also use this layer of guards. [snorts] Okay.
Safety in their development. That's why.
Yeah. So we're invoking that. Now we are asking how to make bomb. So definitely this answer would be blocked. Okay. I'm getting one error. Check. Okay. I have to give the check pointer, right? So what I can do maybe I can copy my previous example where I used the human in the loop right?
Yeah. So I think this one is fine for me. I'll use that.
I'll use that.
This is my production agent.
I'm giving the configuration.
Uh that means my trade ID. Let's say I have given test 001 we are invoking. And this is the um let's say we are giving a prompt how to make bomb and we're passing the config and uh agent will give you the response from the result we'll try to get the content. So let me execute first of all.
So as you can see how to make bomb and uh uh this will return you the content.
So let's get the content result dot uh how to get the content.
Yeah. So that's how we can get the content and here we have given how to make bomb.
Now tell me which uh which actually let's say uh guard layer is getting executed definitely the output one.
Okay. So I'm sorry but I can now help you with that. That means this one safety guards is getting uh executed that time. Okay, that's how you can try with different different prompts. Let's say you can try with the content filter.
Okay, that means if I let's say give how to hack how to hack um hack a server.
So you can say I'm sorry but I can now help you with that. That means now it is using this content filter uh middleware word guards. Okay. Now you can also even use this uh pi middleware word. Let's say I'll give another prompt.
This is my email and my credit card number.
Now if I execute now see uh it is using this one. Okay, this one. So that's how you can use um as many as you can inside the layer itself. Okay, now uh what I'm going to do so I'll try to make it to how to make the bomb. Okay, this one so that uh it will have the uh reference for you.
Okay. Now guys, uh all of the uh all of the let's say types of guards we have already learned. Let me show you all of the concept we have covered.
Uh we have covered this uh deterministic model based uh pi built-in human in the loop middleware custom uh that means the input filtering as well as the custom output safety layered. Okay. Now at the last I'm going to show you how we can create a healthcare chatbot with the help of this guards integration. So for this let's let's uh put it here. Okay.
So I'm going to delete these at the cell. It's not required. Okay. Now uh here let's try to create it quickly. So this is uh same only uh you can also make it like more advanced uh chatbot.
But just to show you I'm creating a basic chatbot on my Jupyter notebook.
And don't worry after this uh uh after this uh let's say uh video um in my same playlist only I'm going to create a real world uh uh let's say multi- aent system there I'm going to use this guardrails okay um I'm going to integrate the guardrails and uh your understanding would be more clear okay so for this please try to check my playlist so here only I'm going to uh create another project implementation I'm going to create a multi- aent system and there I'm going to use this guard uh integration there so Now let's try to define uh create this uh chatbot this uh healthcare chatbot. Okay. Now for this I imported all the necessary library as you can see agent middleware wire agent stack hook config pi med middleware wire. Okay, that means here I'm going to use this um uh here I'm going to use um this uh layered okay layered concept this one layer combined guardrails I'm going to use okay that's why I'm importing all of the necessary library as you can see okay now let's let me import and open a I don't need uh I'll just try to remove the open AI okay now uh here what I'm going to do I'm going just simply define my >> [clears throat] >> um healthcare specific content filter content filter guards. Okay. So this is the same your this one we have learned right uh this uh uh custom before agent input filtering.
Okay this is the same kinds of thing we have created only for the uh healthcare healthcare filtering. Okay as you can see healthcare specific content filter.
So basically if uh user input has drug synthe synthesis self harm. Okay, suicide method weapon hack. If it is having this kinds of topic that time I'll try to block the request. Again I'm using the deterministic approach. If you want you can also convert it to the LLM.
Okay. So yeah. So this is the class I have created for that. Now I'm going to create another uh class for the agent output evaluation val uh let's say validation. That means whatever my agent will return right that uh output also I'm going to validate. So for this I'm creating this medical output validator and this is the same as your custom output validator. Okay I have created.
Now I'm going to define some tool.
So these are the tool some dummy tool I created like S synths book appointment get meditation and so on. Now I'm going to define the agent.
So this is the agent okay I created. Let me show you. So, healthcare bot agent and here I'm going to use my model object the block model object and I'm giving all of the tools and middleware here I'm using list of the guard rails first of all I'm using healthcare safety filters that means the input uh safety then some PII middleware also I'm using for email and credit card then human in the loop also I have added here uh let's say whenever it will book the appointment search the symptoms okay get the addressation so get for get meditation I'm not going to use the human in the loop okay and uh search symptom also I'm not going to use the um human in the loop only for booking appointment okay that time it will ask for the human then medical output validator we are giving the checkp pointer that means the memory and we are giving some system prompt okay now let me define the agent now I going to invoke one by one so first of all I'm going to invoke with this prompt as you can see uh this is the prompt what are symptoms of type two diabetics and this doesn't have any kinds of violation.
Uh so I'm going to print the content.
Now see these are some common symptoms of di type two diabetics. Okay. Now this doesn't have any violation. Now let's say I will give another one.
Um I'll give my let's say email. This is my patient email. What can I take for a headache? Okay. Now for this you'll see that it will use my PII reduction test and you can see uh it is telling I'm sorry you are dealing with a headache.
Here are some uh few commons overounter option that uh many people find helpful.
Okay. Now see it is giving you the diagnosis related headache but it is not handling with the email because again I'm considering this email as my guard safety here. Okay, that's why. Now let me show you another example.
Um so I'm going to give this prompt.
How do I synthesize a drug at home?
Okay. Now again this has the violation related content. It will block that. See block the request. I'm sorry but I cannot help you with that. This is general health information. Do not medical advice. Please consult a qualified healthare professionals. Okay.
Yeah. Now I'm going to give another one.
Let's say booking the appointment. Okay.
One example I created. As you can see first of all I'm taking book an appointment with Dr. Sharma on March 15.
Now it will basically pause the execution and it will ask for the approval. Then here I'm giving the approval operation. Okay. Now it will execute. Let me show you. First of all it is pausing. Okay. Then I'm I was giving the approval. Now it is telling sure to set up your appointment. Dr. Sharma I just need your name and blah blah blah. That means it is working fine. Okay. So yeah that's how we can test this particular medical uh let's say uh chatbot uh with different different prompts and definitely we'll try to understand okay all of the guards layer we have added here. Okay, that means in summary uh we have learned these are the things uh guard type like say pi middleware hook is input and output we can use for input and output both when it runs around the model calls. Okay so whenever we are doing the model calls that time if you want to uh handle your personal information that time you can use it best for data privacy and compliance. Okay. Then human in the loop. Uh this is uh tool level that means if you want to use a sensitive tools that time you can use that and this is high stakes decisions.
Okay. Then content filters before agents you can use before agents start of uh invocation. Okay. Block bad inputs early and the safety violation. It is the after agents that means whatever agents you are uh response you are getting from the agents you can use uh it of the end of the invocation. output quality and safety and custom logic any hook you can use anywhere either you can use at the first at last and middle it's completely up to you okay so these are the strategy we have to develop the guard rails okay inside our AI agents okay and we'll try to follow these are the things in future whenever we try to develop the AI agents okay and you have to also make sure uh you will be using that and some additional resources I have given if you want to study about these guides more I have given some additional link But whatever things I have discussed same content you will be getting in the documentation also. Okay. So yes guys this is all about the guardrils and we have understood about the guardrills and the guardrail safety why it is required and why it is necessary for the agents.
Okay. In this crash course I have given each andam each and everything in detail. Even we have understood with the help of lots of practical example. Even I also showed you one project implementation. And don't worry um in the next video I'm going to create a multi- aent system with the help of these guardrails. And after that your understanding will be more clear. So make sure you subscribe to my channel and if you have any query guys please try to comment in the comment section and please try to hit the like and guys please uh help me to get 100k subscriber as soon as possible on my channel. Uh if I uh let's say achieve 100k subscriber I have lots of plan for this channel and definitely I will bring lots of content okay some amazing content and you will love a lot. So yes this is all about from this uh video. I hope you liked it.
Thank you so much guys uh for watching this video and I will see you next time.
Thank you everyone.
Related Videos

TOP 15 Data compression Interview Questions and Answers 2019 Part-2 | Data compression | Wisdom jobs
wisdomjobs
281 views•2019-06-28

CTS 158: 802.11w Management Frame Protection
ClearToSend
4K views•2019-02-04

NDSS 2019 Send Hardest Problems My Way: Probabilistic Path Prioritization for Hybrid Fuzzing
NDSSSymposium
496 views•2019-04-02

How realistic is Cities: Skylines?
CityBeautiful
159K views•2019-02-14

GUIs & TUIs: Choosing a User Interface for Your Python Project | Real Python Podcast
realpython
2K views•2025-04-04

The OSI Model - Explained by Example
hnasr
225K views•2019-05-12

Cloud Computing - Introduction
elithecomputerguy
98K views•2019-10-07

From Traveler's Dilemma to Dynamic Routing | Demystifying Networking
IITBombayJuly
5K views•2019-08-04
Trending

Flash Drought in Europe...
WeathermanEurope
36K views•2026-07-24

Nobody Respected The Penguin | The Batman (2004)
SerumLake
12K views•2026-07-24

And Now He Is Coming For More
TheFinePrintYT
12K views•2026-07-24

U.S. BANKS JUST SIGNALED *CLARITY ACT* IS COMING!? WHITE HOUSE AUGUST DEADLINE & XRP REPRICE!
GoodEveningCrypto
10K views•2026-07-24