This software is to be considered "sample code", a Type B Deliverable, and is delivered "as-is" to the user. Twilio bears no responsibility to support the use or implementation of this software.
A Twilio ConversationRelay project for building a Voice AI Assistant.
- REST API endpoint for incoming calls
- WebSocket real-time communication
- Uses OpenAI model and ChatCompletion API in
LLMService- Supports both streaming and non-streaming responses
- Jest for unit testing
- Node.js (v20+)
- npm
Before using this project, please follow the setup instructions in SETUP.md.
-
Clone this repository
-
Navigate to the project directory:
cd twilio-conversation-relay-sample -
Install dependencies:
npm install
-
Copy the sample environment file and configure the environment variables:
cp .env.sample .env
Once created, open .env in your code editor. You are required to set the following environment variables for the app to function properly:
| Variable Name | Description | Example Value |
|---|---|---|
NGROK_DOMAIN |
The forwarding URL of your ngrok tunnel initiated above | [your-ngrok-domain].ngrok.app |
TWILIO_ACCOUNT_SID |
Your Twilio Account SID, which can be found in the Twilio Console. | ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
TWILIO_AUTH_TOKEN |
Your Twilio Auth Token, which is also found in the Twilio Console. | your_auth_token_here |
TWILIO_WORKFLOW_SID |
The Taskrouter Workflow SID, which is automatically provisioned with your Flex account. Used to enqueue inbound call with Flex agents. To find this, in the Twilio Console go to TaskRouter > Workspaces > Flex Task Assignment > Workflows | WWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
WELCOME_GREETING |
The message automatically played to the caller | |
OPENAI_API_KEY |
Your OpenAI API Key | your_api_key_here |
Below is an optional environment variable that has default value that can be overridden:
| PORT | The port your local server runs on. | 3000 |
- In the Twilio Console, go to Phone Numbers > Manage > Active Numbers and select an existing phone number (or Buy a number). In your Phone Number configuration settings, update the first A call comes in dropdown to Webhook and set the URL to https://[your-ngrok-domain].ngrok.app/api/incoming-call, ensure HTTP is set to HTTP POST, and click Save configuration.
Before populating the vector database, ensure ChromaDB is running. Follow the setup instructions in SETUP.md.
- Prepare your documents: supports
.txtand.jsonfiles - Add documents: Copy your knowledge base files to the
documents/folder in the project root - Initialize the vector database: From the repository root, run:
npm run init-vectordb
This process will:
- Read all
.txtand.jsonfiles from thedocuments/folder - Generate embeddings for the content
- Store the embeddings in ChromaDB for RAG-based search
Note: Make sure ChromaDB is running before executing the initialization command.
Once dependencies are installed, .env is set up, and Twilio is configured properly, run the dev server with the following command:
npm run dev
With the development server running, you can now begin testing the Voice AI Assistant. Place a call to the configured phone number and start interacting with your AI Assistant
npm run dev: Start the development servernpm run build: Compile TypeScriptnpm start: Run the production buildnpm test: Run unit tests
POST /api/incoming-call: Process incoming call - Initiates ConversationRelay (see src/routes/callRoutes.ts)POST /api/action: Handle connect action - Human agent handoff (see src/routes/connectActionRoutes.ts)
- Real-time communication setup (see src/services/llm/websocketService.ts)
- Environment variables are loaded from the
.envfile (see src/config.ts)
handleIncomingCall: Processes incoming call (see src/controllers/callController.ts)handleConnectAction: Handles connect action (see src/controllers/connectActionController.ts)
LLMService: Manages interactions with the language model (see src/services/llm/llmService.ts)
searchCommonMedicalTerms: Searches common medical terms (see src/services/llm/tools/searchCommonMedicalTerms.ts)humanAgentHandoff: Handles handoff to a human agent (see src/services/llm/tools/humanAgentHandoff.ts)verifyUserIdentity: Verifies user identity (see src/services/llm/tools/verifyUserIdentity.ts)checkPendingBill: Checks for pending medical bills (see src/services/llm/tools/checkPendingBill.ts)checkHsaAccount: Checks if the user has an HSA account (see src/services/llm/tools/checkHsaAccount.ts)checkPaymentOptions: Payment options available to the user (see src/services/llm/tools/checkPaymentOptions.ts)
- Mock data (see src/data/mock-data.ts)
This project is licensed under the MIT License.
