"In this guide, we’ll explore how to use the o1 model, specifically o1-preview, to perform data validation through reasoning. We’ll walk through a practical example involving a synthetic medical dataset and demonstrate how to assess the model’s accuracy in identifying issues within the data."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Overview\n",
"\n",
"Data validation is a critical step in ensuring the quality and reliability of datasets, especially in sensitive fields like healthcare. Traditional validation methods often rely on predefined rules and patterns. However, advanced models like o1 can understand context and reason about data, offering a more flexible and intelligent approach to validation.\n",
"\n",
"In this tutorial, we will:\n",
"- Generate a synthetic dataset of medical data that contains inconsistencies.\n",
"- Define a function that takes in a row of data and validates its accuracy\n",
"- Run the validation process and compute accuracy metrics.\n",
"We will use a lot of the principles described in the [Synthetic Data Generation](https://cookbook.openai.com/examples/sdg1) cookbook to create the foundation of our dataset.\n",
"We will prompt the model to generate sets of medical data for our use case. We have provided detailed instructions to the model on how to create the dataset, what format to follow, and how to fill it with inaccuracies. We also provide a few rows of sample data to get the model started. \n",
"Each row in the dataset will have the following fields:\n",
"- Patient ID: A randomly generated patient id\n",
"- Date of Birth: Date of birth of the patient\n",
"- Gender: M/F\n",
"- Medical History: Past diagnoses\n",
"- Current Medications: Medication the patient is taking\n",
"- Allergies: Identified allergies\n",
"- Lab Results (Glucose mg/dL)\n",
"- Diagnoses: Current diagnosis\n",
"- Treatment Plan: Current treatment plan\n",
"- Is Valid: Whether or not the current row of data is valid (True/False)\n",
"- Issue: If the row of data is not valid, what the issue is\n",
"\n",
"Some examples of inaccuracies that may be present in the data are:\n",
"- Prescribing medications that the patient is allergic to\n",
"- Current medications do not match medical history\n",
"- Treatment plan does not match diagnosis"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def generate_data():\n",
" messages = [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"\"\"\n",
"You are a helpful assistant designed to generate data. You will be given a format for the data to generate and some examples of the data.\n",
"\n",
"When generating Patient IDs, use the format 'P' followed by a three-digit number (e.g., P006, P941, P319).\n",
"\n",
"Intentionally make some mistakes in the data generation and document them in the appropriate columns ('Is Valid' and 'Issue') if the row of data is invalid.\n",
"\n",
"The types of mistakes to include are:\n",
"\n",
"- **Allergy Contradictions**: Prescribing a medication that the patient is allergic to (e.g., prescribing Penicillin to a patient allergic to Penicillin).\n",
"- **Medical History and Medication Mismatch**: A patient with a medical condition not receiving appropriate medication (e.g., a diabetic patient not prescribed any diabetes medication).\n",
"- **Lab Results and Diagnosis Mismatch**: Lab results that do not support the diagnosis (e.g., normal glucose levels but diagnosed with Diabetes Type 2).\n",
"- **Other Plausible Mistakes**: Any other realistic errors that could occur in medical records, such as incorrect gender entries, impossible dates of birth, or inconsistent treatment plans.\n",
"\n",
"Ensure that when 'Is Valid' is 'False', the 'Issue' column clearly explains the problem.\n",
"\n",
"Return 100 rows of data for the user. Your response should strictly be in the format of a valid CSV.\n",
"\n",
"Generate Synthetic Medical Records Dataset with the following columns:\n",
" - Patient ID: A randomly generated patient id\n",
" - Date of Birth: Date of birth of the patient\n",
" - Gender: M/F\n",
" - Medical History: Past diagnoses\n",
" - Current Medications: Medication the patient is taking\n",
" - Allergies: Identified allergies\n",
" - Lab Results (Glucose mg/dL)\n",
" - Diagnoses: Current diagnosis\n",
" - Treatment Plan: Current treatment plan\n",
" - Is Valid: Whether or not the current row of data is valid (True/False)\n",
" - Issue: If the row of data is not valid, what the issue is\n",
"P006,1978-12-05,M,Hypertension; Diabetes Type 2,Lisinopril; Insulin,None,55,Diabetes Type 2,Adjust insulin dosage,False,Low glucose level not properly addressed\n",
"# Append the generated data to the medicalData.csv file\n",
"with open('../data/medicalData.csv', 'a', newline='') as csvfile:\n",
" csvwriter = csv.writer(csvfile)\n",
" for row in generated_data:\n",
" csvwriter.writerow(row.split(','))\n",
"\n",
"print(\"Synthetic data generation and appending completed.\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Data Validation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that we have our dataset prepared, we will prompt the reasoning model to review each row of data and determine whether or not it contains an issue. We will ask the model to output whether or not there is an issue in the data and then offer an explanation of the issue.\n",
"\n",
"Once we have the model determine its list of invalid data, we will pass those results on to a model grader to assess two metrics:\n",
"- Accuracy of the model's ability correctly identify issues with the data\n",
"- For the subset of data that issues have been correctly identified, what is the accuracy of the model in identifying the issue at hand\n",
"\n",
"Given that this task is much more narrow, we can use the faster gpt-4o model to calculate the accuracy.\n",
"\n",
"REMINDER: Given that these models are still in beta, rate limits will be significantly reduced. Please adjust the number of concurrent workers accordingly."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def validate_data(input_data):\n",
" messages = [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": f\"\"\"\n",
"You are a helpful assistant designed to validate the quality of medical datasets. You will be given a single row of medical data, and your task is to determine whether the data is valid.\n",
"\n",
"- Carefully analyze the data for any inconsistencies, contradictions, missing values, or implausible information.\n",
"- Consider the logical relationships between different fields (e.g., treatments should be appropriate for the diagnoses, medications should not conflict with allergies, lab results should be consistent with diagnoses, etc.).\n",
"- Use your general medical knowledge to assess the validity of the data.\n",
"- Focus solely on the information provided without making assumptions beyond the given data.\n",
"\n",
"**Return only a JSON object** with the following two properties:\n",
"\n",
"- `\"is_valid\"`: a boolean (`true` or `false`) indicating whether the data is valid.\n",
"- `\"issue\"`: if `\"is_valid\"` is `false`, provide a brief explanation of the issue; if `\"is_valid\"` is `true`, set `\"issue\"` to `null`.\n",
"\n",
"Both JSON properties must always be present.\n",
"\n",
"Do not include any additional text or explanations outside the JSON object.\n",
"You are a medical expert assistant designed to validate the quality of an LLM-generated answer.\n",
"\n",
"The model was asked to review a medical dataset row to determine if the data is valid. If the data is not valid, it should provide a justification explaining why.\n",
"\n",
"Your task:\n",
"\n",
" •\tCompare the model-generated justification with the correct reason provided.\n",
" •\tDetermine if they address the same underlying medical issue or concern, even if phrased differently.\n",
" •\tFocus on the intent, medical concepts, and implications rather than exact wording.\n",
"\n",
"Instructions:\n",
"\n",
" •\tIf the justifications have the same intent or address the same medical issue, return True.\n",
" •\tIf they address different issues or concerns, return False.\n",
" •\tOnly respond with a single word: True or False.\n",
"\n",
"Examples:\n",
"\n",
" 1.\tExample 1:\n",
" •\tModel Generated Response: “The patient is allergic to penicillin”\n",
" •\tCorrect Response: “The patient was prescribed penicillin despite being allergic”\n",
" •\tAnswer: True\n",
" 2.\tExample 2:\n",
" •\tModel Generated Response: “The date of birth of the patient is incorrect”\n",
" •\tCorrect Response: “The patient was prescribed penicillin despite being allergic”\n",
"Below we'll display the subset of rows that we correctly identified contained an issue. For each row, we'll show the predicted vs. true issue and whether or not there is a match"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: left;\">\n",
" <th></th>\n",
" <th>index</th>\n",
" <th>predicted_issue</th>\n",
" <th>true_issue</th>\n",
" <th>issue_match</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>39</td>\n",
" <td>Amoxicillin is prescribed to a patient with Penicillin allergy.</td>\n",
" <td>Patient diagnosed with Type 1 Diabetes is not on any medications and the treatment field lists the diagnosis instead of appropriate treatment.</td>\n",
" <td>Diabetes Type 1 patient not receiving insulin</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>51</td>\n",
" <td>Lab result of 300 indicates hyperglycemia but no diagnosis or treatment is recorded.</td>\n",
" <td>Extremely high glucose level not diagnosed or treated</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>26</td>\n",
" <td>The patient is being prescribed penicillin despite having an allergy to penicillin.</td>\n",
"We can see from the results here that we're able to generate a high precision/recall for issue identification as well as decent accuracy for pinpointing the exact issue in the data.\n",
"\n",
"This should help streamline data validation for eval sets across a variety of domains."