Skip to content

Latest commit

 

History

History

grok-patient-intake

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Grok Voice Patient Intake

A fictional family-medicine front desk built with LiveKit Agents and the xAI Grok Voice Agent API. The agent can answer practice questions, find and manage appointments, route messages, update insurance, collect pre-visit answers, and escalate possible emergencies.

This example reimplements the workflow of LiveKit's xAI patient-intake demo, but replaces its separately configured speech-to-text, language, and text-to-speech models with one native speech-to-speech connection:

AgentSession(
    llm=xai.realtime.RealtimeModel(
        model="grok-voice-latest",
        voice="Ara",
    ),
    vad=None,
    turn_handling=TurnHandlingOptions(turn_detection="realtime_llm"),
)

Grok listens to the caller's audio, reasons over the conversation, calls the local clinic tools, and speaks the response. There is no separately assembled STT → LLM → TTS cascade, VAD plugin, or turn detector in this example.

The clinic and every patient are fake, in-memory teaching data. This is not a medical device, a source of medical advice, a production scheduling system, or a HIPAA-ready application. Never enter real patient information. The agent repeats this fake-data warning at the start of each call.

Getting Started

Use Python 3.10 through 3.14.

Create a virtual environment and install the worker:

cd voice-agents/grok-patient-intake
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -e ".[dev]"

Create .env.local:

cp .env.example .env.local

Fill in:

LiveKit transports the call and xAI processes its audio, transcript, prompt, and tool context. This worker does not start a recording, but recording can be enabled elsewhere in a LiveKit deployment. Review both providers' current data handling and retention settings before adapting the example.

Talk to the agent directly from the terminal:

python src/agent.py console

Or register the worker with LiveKit:

python src/agent.py dev

Then connect with the LiveKit Agents Playground or another LiveKit client that dispatches the agent named grok-patient-intake. This focused cookbook port does not include the upstream demo's custom Next.js frontend.

Try the fake clinic

The seeded records are recreated for every call:

  • Established adult: Jamie Example, born April 12, 1987.
  • Established child: Riley Example, born September 3, 2018.
  • New patients can use any obviously fake identity and phone number.

Example requests:

  • “What are your office hours?”
  • “I need to move my existing appointment.”
  • “I am a new patient and need an appointment.”
  • “I need a refill message for the nurse.”
  • “I want to complete my pre-visit questions.”

Tool surface

src/agent.py exposes eight typed tools:

Tool Purpose
read_practice_information Read the fictional practice policy
find_open_times Find suitable real slots in the in-memory clinic
book_appointment Register a new patient when needed and book a chosen slot
manage_appointment List, cancel, or reschedule an appointment
take_message Route a refill, results, billing, referral, records, or nurse request
update_insurance Save details from a fictional current insurance card
record_previsit_intake Save a completed set of caller-provided intake answers
record_emergency_escalation Record a possible emergency before urgent direction

src/clinic.py contains the deterministic in-memory records. Tool calls mutate only that per-call object; nothing is persisted or sent to an EHR.

Voice configuration

The defaults follow the current xAI realtime recommendation:

Environment variable Default Purpose
XAI_VOICE_MODEL grok-voice-latest Tracks xAI's recommended realtime voice model
XAI_VOICE Ara Warm, friendly built-in voice

The LiveKit xAI plugin supplies server-side voice activity detection and turn handling; realtime_llm makes that ownership explicit. Its current default ends a turn after 200 ms of silence, so production intake agents should test and tune the realtime model's turn_detection settings for callers who pause while recalling dates, medications, or symptoms. grok-voice-latest is a floating alias; pin a versioned voice model when release-to-release stability is more important than automatically receiving the newest model. See the LiveKit xAI realtime guide for supported voices and lower-level options.

Offline tests

The tests do not connect to LiveKit or xAI and do not consume API credits:

pytest
ruff check .
ruff format --check .

They verify the direct realtime composition, the exact eight-tool surface, fake patient lookup, appointment state changes, new-patient eligibility, message deduplication, intake storage, insurance updates, and emergency recording.

Production safety

A real deployment needs authenticated patient access, consent and disclosure, encrypted durable storage, EHR integration, audit logging, retention controls, human escalation, regional emergency behavior, monitoring, and clinical/legal review. It must also prevent room recording unless recording is explicitly authorized and governed. Prompts alone do not provide those guarantees.