In today's healthcare landscape, empowering patients with relevant information is crucial for better health outcomes and engagement. What if we could seamlessly deliver diagnosis-specific educational materials to patients immediately after their doctor's appointment?
This blog post explores how to implement an AI agent using the powerful automation platform n8n.io to achieve just that. By leveraging HL7 messaging, OpenAI's MiniGPT-4 model, and SendGrid, we can create a truly personalized and efficient patient education workflow.
For this demo, I have used following tools:
- n8n (to create an AI Agent to automate the workflow)
- OpenAI's MiniGPT-4 model (to process the prompt and generate relevant educational content)
- Mirth Connect (to simulate receiving HL7 SIU messages from EHR with Checked-out status. To keep it simple, we assume the SIU feed as DG1 segment with encounter diagnosis)
- SendGrid (to send formatted email to the patient)
Here's a breakdown of the workflow:

HL7 Trigger: The workflow (outside of our AI agent) begins with incoming SIU S14 HL7 messages on Mirth connect server. We configure this channel to specifically look for messages with the
CHECKEDOUT
status. This ensures the workflow only activates after a patient has completed their appointment. Mirth Connect server then parses the minimum required fields from the HL7 message and transforms a JSON. Mirth calls n8n webhook and posts the JSON.n8n Webhook: The workflow (withing our AI agent) begins with an n8n trigger node (Webhook) that listens for incoming messages from Mirth Connect. This Webhook receives the request JSON like this:
-
Patient Information Extraction: Once a relevant message is received, we use n8n's data manipulation nodes to parse the JSON message and extract key patient information, such as:
- Patient Name, Gender, DOB
- Patient Email Address
- Diagnosis Code (e.g., ICD-10 code)
-
AI-Powered Prompt Generation (OpenAI MiniGPT-4): This is where the intelligence comes in. We'll integrate with the OpenAI API, specifically utilizing the MiniGPT-4 model. We'll construct a dynamic prompt based on the extracted diagnosis. For example:
please suggest some patient education materials for Mr. {{ $json.body.firstName }} {{ $json.body.lastNName }} suffering from {{ $json.body.diagnosis }} whose date of birth is {{ $json.body.dob }} and gender is {{ $json.body.gender }}. Please format the response in HTML that can be sent as an email to the patient.The MiniGPT-4 model will then process this prompt and generate relevant educational content.
-
Email Sending (SendGrid Integration): Finally, we'll integrate with SendGrid, a reliable email delivery service. We'll configure the SendGrid node in n8n to send an email to the patient's extracted email address. The email body will contain the formatted educational materials generated by the AI. The email content looks like this: