Skip to content

Commit 3493ffb

Browse files
andrewheardpaulb777
andcommitted
Add forced function calling support in Vertex AI (#12748)
Co-authored-by: Paul Beusterien <[email protected]>
1 parent 7cb7f77 commit 3493ffb

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

FirebaseVertexAI/Sources/FunctionCalling.swift

Lines changed: 45 additions & 0 deletions
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

FirebaseVertexAI/Sources/GenerateContentRequest.swift

Lines changed: 2 additions & 0 deletions
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

FirebaseVertexAI/Sources/GenerativeModel.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public final class GenerativeModel {
3737
/// A list of tools the model may use to generate the next response.
3838
let tools: [Tool]?
3939

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

@@ -49,13 +52,15 @@ public final class GenerativeModel {
4952
/// - generationConfig: The content generation parameters your model should use.
5053
/// - safetySettings: A value describing what types of harmful content your model should allow.
5154
/// - tools: A list of ``Tool`` objects that the model may use to generate the next response.
55+
/// - toolConfig: Tool configuration for any `Tool` specified in the request.
5256
/// - requestOptions: Configuration parameters for sending requests to the backend.
5357
/// - urlSession: The `URLSession` to use for requests; defaults to `URLSession.shared`.
5458
init(name: String,
5559
apiKey: String,
5660
generationConfig: GenerationConfig? = nil,
5761
safetySettings: [SafetySetting]? = nil,
5862
tools: [Tool]?,
63+
toolConfig: ToolConfig? = nil,
5964
requestOptions: RequestOptions,
6065
appCheck: AppCheckInterop?,
6166
urlSession: URLSession = .shared) {
@@ -68,6 +73,7 @@ public final class GenerativeModel {
6873
self.generationConfig = generationConfig
6974
self.safetySettings = safetySettings
7075
self.tools = tools
76+
self.toolConfig = toolConfig
7177
self.requestOptions = requestOptions
7278

7379
Logging.default.info("""
@@ -114,6 +120,7 @@ public final class GenerativeModel {
114120
generationConfig: generationConfig,
115121
safetySettings: safetySettings,
116122
tools: tools,
123+
toolConfig: toolConfig,
117124
isStreaming: false,
118125
options: requestOptions)
119126
response = try await generativeAIService.loadRequest(request: generateContentRequest)
@@ -186,6 +193,7 @@ public final class GenerativeModel {
186193
generationConfig: generationConfig,
187194
safetySettings: safetySettings,
188195
tools: tools,
196+
toolConfig: toolConfig,
189197
isStreaming: true,
190198
options: requestOptions)
191199

FirebaseVertexAI/Sources/VertexAI.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ public class VertexAI: NSObject {
5858
/// - generationConfig: The content generation parameters your model should use.
5959
/// - safetySettings: A value describing what types of harmful content your model should allow.
6060
/// - tools: A list of ``Tool`` objects that the model may use to generate the next response.
61+
/// - toolConfig: Tool configuration for any `Tool` specified in the request.
6162
/// - requestOptions: Configuration parameters for sending requests to the backend.
6263
public func generativeModel(modelName: String,
6364
generationConfig: GenerationConfig? = nil,
6465
safetySettings: [SafetySetting]? = nil,
6566
tools: [Tool]? = nil,
67+
toolConfig: ToolConfig? = nil,
6668
requestOptions: RequestOptions = RequestOptions())
6769
-> GenerativeModel {
6870
let modelResourceName = modelResourceName(modelName: modelName, location: location)
@@ -77,6 +79,7 @@ public class VertexAI: NSObject {
7779
generationConfig: generationConfig,
7880
safetySettings: safetySettings,
7981
tools: tools,
82+
toolConfig: toolConfig,
8083
requestOptions: requestOptions,
8184
appCheck: appCheck
8285
)

0 commit comments

Comments
 (0)