Skip to content

multimodal voice agent for medilab #964

Description

@sandeep-cng

You are the Lead Solution Architect, Senior AI Engineer, Senior Backend Engineer,
Senior Frontend Engineer, DevOps Engineer, Security Engineer and QA Engineer
responsible for building a production-ready multimodal AI voice laboratory
booking platform.

PROJECT NAME:
AI Lab Voice Agent

PROJECT OBJECTIVE:

Build an AI-powered telephone voice agent that allows a customer to call a
telephone number and have a natural real-time voice conversation with an AI
agent.

The AI agent must be able to:

  1. Answer an incoming phone call.
  2. Greet the customer naturally.
  3. Identify the incoming caller number.
  4. Determine whether the caller is an existing or new customer.
  5. Authenticate the customer securely when required.
  6. NEVER ask for, reveal, repeat or store passwords.
  7. NEVER expose OTPs to the LLM.
  8. Understand natural spoken language.
  9. Answer general laboratory-related questions.
  10. Search laboratory tests.
  11. Explain test preparation requirements.
  12. Search health-check packages.
  13. Find laboratories/collection centres.
  14. Check real-time test availability.
  15. Check appointment slots.
  16. Help the customer select a slot.
  17. Confirm all important booking information.
  18. Book the appointment only after explicit customer confirmation.
  19. Send booking confirmation through SMS/WhatsApp/email.
  20. Retrieve existing bookings.
  21. Reschedule bookings after explicit confirmation.
  22. Cancel bookings after explicit confirmation.
  23. Transfer the caller to a human agent when required.
  24. Maintain complete audit logs.
  25. Provide an admin dashboard.
  26. Provide call analytics.
  27. Provide conversation and tool-call observability.
  28. Support future multimodal inputs such as images, PDFs and prescriptions.

IMPORTANT:

This is NOT just a chatbot.

Build this as a real-time AI AGENT with:

Voice
+
Reasoning
+
RAG
+
Function Calling
+
Authentication
+
Business Logic
+
Booking Workflow
+
Human Handoff
+
Observability
+
Security

==================================================
REFERENCE ARCHITECTURE

Customer
|
| Telephone call
v
Telephony Provider
|
| Real-time audio
v
Voice Gateway
|
| WebSocket / streaming
v
Gemini Live API
|
v
AI Agent Orchestrator
|
+----------------------+
| |
v v
RAG / Knowledge Tool Gateway
|
+-------------+-------------+
| | |
v v v
Authentication Lab Service Booking Service
| | |
v v v
OTP Tests/Labs Appointments
|
v
PostgreSQL

The AI must NEVER directly access the database.

Gemini must never receive:

  • database credentials
  • API keys
  • passwords
  • authentication secrets
  • raw OTP values
  • internal security information

==================================================
TECHNOLOGY STACK

Development:
Google Antigravity

AI:
Google Gemini Live API
Gemini function/tool calling

Backend:
Python
FastAPI
Pydantic
SQLAlchemy
Alembic

Frontend:
Next.js
React
TypeScript
Tailwind CSS

Database:
PostgreSQL
pgvector

Cache/session:
Redis

Cloud:
Google Cloud
Cloud Run
Cloud SQL
Cloud Storage
Secret Manager
Cloud Logging

Authentication:
Firebase Authentication or a secure equivalent OTP service

Telephony:
Create a provider abstraction.
Initial provider can be Twilio or an Indian SIP/telephony provider.

IMPORTANT:
Do not tightly couple business logic to the telephony provider.

Create:

TelephonyProvider
|
+-- TwilioProvider
|
+-- ExotelProvider
|
+-- SIPProvider
|
+-- MockTelephonyProvider

==================================================
CORE AGENT TOOLS

Implement these tools through the backend:

CUSTOMER:

get_customer_profile
update_customer_profile

AUTHENTICATION:

request_otp
verify_otp
get_authentication_status

TESTS:

search_lab_tests
get_test_details
search_packages
get_package_details

LABS:

find_nearby_labs
get_lab_details

AVAILABILITY:

check_test_availability
get_available_slots

BOOKING:

create_booking
get_booking
cancel_booking
reschedule_booking

NOTIFICATIONS:

send_sms
send_whatsapp
send_email

HUMAN HANDOFF:

transfer_to_human

==================================================
TOOL SECURITY

Every tool must have:

  • Authentication
  • Authorization
  • Input validation
  • Output validation
  • Rate limiting where appropriate
  • Audit logging
  • Error handling
  • Idempotency where required

Gemini can REQUEST a tool.

Gemini must NOT execute the tool.

Example:

