npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

openai-realtime-api

v1.0.7

Published

TypeScript client OpenAI's realtime voice API.

Downloads

2,113

Readme

openai-realtime-api

TypeScript client for OpenAI's realtime voice API.

Features

Install

npm install openai-realtime-api

This package is ESM-only. It requires Node.js >= 18, a browser environment, or an equivalent JS runtime (Deno, Bun, CF workers, etc).

Usage

[!IMPORTANT] All usage and events are 100% compatible with the OpenAI JS version. The main difference aside from bug fixes is that all events are fully-typed.

import { RealtimeClient } from 'openai-realtime-api'

// Create a new client; all params are optional; apiKey defaults to the
// `OPENAI_API_KEY` environment variable (when using Node.js).
const client = new RealtimeClient({
  sessionConfig: {
    instructions: 'You are a great, upbeat friend.',
    voice: 'alloy'
  }
})

// Can change session config ahead of connecting.
client.updateSession({
  turn_detection: null,
  input_audio_transcription: { model: 'whisper-1' }
})

// Example of custom event handling
client.on('conversation.updated', (event) => {
  // All events are fully-typed based on the event name.
  // In this case, `event` will have the type `RealtimeCustomEvents.ConversationUpdatedEvent`
  const { item, delta } = event

  // Access the full list of conversation items.
  const items = client.conversation.getItems()
})

// Connect to the Realtime API.
await client.connect()

// Send a text message and trigger a response generation.
client.sendUserMessageContent([{ type: 'input_text', text: 'How are you?' }])

// Wait for a completed response from the model.
// (`event` will be of type `RealtimeServerEvents.ResponseDoneEvent`)
const event = await client.realtime.waitForNext('response.done')

See examples for more complete demos.

See also the official OpenAI Realtime API Guide and API Reference.

For more info on usage, tools, and custom events, see OpenAI's readme. Note that this package is 100% compatible with OpenAI's beta package in terms of both official and unofficial events. The only difference is that all events are typed.

Server Usage

RealtimeClient takes in an optional apiKey which defaults to process.env.OPENAI_API_KEY.

Browser Usage

RealtimeClient takes in an optional url which can be pointed at a relay server.

import { RealtimeClient } from 'openai-realtime-api'

// Create a browser client which points to a relay server.
const client = new RealtimeClient({ url: RELAY_SERVER_URL })

Alternatively, you can use apiKey with RealtimeClient in the browser, but you also have to pass dangerouslyAllowAPIKeyInBrowser: true.

import { RealtimeClient } from 'openai-realtime-api'

// Create a browser client which connects directly to the OpenAI realtime API
// with an unsafe, client-side API key.
const client = new RealtimeClient({
  apiKey: process.env.OPENAI_API_KEY,
  dangerouslyAllowAPIKeyInBrowser: true
})

[!CAUTION] We strongly recommend against including your API key in any client (mobile or browser). It can be useful for local testing, but for production, you should be using a relay server.

Relay Server

import { RealtimeClient } from 'openai-realtime-api'
import { RealtimeRelay } from 'openai-realtime-api/node'

// Setting `relay: true` disables tool calls and directly modifying the session,
// since that will be the responsibility of the upstream client.
const client = new RealtimeClient({ relay: true })
const relay = new RealtimeRelay({ client })

relay.listen(8081)

Note that RealtimeRelay uses a different import path because it contains Node.js-specific code.

A full example is included in examples/node/relay-server.ts.

Examples

To run the included examples (requires Node.js >= 18):

  1. Clone this repo
  2. Run pnpm install
  3. Setup .env with your OPENAI_API_KEY

You can set debug: true in the RealtimeClient constructor of these examples to print out the full event log.

Node.js Basic

Simple Node.js demo using the RealtimeClient which sends a text message and waits for a complete response.

Node.js Audio

Simple Node.js demo using the RealtimeClient which sends a short audio message and waits for a complete response.

Node.js Conversation

Simple Node.js demo using the RealtimeClient with a microphone and speaker to simulate a full, back & forth conversation from the terminal.

OpenAI Realtime Console

This example has been imported from https://github.com/openai/openai-realtime-console (at commit 6ea4dba). The only change has been to replace @openai/realtime-api-beta with openai-realtime-api and to fix a few types.

To run the realtime console example:

pnpm install
cd examples/openai-realtime-console
pnpm start

TODO

  • add an example using tools
  • add an example next.js app
  • improve readme docs

License

MIT © Travis Fischer

If you found this project interesting, consider following me on Twitter.