Building Vectus AI: An Intelligent Medical Scheduling Assistant with OpenAI GPT-4

February 5, 2025
AI
NLP
Node.js
OpenAI
GPT-4
JavaScript
Building Vectus AI: An Intelligent Medical Scheduling Assistant with OpenAI GPT-4

Introduction

In the fast-paced world of healthcare, efficient scheduling is crucial for both patients and providers. Vectus AI is an intelligent medical scheduling assistant designed to streamline the appointment booking process using natural language processing (NLP) and OpenAI's cutting-edge GPT-4 language model. This blog post will take you on a technical journey through the development of Vectus AI, from its initial concept to the final implementation.

Architecture and Design

Vectus AI is built on a Node.js backend using the Express web framework. The architecture is designed to be modular and extensible, allowing for easy integration with various components and services.

The key components of Vectus AI include:

  1. Express Server: Handles incoming HTTP requests and routes them to the appropriate handlers.
  2. OpenAI GPT-4: Powers the natural language understanding and generation capabilities, enabling Vectus AI to process user queries and provide contextually appropriate responses.
  3. Mock CRM Database: Simulates a customer relationship management (CRM) system to store and retrieve patient interactions.
  4. AI Qualification Engine: Implements the core logic for qualifying leads and scheduling appointments based on predefined rules and availability.

Implementation Details

Let's dive into some of the key implementation details of Vectus AI.

Express Server

The Express server is the entry point of the application. It sets up the necessary middleware, such as body-parser for parsing JSON request bodies, and defines the API endpoints.

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());

// Define API endpoints
app.post('/api/message', async (req, res) => {
  // Handle incoming messages
});

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Vectus AI running on port ${port}`);
});

Real-World Interactions and Logging

Let's look at how Vectus AI handles a real patient interaction. Here's an example of the system processing a patient's complaint about ankle pain:

CRM Update: {
  "id": 1739368593728,
  "phone": "web-user",
  "status": "new",
  "interactions": [
    {
      "timestamp": "2025-02-12T13:56:33.728Z",
      "inbound": "I have ankle pain",
      "outbound": {
        "score": 8,
        "qualified": true,
        "response": "I'm sorry to hear that you're experiencing ankle pain. It's important to have that checked by a medical professional. Shall I schedule an appointment for you? Please provide me with your day and time preferences.",
        "nextStep": "appointment_scheduling",
        "shouldTerminate": false
      }
    }
  ]
}

In this interaction, we can see several important aspects of how Vectus AI works:

  1. Initial Response: The system immediately acknowledges the patient's concern and shows empathy.
  2. Qualification Score: A score of 8 indicates this is a high-priority case that requires medical attention.
  3. Next Steps: The system transitions to appointment scheduling mode, ready to book a visit.
  4. Interaction Tracking: All interactions are logged with timestamps and unique IDs for future reference.
  5. State Management: The conversation status is tracked, allowing for continuous dialogue.

This structured logging helps monitor the system's performance, track patient interactions, and continuously improve the AI's responses.

OpenAI GPT-4 Integration

Vectus AI leverages the power of OpenAI's GPT-4 language model to understand and generate human-like responses. The analyzeMessage function sends the user's message and the last interaction context to the GPT-4 API for processing.

async function analyzeMessage(text, lastInteraction) {
  const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY
  });

  const systemPrompt = `...`; // Detailed system prompt

  const response = await openai.chat.completions.create({
    model: 'gpt-4',
    messages: [
      { role: 'system', content: systemPrompt },
      { role: 'user', content: text }
    ],
    temperature: 0.7,
    max_tokens: 150
  });

  return JSON.parse(response.choices[0].message.content);
}

AI Qualification Engine

The AIQualifier class encapsulates the core logic for qualifying leads and scheduling appointments. It maintains the availability slots, checks for specific phrases, and handles the conversation flow based on the user's responses.

class AIQualifier {
  constructor() {
    this.availability = { ... };
    this.checkingPhrases = [ ... ];
    this.appointmentDetails = new Map();
  }

  processMessage(message, lastInteraction) {
    // Handle conversation flow based on last interaction
    // ...

    // Initial conversation starters
    // ...

    return {
      score: 3,
      qualified: false,
      response: '...',
      nextStep: 'qualify'
    };
  }
}

Challenges and Solutions

During the development of Vectus AI, I encountered several challenges:

  1. Handling Complex Conversations: To manage the intricacies of medical scheduling conversations, we implemented a state-based approach using the nextStep property. This allowed Vectus AI to keep track of the conversation context and respond accordingly.

  2. Integrating with OpenAI GPT-4: Integrating with the OpenAI API required careful handling of API keys, request formatting, and response parsing. We encapsulated the API integration logic in the analyzeMessage function to keep the codebase clean and maintainable.

  3. Simulating a CRM Database: To showcase the scheduling functionality without a real CRM system, we created a MockCRM class that simulates the behavior of a database. This allowed us to focus on the core scheduling logic while providing a realistic demo experience.

Future Improvements

Vectus AI has immense potential for further enhancements:

  1. Integration with Real CRM Systems: Integrating Vectus AI with popular CRM platforms like Salesforce or HubSpot would enable seamless synchronization of patient data and appointments.

  2. Multi-Language Support: Extending Vectus AI to support multiple languages would make it accessible to a broader audience and cater to diverse patient needs.

  3. Personalized Recommendations: Leveraging patient data and medical history, Vectus AI could provide personalized recommendations for preventive care, follow-up appointments, and health screenings.

  4. Voice-Based Interaction: Integrating voice recognition and synthesis capabilities would enable patients to interact with Vectus AI using natural speech, enhancing accessibility and convenience.

Conclusion

Vectus AI showcases how modern language models can transform healthcare scheduling through natural conversation. The combination of GPT-4's understanding and a robust backend architecture creates a system that handles medical appointments with both efficiency and empathy.

While the current implementation focuses on appointment scheduling, the foundation is laid for expanding into more comprehensive healthcare management features. The modular design allows for easy integration with real CRM systems and additional healthcare services as the project evolves.