diff --git a/Package.swift b/Package.swift index 951e403..c7b72d9 100644 --- a/Package.swift +++ b/Package.swift @@ -17,7 +17,7 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/google-gemini/generative-ai-swift", from: "0.5.6"), - .package(url: "https://github.com/fumito-ito/FunctionCalling", from: "0.3.0"), + .package(url: "https://github.com/fumito-ito/FunctionCalling", from: "0.4.0"), ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. diff --git a/README.md b/README.md index 766c2be..2df8c9b 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ let package = Package( ) ], dependencies: [ - .package(url: "https://github.com/FunctionCalling/FunctionCalling-GoogleGenerativeAI", from: "0.0.1") + .package(url: "https://github.com/FunctionCalling/FunctionCalling-GoogleGenerativeAI", from: "0.1.0") ] ) ``` diff --git a/Sources/FunctionCalling-GoogleGenerativeAI/FunctionCalling_GoogleGenerativeAI.swift b/Sources/FunctionCalling-GoogleGenerativeAI/FunctionCalling_GoogleGenerativeAI.swift index 900d24d..622b4d1 100644 --- a/Sources/FunctionCalling-GoogleGenerativeAI/FunctionCalling_GoogleGenerativeAI.swift +++ b/Sources/FunctionCalling-GoogleGenerativeAI/FunctionCalling_GoogleGenerativeAI.swift @@ -9,12 +9,10 @@ typealias FunctionCallingTool = FunctionCalling.Tool extension ToolContainer { public var googleGenerativeAITools: [GoogleGenerativeAI.Tool] { - get throws { - let data = allTools.data(using: .utf8)! - let functionCallingTools = try JSONDecoder().decode([FunctionCallingTool].self, from: data) - let googleGenerativeAITools = functionCallingTools.map { $0.toFunctionDeclaration } - - return [GoogleGenerativeAI.Tool(functionDeclarations: googleGenerativeAITools)] - } + [ + GoogleGenerativeAI.Tool( + functionDeclarations: allTools?.compactMap { $0.toFunctionDeclaration } + ) + ] } } diff --git a/Tests/FunctionCalling-GoogleGenerativeAITests/Extensions.swift b/Tests/FunctionCalling-GoogleGenerativeAITests/Extensions.swift new file mode 100644 index 0000000..d32a38b --- /dev/null +++ b/Tests/FunctionCalling-GoogleGenerativeAITests/Extensions.swift @@ -0,0 +1,24 @@ +// +// Extensions.swift +// FunctionCalling-GoogleGenerativeAI +// +// Created by 伊藤史 on 2024/09/20. +// + +@testable import GoogleGenerativeAI + +extension GoogleGenerativeAI.Tool { + var functions: [FunctionDeclaration]? { + self.functionDeclarations + } +} + +extension GoogleGenerativeAI.FunctionDeclaration { + func getName() -> String { + self.name + } + + func getDescription() -> String? { + self.description + } +} diff --git a/Tests/FunctionCalling-GoogleGenerativeAITests/FunctionCalling_ GoogleGenerativeAITests.swift b/Tests/FunctionCalling-GoogleGenerativeAITests/FunctionCalling_ GoogleGenerativeAITests.swift index 6a4ccd6..f01bd2c 100644 --- a/Tests/FunctionCalling-GoogleGenerativeAITests/FunctionCalling_ GoogleGenerativeAITests.swift +++ b/Tests/FunctionCalling-GoogleGenerativeAITests/FunctionCalling_ GoogleGenerativeAITests.swift @@ -1,12 +1,38 @@ import XCTest @testable import FunctionCalling_GoogleGenerativeAI +import FunctionCalling final class FunctionCalling_GoogleGenerativeAITests: XCTestCase { - func testExample() throws { - // XCTest Documentation - // https://developer.apple.com/documentation/xctest + @FunctionCalling(service: .claude) + struct FunctionContainer { + /// Return current weather of location that passed by the argument + /// - Parameter location: location that I want to know how the weather + /// - Returns: string of weather + @CallableFunction + func getWeather(location: String) -> String { + return "Sunny" + } - // Defining Test Cases and Test Methods - // https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods + @CallableFunction + func getStock(args: String) -> Int { + return 0 + } + } + + func testConvertedResults() throws { + guard let functions = FunctionContainer().googleGenerativeAITools.first?.functions else { + XCTFail("Conainer should contain some functions") + return + } + + XCTAssertEqual(functions.count, 2) + + let getWeather = try XCTUnwrap(functions.first) + XCTAssertEqual(getWeather.getName(), "getWeather") + XCTAssertEqual(getWeather.getDescription(), "Return current weather of location that passed by the argument- Parameter location: location that I want to know how the weather- Returns: string of weather") + + let getStock = try XCTUnwrap(functions.last) + XCTAssertEqual(getStock.getName(), "getStock") + XCTAssertEqual(getStock.getDescription(), "") } }