Klien TypeScript untuk API Voice Realtime OpenAI.
npm install openai-realtime-api
Paket ini hanya ESM. Dibutuhkan Node.js >= 18
, lingkungan browser, atau runtime JS yang setara (Deno, Bun, CF pekerja, dll).
Penting
Semua penggunaan dan acara kompatibel 100% dengan versi OpenAI JS. Perbedaan utama selain dari perbaikan bug adalah bahwa semua acara diketik sepenuhnya .
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' )
Lihat contoh untuk demo yang lebih lengkap.
Lihat juga Panduan API Realtime Openai Resmi dan Referensi API.
Untuk info lebih lanjut tentang penggunaan, alat, dan acara khusus, lihat ReadMe Openai. Perhatikan bahwa paket ini kompatibel 100% dengan paket beta OpenAI dalam hal acara resmi dan tidak resmi. Satu -satunya perbedaan adalah bahwa semua acara diketik.
RealtimeClient
mengambil apiKey
opsional yang default untuk process.env.OPENAI_API_KEY
.
RealtimeClient
mengambil url
opsional yang dapat diarahkan pada server relay.
import { RealtimeClient } from 'openai-realtime-api'
// Create a browser client which points to a relay server.
const client = new RealtimeClient ( { url : RELAY_SERVER_URL } )
Atau, Anda dapat menggunakan apiKey
dengan RealtimeClient
di browser, tetapi Anda juga harus lulus 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
} )
Peringatan
Kami sangat merekomendasikan agar tidak memasukkan kunci API Anda di klien mana pun (seluler atau browser). Ini dapat berguna untuk pengujian lokal, tetapi untuk produksi, Anda harus menggunakan server relay.
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 )
Perhatikan bahwa RealtimeRelay
menggunakan jalur impor yang berbeda karena berisi kode khusus node.js.
Contoh lengkap termasuk dalam contoh/simpul/relay-server.ts.
Untuk menjalankan contoh yang disertakan (membutuhkan Node.js >= 18
):
pnpm install
.env
dengan OPENAI_API_KEY
Anda Anda dapat mengatur debug: true
di konstruktor RealtimeClient
dari contoh -contoh ini untuk mencetak log acara lengkap.
Demo Simple Node.js Menggunakan RealtimeClient
yang mengirim pesan teks dan menunggu respons lengkap.
npx tsx examples/node/basic.ts
Demo Simple Node.js Menggunakan RealtimeClient
yang mengirimkan pesan audio pendek dan menunggu respons lengkap.
npx tsx examples/node/audio.ts
Demo Simple Node.js Menggunakan RealtimeClient
dengan mikrofon dan pembicara untuk mensimulasikan percakapan penuh, bolak -balik dari terminal.
mic
membutuhkan Sox; Di macOS, Anda dapat menjalankan brew install sox
npx tsx examples/node/convo.ts
Contoh ini telah diimpor dari https://github.com/openai/openai-realtime-console (di Commit 6EA4DBA). Satu-satunya perubahan adalah mengganti @openai/realtime-api-beta
dengan openai-realtime-api
dan untuk memperbaiki beberapa jenis.
Untuk menjalankan contoh konsol realtime:
pnpm install
cd examples/openai-realtime-console
pnpm start
MIT © Travis Fischer
Jika Anda menemukan proyek ini menarik, pertimbangkan untuk mengikuti saya di Twitter.