DEV Community

Beck_Moulton
Beck_Moulton

Posted on

Stop Manually Booking Appointments: Building an Autonomous AI Health Agent with Playwright and GPT-4o

We’ve all been there. You get a notification from your smartwatch saying your heart rate has been a bit funky, or your blood oxygen is dipping. Usually, we ignore it until it becomes a problem. But what if your personal AI was looking out for you? 🤖

In this tutorial, we are building an Autonomous Health Agent. This isn't just a notification bot; it's a proactive system that uses Playwright browser automation, OpenAI Function Calling, and Python to monitor your health trends and—if things look suspicious for three days straight—literally opens a browser and books a doctor's appointment for you.

By leveraging Autonomous AI Agents and Playwright automation, we are moving from "Passive Monitoring" to "Active Intervention." This is the future of Health Tech Automation.


🏗 The Architecture

Before we dive into the code, let's look at how the data flows from a "scary heart rate" to a "confirmed appointment."

graph TD
    A[Wearable Data/Health Logs] --> B{3-Day Anomaly Check}
    B -- Normal --> C[Stay Healthy! 🟢]
    B -- Abnormal --> D[Trigger AI Agent 🤖]
    D --> E[OpenAI Function Calling]
    E --> F[Playwright Browser Automation]
    F --> G[Hospital Booking Platform]
    G --> H[Appointment Confirmation 🏥]
    H --> I[Notify User via SMS/Email]
Enter fullscreen mode Exit fullscreen mode

🛠 Prerequisites

To follow along, you’ll need:

  • Python 3.10+
  • Playwright: The king of modern browser automation.
  • OpenAI API Key: For the "brain" of our agent.
  • A healthy dose of curiosity! 🥑
pip install playwright openai pydantic
playwright install chromium
Enter fullscreen mode Exit fullscreen mode

👨‍💻 Step 1: Defining the "Brain" (OpenAI Function Calling)

We don't want the LLM to just "talk" about booking an appointment; we want it to actually execute the action. We'll use OpenAI's Function Calling to bridge the gap between text and code.

import json
from openai import OpenAI

client = OpenAI()

# Define the tool our agent can use
tools = [
    {
        "type": "function",
        "function": {
            "name": "book_doctor_appointment",
            "description": "Books a medical appointment based on department and symptoms",
            "parameters": {
                "type": "object",
                "properties": {
                    "department": {"type": "string", "enum": ["Cardiology", "General Medicine", "Pulmonology"]},
                    "preferred_time": {"type": "string", "description": "Morning or Afternoon"},
                    "reason": {"type": "string"}
                },
                "required": ["department", "preferred_time", "reason"]
            }
        }
    }
]
Enter fullscreen mode Exit fullscreen mode

🌐 Step 2: The "Hands" (Playwright Automation)

Now, let's write the Playwright logic. This function will be called by the Agent to interact with a (mock) hospital portal.

from playwright.sync_api import sync_playwright

def book_doctor_appointment(department, preferred_time, reason):
    print(f"🚀 Launching browser to book {department}...")
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False) # Headless=False so we can see the magic!
        page = browser.new_page()

        # Navigate to the portal
        page.goto("https://mock-hospital-portal.com/booking")

        # Fill out the form
        page.select_option("select#dept", label=department)
        page.fill("textarea#reason", reason)

        if preferred_time == "Morning":
            page.check("input#slot-am")
        else:
            page.check("input#slot-pm")

        # Submit (Commented out for safety in this demo!)
        # page.click("button#confirm-booking")

        print(f"✅ Successfully booked {department} for the {preferred_time}!")
        browser.close()
        return "Booking Successful"
Enter fullscreen mode Exit fullscreen mode

🧠 Step 3: The Orchestrator Logic

The "Agent" logic checks the health data. If the heart rate or SpO2 levels are abnormal for 3 consecutive days, it triggers the AI.

def check_health_and_trigger_agent(health_logs):
    # Logic: If heart_rate > 100 for 3 consecutive entries
    abnormal_count = sum(1 for log in health_logs[-3:] if log['heart_rate'] > 100)

    if abnormal_count >= 3:
        print("🚨 Health Anomaly Detected! Engaging AI Agent...")

        prompt = f"The user has had an elevated heart rate for 3 days. Logs: {health_logs[-3:]}. Please book a Cardiology appointment."

        response = client.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": prompt}],
            tools=tools
        )

        # Check if the tool was called
        tool_call = response.choices[0].message.tool_calls[0]
        if tool_call.function.name == "book_doctor_appointment":
            args = json.loads(tool_call.function.arguments)
            book_doctor_appointment(**args)

# Mock Data: 3 days of high heart rate
health_data = [
    {"day": 1, "heart_rate": 105},
    {"day": 2, "heart_rate": 110},
    {"day": 3, "heart_rate": 108}
]

check_health_and_trigger_agent(health_data)
Enter fullscreen mode Exit fullscreen mode

🏥 Going Beyond: The "Official" Way

While this DIY script is a great "Learning in Public" project, building production-grade healthcare agents requires much stricter security, HIPAA compliance, and robust error handling.

For more advanced patterns on building resilient AI agents and production-ready automation workflows, I highly recommend checking out the WellAlly Tech Blog. They have some incredible deep dives into how AI is being used to bridge the gap between data and real-world clinical actions. It’s been a huge source of inspiration for my recent builds! 💡


🚀 Conclusion

And there you have it! A fully functional (well, "mock" functional) AI Agent that can literally save you a trip to the computer by booking your doctor's visits for you.

Key Takeaways:

  1. Playwright isn't just for testing; it's the "body" of your AI.
  2. Function Calling allows LLMs to interact with the real world.
  3. Proactive AI is 10x more valuable than reactive AI.

What would you automate next? A bot that books a massage when your stress levels are high? Let me know in the comments! 👇

Top comments (0)