15. Telemedicine Integration

Chapter 15 of 18 · 20 min

Telemedicine platforms generate substantial unstructured data: video transcripts, chat logs, symptom descriptions, and follow-up instructions. Local AI integration can process this data for clinical documentation, coordinate with in-person care teams, and enhance remote monitoring.

Integration architecture requires careful attention to data flow: telemedicine systems often operate across network boundaries, and PHI handling must remain compliant regardless of integration point.

# telemedicine_integration.py
from dataclasses import dataclass
from typing import List, Optional, Dict
from datetime import datetime
import asyncio

@dataclass
class TelemedicineEncounter:
    encounter_id: str
    patient_id: str
    encounter_type: str  # video, audio, chat
    start_time: datetime
    end_time: datetime
    transcript: Optional[str]
    chat_log: Optional[List[dict]]
    shared_images: Optional[List[str]]
    provider_id: str

class TelemedicineAIIntegration:
    """Integrate local AI with telemedicine workflows."""
    
    def __init__(self, ollama_client, emr_connection):
        self.ollama = ollama_client
        self.emr = emr_connection
        
    async def process_encounter(self, encounter: TelemedicineEncounter) -> dict:
        """Process telemedicine encounter with AI."""
        
        results = {
            "encounter_id": encounter.encounter_id,
            "processed_at": datetime.utcnow().isoformat()
        }
        
        # Process transcript for clinical data
        if encounter.transcript:
            results["clinical_extraction"] = await self._extract_clinical_data(
                encounter.transcript
            )
            results["note_draft"] = await self._draft_clinical_note(
                encounter.transcript,
                encounter.encounter_type
            )
        
        # Process chat for follow-up needs
        if encounter.chat_log:
            results["follow_up_items"] = self._extract_follow_up(
                encounter.chat_log
            )
        
        # Analyze shared images
        if encounter.shared_images:
            results["image_analysis"] = await self._analyze_shared_images(
                encounter.shared_images
            )
        
        return results
    
    async def _extract_clinical_data(self, transcript: str) -> dict:
        """Extract structured clinical data from transcript."""
        
        prompt = f"""Extract clinical information from this telemedicine encounter transcript.
        
        Include:
        - Chief complaint
        - Symptoms and their characteristics
        - Relevant history mentioned
        - Assessment impressions
        - Plan items
        
        Transcript:
        {transcript[:4000]}  # Limit for context window
        
        Return structured JSON."""
        
        loop = asyncio.get_event_loop()
        response = await loop.run_in_executor(
            None, 
            lambda: self.ollama.generate(prompt)
        )
        
        import json
        try:
            return json.loads(response)
        except:
            return {"extraction_error": "Failed to parse response"}
    
    async def _draft_clinical_note(self, transcript: str,
                                     encounter_type: str) -> str:
        """Draft clinical note from encounter transcript."""
        
        template = {
            "video": "Generate a clinical note from this video telemedicine encounter.",
            "audio": "Generate a clinical note from this phone telemedicine encounter.",
            "chat": "Generate a clinical note from this asynchronous messaging encounter."
        }.get(encounter_type, "Generate a clinical note.")
        
        prompt = f"""{template}
        
        Include appropriate telemedicine documentation elements:
        - Technology used for encounter
        - Patient location during encounter
        - Visual/audio assessment where applicable
        
        Transcript:
        {transcript[:4000]}
        
        Format as a clinical note with appropriate sections."""
        
        loop = asyncio.get_event_loop()
        response = await loop.run_in_executor(
            None,
            lambda: self.ollama.generate(prompt)
        )
        
        return response
    
    def _extract_follow_up(self, chat_log: List[dict]) -> List[dict]:
        """Extract action items from chat conversation."""
        
        chat_text = "\n".join([
            f"{msg.get('sender')}: {msg.get('content')}"
            for msg in chat_log
        ])
        
        prompt = f"""Extract follow-up items from this chat conversation.
        
        Identify:
        - Patient questions that need answers
        - Information the provider promised to send
        - Scheduling needs mentioned
        - Referrals or orders discussed
        
        Chat:
        {chat_text}
        
        Return as JSON array of action items."""
        
        response = self.ollama.generate(prompt)
        
        import json
        try:
            return json.loads(response)
        except:
            return []
    
    async def _analyze_shared_images(self, image_paths: List[str]) -> List[dict]:
        """Analyze images shared during telemedicine encounter."""
        
        results = []
        for path in image_paths:
            # Read image
            with open(path, "rb") as f:
                import base64
                image_data = base64.b64encode(f.read()).decode()
            
            prompt = """Describe what you see in this image.
            If it appears to be a medical image or photo relevant to health:
            - Describe notable features
            - Note any concerning findings
            - Indicate if follow-up or in-person evaluation recommended"""
            
            loop = asyncio.get_event_loop()
            response = await loop.run_in_executor(
                None,
                lambda: self.ollama.chat(
                    model="llama3.2-vision",
                    messages=[{
                        "role": "user",
                        "content": prompt,
                        "images": [image_data]
                    }]
                )
            )
            
            results.append({
                "image_path": path,
                "analysis": response["message"]["content"]
            })
        
        return results
    
    async def sync_to_emr(self, encounter_id: str, 
                          processed_data: dict) -> bool:
        """Sync processed encounter data to EMR."""
        
        # Create or update clinical note
        note_data = {
            "encounter_id": encounter_id,
            "note_type": "telemedicine_ai_assisted",
            "content": processed_data.get("note_draft", ""),
            "extracted_data": processed_data.get("clinical_extraction", {}),
            "timestamp": datetime.utcnow()
        }
        
        # Store in EMR
        return self.emr.create_note(note_data)

Telemedicine integration requires HIPAA-compliant data handling across the integration boundary. Local AI processing keeps transcript data on-premises, but image analysis may require local vision models. Verify that all processing maintains compliance regardless of data source.

EXERCISE

Design integration architecture for a telemedicine platform that handles video, audio, and chat encounters. Document data flow for each encounter type and identify compliance touchpoints.