Skip to content

Commit 3271557

Browse files
authored
Revert "Introduce FunctionCall and FunctionResponse types (#6311)" (#6360)
This better aligns FunctionCallParts with the other Parts.
1 parent d6ee0d2 commit 3271557

File tree

4 files changed

+38
-63
lines changed

4 files changed

+38
-63
lines changed

firebase-vertexai/CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* [feature] Added support for `title` and `publicationDate` in citations. (#6309)
33
* [feature] Added support for `frequencyPenalty`, `presencePenalty`, and `HarmBlockMethod`. (#6309)
44
* [changed] **Breaking Change**: Introduced `Citations` class. Now `CitationMetadata` wraps that type. (#6276)
5-
* [changed] **Breaking Change**: Introduced `FunctionCall` and `FunctionResponse` types. Now `FunctionCallPart` and `FunctionResponsePart` wrap those types, respectively. (#6311)
65
* [changed] **Breaking Change**: Reworked `Schema` declaration mechanism. (#6258)
76
* [changed] **Breaking Change**: Reworked function calling mechanism to use the new `Schema` format. Function calls no longer use native types, nor include references to the actual executable code. (#6258)
87
* [changed] **Breaking Change**: Made `totalBillableCharacters` field in `CountTokens` nullable and optional. (#6294)

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import android.graphics.BitmapFactory
2121
import android.util.Base64
2222
import com.google.firebase.vertexai.common.client.Schema
2323
import com.google.firebase.vertexai.common.shared.FileData
24+
import com.google.firebase.vertexai.common.shared.FunctionCall
25+
import com.google.firebase.vertexai.common.shared.FunctionCallPart
26+
import com.google.firebase.vertexai.common.shared.FunctionResponse
27+
import com.google.firebase.vertexai.common.shared.FunctionResponsePart
2428
import com.google.firebase.vertexai.common.shared.InlineData
2529
import com.google.firebase.vertexai.type.BlockReason
2630
import com.google.firebase.vertexai.type.Candidate
@@ -30,12 +34,8 @@ import com.google.firebase.vertexai.type.Content
3034
import com.google.firebase.vertexai.type.CountTokensResponse
3135
import com.google.firebase.vertexai.type.FileDataPart
3236
import com.google.firebase.vertexai.type.FinishReason
33-
import com.google.firebase.vertexai.type.FunctionCall
34-
import com.google.firebase.vertexai.type.FunctionCallPart
3537
import com.google.firebase.vertexai.type.FunctionCallingConfig
3638
import com.google.firebase.vertexai.type.FunctionDeclaration
37-
import com.google.firebase.vertexai.type.FunctionResponse
38-
import com.google.firebase.vertexai.type.FunctionResponsePart
3939
import com.google.firebase.vertexai.type.GenerateContentResponse
4040
import com.google.firebase.vertexai.type.GenerationConfig
4141
import com.google.firebase.vertexai.type.HarmBlockMethod
@@ -81,10 +81,10 @@ internal fun Part.toInternal(): com.google.firebase.vertexai.common.shared.Part
8181
com.google.firebase.vertexai.common.shared.InlineDataPart(
8282
InlineData(mimeType, Base64.encodeToString(inlineData, BASE_64_FLAGS))
8383
)
84-
is FunctionCallPart ->
85-
com.google.firebase.vertexai.common.shared.FunctionCallPart(functionCall.toInternal())
86-
is FunctionResponsePart ->
87-
com.google.firebase.vertexai.common.shared.FunctionResponsePart(functionResponse.toInternal())
84+
is com.google.firebase.vertexai.type.FunctionCallPart ->
85+
FunctionCallPart(FunctionCall(name, args))
86+
is com.google.firebase.vertexai.type.FunctionResponsePart ->
87+
FunctionResponsePart(FunctionResponse(name, response))
8888
is FileDataPart ->
8989
com.google.firebase.vertexai.common.shared.FileDataPart(
9090
FileData(mimeType = mimeType, fileUri = uri)
@@ -96,12 +96,6 @@ internal fun Part.toInternal(): com.google.firebase.vertexai.common.shared.Part
9696
}
9797
}
9898

99-
internal fun FunctionCall.toInternal() =
100-
com.google.firebase.vertexai.common.shared.FunctionCall(name, args)
101-
102-
internal fun FunctionResponse.toInternal() =
103-
com.google.firebase.vertexai.common.shared.FunctionResponse(name, response)
104-
10599
internal fun SafetySetting.toInternal() =
106100
com.google.firebase.vertexai.common.shared.SafetySetting(
107101
harmCategory.toInternal(),
@@ -235,10 +229,16 @@ internal fun com.google.firebase.vertexai.common.shared.Part.toPublic(): Part {
235229
InlineDataPart(data, inlineData.mimeType)
236230
}
237231
}
238-
is com.google.firebase.vertexai.common.shared.FunctionCallPart ->
239-
FunctionCallPart(functionCall.toPublic())
240-
is com.google.firebase.vertexai.common.shared.FunctionResponsePart ->
241-
FunctionResponsePart(functionResponse.toPublic())
232+
is FunctionCallPart ->
233+
com.google.firebase.vertexai.type.FunctionCallPart(
234+
functionCall.name,
235+
functionCall.args.orEmpty().mapValues { it.value ?: JsonNull }
236+
)
237+
is FunctionResponsePart ->
238+
com.google.firebase.vertexai.type.FunctionResponsePart(
239+
functionResponse.name,
240+
functionResponse.response,
241+
)
242242
is com.google.firebase.vertexai.common.shared.FileDataPart ->
243243
FileDataPart(fileData.mimeType, fileData.fileUri)
244244
else ->
@@ -248,15 +248,6 @@ internal fun com.google.firebase.vertexai.common.shared.Part.toPublic(): Part {
248248
}
249249
}
250250

251-
internal fun com.google.firebase.vertexai.common.shared.FunctionCall.toPublic() =
252-
FunctionCall(name, args.orEmpty().mapValues { it.value ?: JsonNull })
253-
254-
internal fun com.google.firebase.vertexai.common.shared.FunctionResponse.toPublic() =
255-
FunctionResponse(
256-
name,
257-
response,
258-
)
259-
260251
internal fun com.google.firebase.vertexai.common.server.CitationSources.toPublic(): Citation {
261252
val publicationDateAsCalendar =
262253
publicationDate?.let {

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

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.google.firebase.vertexai.type
1919
import android.graphics.Bitmap
2020
import kotlinx.serialization.json.JsonElement
2121
import kotlinx.serialization.json.JsonObject
22+
import org.json.JSONObject
2223

2324
/** Interface representing data sent to and received from requests. */
2425
public interface Part
@@ -44,35 +45,21 @@ public class ImagePart(public val image: Bitmap) : Part
4445
public class InlineDataPart(public val inlineData: ByteArray, public val mimeType: String) : Part
4546

4647
/**
47-
* Represents a function call request from the model
48-
*
49-
* @param functionCall The information provided by the model to call a function.
50-
*/
51-
public class FunctionCallPart(public val functionCall: FunctionCall) : Part
52-
53-
/**
54-
* The result of calling a function as requested by the model.
55-
*
56-
* @param functionResponse The information to send back to the model as the result of a functions
57-
* call.
58-
*/
59-
public class FunctionResponsePart(public val functionResponse: FunctionResponse) : Part
60-
61-
/**
62-
* The data necessary to invoke function [name] using the arguments [args].
48+
* Represents function call name and params received from requests.
6349
*
6450
* @param name the name of the function to call
6551
* @param args the function parameters and values as a [Map]
6652
*/
67-
public class FunctionCall(public val name: String, public val args: Map<String, JsonElement>)
53+
public class FunctionCallPart(public val name: String, public val args: Map<String, JsonElement>) :
54+
Part
6855

6956
/**
70-
* The [response] generated after calling function [name].
57+
* Represents function call output to be returned to the model when it requests a function call.
7158
*
7259
* @param name the name of the called function
73-
* @param response the response produced by the function as a [JsonObject]
60+
* @param response the response produced by the function as a [JSONObject]
7461
*/
75-
public class FunctionResponse(public val name: String, public val response: JsonObject)
62+
public class FunctionResponsePart(public val name: String, public val response: JsonObject) : Part
7663

7764
/**
7865
* Represents file data stored in Cloud Storage for Firebase, referenced by URI.

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ internal class UnarySnapshotTests {
353353
val response = model.generateContent("prompt")
354354
val callPart = (response.candidates.first().content.parts.first() as FunctionCallPart)
355355

356-
callPart.functionCall.args["season"] shouldBe JsonPrimitive(null)
356+
callPart.args["season"] shouldBe JsonPrimitive(null)
357357
}
358358
}
359359

@@ -370,7 +370,7 @@ internal class UnarySnapshotTests {
370370
it.parts.first().shouldBeInstanceOf<FunctionCallPart>()
371371
}
372372

373-
callPart.functionCall.args["current"] shouldBe JsonPrimitive(true)
373+
callPart.args["current"] shouldBe JsonPrimitive(true)
374374
}
375375
}
376376

@@ -387,11 +387,9 @@ internal class UnarySnapshotTests {
387387
it.parts.first().shouldBeInstanceOf<FunctionCallPart>()
388388
}
389389

390-
callPart.functionCall.args["current"] shouldBe JsonPrimitive(true)
391-
callPart.functionCall.args["testObject"]!!
392-
.jsonObject["testProperty"]!!
393-
.jsonPrimitive
394-
.content shouldBe "string property"
390+
callPart.args["current"] shouldBe JsonPrimitive(true)
391+
callPart.args["testObject"]!!.jsonObject["testProperty"]!!.jsonPrimitive.content shouldBe
392+
"string property"
395393
}
396394
}
397395

@@ -402,8 +400,8 @@ internal class UnarySnapshotTests {
402400
val response = model.generateContent("prompt")
403401
val callPart = response.functionCalls.shouldNotBeEmpty().first()
404402

405-
callPart.functionCall.name shouldBe "current_time"
406-
callPart.functionCall.args.isEmpty() shouldBe true
403+
callPart.name shouldBe "current_time"
404+
callPart.args.isEmpty() shouldBe true
407405
}
408406
}
409407

@@ -414,9 +412,9 @@ internal class UnarySnapshotTests {
414412
val response = model.generateContent("prompt")
415413
val callPart = response.functionCalls.shouldNotBeEmpty().first()
416414

417-
callPart.functionCall.name shouldBe "sum"
418-
callPart.functionCall.args["x"] shouldBe JsonPrimitive(4)
419-
callPart.functionCall.args["y"] shouldBe JsonPrimitive(5)
415+
callPart.name shouldBe "sum"
416+
callPart.args["x"] shouldBe JsonPrimitive(4)
417+
callPart.args["y"] shouldBe JsonPrimitive(5)
420418
}
421419
}
422420

@@ -429,8 +427,8 @@ internal class UnarySnapshotTests {
429427

430428
callList.size shouldBe 3
431429
callList.forEach {
432-
it.functionCall.name shouldBe "sum"
433-
it.functionCall.args.size shouldBe 2
430+
it.name shouldBe "sum"
431+
it.args.size shouldBe 2
434432
}
435433
}
436434
}
@@ -444,7 +442,7 @@ internal class UnarySnapshotTests {
444442

445443
response.text shouldBe "The sum of [1, 2, 3] is"
446444
callList.size shouldBe 2
447-
callList.forEach { it.functionCall.args.size shouldBe 2 }
445+
callList.forEach { it.args.size shouldBe 2 }
448446
}
449447
}
450448

0 commit comments

Comments
 (0)