Let's explore how to use guardrails AI framework to systematically process and validate medical data
records derived from doctor's notes.
By employing guardrails, AI's a built in validators.
The application underscores the importance of structured data validation in healthcare information processing.
The program starts by importing necessary modules and packages.
Base model and field from parenthesis are used to define data models with specific attributes and validation
rules.
The guardrails framework is represented by GD, which facilitates the validation process alongside valid
range and valid choices.
Additional imports include OpenAI for accessing AI driven text processing capabilities.
JSON for data formatting and reach for enhanced console printing.
The next step would be to define the output model and prompt and build rail specification using these
two components and creating a guard object.
So in this case, the output models are defined using symptom medication and patient info models.
The symptom model captures.
Medical symptoms experienced by a patient.
Each symptom is associated with an effective body part with validation ensuring only specified areas
such as head, neck and chest are accepted.
This precision is crucial for accurate medical record keeping.
The medication model documents medications a patient is taking, including medication name and patient's
response.
This model is essential for tracking treatment efficiency and patient reactions.
Finally, the patient model aggregates comprehensive patient information including gender, age, a
list of symptoms, and current medications.
Age validation checks are realistic values between 0 to 100 years.
Enforcing logical data integrity.
List symptoms and medication.
Tie back to their respective models.
Ensuring a detailed, validated patient profile.
Now that we have defined the output model, let's construct the AI prompt.
A meticulously crafted prompt instructs the AI to analyze the doctor's notes and extract structured
patient information, aligning with defined pydantic models.
This approach leverages AI's potential to interpret complex medical text, transforming unstructured
data into a structured format for further analysis.
In this case, The doctor's notes will be replaced by the value defined in this variable, which states
45 year old male with chronic macular rash to face and hair worse in beard, eyebrows and nares.
Itchy, flaky, slightly scaly.
Moderate response to OTC steroid system.
And finally, this is built in variable within the Gardel serie framework that would define a structured
JSON example that can help the LLM to create a valid JSON structure.
Once the prompts and output models are defined, this presents the base for creating the guard object.
The guard object is instantiated with patient info model, setting the stage for Garters framework to
enforce data validation based on the model's constraints.
This integration is pivotal for translating AI generated insights into validated structures that adhere
to predefined medical and data integrity standards.
Finally.
The call to an LLM is wrapped into the guard object, which basically invokes a call to LLM and validates
the output and performs necessary action.
In this case, we have defined.
That if the affected area of symptom is outside these three varied choices, the LLM should be re-asked
to generate the output.
This action is performed internally by the guardrail CF framework within this call.
Let's execute this program and understand how it works.
As you can see here.
This is the initial step in which the prompt prepared by guardrails AI framework is given as an input
to LLM.
This includes doctors notes and the output model that we are expecting.
Which includes patients gender age in the varied range.
Symptom.
Valid choices current medications.
And then this is the raw LLM output generated by.
LLM which includes gender symptoms and other information.
This output is validated by cartels.
AI framework against the criteria that has been defined in this case.
As you can see here.
Some of the values doesn't match our valid choices.
So value face and head.
Is not in the choice for the affected area and hence guardrail.
Say I prepare Another call against LM by modifying the prompt, which says some of the input doesn't
match to my criteria.
Can you regenerate the output?
And then LM again creates another output model.
As you can see here which matches our defined standards.
And this output is again validated by the guardrail CI framework.
And then can be presented here.
Since this program demonstrates a holistic approach to health care data processing, leveraging AI capabilities
and stringent data validation to ensure accuracy, compliance and integrity in medical record keeping.