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 POC for initializing function declarations from JSON #14084

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
20 changes: 18 additions & 2 deletions FirebaseVertexAI/Sources/FunctionCalling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Foundation
/// This `FunctionDeclaration` is a representation of a block of code that can be used as a ``Tool``
/// by the model and executed by the client.
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
public struct FunctionDeclaration {
public struct FunctionDeclaration: Equatable {
/// The name of the function.
let name: String

Expand Down Expand Up @@ -48,6 +48,22 @@ public struct FunctionDeclaration {
nullable: false
)
}

/// Constructs a new `FunctionDeclaration`.
///
/// - Parameters:
/// - jsonString: A string representing a function declaration in JSON format; see the [REST
/// documentation]( https://cloud.google.com/vertex-ai/generative-ai/docs/reference/rest/v1/Tool#FunctionDeclaration)
/// for details.
public init(jsonString: String) throws {
guard let jsonData = jsonString.data(using: .utf8) else {
throw DecodingError.dataCorrupted(DecodingError.Context(
codingPath: [],
debugDescription: "Could not parse JSON string as UTF8."
))
}
self = try JSONDecoder().decode(Self.self, from: jsonData)
}
}

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

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
extension FunctionDeclaration: Encodable {
extension FunctionDeclaration: Codable {
enum CodingKeys: String, CodingKey {
case name
case description
Expand Down
2 changes: 1 addition & 1 deletion FirebaseVertexAI/Sources/Types/Internal/DataType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ enum DataType: String {

// MARK: - Codable Conformance

extension DataType: Encodable {}
extension DataType: Codable {}
16 changes: 14 additions & 2 deletions FirebaseVertexAI/Sources/Types/Public/Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Foundation
/// These types can be objects, but also primitives and arrays. Represents a select subset of an
/// [OpenAPI 3.0 schema object](https://spec.openapis.org/oas/v3.0.3#schema).
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
public class Schema {
public final class Schema {
/// Modifiers describing the expected format of a string `Schema`.
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
public struct StringFormat: EncodableProtoEnum {
Expand Down Expand Up @@ -319,7 +319,7 @@ public class Schema {
// MARK: - Codable Conformance

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
extension Schema: Encodable {
extension Schema: Codable {
enum CodingKeys: String, CodingKey {
case dataType = "type"
case format
Expand All @@ -331,3 +331,15 @@ extension Schema: Encodable {
case requiredProperties = "required"
}
}

// MARK: - Equatable Conformance

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
extension Schema: Equatable {
public static func == (lhs: Schema, rhs: Schema) -> Bool {
return lhs.dataType == rhs.dataType && lhs.format == rhs.format
&& lhs.description == rhs.description && lhs.nullable == rhs.nullable
&& lhs.enumValues == rhs.enumValues && lhs.items == rhs.items
&& lhs.properties == rhs.properties && lhs.requiredProperties == rhs.requiredProperties
}
}
48 changes: 48 additions & 0 deletions FirebaseVertexAI/Tests/Unit/Types/FunctionDeclarationTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import FirebaseVertexAI
import XCTest

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
final class FunctionDeclarationTests: XCTestCase {
func testFunctionDeclaration_jsonString() throws {
let json = """
{
"name": "getWeather",
"description": "gets the weather for a requested city",
"parameters": {
"type": "OBJECT",
"properties": {
"city": {
"type": "STRING",
"nullable": false
}
},
"nullable": false,
"required": ["city"]
}
}
"""
let expectedFunctionDeclaration = FunctionDeclaration(
name: "getWeather",
description: "gets the weather for a requested city",
parameters: ["city": .string()]
)

let functionDeclaration = try FunctionDeclaration(jsonString: json)

XCTAssertEqual(functionDeclaration, expectedFunctionDeclaration)
}
}
Loading