ไคลเอนต์ TypeScript สำหรับ API Voice REALTIME ของ OpenAI
npm install openai-realtime-api
แพ็คเกจนี้เป็น ESM เท่านั้น มันต้องใช้ Node.js >= 18
, สภาพแวดล้อมของเบราว์เซอร์หรือรันไทม์ JS เทียบเท่า (deno, Bun, CF คนงาน ฯลฯ )
สำคัญ
การใช้งานและกิจกรรมทั้งหมดเข้ากันได้ 100% กับเวอร์ชัน Openai JS ความแตกต่างที่สำคัญนอกเหนือจากการแก้ไขข้อผิดพลาดคือ เหตุการณ์ทั้งหมดถูกพิมพ์อย่างเต็มที่
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 Realtime API อย่างเป็นทางการและการอ้างอิง API
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการใช้งานเครื่องมือและกิจกรรมที่กำหนดเองดู readme ของ Openai โปรดทราบว่าแพ็คเกจนี้เข้ากันได้ 100% กับแพ็คเกจเบต้าของ Openai ในแง่ของเหตุการณ์ทั้งอย่างเป็นทางการและไม่เป็นทางการ ความแตกต่างเพียงอย่างเดียวคือการพิมพ์เหตุการณ์ทั้งหมด
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 } )
หรือคุณสามารถใช้ 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
ตัวอย่างเต็มรูปแบบรวมอยู่ในตัวอย่าง/โหนด/รีเลย์-เซิร์ฟเวอร์
ในการเรียกใช้ตัวอย่างที่รวมอยู่ (ต้องใช้ Node.js >= 18
):
pnpm install
.env
กับ OPENAI_API_KEY
ของคุณ คุณสามารถตั้ง debug: true
ในตัวสร้าง RealtimeClient
ของตัวอย่างเหล่านี้เพื่อพิมพ์บันทึกเหตุการณ์เต็มรูปแบบ
การสาธิตอย่างง่าย Node.js โดยใช้ RealtimeClient
ซึ่งส่งข้อความและรอการตอบกลับที่สมบูรณ์
npx tsx examples/node/basic.ts
การสาธิต Simple Node.js โดยใช้ 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 (ที่ Pommea4dba) การเปลี่ยนแปลงเพียงอย่างเดียวคือการแทนที่ @openai/realtime-api-beta
ด้วย openai-realtime-api
และเพื่อแก้ไขบางประเภท
ในการเรียกใช้ตัวอย่างคอนโซลเรียลไทม์:
pnpm install
cd examples/openai-realtime-console
pnpm start
MIT © Travis Fischer
หากคุณพบว่าโครงการนี้น่าสนใจลองติดตามฉันบน Twitter