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의 베타 패키지와 100% 호환됩니다. 유일한 차이점은 모든 이벤트가 입력된다는 것입니다.
RealtimeClient
기본적으로 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 } )
또는 브라우저에서 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 )
RealtimeRelay
node.js- 특이 적 코드를 포함하기 때문에 다른 가져 오기 경로를 사용합니다.
전체 예제는 예/노드/릴레이 서버에 포함되어 있습니다.
포함 된 예제를 실행하려면 (필요 Node.js >= 18
) :
pnpm install
실행하십시오OPENAI_API_KEY
로 .env
설정하십시오 이 예제의 RealtimeClient
생성자에서 debug: true
설정하여 전체 이벤트 로그를 인쇄 할 수 있습니다.
간단한 node.js Demo 문자 메시지를 보내고 완전한 응답을 기다리는 RealtimeClient
를 사용합니다.
npx tsx examples/node/basic.ts
실행하십시오 간단한 node.js demo RealtimeClient
사용하여 짧은 오디오 메시지를 보내고 완전한 응답을 기다립니다.
npx tsx examples/node/audio.ts
실행하십시오 Simple Node.js Demo 마이크와 스피커를 사용하여 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
이 프로젝트가 흥미로워지면 트위터에서 나를 따라가는 것을 고려하십시오.