@@ -4,11 +4,14 @@ import ai.pipecat.client.RTVIClient
4
4
import ai.pipecat.client.RTVIClientOptions
5
5
import ai.pipecat.client.RTVIClientParams
6
6
import ai.pipecat.client.RTVIEventCallbacks
7
+ import ai.pipecat.client.helper.LLMFunctionCall
8
+ import ai.pipecat.client.helper.LLMHelper
7
9
import ai.pipecat.client.openai_realtime_webrtc.OpenAIRealtimeSessionConfig
8
10
import ai.pipecat.client.openai_realtime_webrtc.OpenAIRealtimeWebRTCTransport
9
11
import ai.pipecat.client.result.Future
10
12
import ai.pipecat.client.result.RTVIError
11
13
import ai.pipecat.client.result.Result
14
+ import ai.pipecat.client.transport.MsgClientToServer
12
15
import ai.pipecat.client.transport.MsgServerToClient
13
16
import ai.pipecat.client.types.ActionDescription
14
17
import ai.pipecat.client.types.Participant
@@ -26,6 +29,7 @@ import androidx.compose.runtime.Stable
26
29
import androidx.compose.runtime.mutableFloatStateOf
27
30
import androidx.compose.runtime.mutableStateListOf
28
31
import androidx.compose.runtime.mutableStateOf
32
+ import kotlinx.serialization.json.JsonElement
29
33
30
34
@Immutable
31
35
data class Error (val message : String )
@@ -65,6 +69,8 @@ class VoiceClientManager(private val context: Context) {
65
69
66
70
fun start () {
67
71
72
+ infix fun String.toStr (rhs : String ) = this to Value .Str (rhs)
73
+
68
74
if (client.value != null ) {
69
75
return
70
76
}
@@ -91,14 +97,26 @@ class VoiceClientManager(private val context: Context) {
91
97
)
92
98
),*/
93
99
initialConfig = OpenAIRealtimeSessionConfig (
94
- turnDetection = Value .Object (
95
- " type" to Value .Str (" semantic_vad" )
96
- ),
97
- inputAudioNoiseReduction = Value .Object (
98
- " type" to Value .Str (" near_field" )
99
- ),
100
- inputAudioTranscription = Value .Object (
101
- " model" to Value .Str (" whisper-1" )
100
+ voice = " ballad" ,
101
+ turnDetection = Value .Object (" type" to Value .Str (" semantic_vad" )),
102
+ inputAudioNoiseReduction = Value .Object (" type" to Value .Str (" near_field" )),
103
+ inputAudioTranscription = Value .Object (" model" to Value .Str (" gpt-4o-transcribe" )),
104
+ tools = Value .Array (
105
+ Value .Object (
106
+ " type" toStr " function" ,
107
+ " name" toStr " get_current_weather" ,
108
+ " description" toStr " Get the current weather for a given location" ,
109
+ " parameters" to Value .Object (
110
+ " type" toStr " object" ,
111
+ " properties" to Value .Object (
112
+ " location" to Value .Object (
113
+ " type" toStr " string" ,
114
+ " description" toStr " The city and country, eg. San Francisco, USA" ,
115
+ )
116
+ ),
117
+ " required" to Value .Array (Value .Str (" location" ))
118
+ )
119
+ )
102
120
)
103
121
)
104
122
)
@@ -196,10 +214,26 @@ class VoiceClientManager(private val context: Context) {
196
214
override fun onRemoteAudioLevel (level : Float , participant : Participant ) {
197
215
botAudioLevel.floatValue = level
198
216
}
217
+
218
+ override fun onGenericMessage (msg : MsgServerToClient ) {
219
+ Log .i(TAG , " onGenericMessage: $msg " )
220
+ }
199
221
}
200
222
201
223
val client = RTVIClient (OpenAIRealtimeWebRTCTransport .Factory (context), callbacks, options)
202
224
225
+ val llmHelper = LLMHelper (object : LLMHelper .Callbacks () {
226
+ override fun onLLMFunctionCall (
227
+ func : LLMFunctionCall ,
228
+ onResult : (Value ) -> Unit
229
+ ) {
230
+ Log .i(TAG , " Function call from bot: $func " )
231
+ onResult(Value .Str (" 27 degrees celsius, rainy" ))
232
+ }
233
+ })
234
+
235
+ client.registerHelper(" llm" , llmHelper)
236
+
203
237
client.connect().displayErrors().withErrorCallback {
204
238
callbacks.onDisconnected()
205
239
}
@@ -216,4 +250,13 @@ class VoiceClientManager(private val context: Context) {
216
250
fun stop () {
217
251
client.value?.disconnect()?.displayErrors()
218
252
}
253
+
254
+ fun sendCustomMessage (msg : JsonElement ) =
255
+ client.value?.sendMessage(
256
+ MsgClientToServer (
257
+ type = " custom-request" ,
258
+ data = msg
259
+ )
260
+ )
261
+
219
262
}
0 commit comments