-
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 all 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
// 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. | ||
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 | ||
} | ||
|
||
/// A `Schema` object allows the definition of input and output data types. | ||
/// | ||
/// 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). | ||
public class Schema: Encodable { | ||
/// The data type. | ||
let type: DataType | ||
|
||
/// The format of the data. | ||
let format: String? | ||
|
||
/// A brief description of the parameter. | ||
let description: String? | ||
|
||
/// Indicates if the value may be null. | ||
let nullable: Bool? | ||
|
||
/// Possible values of the element of type ``DataType/string`` with "enum" format. | ||
let enumValues: [String]? | ||
|
||
/// Schema of the elements of type ``DataType/array``. | ||
let items: Schema? | ||
|
||
/// Properties of type ``DataType/object``. | ||
let properties: [String: Schema]? | ||
|
||
/// Required properties of type ``DataType/object``. | ||
let requiredProperties: [String]? | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case type | ||
case format | ||
case description | ||
case nullable | ||
case enumValues = "enum" | ||
case items | ||
case properties | ||
case requiredProperties = "required" | ||
} | ||
|
||
/// Constructs a new `Schema`. | ||
/// | ||
/// - Parameters: | ||
/// - type: The data type. | ||
/// - format: The format of the data; used only for primitive datatypes. | ||
/// Supported formats: | ||
/// - ``DataType/integer``: int32, int64 | ||
andrewheard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// - ``DataType/number``: float, double | ||
/// - ``DataType/string``: enum | ||
/// - description: A brief description of the parameter; may be formatted as Markdown. | ||
/// - nullable: Indicates if the value may be null. | ||
/// - enumValues: Possible values of the element of type ``DataType/string`` with "enum" format. | ||
/// For example, an enum `Direction` may be defined as `["EAST", NORTH", "SOUTH", "WEST"]`. | ||
/// - items: Schema of the elements of type ``DataType/array``. | ||
/// - properties: Properties of type ``DataType/object``. | ||
/// - requiredProperties: Required properties of type ``DataType/object``. | ||
public init(type: DataType, format: String? = nil, description: String? = nil, | ||
nullable: Bool? = nil, | ||
enumValues: [String]? = nil, items: Schema? = nil, | ||
properties: [String: Schema]? = nil, | ||
requiredProperties: [String]? = nil) { | ||
self.type = type | ||
self.format = format | ||
self.description = description | ||
self.nullable = nullable | ||
self.enumValues = enumValues | ||
self.items = items | ||
self.properties = properties | ||
self.requiredProperties = requiredProperties | ||
} | ||
} | ||
|
||
/// A data type. | ||
/// | ||
/// Contains the set of OpenAPI [data types](https://spec.openapis.org/oas/v3.0.3#data-types). | ||
public enum DataType: String, Encodable { | ||
/// A `String` type. | ||
case string = "STRING" | ||
|
||
/// A floating-point number type. | ||
case number = "NUMBER" | ||
|
||
/// An integer type. | ||
case integer = "INTEGER" | ||
|
||
/// A boolean type. | ||
case boolean = "BOOLEAN" | ||
|
||
/// An array type. | ||
case array = "ARRAY" | ||
|
||
/// An object type. | ||
case object = "OBJECT" | ||
} | ||
|
||
/// Structured representation of a function declaration. | ||
/// | ||
/// 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. | ||
public struct FunctionDeclaration { | ||
/// The name of the function. | ||
let name: String | ||
|
||
/// A brief description of the function. | ||
let description: String | ||
|
||
/// Describes the parameters to this function; must be of type ``DataType/object``. | ||
let parameters: Schema? | ||
|
||
/// Constructs a new `FunctionDeclaration`. | ||
/// | ||
/// - Parameters: | ||
/// - name: The name of the function; must be a-z, A-Z, 0-9, or contain underscores and dashes, | ||
/// with a maximum length of 63. | ||
/// - description: A brief description of the function. | ||
/// - parameters: Describes the parameters to this function; the keys are parameter names and | ||
/// the values are ``Schema`` objects describing them. | ||
/// - requiredParameters: A list of required parameters by name. | ||
public init(name: String, description: String, parameters: [String: Schema]?, | ||
requiredParameters: [String]?) { | ||
self.name = name | ||
self.description = description | ||
self.parameters = Schema( | ||
type: .object, | ||
properties: parameters, | ||
paulb777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
requiredProperties: requiredParameters | ||
) | ||
} | ||
} | ||
|
||
/// Helper tools that the model may use to generate response. | ||
/// | ||
/// A `Tool` is a piece of code that enables the system to interact with external systems to | ||
/// perform an action, or set of actions, outside of knowledge and scope of the model. | ||
public struct Tool: Encodable { | ||
/// A list of `FunctionDeclarations` available to the model. | ||
let functionDeclarations: [FunctionDeclaration]? | ||
|
||
/// Constructs a new `Tool`. | ||
/// | ||
/// - Parameters: | ||
/// - functionDeclarations: A list of `FunctionDeclarations` available to the model that can be | ||
/// used for function calling. | ||
/// The model or system does not execute the function. Instead the defined function may be | ||
/// returned as a ``FunctionCall`` in ``ModelContent/Part/functionCall(_:)`` with arguments to | ||
/// the client side for execution. The model may decide to call a subset of these functions by | ||
/// populating ``FunctionCall`` in the response. The next conversation turn may contain a | ||
/// ``FunctionResponse`` in ``ModelContent/Part/functionResponse(_:)`` with the | ||
/// ``ModelContent/role`` "function", providing generation context for the next model turn. | ||
public init(functionDeclarations: [FunctionDeclaration]?) { | ||
self.functionDeclarations = functionDeclarations | ||
} | ||
} | ||
|
||
/// Result output from a ``FunctionCall``. | ||
/// | ||
/// Contains a string representing the `FunctionDeclaration.name` and a structured JSON object | ||
/// containing any output from the function is used as context to the model. This should contain the | ||
/// result of a ``FunctionCall`` made based on model prediction. | ||
public struct FunctionResponse: Equatable, Encodable { | ||
/// The name of the function that was called. | ||
let name: String | ||
|
||
/// The function's response. | ||
let response: JSONObject | ||
|
||
/// Constructs a new `FunctionResponse`. | ||
/// | ||
/// - Parameters: | ||
/// - name: The name of the function that was called. | ||
/// - response: The function's response. | ||
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
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.