-
Notifications
You must be signed in to change notification settings - Fork 4
/
client_event.go
302 lines (267 loc) · 11.4 KB
/
client_event.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package openairt
import "encoding/json"
// ClientEventType is the type of client event. See https://platform.openai.com/docs/guides/realtime/client-events
type ClientEventType string
const (
ClientEventTypeSessionUpdate ClientEventType = "session.update"
ClientEventTypeInputAudioBufferAppend ClientEventType = "input_audio_buffer.append"
ClientEventTypeInputAudioBufferCommit ClientEventType = "input_audio_buffer.commit"
ClientEventTypeInputAudioBufferClear ClientEventType = "input_audio_buffer.clear"
ClientEventTypeConversationItemCreate ClientEventType = "conversation.item.create"
ClientEventTypeConversationItemTruncate ClientEventType = "conversation.item.truncate"
ClientEventTypeConversationItemDelete ClientEventType = "conversation.item.delete"
ClientEventTypeResponseCreate ClientEventType = "response.create"
ClientEventTypeResponseCancel ClientEventType = "response.cancel"
)
// ClientEvent is the interface for client event.
type ClientEvent interface {
ClientEventType() ClientEventType
}
// EventBase is the base struct for all client events.
type EventBase struct {
// Optional client-generated ID used to identify this event.
EventID string `json:"event_id,omitempty"`
}
type ClientSession struct {
// The set of modalities the model can respond with. To disable audio, set this to ["text"].
Modalities []Modality `json:"modalities,omitempty"`
// The default system instructions prepended to model calls.
Instructions string `json:"instructions,omitempty"`
// The voice the model uses to respond - one of alloy, echo, or shimmer. Cannot be changed once the model has responded with audio at least once.
Voice Voice `json:"voice,omitempty"`
// The format of input audio. Options are "pcm16", "g711_ulaw", or "g711_alaw".
InputAudioFormat AudioFormat `json:"input_audio_format,omitempty"`
// The format of output audio. Options are "pcm16", "g711_ulaw", or "g711_alaw".
OutputAudioFormat AudioFormat `json:"output_audio_format,omitempty"`
// Configuration for input audio transcription. Can be set to `nil` to turn off.
InputAudioTranscription *InputAudioTranscription `json:"input_audio_transcription,omitempty"`
// Configuration for turn detection. Can be set to `nil` to turn off.
TurnDetection *ClientTurnDetection `json:"turn_detection"`
// Tools (functions) available to the model.
Tools []Tool `json:"tools,omitempty"`
// How the model chooses tools. Options are "auto", "none", "required", or specify a function.
ToolChoice ToolChoiceInterface `json:"tool_choice,omitempty"`
// Sampling temperature for the model.
Temperature *float32 `json:"temperature,omitempty"`
// Maximum number of output tokens for a single assistant response, inclusive of tool calls. Provide an integer between 1 and 4096 to limit output tokens, or "inf" for the maximum available tokens for a given model. Defaults to "inf".
MaxOutputTokens IntOrInf `json:"max_response_output_tokens,omitempty"`
}
// SessionUpdateEvent is the event for session update.
// Send this event to update the session’s default configuration.
// See https://platform.openai.com/docs/api-reference/realtime-client-events/session/update
type SessionUpdateEvent struct {
EventBase
// Session configuration to update.
Session ClientSession `json:"session"`
}
func (m SessionUpdateEvent) ClientEventType() ClientEventType {
return ClientEventTypeSessionUpdate
}
func (m SessionUpdateEvent) MarshalJSON() ([]byte, error) {
type sessionUpdateEvent SessionUpdateEvent
v := struct {
*sessionUpdateEvent
Type ClientEventType `json:"type"`
}{
sessionUpdateEvent: (*sessionUpdateEvent)(&m),
Type: m.ClientEventType(),
}
return json.Marshal(v)
}
// InputAudioBufferAppendEvent is the event for input audio buffer append.
// Send this event to append audio bytes to the input audio buffer.
// See https://platform.openai.com/docs/api-reference/realtime-client-events/input_audio_buffer/append
type InputAudioBufferAppendEvent struct {
EventBase
Audio string `json:"audio"` // Base64-encoded audio bytes.
}
func (m InputAudioBufferAppendEvent) ClientEventType() ClientEventType {
return ClientEventTypeInputAudioBufferAppend
}
func (m InputAudioBufferAppendEvent) MarshalJSON() ([]byte, error) {
type inputAudioBufferAppendEvent InputAudioBufferAppendEvent
v := struct {
*inputAudioBufferAppendEvent
Type ClientEventType `json:"type"`
}{
inputAudioBufferAppendEvent: (*inputAudioBufferAppendEvent)(&m),
Type: m.ClientEventType(),
}
return json.Marshal(v)
}
// InputAudioBufferCommitEvent is the event for input audio buffer commit.
// Send this event to commit audio bytes to a user message.
// See https://platform.openai.com/docs/api-reference/realtime-client-events/input_audio_buffer/commit
type InputAudioBufferCommitEvent struct {
EventBase
}
func (m InputAudioBufferCommitEvent) ClientEventType() ClientEventType {
return ClientEventTypeInputAudioBufferCommit
}
func (m InputAudioBufferCommitEvent) MarshalJSON() ([]byte, error) {
type inputAudioBufferCommitEvent InputAudioBufferCommitEvent
v := struct {
*inputAudioBufferCommitEvent
Type ClientEventType `json:"type"`
}{
inputAudioBufferCommitEvent: (*inputAudioBufferCommitEvent)(&m),
Type: m.ClientEventType(),
}
return json.Marshal(v)
}
// InputAudioBufferClearEvent is the event for input audio buffer clear.
// Send this event to clear the audio bytes in the buffer.
// See https://platform.openai.com/docs/api-reference/realtime-client-events/input_audio_buffer/clear
type InputAudioBufferClearEvent struct {
EventBase
}
func (m InputAudioBufferClearEvent) ClientEventType() ClientEventType {
return ClientEventTypeInputAudioBufferClear
}
func (m InputAudioBufferClearEvent) MarshalJSON() ([]byte, error) {
type inputAudioBufferClearEvent InputAudioBufferClearEvent
v := struct {
*inputAudioBufferClearEvent
Type ClientEventType `json:"type"`
}{
inputAudioBufferClearEvent: (*inputAudioBufferClearEvent)(&m),
Type: m.ClientEventType(),
}
return json.Marshal(v)
}
// ConversationItemCreateEvent is the event for conversation item create.
// Send this event when adding an item to the conversation.
// See https://platform.openai.com/docs/api-reference/realtime-client-events/conversation/item/create
type ConversationItemCreateEvent struct {
EventBase
// The ID of the preceding item after which the new item will be inserted.
PreviousItemID string `json:"previous_item_id,omitempty"`
// The item to add to the conversation.
Item MessageItem `json:"item"`
}
func (m ConversationItemCreateEvent) ClientEventType() ClientEventType {
return ClientEventTypeConversationItemCreate
}
func (m ConversationItemCreateEvent) MarshalJSON() ([]byte, error) {
type conversationItemCreateEvent ConversationItemCreateEvent
v := struct {
*conversationItemCreateEvent
Type ClientEventType `json:"type"`
}{
conversationItemCreateEvent: (*conversationItemCreateEvent)(&m),
Type: m.ClientEventType(),
}
return json.Marshal(v)
}
// ConversationItemTruncateEvent is the event for conversation item truncate.
// Send this event when you want to truncate a previous assistant message’s audio.
// See https://platform.openai.com/docs/api-reference/realtime-client-events/conversation/item/truncate
type ConversationItemTruncateEvent struct {
EventBase
// The ID of the assistant message item to truncate.
ItemID string `json:"item_id"`
// The index of the content part to truncate.
ContentIndex int `json:"content_index"`
// Inclusive duration up to which audio is truncated, in milliseconds.
AudioEndMs int `json:"audio_end_ms"`
}
func (m ConversationItemTruncateEvent) ClientEventType() ClientEventType {
return ClientEventTypeConversationItemTruncate
}
func (m ConversationItemTruncateEvent) MarshalJSON() ([]byte, error) {
type conversationItemTruncateEvent ConversationItemTruncateEvent
v := struct {
*conversationItemTruncateEvent
Type ClientEventType `json:"type"`
}{
conversationItemTruncateEvent: (*conversationItemTruncateEvent)(&m),
Type: m.ClientEventType(),
}
return json.Marshal(v)
}
// ConversationItemDeleteEvent is the event for conversation item delete.
// Send this event when you want to remove any item from the conversation history.
// See https://platform.openai.com/docs/api-reference/realtime-client-events/conversation/item/delete
type ConversationItemDeleteEvent struct {
EventBase
// The ID of the item to delete.
ItemID string `json:"item_id"`
}
func (m ConversationItemDeleteEvent) ClientEventType() ClientEventType {
return ClientEventTypeConversationItemDelete
}
func (m ConversationItemDeleteEvent) MarshalJSON() ([]byte, error) {
type conversationItemDeleteEvent ConversationItemDeleteEvent
v := struct {
*conversationItemDeleteEvent
Type ClientEventType `json:"type"`
}{
conversationItemDeleteEvent: (*conversationItemDeleteEvent)(&m),
Type: m.ClientEventType(),
}
return json.Marshal(v)
}
type ResponseCreateParams struct {
// The modalities for the response.
Modalities []Modality `json:"modalities,omitempty"`
// Instructions for the model.
Instructions string `json:"instructions,omitempty"`
// The voice the model uses to respond - one of alloy, echo, or shimmer.
Voice Voice `json:"voice,omitempty"`
// The format of output audio.
OutputAudioFormat AudioFormat `json:"output_audio_format,omitempty"`
// Tools (functions) available to the model.
Tools []Tool `json:"tools,omitempty"`
// How the model chooses tools.
ToolChoice ToolChoiceInterface `json:"tool_choice,omitempty"`
// Sampling temperature.
Temperature *float32 `json:"temperature,omitempty"`
// Maximum number of output tokens for a single assistant response, inclusive of tool calls. Provide an integer between 1 and 4096 to limit output tokens, or "inf" for the maximum available tokens for a given model. Defaults to "inf".
MaxOutputTokens IntOrInf `json:"max_output_tokens,omitempty"`
}
// ResponseCreateEvent is the event for response create.
// Send this event to trigger a response generation.
// See https://platform.openai.com/docs/api-reference/realtime-client-events/response/create
type ResponseCreateEvent struct {
EventBase
// Configuration for the response.
Response ResponseCreateParams `json:"response"`
}
func (m ResponseCreateEvent) ClientEventType() ClientEventType {
return ClientEventTypeResponseCreate
}
func (m ResponseCreateEvent) MarshalJSON() ([]byte, error) {
type responseCreateEvent ResponseCreateEvent
v := struct {
*responseCreateEvent
Type ClientEventType `json:"type"`
}{
responseCreateEvent: (*responseCreateEvent)(&m),
Type: m.ClientEventType(),
}
return json.Marshal(v)
}
// ResponseCancelEvent is the event for response cancel.
// Send this event to cancel an in-progress response.
// See https://platform.openai.com/docs/api-reference/realtime-client-events/response/cancel
type ResponseCancelEvent struct {
EventBase
}
func (m ResponseCancelEvent) ClientEventType() ClientEventType {
return ClientEventTypeResponseCancel
}
func (m ResponseCancelEvent) MarshalJSON() ([]byte, error) {
type responseCancelEvent ResponseCancelEvent
v := struct {
*responseCancelEvent
Type ClientEventType `json:"type"`
}{
responseCancelEvent: (*responseCancelEvent)(&m),
Type: m.ClientEventType(),
}
return json.Marshal(v)
}
// MarshalClientEvent marshals the client event to JSON.
func MarshalClientEvent(event ClientEvent) ([]byte, error) {
return json.Marshal(event)
}