Example: Half-cascade with Inworld

View as Markdown

For the complete documentation index, see llms.txt.

Overview

This half-cascade example uses OpenAI Realtime for speech‑to‑text and reasoning, then sends OpenAI text responses to Inworld Realtime TTS.

⬇️ Jump to the Full VoxEngine scenario.

Prerequisites

  • Store your OpenAI API key in Voximplant Secrets under OPENAI_API_KEY.
  • Set a voiceId in the Inworld request (createContextParameters.create.voiceId) to choose the TTS voice used in this scenario.
  • (Optional) Store your Inworld API key in Voximplant Secrets under INWORLD_API_KEY if you want to use your own Inworld account.

How it works

  • OpenAI runs gpt-realtime-2.1 in text mode (output_modalities: ["text"]).
  • Caller audio is sent to OpenAI: call.sendMediaTo(voiceAIClient).
  • Completed OpenAI text responses are sent to Inworld with send({ send_text: { text } }), followed by send({ flush_context: {} }); Inworld streams the generated speech to the call.
  • When OpenAI detects caller speech, the scenario clears both the OpenAI and Inworld output buffers.

Developer notes

  • Do not set audio format parameters in half-cascade connector requests. VoxEngine’s WebSocket gateway handles media format negotiation automatically.
  • If no Inworld API key is provided, Voximplant’s default account and billing are used.
  • Custom or cloned voices are only available when using your own API key.

Full VoxEngine scenario

Notes

  • The example sets voiceId: "Ashley" and modelId: "inworld-tts-1.5-mini" in createContextParameters.create. Change these to any supported Inworld voice or model.
  • The example handles ResponseOutputTextDone, so each Inworld request contains a completed OpenAI text response.

This example runs gpt-realtime-2.1 using the Realtime 2.x session shape, with input settings nested under session.audio.input.

voxeengine-openai-half-cascade-inworld.js
1/**
2 * Voximplant + OpenAI Realtime API + Inworld TTS demo
3 * Scenario: OpenAI handles STT/LLM, Inworld handles TTS (half-cascade).
4 */
5
6require(Modules.OpenAI);
7require(Modules.Inworld);
8
9const OPENAI_MODEL = "gpt-realtime-2.1";
10
11const SYSTEM_PROMPT = `
12You are Voxi, a helpful phone assistant.
13Keep responses short and telephony-friendly.
14Always reply in English.
15`;
16
17const INWORLD_VOICE_ID = "Ashley"; // set your preference here
18const INWORLD_MODEL_ID = "inworld-tts-1.5-mini"; // set your preference here, or leave blank to use the default model for the voice
19
20const SESSION_CONFIG = {
21 session: {
22 type: "realtime",
23 model: OPENAI_MODEL,
24 instructions: SYSTEM_PROMPT,
25 output_modalities: ["text"],
26 audio: {
27 input: {
28 turn_detection: {type: "server_vad", interrupt_response: true},
29 },
30 },
31 },
32};
33
34VoxEngine.addEventListener(AppEvents.CallAlerting, async ({call}) => {
35 let voiceAIClient;
36 let ttsPlayer;
37
38 call.addEventListener(CallEvents.Disconnected, () => VoxEngine.terminate());
39 call.addEventListener(CallEvents.Failed, () => VoxEngine.terminate());
40
41 try {
42 call.answer();
43 // call.record({hd_audio: true, stereo: true}); // Optional: record the call
44
45 const openAiKey = VoxEngine.getSecretValue("OPENAI_API_KEY");
46
47 voiceAIClient = await OpenAI.createRealtimeAPIClient({
48 apiKey: openAiKey,
49 model: OPENAI_MODEL,
50 onWebSocketClose: (event) => {
51 Logger.write("===OpenAI.WebSocket.Close===");
52 if (event) Logger.write(JSON.stringify(event));
53 VoxEngine.terminate();
54 },
55 });
56
57 voiceAIClient.addEventListener(OpenAI.RealtimeAPIEvents.SessionCreated, () => {
58 voiceAIClient.sessionUpdate(SESSION_CONFIG);
59 });
60
61 voiceAIClient.addEventListener(OpenAI.RealtimeAPIEvents.SessionUpdated, async () => {
62 call.sendMediaTo(voiceAIClient); // bridge media between the call and OpenAI
63
64 // create the TTS player and pass the config parameters
65 ttsPlayer = Inworld.createRealtimeTTSPlayer({
66 // apiKey: VoxEngine.getSecretValue('INWORLD_API_KEY'), // optional,
67 createContextParameters: {
68 create: {
69 voiceId: INWORLD_VOICE_ID,
70 modelId: INWORLD_MODEL_ID,
71 speakingRate: 1.1,
72 temperature: 1.3,
73 },
74 },
75 });
76 ttsPlayer.sendMediaTo(call); // bridge media between the TTS player and the call
77
78 voiceAIClient.responseCreate({instructions: "Hello! How can I help today?"});
79 });
80
81 voiceAIClient.addEventListener(OpenAI.RealtimeAPIEvents.ResponseOutputTextDone, (event) => {
82 const payload = event?.data?.payload || event?.data || {};
83 const text = payload.text || payload.delta;
84 if (!text || !ttsPlayer) return;
85 Logger.write(`===AGENT_TEXT=== ${text}`);
86 ttsPlayer.send({send_text: {text}});
87 ttsPlayer.send({flush_context: {}});
88 });
89
90 // Barge-in: clear both OpenAI and Inworld buffers
91 voiceAIClient.addEventListener(OpenAI.RealtimeAPIEvents.InputAudioBufferSpeechStarted, () => {
92 Logger.write("===BARGE-IN: OpenAI.InputAudioBufferSpeechStarted===");
93 voiceAIClient.clearMediaBuffer();
94 ttsPlayer?.clearBuffer();
95 });
96
97 // ---------------------- Log all other events for debugging -----------------------
98 [
99 OpenAI.RealtimeAPIEvents.ResponseCreated,
100 OpenAI.RealtimeAPIEvents.ResponseDone,
101 OpenAI.RealtimeAPIEvents.ResponseOutputTextDelta,
102 OpenAI.RealtimeAPIEvents.ConnectorInformation,
103 OpenAI.RealtimeAPIEvents.HTTPResponse,
104 OpenAI.RealtimeAPIEvents.WebSocketError,
105 OpenAI.RealtimeAPIEvents.Unknown,
106 OpenAI.Events.WebSocketMediaStarted,
107 OpenAI.Events.WebSocketMediaEnded,
108 ].forEach((eventName) => {
109 voiceAIClient.addEventListener(eventName, (event) => {
110 Logger.write(`===${event.name}===`);
111 if (event?.data) Logger.write(JSON.stringify(event.data));
112 });
113 });
114 } catch (error) {
115 Logger.write("===UNHANDLED_ERROR===");
116 Logger.write(error);
117 voiceAIClient?.close();
118 VoxEngine.terminate();
119 }
120});

More information