wellallyTechWe've all been there: staring at a complex medical prescription, trying to remember if "twice daily"...
We've all been there: staring at a complex medical prescription, trying to remember if "twice daily" means every 12 hours or just with breakfast and dinner. Even worse is the realization that youβre down to your last pill at 11 PM on a Sunday. Manual reminders are a chore, and inventory management is a headache.
In this tutorial, we are building a smart healthcare agent using AI Agents, Function Calling, and Healthcare Automation. This agent leverages GPT-4-turbo to parse prescription semantics, automatically schedule reminders via the Google Calendar API, and even trigger a restock flow using Puppeteer when your supply runs low.
By the end of this post, you'll have a fully functional prototype that transforms "doctor-speak" into a streamlined, automated health routine.
Before we dive into the code, let's look at how the data flows. Our agent acts as the brain, deciding whether to update your schedule or go shopping based on the context of your input.
graph TD
A[User Input: Prescription Image/Text] --> B{GPT-4-turbo Agent}
B -->|Extract Schedule| C[Function: create_calendar_event]
B -->|Inventory Low| D[Function: trigger_restock_flow]
C --> E[Google Calendar API]
D --> F[Puppeteer / Delivery Platform]
E --> G[User Notification]
F --> H[Order Confirmation]
To follow along, youβll need:
The magic of this build lies in Function Calling. We don't just want the AI to talk; we want it to act. We define a schema that tells GPT-4 exactly how to call our local functions.
const tools = [
{
type: "function",
function: {
name: "schedule_medication",
description: "Schedules medication reminders in Google Calendar",
parameters: {
type: "object",
properties: {
medicationName: { type: "string" },
frequency: { type: "string", description: "e.g., 'twice a day'" },
durationDays: { type: "number" },
startTime: { type: "string", description: "ISO format string" }
},
required: ["medicationName", "frequency", "durationDays"]
}
}
},
{
type: "function",
function: {
name: "check_and_restock",
description: "Checks current inventory and orders more if needed",
parameters: {
type: "object",
properties: {
medicationName: { type: "string" },
currentQuantity: { type: "number" }
},
required: ["medicationName", "currentQuantity"]
}
}
}
];
When GPT-4 determines a schedule, it calls schedule_medication. Here is how we handle that using the official Google SDK:
const { google } = require('googleapis');
async function scheduleMedication({ medicationName, frequency, durationDays, startTime }) {
// Logic to calculate recurring events based on frequency...
const calendar = google.calendar({ version: 'v3', auth: oauth2Client });
const event = {
summary: `π Take ${medicationName}`,
description: `Automated reminder: ${frequency}`,
start: { dateTime: startTime, timeZone: 'UTC' },
end: { dateTime: new Date(new Date(startTime).getTime() + 30*60000).toISOString(), timeZone: 'UTC' },
recurrence: [`RRULE:FREQ=DAILY;COUNT=${durationDays}`],
};
await calendar.events.insert({ calendarId: 'primary', resource: event });
return `Successfully scheduled reminders for ${medicationName}.`;
}
When the agent realizes your stock is low (e.g., "I have 2 pills left"), it triggers Puppeteer to navigate a pharmacy or delivery site.
const puppeteer = require('puppeteer');
async function triggerRestockFlow({ medicationName }) {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://example-pharmacy.com/search');
await page.type('#search-input', medicationName);
await page.click('#submit-search');
// Wait for results and add the first one to cart
await page.waitForSelector('.add-to-cart');
await page.click('.add-to-cart');
return `Added ${medicationName} to your cart on the delivery platform! π`;
}
While this DIY approach is great for learning, production-grade AI agents require robust error handling, HIPAA compliance, and state management.
For more production-ready examples and advanced patterns on building secure healthcare AI, I highly recommend checking out the WellAlly Tech Blog. They dive deep into the nuances of LLM orchestration and data privacy that are crucial for real-world deployments.
Finally, we create the main loop where the LLM processes the user's prescription text.
async function runPharmacyAgent(userInput) {
const response = await openai.chat.completions.create({
model: "gpt-4-turbo",
messages: [{ role: "user", content: userInput }],
tools: tools,
tool_choice: "auto",
});
const toolCalls = response.choices[0].message.tool_calls;
if (toolCalls) {
for (const toolCall of toolCalls) {
const functionName = toolCall.function.name;
const args = JSON.parse(toolCall.function.arguments);
if (functionName === 'schedule_medication') {
const result = await scheduleMedication(args);
console.log(result);
} else if (functionName === 'check_and_restock') {
const result = await triggerRestockFlow(args);
console.log(result);
}
}
}
}
// Example usage:
runPharmacyAgent("I just got a prescription for Amoxicillin, 500mg. Take 3 times a day for 7 days starting tomorrow morning. I only have 2 pills left from an old batch.");
We've just built a bridge between messy, real-world medical instructions and structured, automated actions. By combining GPT-4's reasoning with Puppeteer's execution and Google Calendar's scheduling, we've created a prototype that solves a real human problem.
What's next?
gpt-4-vision.If you enjoyed this build, don't forget to Subscribe and let me know in the comments: What would you automate next with Function Calling? π₯
Happy hacking! π