Triggering function calls manually
Closed this issue · 1 comments
kozikkamil commented
Hey!
I am trying to trigger a function call manually from code.
This is my rough setup:
this.realtimeClient.updateSession({
instructions: '... End the session once user has generated 10 messages.',
tools: [
{
type: 'function',
name: 'getUserMessagesCount',
description:
'Retrieves how many messages has the user sent.\n'
parameters: {},
},
],
});
this.realtimeClient.realtime.send('conversation.item.create', {
previous_item_id: null,
item: {
type: 'function_call',
status: 'incomplete',
name: 'getUserMessagesCount',
call_id: uuid(),
arguments: '',
},
});
realtimeClient.on(
'conversation.updated',
(event) => {
if (event.item.type === 'function_call') {
this.realtimeClient.realtime.send('conversation.item.create', {
item: {
type: 'function_call_output',
call_id: event.item.call_id,
output: JSON.stringify({
user_messages_count: 10,
}),
},
});
}
},
);
realtimeClient.realtime.on('error', (event: unknown) => {
console.log('Realtime client error:', { event });
});
Now, calling
const items = this.realtimeClient.conversation.getItems();
- I can verify the .send() is being triggered
- No function call or function call is present in the items
- The LLM is not aware of the number of the number of user messages. It does not adhere to the instruction provided. Only after explicitly asking for checking how many messages the user has sent, does it properly call the function.
- There is no error logged
Could you please tell me if I am calling the function wrong? It seems to be in line with the documentation provided in https://platform.openai.com/docs/api-reference/realtime-client-events/conversation/item/create, and there is no mention of this behavior in this repo's Readme https://github.com/openai/openai-realtime-api-beta?tab=readme-ov-file#manually-using-tools
kozikkamil commented
I was able to debug the issue by running this in debug mode
new RealtimeClient({
apiKey: OPENAI_API_KEY,
debug: true,
}),
Which surfaced these error logs
[
'received:',
'error',
{
type: 'error',
error: {
type: 'invalid_request_error',
code: 'string_above_max_length',
message: "Invalid 'item.call_id': string too long. Expected a string with maximum length 32, but got a string with length 36 instead.",
param: 'item.call_id',
}
}
]
I had to change
this.realtimeClient.realtime.send('conversation.item.create', {
previous_item_id: null,
item: {
type: 'function_call',
status: 'incomplete',
name: 'getUserMessagesCount',
call_id: uuid(),
arguments: '',
},
});
to
this.realtimeClient.realtime.send('conversation.item.create', {
previous_item_id: null,
item: {
type: 'function_call',
status: 'incomplete',
name: 'getUserMessagesCount',
call_id: RealtimeUtils.generateId('evt_'),
arguments: '',
},
});
It works now:)