Notebooks
M
Meta Llama
SDOH Json Mode

SDOH Json Mode

llamagroqAIvllmmachine-learning3p-integrationsllama2LLMjson-mode-social-determinants-of-healthllama-cookbookPythonfinetuningpytorchgroq-api-cookbooklangchain

Structured Data Extraction: Social Determinants of Health from Clinical Notes with Groq API's Json Mode

Since the dawn of the Electronic Health Record, deriving meaningful insights about the social determinants of health of a patient population has been the holy grail of healthcare analytics. While discrete clinical data (vitals, lab results, diagnoses, etc) is well understood, social determinants - things like financial insecurity, which can determine patient outcomes and barriers to care as much as the patient's clinical chart - are often hidden in clinical notes and unused by analytics departments. While some providers code social determinants using Z codes, these are often too inconsistently documented and many risk models seeking to add a social determinant score will simply default to using zip code as a crude proxy. With the emergence of Large Language Models, AI has the ability to extract and structure meaningful insights from free-text clinical notes at scale, enabling more effective patient outreach, better risk modeling and a more robust understanding of a patient population as a whole.

This notebook shows how we can use Groq API's JSON mode feature to extract social determinants of health from fake clinical notes, structure them into a neat table that can be used for analytics and load them into BigQuery. With JSON mode, we can return structured data from the chat completion in a pre-defined format, making it a great feature for structuring unstructrued data. We will read in each note, ask the LLM to determine if certain social determinant features are met, output structured data and load it into a database to be incorporated with the rest of our clinical data marts.

Setup

[1]

This code block loads in clinical notes from our repository and displays the first note. As you can see, this hypothetical patient has quite a few notable social determinants of health that contribute to their health outcomes and treatment:

[2]

Define System and User Prompts

Crafting clear and effective prompts is crucial for generating valid LLM responses. In our case, we've defined the exact JSON schema for our social determinants of health table we expect the LLM to output and are including it in the system prompt. Then in the user prompt, we will include the entire clinical note in the context window.

[3]
[4]

Executing Chat Completions with JSON Mode

Now that we have our notes and our prompts, let's try running a Groq chat completion with JSON mode enabled on the first clinical note to see if the speedy llama3-8b-8192 model can correctly identify this patient's social determinants. Note that you will need a Groq API Key to proceed and can create an account here to generate one for free:

[5]
{
  "employment_status": "Part-time",
  "financial_stress": true,
  "housing_insecurity": true,
  "neighborhood_unsafety": true,
  "food_insecurity": true,
  "education_level": "High School",
  "transportation_inaccessibility": true,
  "social_isolation": true,
  "health_insurance_inadequacy": true,
  "skipped_care_due_to_cost": true,
  "marital_status": "Divorced",
  "language_barrier": false
}

Looks good! The patient does in fact work part time, is divorced and has expressed concerns pertaining their financial, housing and transportation situations, food insecurity, social isolation and healthcare costs. They do not have a language barrier.

Now, let's wrap this in a function and apply it to the rest of our clinical notes:

[6]
[7]

Nice! In just 4 seconds we've parsed through five clinical notes, extracted discrete features and structured them into a neat table. That low latency is important for scaling up, and is why Groq's best-in-class speed makes it an ideal provider for this type of task - in a healthcare network with many providers, it would allow us to process clinical notes for 900 patients in an hour.

Analyzing Structured Data

Now that our Social Determinants of Health are stored in a neat, structured format, we can analyze them much easier than when they're trapped in unstructured clinical notes. Here is a bar plot showing the percent of the patient population impacted by each social determinant - but with more data we could do far more advanced analyses such as showing which determinants are most correlated with each other, or which ones are most predictive of various chronic conditions or negative health outcomes:

[8]
Output

Loading to a Database

Finally, we will use SQLAlchemy to load the results to our database - in this case, a BigQuery dataset called clinical. In a real production environment, we could use a tool like Airflow to orchestrate the scheduling of this script and process any new notes from recent appointments.

[9]
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 6034.97it/s]

Conclusion

In this notebook, we've used Llama3 with Groq API's JSON mode to extract social determinants of health from and structure them in a relational table, then loaded the results into BigQuery where they can be combined and analyzed with the rest of our patient data. With our social determinants of health now structured in our clinical data warehouse, our analytics team can use it in countless ways by delivering much-needed SDOH insights and enhancing risk models. This allows the clinical practice to not just identify high-risk patients in their population, but to implement more targeted interventions by better understanding their barriers to care.

More broadly, we've shown how to use Groq to build an LLM-infused data pipeline, one that transforms unstructured text data into structured, relational data that can reside in a warehouse. And with Groq's low latency, the ability to process more files per minute makes for a more efficient pipeline.