OpenAI的實時語音API的打字稿客戶端。
npm install openai-realtime-api
此軟件包僅限ESM。它需要Node.js >= 18
,瀏覽器環境或等效的JS運行時(DENO,BUN,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的Beta套餐100%兼容,從官方事件和非官方事件角度來看。唯一的區別是所有事件均已鍵入。
RealtimeClient
帶有可選的apiKey
,該apikey默認為process.env.OPENAI_API_KEY
。
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 } )
另外,您可以在瀏覽器中使用apiKey
與RealtimeClient
一起使用,但是您還必須通過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 )
請注意, RealtimeRelay
使用不同的導入路徑,因為它包含node.js特定代碼。
示例/節點/中繼server.ts中包含一個完整示例。
要運行隨附的示例(需要Node.js >= 18
):
pnpm install
.env
與您的OPENAI_API_KEY
您可以在這些示例的RealtimeClient
構造函數中設置debug: true
以打印完整的事件日誌。
使用RealtimeClient
發送短信並等待完整的響應。
npx tsx examples/node/basic.ts
使用RealtimeClient
發送簡短的音頻消息並等待完整的響應。
npx tsx examples/node/audio.ts
簡單的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 openai-realtime-api
替換@openai/realtime-api-beta
並修復了幾種類型。
運行實時控制台示例:
pnpm install
cd examples/openai-realtime-console
pnpm start
麻省理工學院©Travis Fischer
如果您發現這個項目很有趣,請考慮在Twitter上關注我。