An intelligent voice assistant for hospitals that interacts with patients in real-time over phone calls, using natural language via Twilio <Stream>, OpenAI (STT, Chat, TTS), and FastAPI.
It listens, understands intent, books appointments, and responds instantly — all via a real-time WebSocket pipeline.
When a patient calls the hospital:
- Twilio receives the call and sends live audio over a WebSocket.
- FastAPI receives audio frames and transcribes them using OpenAI STT.
- Transcribed text is passed into OpenAI ChatCompletion with:
- Conversation history
- A function-calling schema
- LLM agents dynamically decide:
- Whether to respond conversationally
- Or invoke a backend function (e.g.,
list_doctors,book_appointment)
- If a function is called:
- The corresponding function in
crud.pyexecutes and updates the database. - The response is passed back to the LLM for rephrasing into natural language.
- The corresponding function in
- The final response is spoken using OpenAI TTS and played to the caller.
- All in under ~1 second per interaction.
Patient → Twilio <Stream> → FastAPI WebSocket /media-stream
└─ Transcribe (STT) → OpenAI Chat w/ memory + function schema
└─ LLM decides → [speak OR function call]
├─ If function call → CRUD + DB → response → Chat
└─ Final response → TTS → Twilio Playback → Patient
LLM agent logic is fully dynamic and declarative:
Function schemas are sent as part of the system prompt. The LLM chooses whether to call a function or not (e.g., list slots, cancel appointment). After function output, LLM wraps it in natural dialog ("You’re booked with Dr. Kim at 3:00 PM"). No conditional if/else logic needed.
- Method Endpoint Description
- GET /doctors List doctors by specialty
- GET /slots?doctor_id=1 Available time slots for a doctor
- POST /appointments Book a slot for a patient
- POST /tts Convert text to audio (TTS service) All routes are documented at /docs (OpenAPI).
- git clone https://github.com/deepak-pagadala/Lilly-Hospital-Assistant.git
- cd hospital-voice-assistant
- python -m venv env
- source env/bin/activate
- pip install -r requirements.txt
- cp .env.example .env edit with your DB + OpenAI + Twilio credentials
- uvicorn app.main:app --reload --port 8010
- 📞 Connect Twilio Voice Stream : Use wss://your-domain/media-stream as the stream URL in Twilio console (enable dual-channel + mute audio).
- FastAPI – backend API + WebSocket engine
- Twilio – call streaming and audio playback
- OpenAI Whisper – speech-to-text
- OpenAI GPT-4o – dynamic conversation and function calling
- OpenAI TTS (Alloy voice) – text-to-speech response
- PostgreSQL + SQLAlchemy – persistent doctor/slot/appointment DB
- n8n – external automation via REST API
- User Authentication (JWT)
- Admin Dashboard (for CRUD and analytics)
- Multilingual Support
- GPT-function logs for supervision
- Retry queue for failed audio responses
Patient asks: “Do you have a cardiologist tomorrow afternoon?”
LLM recognizes specialty and date → calls list_doctors and list_slots
Responds with: “Yes! Dr. Samir is available at 2:30 and 3:00 PM. Would you like me to book it?”
