Skip to content

Commit 016b555

Browse files
Forced function calling (#124)
Co-authored-by: Andrew Heard <[email protected]>
1 parent 8565a55 commit 016b555

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

Examples/GenerativeAISample/GenerativeAIUIComponents/Sources/GenerativeAIUIComponents/InputField.swift

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
import SwiftUI
16+
1617
public struct InputField<Label>: View where Label: View {
1718
@Binding
1819
private var text: String

Mintfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nicklockwood/SwiftFormat@0.52.10
1+
nicklockwood/SwiftFormat@0.53.5

Sources/GoogleAI/FunctionCalling.swift

+45
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,51 @@ public struct Tool: Encodable {
177177
}
178178
}
179179

180+
/// Configuration for specifying function calling behavior.
181+
public struct FunctionCallingConfig: Encodable {
182+
/// Defines the execution behavior for function calling by defining the
183+
/// execution mode.
184+
public enum Mode: String, Encodable {
185+
/// The default behavior for function calling. The model calls functions to answer queries at
186+
/// its discretion.
187+
case auto = "AUTO"
188+
189+
/// The model always predicts a provided function call to answer every query.
190+
case any = "ANY"
191+
192+
/// The model will never predict a function call to answer a query. This can also be achieved by
193+
/// not passing any tools to the model.
194+
case none = "NONE"
195+
}
196+
197+
/// Specifies the mode in which function calling should execute. If
198+
/// unspecified, the default value will be set to AUTO.
199+
let mode: Mode?
200+
201+
/// A set of function names that, when provided, limits the functions the model
202+
/// will call.
203+
///
204+
/// This should only be set when the Mode is ANY. Function names
205+
/// should match [FunctionDeclaration.name]. With mode set to ANY, model will
206+
/// predict a function call from the set of function names provided.
207+
let allowedFunctionNames: [String]?
208+
209+
public init(mode: FunctionCallingConfig.Mode? = nil, allowedFunctionNames: [String]? = nil) {
210+
self.mode = mode
211+
self.allowedFunctionNames = allowedFunctionNames
212+
}
213+
}
214+
215+
/// Tool configuration for any `Tool` specified in the request.
216+
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
217+
public struct ToolConfig: Encodable {
218+
let functionCallingConfig: FunctionCallingConfig?
219+
220+
public init(functionCallingConfig: FunctionCallingConfig? = nil) {
221+
self.functionCallingConfig = functionCallingConfig
222+
}
223+
}
224+
180225
/// Result output from a ``FunctionCall``.
181226
///
182227
/// Contains a string representing the `FunctionDeclaration.name` and a structured JSON object

Sources/GoogleAI/GenerateContentRequest.swift

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct GenerateContentRequest {
2222
let generationConfig: GenerationConfig?
2323
let safetySettings: [SafetySetting]?
2424
let tools: [Tool]?
25+
let toolConfig: ToolConfig?
2526
let isStreaming: Bool
2627
let options: RequestOptions
2728
}
@@ -33,6 +34,7 @@ extension GenerateContentRequest: Encodable {
3334
case generationConfig
3435
case safetySettings
3536
case tools
37+
case toolConfig
3638
}
3739
}
3840

Sources/GoogleAI/GenerativeModel.swift

+10
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public final class GenerativeModel {
3636
/// A list of tools the model may use to generate the next response.
3737
let tools: [Tool]?
3838

39+
/// Tool configuration for any `Tool` specified in the request.
40+
let toolConfig: ToolConfig?
41+
3942
/// Configuration parameters for sending requests to the backend.
4043
let requestOptions: RequestOptions
4144

@@ -48,19 +51,22 @@ public final class GenerativeModel {
4851
/// - generationConfig: The content generation parameters your model should use.
4952
/// - safetySettings: A value describing what types of harmful content your model should allow.
5053
/// - tools: A list of ``Tool`` objects that the model may use to generate the next response.
54+
/// - toolConfig: Tool configuration for any `Tool` specified in the request.
5155
/// - requestOptions Configuration parameters for sending requests to the backend.
5256
public convenience init(name: String,
5357
apiKey: String,
5458
generationConfig: GenerationConfig? = nil,
5559
safetySettings: [SafetySetting]? = nil,
5660
tools: [Tool]? = nil,
61+
toolConfig: ToolConfig? = nil,
5762
requestOptions: RequestOptions = RequestOptions()) {
5863
self.init(
5964
name: name,
6065
apiKey: apiKey,
6166
generationConfig: generationConfig,
6267
safetySettings: safetySettings,
6368
tools: tools,
69+
toolConfig: toolConfig,
6470
requestOptions: requestOptions,
6571
urlSession: .shared
6672
)
@@ -72,13 +78,15 @@ public final class GenerativeModel {
7278
generationConfig: GenerationConfig? = nil,
7379
safetySettings: [SafetySetting]? = nil,
7480
tools: [Tool]? = nil,
81+
toolConfig: ToolConfig? = nil,
7582
requestOptions: RequestOptions = RequestOptions(),
7683
urlSession: URLSession) {
7784
modelResourceName = GenerativeModel.modelResourceName(name: name)
7885
generativeAIService = GenerativeAIService(apiKey: apiKey, urlSession: urlSession)
7986
self.generationConfig = generationConfig
8087
self.safetySettings = safetySettings
8188
self.tools = tools
89+
self.toolConfig = toolConfig
8290
self.requestOptions = requestOptions
8391

8492
Logging.default.info("""
@@ -125,6 +133,7 @@ public final class GenerativeModel {
125133
generationConfig: generationConfig,
126134
safetySettings: safetySettings,
127135
tools: tools,
136+
toolConfig: toolConfig,
128137
isStreaming: false,
129138
options: requestOptions)
130139
response = try await generativeAIService.loadRequest(request: generateContentRequest)
@@ -197,6 +206,7 @@ public final class GenerativeModel {
197206
generationConfig: generationConfig,
198207
safetySettings: safetySettings,
199208
tools: tools,
209+
toolConfig: toolConfig,
200210
isStreaming: true,
201211
options: requestOptions)
202212

0 commit comments

Comments
 (0)