Skip to content

Commit 533a4b4

Browse files
Add manual tool handling
1 parent 7fd2cff commit 533a4b4

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const client = new RealtimeClient({ url: RELAY_SERVER_URL });
8181
1. [Sending messages](#sending-messages)
8282
1. [Sending streaming audio](#sending-streaming-audio)
8383
1. [Adding and using tools](#adding-and-using-tools)
84+
1. [Manually using tools](#manually-using-tools)
8485
1. [Interrupting the model](#interrupting-the-model)
8586
1. [Client events](#client-events)
8687
1. [Reference Client Utility Events](#reference-client-utility-events)
@@ -190,6 +191,70 @@ client.addTool(
190191
);
191192
```
192193

194+
### Manually using tools
195+
196+
The `.addTool()` method automatically runs a tool handler and triggers a response
197+
on handler completion. Sometimes you may not want that, for example: using tools
198+
to generate a schema that you use for other purposes.
199+
200+
In this case, we can use the `tools` item with `updateSession`. In this case you
201+
**must** specify `type: 'function'`, which is not required for `.addTool()`.
202+
203+
**Note:** Tools added with `.addTool()` will **not** be overridden when updating
204+
sessions manually like this, but every `updateSession()` change will override previous
205+
`updateSession()` changes. Tools added via `.addTool()` are persisted and appended
206+
to anything set manually here.
207+
208+
```javascript
209+
client.updateSession({
210+
tools: [
211+
{
212+
type: 'function',
213+
name: 'get_weather',
214+
description:
215+
'Retrieves the weather for a given lat, lng coordinate pair. Specify a label for the location.',
216+
parameters: {
217+
type: 'object',
218+
properties: {
219+
lat: {
220+
type: 'number',
221+
description: 'Latitude',
222+
},
223+
lng: {
224+
type: 'number',
225+
description: 'Longitude',
226+
},
227+
location: {
228+
type: 'string',
229+
description: 'Name of the location',
230+
},
231+
},
232+
required: ['lat', 'lng', 'location'],
233+
},
234+
},
235+
],
236+
});
237+
```
238+
239+
Then, to handle function calls...
240+
241+
```javascript
242+
client.on('conversation.updated', ({ item, delta }) => {
243+
if (item.type === 'function_call') {
244+
// do something
245+
if (delta.arguments) {
246+
// populating the arguments
247+
}
248+
}
249+
});
250+
251+
client.on('conversation.item.completed', ({ item }) => {
252+
if (item.type === 'function_call') {
253+
// your function call is complete, execute some custom code
254+
}
255+
});
256+
```
257+
193258
## Interrupting the model
194259

195260
You may want to manually interrupt the model, especially in `turn_detection: 'disabled'` mode.

0 commit comments

Comments
 (0)