Skip to content

Commit 41824f7

Browse files
committed
Add sendCustomMessage(), log in onGenericMessage, use gpt-4o-transcribe model
1 parent 4a6b215 commit 41824f7

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
accompanistPermissions = "0.34.0"
33
agp = "8.5.2"
44
constraintlayoutCompose = "1.0.1"
5-
pipecatClient = "0.3.3"
5+
pipecatClient = "0.3.5"
66
kotlin = "2.0.20"
77
coreKtx = "1.13.1"
88
lifecycleRuntimeKtx = "2.8.6"

openai-realtime-webrtc-demo/src/main/java/ai/pipecat/openai_realtime_webrtc_demo/VoiceClientManager.kt

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import ai.pipecat.client.RTVIClient
44
import ai.pipecat.client.RTVIClientOptions
55
import ai.pipecat.client.RTVIClientParams
66
import ai.pipecat.client.RTVIEventCallbacks
7+
import ai.pipecat.client.helper.LLMFunctionCall
8+
import ai.pipecat.client.helper.LLMHelper
79
import ai.pipecat.client.openai_realtime_webrtc.OpenAIRealtimeSessionConfig
810
import ai.pipecat.client.openai_realtime_webrtc.OpenAIRealtimeWebRTCTransport
911
import ai.pipecat.client.result.Future
1012
import ai.pipecat.client.result.RTVIError
1113
import ai.pipecat.client.result.Result
14+
import ai.pipecat.client.transport.MsgClientToServer
1215
import ai.pipecat.client.transport.MsgServerToClient
1316
import ai.pipecat.client.types.ActionDescription
1417
import ai.pipecat.client.types.Participant
@@ -26,6 +29,7 @@ import androidx.compose.runtime.Stable
2629
import androidx.compose.runtime.mutableFloatStateOf
2730
import androidx.compose.runtime.mutableStateListOf
2831
import androidx.compose.runtime.mutableStateOf
32+
import kotlinx.serialization.json.JsonElement
2933

3034
@Immutable
3135
data class Error(val message: String)
@@ -65,6 +69,8 @@ class VoiceClientManager(private val context: Context) {
6569

6670
fun start() {
6771

72+
infix fun String.toStr(rhs: String) = this to Value.Str(rhs)
73+
6874
if (client.value != null) {
6975
return
7076
}
@@ -91,14 +97,26 @@ class VoiceClientManager(private val context: Context) {
9197
)
9298
),*/
9399
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+
)
102120
)
103121
)
104122
)
@@ -196,10 +214,26 @@ class VoiceClientManager(private val context: Context) {
196214
override fun onRemoteAudioLevel(level: Float, participant: Participant) {
197215
botAudioLevel.floatValue = level
198216
}
217+
218+
override fun onGenericMessage(msg: MsgServerToClient) {
219+
Log.i(TAG, "onGenericMessage: $msg")
220+
}
199221
}
200222

201223
val client = RTVIClient(OpenAIRealtimeWebRTCTransport.Factory(context), callbacks, options)
202224

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+
203237
client.connect().displayErrors().withErrorCallback {
204238
callbacks.onDisconnected()
205239
}
@@ -216,4 +250,13 @@ class VoiceClientManager(private val context: Context) {
216250
fun stop() {
217251
client.value?.disconnect()?.displayErrors()
218252
}
253+
254+
fun sendCustomMessage(msg: JsonElement) =
255+
client.value?.sendMessage(
256+
MsgClientToServer(
257+
type = "custom-request",
258+
data = msg
259+
)
260+
)
261+
219262
}

0 commit comments

Comments
 (0)