You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To send streaming audio, use the `.appendInputAudio()` method. If you're in `turn_detection: 'disabled'`mode, then you need to use `.createResponse()` to tell the model to respond.
128
+
To send streaming audio, use the `.appendInputAudio()` method. If you're in manual mode (no turn detection), then you need to use `.createResponse()` to tell the model to respond.
124
129
125
130
```dart
126
131
// Send user audio, must be Uint8List
@@ -132,53 +137,48 @@ for (var i = 0; i < 10; i++) {
132
137
final value = (Random().nextDouble() * 2 - 1) * 0x8000;
133
138
data[n] = value.toInt();
134
139
}
135
-
client.appendInputAudio(data);
140
+
await client.appendInputAudio(data);
136
141
}
137
142
// Pending audio is committed and model is asked to generate
138
-
client.createResponse();
143
+
await client.createResponse();
139
144
```
140
145
141
146
### Adding and using tools
142
147
143
148
Working with tools is easy. Just call `.addTool()` and set a callback as the second parameter. The callback will be executed with the parameters for the tool, and the result will be automatically sent back to the model.
144
149
145
150
```dart
146
-
// We can add tools as well, with callbacks specified
147
-
client.addTool(
148
-
{
149
-
'name': 'get_weather',
150
-
'description': 'Retrieves the weather for a given lat, lng coordinate pair. Specify a label for the location.',
151
-
'parameters': {
151
+
await client.addTool(
152
+
const ToolDefinition(
153
+
name: 'get_weather',
154
+
description: 'Retrieves the weather for a location given its latitude and longitude coordinate pair.',
The `.addTool()` method automatically runs a tool handler and triggers a response on handler completion. Sometimes you may not want that, for example: using tools to generate a schema that you use for other purposes.
191
191
192
-
In this case, we can use the `tools`item with `updateSession`. In this case you **must** specify `type: 'function'`, which is not required for `.addTool()`.
192
+
In this case, we can use the `tools`parameter with `updateSession`.
193
193
194
194
**Note:** Tools added with `.addTool()` will **not** be overridden when updating sessions manually like this, but every `updateSession()` change will override previous `updateSession()` changes. Tools added via `.addTool()` are persisted and appended to anything set manually here.
195
195
196
196
```dart
197
-
client.updateSession(
197
+
await client.updateSession(
198
198
tools: [
199
-
{
200
-
'type': 'function',
201
-
'name': 'get_weather',
202
-
'description':
203
-
'Retrieves the weather for a given lat, lng coordinate pair. Specify a label for the location.',
204
-
'parameters': {
199
+
const ToolDefinition(
200
+
name: 'get_weather',
201
+
description: 'Retrieves the weather for a location given its latitude and longitude coordinate pair.',
final item = (event as RealtimeEventConversationItemCompleted).item;
226
+
if (item.item is ItemFunctionCall) {
233
227
// your function call is complete, execute some custom code
234
228
}
235
229
});
236
230
```
237
231
238
232
### Interrupting the model
239
233
240
-
You may want to manually interrupt the model, especially in `turn_detection: 'disabled'` mode. To do this, we can use:
234
+
You may want to manually interrupt the model, especially when not using turn detection. To do this, we can use:
241
235
242
236
```dart
243
237
// id is the id of the item currently being generated
244
238
// sampleCount is the number of audio samples that have been heard by the listener
245
-
client.cancelResponse(id, sampleCount);
239
+
await client.cancelResponse(id, sampleCount);
246
240
```
247
241
248
242
This method will cause the model to immediately cease generation, but also truncate the item being played by removing all audio after `sampleCount` and clearing the text response. By using this method you can interrupt the model and prevent it from "remembering" anything it has generated that is ahead of where the user's state is.
249
243
250
244
## Client events
251
245
252
-
If you need more manual control and want to send custom client events according to the [Realtime Client Events API Reference](https://platform.openai.com/docs/api-reference/realtime-client-events), you can use `client.realtime.send()` like so:
246
+
The `RealtimeClient` provides strongly typed events that map to the [Realtime API Events](https://platform.openai.com/docs/api-reference/realtime-events). You can listen to specific events using the `RealtimeEventType` enum.
With `RealtimeClient` we have reduced the event overhead from server events to **five** main events that are most critical for your application control flow. These events **are not** part of the API specification itself, but wrap logic to make application development easier.
263
+
With `RealtimeClient` we have reduced the event overhead from server events to **five** main events that are most critical for your application control flow:
267
264
268
265
```dart
269
266
// Errors like connection failures
270
-
client.on('error', (event) {
271
-
// do something
267
+
client.on(RealtimeEventType.error, (event) {
268
+
final error = (event as RealtimeEventError).error;
269
+
// do something with the error
272
270
});
273
271
274
272
// In VAD mode, the user starts speaking
275
273
// we can use this to stop audio playback of a previous response if necessary
final item = (event as RealtimeEventConversationItemCompleted).item;
313
+
// item.statuswill always be ItemStatus.completed
317
314
});
318
315
```
319
316
320
317
### Server events
321
318
322
-
If you want more control over your application development, you can use the `realtime.event` event and choose only to respond to **server** events. The full documentation for these events are available on the [Realtime Server Events API Reference](https://platform.openai.com/docs/api-reference/realtime-server-events).
319
+
If you want more control over your application development, you can use the `RealtimeEventType.all` event and choose only to respond to **server** events. The full documentation for these events are available on the [Realtime Server Events API Reference](https://platform.openai.com/docs/api-reference/realtime-server-events).
323
320
324
321
```dart
325
322
// all events, can use for logging, debugging, or manual event handling
326
-
client.on('realtime.event', (event ) {
327
-
final time = event?['time'] as String?;
328
-
final source = event?['source'] as String?;
329
-
final eventPayload = event?['event'] as Map<String, dynamic>?;
0 commit comments