Skip to content

Commit 5c5a629

Browse files
committed
[Vertex AI] Add POC for initializing function declarations from JSON
1 parent 45db5d1 commit 5c5a629

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

FirebaseVertexAI/Sources/FunctionCalling.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ public struct FunctionDeclaration {
4848
nullable: false
4949
)
5050
}
51+
52+
/// Constructs a new `FunctionDeclaration`.
53+
///
54+
/// - Parameters:
55+
/// - jsonString: A string representing a function declaration in JSON format; see the [REST
56+
/// documentation]( https://cloud.google.com/vertex-ai/generative-ai/docs/reference/rest/v1/Tool#FunctionDeclaration)
57+
/// for details.
58+
public init(jsonString: String) throws {
59+
guard let jsonData = jsonString.data(using: .utf8) else {
60+
throw DecodingError.dataCorrupted(DecodingError.Context(
61+
codingPath: [],
62+
debugDescription: "Could not parse JSON string as UTF8."
63+
))
64+
}
65+
self = try JSONDecoder().decode(Self.self, from: jsonData)
66+
}
5167
}
5268

5369
/// A helper tool that the model may use when generating responses.
@@ -146,7 +162,7 @@ public struct ToolConfig {
146162
// MARK: - Codable Conformance
147163

148164
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
149-
extension FunctionDeclaration: Encodable {
165+
extension FunctionDeclaration: Codable {
150166
enum CodingKeys: String, CodingKey {
151167
case name
152168
case description

FirebaseVertexAI/Sources/Types/Internal/DataType.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ enum DataType: String {
3737

3838
// MARK: - Codable Conformance
3939

40-
extension DataType: Encodable {}
40+
extension DataType: Codable {}

FirebaseVertexAI/Sources/Types/Public/Schema.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Foundation
1919
/// These types can be objects, but also primitives and arrays. Represents a select subset of an
2020
/// [OpenAPI 3.0 schema object](https://spec.openapis.org/oas/v3.0.3#schema).
2121
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
22-
public class Schema {
22+
public final class Schema {
2323
/// Modifiers describing the expected format of a string `Schema`.
2424
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
2525
public struct StringFormat: EncodableProtoEnum {
@@ -319,7 +319,7 @@ public class Schema {
319319
// MARK: - Codable Conformance
320320

321321
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
322-
extension Schema: Encodable {
322+
extension Schema: Codable {
323323
enum CodingKeys: String, CodingKey {
324324
case dataType = "type"
325325
case format

FirebaseVertexAI/Tests/Unit/Snippets/FunctionCallingSnippets.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ final class FunctionCallingSnippets: XCTestCase {
2929
await FirebaseApp.deleteDefaultAppForSnippets()
3030
}
3131

32+
func testFunctionDeclaration_jsonString() throws {
33+
let json = """
34+
{
35+
"name": "getWeather",
36+
"description": "gets the weather for a requested city",
37+
"parameters": {
38+
"type": "OBJECT",
39+
"properties": {
40+
"city": {
41+
"type": "STRING"
42+
}
43+
}
44+
}
45+
}
46+
"""
47+
48+
let functionDeclaration = try FunctionDeclaration(jsonString: json)
49+
50+
print(functionDeclaration)
51+
}
52+
3253
func testFunctionCalling() async throws {
3354
// This function calls a hypothetical external API that returns
3455
// a collection of weather information for a given location on a given date.

0 commit comments

Comments
 (0)