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 soxnpx 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上关注我。