-
Notifications
You must be signed in to change notification settings - Fork 174
Add function calling support #116
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5d8b31c
Add types to represent JSON values (#112)
andrewheard 45bc200
Add `Encodable` conformance to `JSONValue` (#113)
andrewheard 667eccf
Add `FunctionCall` decoding (#114)
andrewheard d1e97b6
Function calling prototype
andrewheard 3d0642d
Add prototype for function calling
andrewheard 44edbb1
Use static exchange rate data
andrewheard ae60f57
Simplify top-level schema declaration in `FunctionDeclaration` (#117)
andrewheard 8ddd10c
Add docs to function calling types
andrewheard a9a17cb
Fix formatting
andrewheard c75a498
Revert CLI tool changes
andrewheard d082819
Add doc to `functionResponse` case
andrewheard cd43d8d
Add `tools` documentation in `GenerativeModel`
andrewheard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// 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 Foundation | ||
|
||
/// A predicted function call returned from the model. | ||
/// | ||
/// REST Docs: https://ai.google.dev/api/rest/v1beta/Content#functioncall | ||
public struct FunctionCall: Equatable, Encodable { | ||
/// The name of the function to call. | ||
public let name: String | ||
|
||
/// The function parameters and values. | ||
public let args: JSONObject | ||
} | ||
|
||
// REST Docs: https://ai.google.dev/api/rest/v1beta/Tool#schema | ||
public class Schema: Encodable { | ||
andrewheard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let type: DataType | ||
|
||
let format: String? | ||
|
||
let description: String? | ||
|
||
let nullable: Bool? | ||
|
||
let enumValues: [String]? | ||
|
||
let items: Schema? | ||
|
||
let properties: [String: Schema]? | ||
|
||
let required: [String]? | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case type | ||
case format | ||
case description | ||
case nullable | ||
case enumValues = "enum" | ||
case items | ||
case properties | ||
case required | ||
} | ||
|
||
public init(type: DataType, format: String? = nil, description: String? = nil, | ||
nullable: Bool? = nil, | ||
enumValues: [String]? = nil, items: Schema? = nil, | ||
properties: [String: Schema]? = nil, | ||
required: [String]? = nil) { | ||
self.type = type | ||
self.format = format | ||
self.description = description | ||
self.nullable = nullable | ||
self.enumValues = enumValues | ||
self.items = items | ||
self.properties = properties | ||
self.required = required | ||
} | ||
} | ||
|
||
// REST Docs: https://ai.google.dev/api/rest/v1beta/Tool#Type | ||
public enum DataType: String, Encodable { | ||
case string = "STRING" | ||
case number = "NUMBER" | ||
case integer = "INTEGER" | ||
case boolean = "BOOLEAN" | ||
case array = "ARRAY" | ||
case object = "OBJECT" | ||
} | ||
|
||
// REST Docs: https://ai.google.dev/api/rest/v1beta/Tool#FunctionDeclaration | ||
public struct FunctionDeclaration { | ||
let name: String | ||
|
||
let description: String | ||
|
||
let parameters: Schema? | ||
|
||
public init(name: String, description: String, parameters: Schema?) { | ||
self.name = name | ||
self.description = description | ||
self.parameters = parameters | ||
} | ||
} | ||
|
||
// REST Docs: https://ai.google.dev/api/rest/v1beta/Tool | ||
public struct Tool: Encodable { | ||
let functionDeclarations: [FunctionDeclaration]? | ||
|
||
public init(functionDeclarations: [FunctionDeclaration]?) { | ||
self.functionDeclarations = functionDeclarations | ||
} | ||
} | ||
|
||
// REST Docs: https://ai.google.dev/api/rest/v1beta/Content#functionresponse | ||
public struct FunctionResponse: Equatable, Encodable { | ||
let name: String | ||
|
||
let response: JSONObject | ||
|
||
public init(name: String, response: JSONObject) { | ||
self.name = name | ||
self.response = response | ||
} | ||
} | ||
|
||
// MARK: - Codable Conformance | ||
|
||
extension FunctionCall: Decodable { | ||
enum CodingKeys: CodingKey { | ||
case name | ||
case args | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
name = try container.decode(String.self, forKey: .name) | ||
if let args = try container.decodeIfPresent(JSONObject.self, forKey: .args) { | ||
self.args = args | ||
} else { | ||
args = JSONObject() | ||
} | ||
} | ||
} | ||
|
||
extension FunctionDeclaration: Encodable { | ||
enum CodingKeys: String, CodingKey { | ||
case name | ||
case description | ||
case parameters | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(name, forKey: .name) | ||
try container.encode(description, forKey: .description) | ||
try container.encode(parameters, forKey: .parameters) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.