客户端事件
这些是 OpenAI Realtime WebSocket 服务器将从客户端接受的事件。
会话更新
发送此事件以更新会话的默认配置。客户可以
随时发送此事件以更新会话配置,以及任何
字段可能随时更新,但 “voice” 除外。服务器将响应
替换为session.updated
事件,该事件显示完整的有效配置。
只有存在的字段才会更新,因此清除
字段(如 “instructions” )用于传递空字符串。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{
"event_id": "event_123",
"type": "session.update",
"session": {
"modalities": ["text", "audio"],
"instructions": "You are a helpful assistant.",
"voice": "sage",
"input_audio_format": "pcm16",
"output_audio_format": "pcm16",
"input_audio_transcription": {
"model": "whisper-1"
},
"turn_detection": {
"type": "server_vad",
"threshold": 0.5,
"prefix_padding_ms": 300,
"silence_duration_ms": 500
},
"tools": [
{
"type": "function",
"name": "get_weather",
"description": "Get the current weather...",
"parameters": {
"type": "object",
"properties": {
"location": { "type": "string" }
},
"required": ["location"]
}
}
],
"tool_choice": "auto",
"temperature": 0.8,
"max_response_output_tokens": "inf"
}
}
input_audio_buffer.append
发送此事件以将音频字节追加到输入音频缓冲区。音频 buffer 是您可以写入并在以后提交的临时存储。在服务器 VAD 中 模式下,音频缓冲区用于检测语音,服务器将决定 何时提交。禁用服务器 VAD 后,必须提交音频缓冲区 手动地。
客户端可以选择在每个事件中放置多少音频,但最多可以放置多少音频 的 15 MiB 中,例如,从客户端流式传输较小的数据块可能允许 VAD 的响应速度更快。与创建的其他客户端事件不同,服务器将 不发送对此事件的确认响应。
1
2
3
4
5
{
"event_id": "event_456",
"type": "input_audio_buffer.append",
"audio": "Base64EncodedAudioData"
}
input_audio_buffer.commit
发送此事件以提交用户输入音频缓冲区,这将创建一个 对话中的 New User Message 项。此事件将产生错误 如果 Input Audio buffer 为空。当处于 Server VAD 模式时,客户端执行 不需要发送此事件,服务器将提交音频缓冲区 自然而然。
提交输入音频缓冲区将触发输入音频转录
(如果在会话配置中启用),但它不会创建响应
从模型中。服务器将使用input_audio_buffer.committed
事件。
1
2
3
4
{
"event_id": "event_789",
"type": "input_audio_buffer.commit"
}
input_audio_buffer.clear
conversation.item.create
向 Conversation 的上下文添加新 Item,包括 messages, function calls 和函数调用响应。此事件可用于填充 “history” 并添加新项目,但具有 当前限制,它无法填充 Assistant 音频消息。
如果成功,服务器将使用conversation.item.created
event 的error
事件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"event_id": "event_345",
"type": "conversation.item.create",
"previous_item_id": null,
"item": {
"id": "msg_001",
"type": "message",
"role": "user",
"content": [
{
"type": "input_text",
"text": "Hello, how are you?"
}
]
}
}
conversation.item.truncate
发送此事件可截断上一个助理消息的音频。服务器 将比 RealTime 更快地生成音频,因此当用户 interrupts 截断已发送到客户端但未发送到客户端的音频 但玩了。这将使服务器对音频的理解与 客户端的播放。
截断音频将删除服务器端文本转录,以确保存在 不是用户未听到的上下文中的文本。
如果成功,服务器将使用conversation.item.truncated
事件。
1
2
3
4
5
6
7
{
"event_id": "event_678",
"type": "conversation.item.truncate",
"item_id": "msg_002",
"content_index": 0,
"audio_end_ms": 1500
}
conversation.item.delete
当您想从对话中删除任何项目时发送此事件
历史。服务器将使用conversation.item.deleted
事件
除非该项目在对话历史记录中不存在,在这种情况下,
服务器将响应错误。
1
2
3
4
5
{
"event_id": "event_901",
"type": "conversation.item.delete",
"item_id": "msg_003"
}
response.create
此事件指示服务器创建一个 Response,这意味着触发 模型推理。当处于 Server VAD 模式时,服务器将创建 Responses 自然而然。
响应将至少包含一个 Item,并且可能包含两个,在这种情况下 第二个将是函数调用。这些 Item 将被附加到 对话历史。
服务器将使用response.created
事件, 项目 的事件
和内容创建,最后创建一个response.done
event 来指示
响应已完成。
这response.create
事件包括推理配置,例如instructions
和temperature
.这些字段将覆盖 Session 的
配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
"event_id": "event_234",
"type": "response.create",
"response": {
"modalities": ["text", "audio"],
"instructions": "Please assist the user.",
"voice": "sage",
"output_audio_format": "pcm16",
"tools": [
{
"type": "function",
"name": "calculate_sum",
"description": "Calculates the sum of two numbers.",
"parameters": {
"type": "object",
"properties": {
"a": { "type": "number" },
"b": { "type": "number" }
},
"required": ["a", "b"]
}
}
],
"tool_choice": "auto",
"temperature": 0.7,
"max_output_tokens": 150
}
}