Integration

How to Return OpenAI API Replies as AI Avatar Video and Audio (Node.js)

Steps for returning a reply generated with the OpenAI API as avacast AI avatar video and audio. Covers receiving the reply on the server and passing it to speak, in Node.js.


To have an AI avatar read out an OpenAI API reply, receive the reply text from the OpenAI API on your server and pass that text to avacast's POST /v1/sessions/:id/speak. The video and audio reach the browser over WebRTC, so the browser only has to display the video with the SDK.

How you call the OpenAI API depends on the version of the SDK you use. This article wraps the OpenAI part in a function called llm.reply() and focuses on how to connect it to avacast.

Overall flow

  1. Create an avacast session on the server and return client_token and webrtc to the browser
  2. Pass them to the SDK in the browser and display the video
  3. Send the user's question to the server
  4. Pass the question to the OpenAI API on the server and receive the reply text
  5. Pass the reply to avacast's speak

Both API keys stay on the server. Neither the OpenAI API key nor the avacast API key is passed to the browser.

Creating a session

With the Node.js server SDK, session creation and speech take only a few lines.

Server (create a session)

import { Avacast } from 'avacast';

const avacast = new Avacast(process.env.AVACAST_API_KEY);

export async function createAvatarSession() {
  const session = await avacast.sessions.create({ avatar_id: 'avt_xxxxxxxx' });
  // Return only session_id, client_token and webrtc to the browser
  const { session_id, client_token, webrtc } = session;
  return { session_id, client_token, webrtc };
}

In the browser, pass the values you received to AvacastSession. The display code is the same as in How to Display an AI Avatar in Next.js.

Passing the OpenAI API reply to speak

In the endpoint that receives the user's question, have the OpenAI API generate a reply and pass it straight to avacast.

Server (receive a question and speak the reply)

import { Avacast } from 'avacast';
import { llm } from './llm'; // Your own function that calls the OpenAI API

const avacast = new Avacast(process.env.AVACAST_API_KEY);

export async function answer(sessionId: string, question: string) {
  // Check here that the session belongs to the signed-in user
  const reply = await llm.reply(question);
  await avacast.sessions.speak(sessionId, reply);
  return reply;
}

Inside llm.reply(), use the OpenAI SDK to pass the question to the model and get the reply text. Match the model name and arguments to the SDK you use. If you also want to show the reply as text on screen, return reply to the browser and display it.

Making replies suit speech

LLM replies tend to be long as they are, and can contain bullet points and symbols. Since the avatar reads them aloud, include instructions like the following in the system prompt.

  • Answer in one or two sentences
  • Write in spoken language, without bullet points or symbols
  • When you do not know, say that you do not know

The shorter the reply, the shorter the wait before speech starts.

Long replies and streaming

A single speak call accepts up to 1,000 characters. Split long replies at sentence ends and call speak in order. You do not need to wait for the previous utterance to finish; up to 20 utterances can be queued per session.

If you use OpenAI API streaming, calling speak each time one sentence arrives lets speech start without waiting for the whole reply. The speak parameters are in the API reference (in Japanese).

FAQ

Can I use the audio from OpenAI's Realtime API as is?

avacast takes text and generates the audio and mouth movement. It does not accept audio input, so receive a text reply from the OpenAI API and pass it to speak.

Can I call the OpenAI API directly from the browser?

Avoid it, because the API key would be sent to the browser. Make both the OpenAI API call and the avacast speak call from the server.

What happens if the user asks the next question before the reply has finished?

A reply that is being spoken can be stopped with POST /v1/sessions/:id/interrupt. Stopping also discards the unplayed utterances left in the queue.

30 minutes a month free

Try it free

Related posts

Back to the blog