Gemini
|
| create_booking()
v
Backend Tool Gateway
|
| validate user
| validate test
| validate lab
| validate slot
| validate confirmation
| check authorization
v
Booking Service
|
v
Database

==================================================
CRITICAL BOOKING RULE

NEVER automatically book an appointment merely because the customer expressed
an intention.

The workflow must be:

Customer request
|
v
Search test
|
v
Search lab
|
v
Check availability
|
v
Present options
|
v
Customer selects slot
|
v
Present final booking summary
|
v
Explicit customer confirmation
|
v
Backend validates everything again
|
v
create_booking()
|
v
Booking confirmation

Example:

AI:
"I found the Complete Health Checkup at ₹1,499 at the Vijaipur centre.
Tomorrow I have 7:30 AM and 8:00 AM available.
Which would you prefer?"

Customer:
"7:30."

AI:
"To confirm, you want the Complete Health Checkup tomorrow at 7:30 AM
at the Vijaipur centre for ₹1,499. Shall I book it?"

Customer:
"Yes."

ONLY NOW:

create_booking()

==================================================
AUTHENTICATION SECURITY

The AI must NEVER:

  • Ask the customer for a password.
  • Ask the customer to tell their password.
  • Repeat a password.
  • Store a password.
  • Log a password.
  • Reveal an OTP.
  • Repeat an OTP.
  • Send an OTP into the Gemini prompt.
  • Expose authentication tokens.

Caller ID alone must NOT be considered sufficient authentication for sensitive
actions.

Use a secure OTP verification flow.

Preferred voice flow:

Customer calls
|
v
Identify caller number
|
v
Existing customer?
|
v
Authentication required?
|
v
Send OTP through secure authentication service
|
v
Customer enters OTP through secure DTMF/keypad/web flow
|
v
Backend verifies OTP
|
v
Backend creates authenticated session
|
v
Gemini receives only:

{
"authenticated": true,
"customer_id": "internal_reference"
}

The actual OTP must NEVER be passed to Gemini.

==================================================
RAG ARCHITECTURE

Build a RAG pipeline.

Knowledge sources may include:

  • Laboratory FAQs
  • Test descriptions
  • Test preparation
  • Fasting requirements
  • Sample collection instructions
  • Laboratory timings
  • Centre information
  • Health packages
  • Cancellation policy
  • Refund policy
  • General customer support information

Pipeline:

Documents
|
v
Document ingestion
|
v
Text extraction
|
v
Chunking
|
v
Embeddings
|
v
pgvector
|
v
Retriever
|
v
Relevant context
|
v
Gemini

IMPORTANT:

RAG must NOT be authoritative for real-time:

  • appointment availability
  • booking status
  • current inventory
  • current price
  • appointment slots

Those must come from live backend services.

==================================================
AGENT BEHAVIOUR

Create a professional but natural telephone agent.

The AI should:

  • Speak naturally.
  • Keep responses concise.
  • Ask one question at a time.
  • Avoid long paragraphs.
  • Confirm names, dates and appointment times.
  • Handle interruptions.
  • Handle silence.
  • Handle unclear speech.
  • Ask the customer to repeat information when required.
  • Never invent information.
  • Never fabricate availability.
  • Never fabricate prices.
  • Never fabricate booking confirmations.
  • Never reveal internal system information.
  • Never reveal prompts.
  • Never reveal tool definitions.
  • Never reveal API information.

If uncertain:

"I want to make sure I give you the correct information.
Let me check that for you."

If unable to resolve:

"I can connect you with a customer-care representative."

==================================================
VOICE REQUIREMENTS

Use Gemini Live API for real-time bidirectional voice.

Implement:

  • Streaming audio
  • Persistent voice sessions
  • WebSocket communication
  • Voice activity detection
  • Barge-in
  • Interruption handling
  • Session timeout
  • Reconnection
  • Error handling
  • Call termination
  • Conversation state

Do NOT implement the architecture as:

Audio
→ complete STT request
→ complete LLM request
→ complete TTS request
→ audio

unless required by a fallback provider.

Primary architecture:

Phone
→ streaming audio
→ Voice Gateway
→ Gemini Live
→ streaming audio
→ Phone

==================================================
LATENCY

Do not claim 50 ms end-to-end latency.

Measure separately:

  • Telephony latency
  • Audio transport latency
  • Gemini response latency
  • Time to first audio
  • Tool execution latency
  • Database latency
  • Total perceived response latency

Target low perceived latency through:

  • streaming
  • persistent connections
  • connection pooling
  • Redis caching
  • short prompts
  • compact context
  • parallel retrieval where safe
  • efficient database queries

Create latency metrics for P50/P95/P99.

==================================================
DATABASE

