OpenAIのリアルタイム音声APIのタイプスクリプトクライアント。
npm install openai-realtime-api
このパッケージはESMのみです。 Node.js >= 18
、ブラウザ環境、または同等のJSランタイム(デノ、パン、CFワーカーなど)が必要です。
重要
すべての使用法とイベントは、OpenAI JSバージョンと100%互換性があります。バグ修正以外の主な違いは、すべてのイベントが完全にタイプであることです。
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' )
より完全なデモについては、例を参照してください。
公式OpenaiリアルタイムAPIガイドとAPIリファレンスも参照してください。
使用、ツール、カスタムイベントの詳細については、OpenaiのReadmeを参照してください。このパッケージは、公式イベントと非公式の両方のイベントの両方で、Openaiのベータパッケージと100%互換性があることに注意してください。唯一の違いは、すべてのイベントが入力されることです。
RealtimeClient
、 process.env.OPENAI_API_KEY
でデフォルトでenv.openai_api_keyを使用するオプションのapiKey
を取り入れます。
RealtimeClient
、リレーサーバーを指すことができるオプションのurl
を取り入れます。
import { RealtimeClient } from 'openai-realtime-api'
// Create a browser client which points to a relay server.
const client = new RealtimeClient ( { url : RELAY_SERVER_URL } )
または、ブラウザでRealtimeClient
を使用してapiKey
を使用することもできますが、 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
} )
注意
APIキーを任意のクライアント(モバイルまたはブラウザ)に含めることを強くお勧めします。ローカルテストに役立つ場合がありますが、生産にはリレーサーバーを使用する必要があります。
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 )
node.js固有のコードが含まれているため、 RealtimeRelay
異なるインポートパスを使用することに注意してください。
Examples/Node/Relay-Server.tsには、完全な例が含まれています。
付属の例を実行するには( Node.js >= 18
が必要です):
pnpm install
を実行しますOPENAI_API_KEY
で.env
をセットアップしますこれらの例のRealtimeClient
コンストラクターでdebug: true
を設定して、イベントログ全体を印刷できます。
simple node.jsデモテキストメッセージを送信して完全な応答を待つRealtimeClient
を使用します。
npx tsx examples/node/basic.ts
を実行しますSimple node.jsデモは、短いオーディオメッセージを送信し、完全な応答を待つRealtimeClient
を使用しています。
npx tsx examples/node/audio.ts
を実行しますsimple node.jsデモは、マイクとスピーカーを使用してRealtimeClient
使用して、端末からの完全な後方および最初の会話をシミュレートします。
mic
にはSOXが必要です。 MacOSでは、 brew install sox
を実行できますnpx tsx examples/node/convo.ts
この例は、https://github.com/openai/openai-realtime-console(Commit 6EA4DBA)からインポートされています。唯一の変更は@openai/realtime-api-beta
をopenai-realtime-api
に置き換えて、いくつかのタイプを修正することでした。
リアルタイムコンソールの例を実行するには:
pnpm install
cd examples/openai-realtime-console
pnpm start
MIT©Travis Fischer
このプロジェクトが面白いと感じた場合は、Twitterで私をフォローしてください。