Skip to content

Commit 87df72c

Browse files
committed
working
1 parent a2fa02f commit 87df72c

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/LiveSession.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ internal constructor(
4545
@SerialName("client_content") val clientContent: ClientContent
4646
)
4747

48+
@Serializable
49+
internal data class ToolResponseSetup(
50+
val toolResponse: ToolResponse
51+
)
52+
@Serializable
53+
internal data class ToolResponse(
54+
val functionResponses: List<FunctionResponsePart.Internal.FunctionResponse>
55+
)
56+
4857
@Serializable
4958
internal data class ServerContentSetup(
5059
@SerialName("serverContent") val serverContent: ServerContent
@@ -207,6 +216,13 @@ internal constructor(
207216
}
208217
}
209218

219+
public suspend fun sendFunctionResponse(
220+
functionList: List<FunctionResponsePart>
221+
) {
222+
val jsonString = Json.encodeToString(ToolResponseSetup(ToolResponse(functionList.map{it.toInternalFunctionCall()})))
223+
session?.send(Frame.Text(jsonString))
224+
}
225+
210226
public suspend fun sendMediaStream(
211227
mediaChunks: List<MediaData>,
212228
) {

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Part.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public class FunctionCallPart(public val name: String, public val args: Map<Stri
8181
@Serializable
8282
internal data class FunctionCall(val name: String, val args: Map<String, JsonElement> )
8383
}
84+
8485
}
8586

8687
/**
@@ -94,7 +95,12 @@ public class FunctionResponsePart(public val name: String, public val response:
9495
@Serializable
9596
internal data class Internal(val functionResponse: FunctionResponse) : InternalPart {
9697

97-
@Serializable internal data class FunctionResponse(val name: String, val response: JsonObject)
98+
@Serializable
99+
public data class FunctionResponse(val name: String, val response: JsonObject)
100+
}
101+
102+
internal fun toInternalFunctionCall(): Internal.FunctionResponse {
103+
return Internal.FunctionResponse(this.name, this.response)
98104
}
99105
}
100106

@@ -141,7 +147,7 @@ internal object PartSerializer :
141147
val jsonObject = element.jsonObject
142148
return when {
143149
"text" in jsonObject -> TextPart.Internal.serializer()
144-
"functionCalls" in jsonObject -> FunctionCallPart.Internal.serializer()
150+
"functionCall" in jsonObject -> FunctionCallPart.Internal.serializer()
145151
"functionResponse" in jsonObject -> FunctionResponsePart.Internal.serializer()
146152
"inlineData" in jsonObject -> InlineDataPart.Internal.serializer()
147153
"fileData" in jsonObject -> FileDataPart.Internal.serializer()

firebase-vertexai/src/test/java/com/google/firebase/vertexai/LiveModelTesting.kt

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,24 @@ class LiveModelTesting {
5757
)
5858
runBlocking{
5959
val session = generativeModel.connect()
60-
session!!.send("Tell me a story")
60+
session!!.send("\"Tell me the status of the light tubelight")
61+
6162
session!!.receive(listOf(ContentModality.TEXT)).collect {
62-
println(it.text)
63-
}
63+
if(!it.functionCalls.isNullOrEmpty()) {
64+
val fetchWeatherCall = it.functionCalls!!.find { it.name == "fetchLight" }
6465

65-
// session!!.send("Tell me the status of the light ", listOf(ContentModality.TEXT)).collect {
66-
// println(it)
67-
// if(!it.functionCalls.isNullOrEmpty()) {
68-
// println("Entered??")
69-
// val fetchWeatherCall = it.functionCalls!!.find { it.name == "fetchLight" }
70-
//
71-
// val functionResponse = fetchWeatherCall?.let {
72-
// // Alternatively, if your `Location` class is marked as @Serializable, you can use
73-
//
74-
// val type = it.args["type"]!!.jsonPrimitive.content
75-
// fetchWeather(type)
76-
// }
77-
// session!!.send(Content("function", listOf(FunctionResponsePart("fetchLight", functionResponse!!))), listOf(ContentModality.TEXT)).collect {
78-
// x-> println(x.text)
79-
// }
80-
// }
81-
// }
66+
val functionResponse = fetchWeatherCall?.let {
67+
// Alternatively, if your `Location` class is marked as @Serializable, you can use
8268

69+
val type = it.args["type"]!!.jsonPrimitive.content
70+
fetchWeather(type)
71+
}
72+
session!!.sendFunctionResponse(listOf(FunctionResponsePart("fetchLight", functionResponse!!)))
73+
} else {
74+
println(it.text)
75+
}
76+
77+
}
8378

8479
}
8580

0 commit comments

Comments
 (0)