Skip to content

Commit 1007a29

Browse files
committed
Add functionCalls accessor to GenerateContentResponse (#12690)
1 parent 56feccc commit 1007a29

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

FirebaseVertexAI/Sources/GenerateContentResponse.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ public struct GenerateContentResponse {
3737
return text
3838
}
3939

40+
/// Returns function calls found in any `Part`s of the first candidate of the response, if any.
41+
public var functionCalls: [FunctionCall] {
42+
guard let candidate = candidates.first else {
43+
return []
44+
}
45+
return candidate.content.parts.compactMap { part in
46+
guard case let .functionCall(functionCall) = part else {
47+
return nil
48+
}
49+
return functionCall
50+
}
51+
}
52+
4053
/// Initializer for SwiftUI previews or tests.
4154
public init(candidates: [CandidateResponse], promptFeedback: PromptFeedback?) {
4255
self.candidates = candidates

FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ final class GenerativeModelTests: XCTestCase {
7070
let partText = try XCTUnwrap(part.text)
7171
XCTAssertTrue(partText.hasPrefix("You can ask me a wide range of questions"))
7272
XCTAssertEqual(response.text, partText)
73+
XCTAssertEqual(response.functionCalls, [])
7374
}
7475

7576
func testGenerateContent_success_basicReplyShort() async throws {
@@ -90,6 +91,7 @@ final class GenerativeModelTests: XCTestCase {
9091
let part = try XCTUnwrap(candidate.content.parts.first)
9192
XCTAssertEqual(part.text, "Mountain View, California, United States")
9293
XCTAssertEqual(response.text, part.text)
94+
XCTAssertEqual(response.functionCalls, [])
9395
}
9496

9597
func testGenerateContent_success_citations() async throws {
@@ -195,6 +197,7 @@ final class GenerativeModelTests: XCTestCase {
195197
}
196198
XCTAssertEqual(functionCall.name, "current_time")
197199
XCTAssertTrue(functionCall.args.isEmpty)
200+
XCTAssertEqual(response.functionCalls, [functionCall])
198201
}
199202

200203
func testGenerateContent_success_functionCall_noArguments() async throws {
@@ -216,6 +219,7 @@ final class GenerativeModelTests: XCTestCase {
216219
}
217220
XCTAssertEqual(functionCall.name, "current_time")
218221
XCTAssertTrue(functionCall.args.isEmpty)
222+
XCTAssertEqual(response.functionCalls, [functionCall])
219223
}
220224

221225
func testGenerateContent_success_functionCall_withArguments() async throws {
@@ -241,6 +245,7 @@ final class GenerativeModelTests: XCTestCase {
241245
XCTAssertEqual(argX, .number(4))
242246
let argY = try XCTUnwrap(functionCall.args["y"])
243247
XCTAssertEqual(argY, .number(5))
248+
XCTAssertEqual(response.functionCalls, [functionCall])
244249
}
245250

246251
func testGenerateContent_appCheck_validToken() async throws {

0 commit comments

Comments
 (0)