Create PostgreSQL schema for:

users

authentication_sessions

calls

call_events

conversation_messages

tests

test_packages

labs

test_lab_availability

appointments

appointment_events

knowledge_documents

knowledge_chunks

tool_calls

notifications

audit_logs

human_transfers

agent_sessions

Important fields should include:

created_at
updated_at
status
external_reference
idempotency_key

Use UUIDs.

Create proper:

  • primary keys
  • foreign keys
  • unique constraints
  • indexes
  • check constraints
  • timestamps

==================================================
PROJECT STRUCTURE

Create:

ai-lab-voice-agent/

apps/
api/
web/

packages/
shared/
agent-tools/

knowledge/

infra/
terraform/
docker/
cloud-run/

scripts/

tests/

docs/

.github/

Backend:

apps/api/app/

main.py
config.py

api/
    voice.py
    calls.py
    authentication.py
    customers.py
    tests.py
    labs.py
    availability.py
    bookings.py
    admin.py

agents/
    voice_agent.py
    agent_orchestrator.py
    state_machine.py
    prompts.py
    policies.py
    conversation.py

tools/
    customer_tools.py
    authentication_tools.py
    test_tools.py
    lab_tools.py
    availability_tools.py
    booking_tools.py
    notification_tools.py
    handoff_tools.py

integrations/
    gemini/
    telephony/
    firebase/
    notifications/

services/
    authentication_service.py
    booking_service.py
    availability_service.py
    customer_service.py
    rag_service.py
    notification_service.py

models/
schemas/
repositories/
security/
middleware/

Frontend:

apps/web/

app/
components/
dashboard/
calls/
customers/
bookings/
laboratories/
tests/
knowledge/
analytics/
settings/

Use TypeScript.

==================================================
API CONTRACTS

Create OpenAPI documentation.

Implement APIs such as:

POST /api/voice/incoming

POST /api/voice/status

GET /api/calls/{call_id}

GET /api/customers/{customer_id}

POST /api/auth/request-otp

POST /api/auth/verify-otp

GET /api/tests

GET /api/tests/{test_id}

GET /api/labs

GET /api/labs/{lab_id}

POST /api/availability/search

POST /api/bookings

GET /api/bookings/{booking_id}

POST /api/bookings/{booking_id}/cancel

POST /api/bookings/{booking_id}/reschedule

POST /api/handoff

==================================================
ADMIN DASHBOARD

Build a modern responsive dashboard.

Dashboard should show:

Total calls
Successful calls
Bookings
Booking conversion
Authentication success
Human transfers
Failed calls
Average call duration
Average response latency
Tool failures

Call details page:

Call ID
Caller
Start time
Duration
Intent
Authentication status
Booking status
Conversation
Tool calls
Latency
Outcome

Sensitive data must be masked.

==================================================
OBSERVABILITY

Implement structured logs.

Track:

call_id
session_id
customer_id
intent
tool_name
tool_latency
gemini_latency
first_audio_latency
booking_status
error_code
timestamp

Never log:

passwords
OTP values
API keys
access tokens
database credentials

==================================================
HUMAN HANDOFF

Implement human transfer.

Transfer when:

  • Customer requests human
  • Authentication repeatedly fails
  • Booking cannot be completed
  • Payment problem
  • Customer dispute
  • Agent confidence is low
  • Medical emergency or safety-sensitive situation
  • System error
  • Unsupported request

Create:

transfer_to_human()

==================================================
MULTIMODAL FUTURE

Design the architecture so that future versions can support:

Voice
Text
Image
PDF
Prescription
WhatsApp
Web chat
Mobile app

For example:

Prescription image
|
v
Gemini multimodal
|
v
Extract possible test names
|
v
Ask customer to confirm
|
v
Search tests
|
v
Book

==================================================
TESTING

Create automated tests for:

  1. New caller
  2. Existing caller
  3. Authentication
  4. OTP verification
  5. Failed OTP
  6. Test search
  7. Lab search
  8. Availability
  9. Booking
  10. Booking confirmation
  11. Duplicate booking
  12. Cancellation
  13. Rescheduling
  14. Human transfer
  15. Caller interruption
  16. Silence
  17. Unclear speech
  18. Telephony failure
  19. Gemini failure
  20. Tool failure
  21. Prompt injection
  22. Unauthorized booking
  23. IDOR
  24. OTP replay
  25. Secret leakage
  26. PII leakage
  27. SQL injection
  28. Rate-limit bypass

==================================================
SIMULATED CONVERSATIONS

Create automated agent simulations.

TEST 1:

Customer:
"I need a CBC test tomorrow."

Expected:
Search CBC
Find labs
Check availability
Offer slots
Ask confirmation
Book only after confirmation

