Skip to content

Commit

Permalink
Try old revision of system prompt for demo
Browse files Browse the repository at this point in the history
  • Loading branch information
khorwood-openai committed Oct 1, 2024
1 parent 528bed2 commit 298680f
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/lib/realtime-api-beta/dist/lib/client.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions src/lib/realtime-api-beta/dist/lib/event_handler.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* EventHandler callback
* @typedef {(event: {[key: string]: any}): void} EventHandlerCallbackType
*/
/**
* Inherited class for RealtimeAPI and RealtimeClient
* Adds basic event handling
Expand Down Expand Up @@ -45,6 +41,15 @@ export class RealtimeEventHandler {
* @returns {true}
*/
offNext(eventName: string, callback?: EventHandlerCallbackType): true;
/**
* Waits for next event of a specific type and returns the payload
* @param {string} eventName
* @param {number|null} [timeout]
* @returns {Promise<{[key: string]: any}|null>}
*/
waitForNext(eventName: string, timeout?: number | null): Promise<{
[key: string]: any;
} | null>;
/**
* Executes all events in the order they were added, with .on() event handlers executing before .onNext() handlers
* @param {string} eventName
Expand Down
2 changes: 1 addition & 1 deletion src/lib/realtime-api-beta/dist/lib/event_handler.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/lib/realtime-api-beta/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
if (globalThis.WebSocket && this.apiKey) {
if (!dangerouslyAllowAPIKeyInBrowser) {
throw new Error(
`Can not provide API key in the browser without "dangerouslyAllowAPIKeyInBrowser" set to true`
`Can not provide API key in the browser without "dangerouslyAllowAPIKeyInBrowser" set to true`,
);
}
}
Expand Down Expand Up @@ -69,7 +69,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
*/
if (this.apiKey) {
console.warn(
'Warning: Connecting using API key in the browser, this is not recommended'
'Warning: Connecting using API key in the browser, this is not recommended',
);
}
const WebSocket = globalThis.WebSocket;
Expand Down Expand Up @@ -122,10 +122,10 @@ export class RealtimeAPI extends RealtimeEventHandler {
request.setHeader('OpenAI-Beta', 'realtime=v1');
request.end();
},
}
},
);
ws.on('message', (data) => {
const message = JSON.parse(data);
const message = JSON.parse(data.toString());
this.receive(message.type, message);
});
return new Promise((resolve, reject) => {
Expand Down
48 changes: 17 additions & 31 deletions src/lib/realtime-api-beta/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { RealtimeAPI } from './api.js';
import { RealtimeConversation } from './conversation.js';
import { RealtimeUtils } from './utils.js';

const sleep = (t) => new Promise((r) => setTimeout(() => r(), t));

/**
* Valid audio formats
* @typedef {"pcm16"|"g711-ulaw"|"g711-alaw"} AudioFormatType
Expand Down Expand Up @@ -272,7 +270,7 @@ export class RealtimeClient extends RealtimeEventHandler {
// Handles session created event, can optionally wait for it
this.realtime.on(
'server.session.created',
() => (this.sessionCreated = true)
() => (this.sessionCreated = true),
);

// Setup for application control flow
Expand Down Expand Up @@ -326,7 +324,7 @@ export class RealtimeClient extends RealtimeEventHandler {
this.dispatch('conversation.interrupted');
});
this.realtime.on('server.input_audio_buffer.speech_stopped', (event) =>
handler(event, this.inputAudioBuffer)
handler(event, this.inputAudioBuffer),
);

// Handlers to update application state
Expand All @@ -341,16 +339,16 @@ export class RealtimeClient extends RealtimeEventHandler {
this.realtime.on('server.conversation.item.deleted', handlerWithDispatch);
this.realtime.on(
'server.conversation.item.input_audio_transcription.completed',
handlerWithDispatch
handlerWithDispatch,
);
this.realtime.on(
'server.response.audio_transcript.delta',
handlerWithDispatch
handlerWithDispatch,
);
this.realtime.on('server.response.audio.delta', handlerWithDispatch);
this.realtime.on(
'server.response.function_call_arguments.delta',
handlerWithDispatch
handlerWithDispatch,
);
this.realtime.on('server.response.output_item.done', async (event) => {
const { item } = handlerWithDispatch(event);
Expand Down Expand Up @@ -444,7 +442,7 @@ export class RealtimeClient extends RealtimeEventHandler {
const name = definition?.name;
if (this.tools[name]) {
throw new Error(
`Tool "${name}" already added. Please use .removeTool("${name}") before trying to add again.`
`Tool "${name}" already added. Please use .removeTool("${name}") before trying to add again.`,
);
}
if (typeof handler !== 'function') {
Expand Down Expand Up @@ -523,7 +521,7 @@ export class RealtimeClient extends RealtimeEventHandler {
};
if (this.tools[definition?.name]) {
throw new Error(
`Tool "${definition?.name}" has already been defined`
`Tool "${definition?.name}" has already been defined`,
);
}
return definition;
Expand All @@ -533,7 +531,7 @@ export class RealtimeClient extends RealtimeEventHandler {
type: 'function',
...this.tools[key].definition,
};
})
}),
);
const session = { ...this.sessionConfig };
session.tools = useTools;
Expand Down Expand Up @@ -581,7 +579,7 @@ export class RealtimeClient extends RealtimeEventHandler {
});
this.inputAudioBuffer = RealtimeUtils.mergeInt16Arrays(
this.inputAudioBuffer,
arrayBuffer
arrayBuffer,
);
}
return true;
Expand Down Expand Up @@ -624,7 +622,7 @@ export class RealtimeClient extends RealtimeEventHandler {
throw new Error(`Can only cancelResponse messages with type "message"`);
} else if (item.role !== 'assistant') {
throw new Error(
`Can only cancelResponse messages with role "assistant"`
`Can only cancelResponse messages with role "assistant"`,
);
}
this.realtime.send('response.cancel');
Expand All @@ -636,7 +634,7 @@ export class RealtimeClient extends RealtimeEventHandler {
item_id: id,
content_index: audioIndex,
audio_end_ms: Math.floor(
(sampleCount / this.conversation.defaultFrequency) * 1000
(sampleCount / this.conversation.defaultFrequency) * 1000,
),
});
return { item };
Expand All @@ -648,30 +646,18 @@ export class RealtimeClient extends RealtimeEventHandler {
* @returns {Promise<{item: ItemType}>}
*/
async waitForNextItem() {
let nextItem;
this.onNext('conversation.item.appended', (event) => {
const { item } = event;
nextItem = item;
});
while (!nextItem) {
await sleep(1);
}
return { item: nextItem };
const event = await this.waitForNext('conversation.item.appended');
const { item } = event;
return { item };
}

/**
* Utility for waiting for the next `conversation.item.completed` event to be triggered by the server
* @returns {Promise<{item: ItemType}>}
*/
async waitForNextCompletedItem() {
let nextItem;
this.onNext('conversation.item.completed', (event) => {
const { item } = event;
nextItem = item;
});
while (!nextItem) {
await sleep(1);
}
return { item: nextItem };
const event = await this.waitForNext('conversation.item.completed');
const { item } = event;
return { item };
}
}
24 changes: 24 additions & 0 deletions src/lib/realtime-api-beta/lib/event_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* @typedef {(event: {[key: string]: any}): void} EventHandlerCallbackType
*/

const sleep = (t) => new Promise((r) => setTimeout(() => r(), t));

/**
* Inherited class for RealtimeAPI and RealtimeClient
* Adds basic event handling
Expand Down Expand Up @@ -97,6 +99,28 @@ export class RealtimeEventHandler {
return true;
}

/**
* Waits for next event of a specific type and returns the payload
* @param {string} eventName
* @param {number|null} [timeout]
* @returns {Promise<{[key: string]: any}|null>}
*/
async waitForNext(eventName, timeout = null) {
const t0 = Date.now();
let nextEvent;
this.onNext(eventName, (event) => (nextEvent = event));
while (!nextEvent) {
if (timeout) {
const t1 = Date.now();
if (t1 - t0 > timeout) {
return null;
}
}
await sleep(1);
}
return nextEvent;
}

/**
* Executes all events in the order they were added, with .on() event handlers executing before .onNext() handlers
* @param {string} eventName
Expand Down
13 changes: 6 additions & 7 deletions src/utils/conversation_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ export const instructions = `System settings:
Tool use: enabled.
Instructions:
- You are a conversational assistant in a test playground that helps developers learn about your abilities
- Your personality is upbeat and enthusiastic
- You speak quickly and are concise; avoid rambling and useless information
- You are capable of modifying your tone, energy level, talking speed, and emotional demeanor
- You are capable of many types of expression (including, but not limited to) whispering, speaking loud, being out of breath, weak, dying, full of power
- Let the speaker you're talking to play with your settings and have fun
- You are an artificial intelligence agent responsible for helping test realtime voice capabilities.
- Be kind, helpful, and curteous
- It is okay to ask the speaker questions
- It is okay to ask the user questions
- Use tools and functions you have available liberally, it is part of the training apparatus
- Be open to exploration and conversation
- Remember: this is just for fun and testing!
Personality:
- Be upbeat and genuine
- Try speaking quickly as if excited
`;

0 comments on commit 298680f

Please sign in to comment.