Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Vertex AI] Add Decodable conformance for FunctionResponse #13606

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions FirebaseVertexAI/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Unreleased
- [added] Added `Decodable` conformance for `FunctionResponse`. (#13606)

# 11.2.0
- [fixed] Resolved a decoding error for citations without a `uri` and added
support for decoding `title` fields, which were previously ignored. (#13518)
Expand Down
2 changes: 1 addition & 1 deletion FirebaseVertexAI/Sources/FunctionCalling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,4 @@ extension FunctionCallingConfig.Mode: Encodable {}

extension ToolConfig: Encodable {}

extension FunctionResponse: Encodable {}
extension FunctionResponse: Codable {}
9 changes: 6 additions & 3 deletions FirebaseVertexAI/Sources/ModelContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,13 @@ extension ModelContent.Part: Codable {
self = .data(mimetype: mimetype, bytes)
} else if values.contains(.functionCall) {
self = try .functionCall(values.decode(FunctionCall.self, forKey: .functionCall))
} else if values.contains(.functionResponse) {
self = try .functionResponse(values.decode(FunctionResponse.self, forKey: .functionResponse))
} else {
throw DecodingError.dataCorrupted(.init(
codingPath: [CodingKeys.text, CodingKeys.inlineData],
debugDescription: "No text, inline data or function call was found."
let unexpectedKeys = values.allKeys.map { $0.stringValue }
throw DecodingError.dataCorrupted(DecodingError.Context(
codingPath: values.codingPath,
debugDescription: "Unexpected ModelContent.Part type(s): \(unexpectedKeys)"
))
}
}
Expand Down
31 changes: 30 additions & 1 deletion FirebaseVertexAI/Tests/Unit/ModelContentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import XCTest

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
final class ModelContentTests: XCTestCase {
let decoder = JSONDecoder()
let encoder = JSONEncoder()

override func setUp() {
Expand All @@ -27,7 +28,35 @@ final class ModelContentTests: XCTestCase {
)
}

// MARK: ModelContent.Part Encoding
// MARK: - ModelContent.Part Decoding

func testDecodeFunctionResponsePart() throws {
let functionName = "test-function-name"
let resultParameter = "test-result-parameter"
let resultValue = "test-result-value"
let json = """
{
"functionResponse" : {
"name" : "\(functionName)",
"response" : {
"\(resultParameter)" : "\(resultValue)"
}
}
}
"""
let jsonData = try XCTUnwrap(json.data(using: .utf8))

let part = try decoder.decode(ModelContent.Part.self, from: jsonData)

guard case let .functionResponse(functionResponse) = part else {
XCTFail("Decoded Part was not a FunctionResponse.")
return
}
XCTAssertEqual(functionResponse.name, functionName)
XCTAssertEqual(functionResponse.response, [resultParameter: .string(resultValue)])
}

// MARK: - ModelContent.Part Encoding

func testEncodeFileDataPart() throws {
let mimeType = "image/jpeg"
Expand Down
Loading