Skip to content

Commit 5482c7f

Browse files
committed
add modules
1 parent bc19746 commit 5482c7f

File tree

9 files changed

+174
-10
lines changed

9 files changed

+174
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ DerivedData/
66
.swiftpm/configuration/registries.json
77
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
88
.netrc
9+
Package.resolved

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2024 Fumito Ito
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package.swift

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,33 @@
44
import PackageDescription
55

66
let package = Package(
7-
name: "FunctionCalling-generative-ai-swift",
7+
name: "FunctionCalling-GoogleGenerativeAI",
8+
platforms: [
9+
.macOS(.v13),
10+
.iOS(.v17)
11+
],
812
products: [
913
// Products define the executables and libraries a package produces, making them visible to other packages.
1014
.library(
11-
name: "FunctionCalling-generative-ai-swift",
12-
targets: ["FunctionCalling-generative-ai-swift"]),
15+
name: "FunctionCalling-GoogleGenerativeAI",
16+
targets: ["FunctionCalling-GoogleGenerativeAI"]),
17+
],
18+
dependencies: [
19+
.package(url: "https://github.com/google-gemini/generative-ai-swift", from: "0.5.6"),
20+
.package(url: "https://github.com/fumito-ito/FunctionCalling", from: "0.3.0"),
1321
],
1422
targets: [
1523
// Targets are the basic building blocks of a package, defining a module or a test suite.
1624
// Targets can depend on other targets in this package and products from dependencies.
1725
.target(
18-
name: "FunctionCalling-generative-ai-swift"),
26+
name: "FunctionCalling-GoogleGenerativeAI",
27+
dependencies: [
28+
.product(name: "GoogleGenerativeAI", package: "generative-ai-swift"),
29+
.product(name: "FunctionCalling", package: "FunctionCalling"),
30+
]
31+
),
1932
.testTarget(
20-
name: "FunctionCalling-generative-ai-swiftTests",
21-
dependencies: ["FunctionCalling-generative-ai-swift"]),
33+
name: "FunctionCalling-GoogleGenerativeAITests",
34+
dependencies: ["FunctionCalling-GoogleGenerativeAI"]),
2235
]
2336
)

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# FunctionCalling-GoogleGenerativeAI
2+
3+
This library simplifies the integration of the [FunctionCalling](https://github.com/fumito-ito/FunctionCalling) macro into [GoogleGenerativeAI](https://github.com/google-gemini/generative-ai-swift). By using this library, you can directly generate `Tool` objects from Swift native functions, which can then be specified as FunctionCalling when invoking VertexAI.
4+
5+
## Usage
6+
7+
```swift
8+
9+
import FunctionCalling
10+
import FunctionCalling_GoogleGenerativeAI
11+
import GoogleGenerativeAI
12+
13+
// MARK: Declare the container and functions for the tools to be called from FunctionCalling.
14+
15+
@FunctionCalling(service: .llamaOrGemini)
16+
struct MyFunctionTools {
17+
@CallableFunction
18+
/// Get the current stock price for a given ticker symbol
19+
///
20+
/// - Parameter: The stock ticker symbol, e.g. GOOG for Google Inc.
21+
func getStockPrice(ticker: String) async throws -> String {
22+
// code to return stock price of passed ticker
23+
}
24+
}
25+
26+
// MARK: You can directly pass the tools generated from objects to the model in VertexAI.
27+
28+
let model = GenerativeModel(
29+
modelName: "gemini-1.5-flash",
30+
tools: MyFunctionTools().googleGenerativeAITools
31+
)
32+
```
33+
34+
## Installation
35+
36+
### Swift Package Manager
37+
38+
```
39+
let package = Package(
40+
name: "MyPackage",
41+
products: [...],
42+
targets: [
43+
.target(
44+
"YouAppModule",
45+
dependencies: [
46+
.product(name: "FunctionCalling-GoogleGenerativeAI", package: "FunctionCalling-GoogleGenerativeAI")
47+
]
48+
)
49+
],
50+
dependencies: [
51+
.package(url: "https://github.com/fumito-ito/FunctionCalling-GoogleGenerativeAI", from: "0.0.1")
52+
]
53+
)
54+
```
55+
56+
## Contributing
57+
58+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.Please make sure to update tests as appropriate.
59+
60+
## License
61+
62+
The MIT License
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// InputSchema+Extension.swift
3+
//
4+
//
5+
// Created by 伊藤史 on 2024/09/13.
6+
//
7+
8+
import FunctionCalling
9+
import GoogleGenerativeAI
10+
11+
extension FunctionCalling.InputSchema {
12+
var toSchema: GoogleGenerativeAI.Schema {
13+
Schema(
14+
type: type.toDataType,
15+
format: format,
16+
description: description,
17+
nullable: nullable,
18+
enumValues: enumValues,
19+
items: items?.toSchema,
20+
properties: properties?.mapValues { $0.toSchema },
21+
requiredProperties: requiredProperties
22+
)
23+
}
24+
}
25+
26+
extension FunctionCalling.InputSchema.DataType {
27+
var toDataType: GoogleGenerativeAI.DataType {
28+
switch self {
29+
case .string:
30+
return .string
31+
case .number:
32+
return .number
33+
case .integer:
34+
return .integer
35+
case .boolean:
36+
return .boolean
37+
case .array:
38+
return .array
39+
case .object:
40+
return .object
41+
}
42+
}
43+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// Tool+Extension.swift
3+
//
4+
//
5+
// Created by 伊藤史 on 2024/09/13.
6+
//
7+
8+
import FunctionCalling
9+
import GoogleGenerativeAI
10+
11+
extension FunctionCalling.Tool {
12+
var toFunctionDeclaration: GoogleGenerativeAI.FunctionDeclaration {
13+
FunctionDeclaration(
14+
name: name,
15+
description: description,
16+
parameters: inputSchema.properties?.mapValues { $0.toSchema },
17+
requiredParameters: inputSchema.requiredProperties
18+
)
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// The Swift Programming Language
2+
// https://docs.swift.org/swift-book
3+
4+
import FunctionCalling
5+
import GoogleGenerativeAI
6+
import Foundation
7+
8+
typealias FunctionCallingTool = FunctionCalling.Tool
9+
10+
extension ToolContainer {
11+
public var googleGenerativeAITools: [GoogleGenerativeAI.Tool] {
12+
get throws {
13+
let data = allTools.data(using: .utf8)!
14+
let functionCallingTools = try JSONDecoder().decode([FunctionCallingTool].self, from: data)
15+
let googleGenerativeAITools = functionCallingTools.map { $0.toFunctionDeclaration }
16+
17+
return [GoogleGenerativeAI.Tool(functionDeclarations: googleGenerativeAITools)]
18+
}
19+
}
20+
}

Sources/FunctionCalling-generative-ai-swift/FunctionCalling_generative_ai_swift.swift

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import XCTest
2-
@testable import FunctionCalling_generative_ai_swift
2+
@testable import FunctionCalling_GoogleGenerativeAI
33

4-
final class FunctionCalling_generative_ai_swiftTests: XCTestCase {
4+
final class FunctionCalling_GoogleGenerativeAITests: XCTestCase {
55
func testExample() throws {
66
// XCTest Documentation
77
// https://developer.apple.com/documentation/xctest

0 commit comments

Comments
 (0)