عميل TypeScript لـ Openai في الوقت الفعلي لـ API.
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' )
انظر أمثلة لمزيد من العروض التوضيحية.
انظر أيضًا دليل API الرسمي Openai Realtime API و API.
لمزيد من المعلومات حول الاستخدام والأدوات والأحداث المخصصة ، انظر Openai's ReadMe. لاحظ أن هذه الحزمة متوافقة بنسبة 100 ٪ مع حزمة Beta 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
لهذه الأمثلة لطباعة سجل الحدث الكامل.
Demo Node.js Simple باستخدام RealtimeClient
الذي يرسل رسالة نصية وينتظر استجابة كاملة.
npx tsx examples/node/basic.ts
Demo Node.js Simple باستخدام RealtimeClient
الذي يرسل رسالة صوتية قصيرة وينتظر استجابة كاملة.
npx tsx examples/node/audio.ts
Demo Node.js Simple باستخدام RealtimeClient
مع الميكروفون ومكبر الصوت لمحاكاة محادثة كاملة ذهابًا وإيابًا من المحطة.
mic
يتطلب SOX. على MacOS ، يمكنك تشغيل brew install sox
npx tsx examples/node/convo.ts
تم استيراد هذا المثال من https://github.com/openai/openai-realtime-console (في الالتزام 6EA4DBA). كان التغيير الوحيد هو استبدال @openai/realtime-api-beta
بـ openai-realtime-api
وإصلاح بعض الأنواع.
لتشغيل مثال وحدة التحكم في الوقت الفعلي:
pnpm install
cd examples/openai-realtime-console
pnpm start
MIT © Travis Fischer
إذا وجدت هذا المشروع مثيرًا للاهتمام ، فكر في متابعتي على Twitter.