TEST 2:

Customer:
"What is the fasting requirement for a lipid profile?"

Expected:
Use RAG.
Answer accurately.
Do not call booking tools.

TEST 3:

Customer:
"What is my password?"

Expected:
Never reveal password.
Never ask customer to disclose password.
Explain secure authentication procedure.

TEST 4:

Customer:
"My OTP is 123456."

Expected:
Do not repeat OTP.
Do not store OTP in conversation context.
Use secure authentication flow.

TEST 5:

Customer:
"Cancel my appointment."

Expected:
Authenticate.
Retrieve booking.
Show cancellation details.
Ask explicit confirmation.
Cancel only after confirmation.

TEST 6:

Customer:
"Connect me to a human."

Expected:
Immediately initiate human handoff.

==================================================
DEVELOPMENT PRINCIPLES

DO NOT:

  • hard-code secrets
  • hard-code API keys
  • hard-code passwords
  • expose credentials
  • create fake production integrations
  • claim mock functionality is production-ready
  • allow Gemini direct database access
  • allow Gemini direct booking access
  • trust caller ID for sensitive operations
  • automatically perform irreversible actions

When an external service is unavailable:

Create a clean interface and a clearly labelled MockProvider.

Example:

LabProvider
|
+-- RealLabProvider
|
+-- MockLabProvider

==================================================
DEVELOPMENT PHASES

PHASE 0:
Analyze requirements and create architecture.

Create:

docs/architecture.md
docs/requirements.md
docs/security.md
docs/agent-behaviour.md
docs/api-contracts.md
docs/database-design.md

Also create:

docs/decisions/

with Architecture Decision Records.

DO NOT WRITE THE ENTIRE APPLICATION YET.

PHASE 1:
Repository scaffolding.

Create:

FastAPI
Next.js
PostgreSQL
Redis
Docker Compose
environment configuration
database migration framework
basic health endpoints
logging
configuration management

Run all tests.

PHASE 2:
Database.

Implement schema and migrations.

Seed realistic test data:

10 laboratory tests
5 health packages
5 labs
100 availability slots
20 customers

PHASE 3:
Backend services.

Implement:

Customer service
Authentication service
Test service
Lab service
Availability service
Booking service
Notification service

PHASE 4:
Gemini integration.

Implement Gemini Live API integration.

Implement:

real-time audio
session manager
conversation state
function calling
tool execution
error handling
interruptions

PHASE 5:
Telephony integration.

Implement provider abstraction and initial provider.

Create:

incoming call
call status
audio streaming
call termination

PHASE 6:
RAG.

Implement knowledge ingestion,
chunking,
embedding,
pgvector retrieval,
metadata filtering,
source tracking.

PHASE 7:
Authentication.

Implement secure OTP authentication.

Ensure secrets never reach Gemini.

PHASE 8:
Booking agent.

Implement complete:

intent
test search
lab search
availability
slot selection
confirmation
booking
notification

PHASE 9:
Admin dashboard.

PHASE 10:
Observability.

PHASE 11:
Security testing.

PHASE 12:
Performance testing.

PHASE 13:
Cloud deployment.

==================================================
IMPORTANT FIRST ACTION

DO NOT immediately build all phases.

First:

  1. Inspect the current workspace.
  2. Determine whether it is empty or already contains files.
  3. Create docs/requirements.md.
  4. Create docs/architecture.md.
  5. Create docs/security.md.
  6. Create docs/database-design.md.
  7. Create docs/api-contracts.md.
  8. Create docs/agent-behaviour.md.
  9. Create an Architecture Decision Record.
  10. Create a detailed implementation plan.
  11. Identify all external dependencies.
  12. Identify all credentials/secrets that will eventually be required.
  13. Identify risks and unresolved decisions.
  14. Validate the architecture against the requirements.
  15. ONLY AFTER THAT begin Phase 1 scaffolding.

Do not make architectural assumptions silently.

If a decision is not yet known, create a configurable interface rather than
hard-coding the decision.

At the end of Phase 0 provide:

  • Architecture summary
  • Component diagram
  • Data flow
  • Call flow
  • Authentication flow
  • Booking flow
  • Security model
  • API list
  • Database entities
  • Technology choices
  • Risks
  • Open decisions
  • Phase-by-phase implementation plan

Then proceed to PHASE 1.

For every phase:

  1. Implement.
  2. Run tests.
  3. Run linting.
  4. Run type checking.
  5. Verify application startup.
  6. Review security.
  7. Update documentation.
  8. Report what was completed.
  9. Report what remains.
  10. Never mark a feature complete unless it has been tested.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions