mirror of
https://github.com/firebase/firebase-ios-sdk
synced 2026-11-11 22:55:59 +05:45
Revert commit dd2cfa8df1 (#16685) to undo
the renaming from Gemini Enterprise Agent Platform to Gemini Enterprise
API.
- Restore Backend.agentPlatform(location:) as the primary public API
and remove Backend.enterprise(location:).
- Revert APIConfig.Service.enterprise to .agentPlatform and restore
agentPlatformStagingBypassProxy.
- Revert documentation comments, DocC topics, and guides back to
Agent Platform Gemini API and Gemini Enterprise Agent Platform.
- Clean up leftover Vertex AI symbols to align with Agent Platform and
FirebaseAILogic (isFirebaseAILogicServiceDisabledError and
defaultAgentPlatformAPIConfig).
- Re-align tests and conflict resolutions from #16689, #16693, #16676,
and #16756.
302 lines
12 KiB
Swift
302 lines
12 KiB
Swift
// 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 CoreLocation
|
|
import Foundation
|
|
|
|
/// Structured representation of a function declaration.
|
|
///
|
|
/// This `FunctionDeclaration` is a representation of a block of code that can be used as a
|
|
/// ``GenerativeModel/Tool`` by the model and executed by the client.
|
|
public struct FunctionDeclaration: Sendable {
|
|
/// 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.
|
|
/// - optionalParameters: The names of parameters that may be omitted by the model in function
|
|
/// calls; by default, all parameters are considered required.
|
|
public init(name: String, description: String, parameters: [String: Schema],
|
|
optionalParameters: [String] = []) {
|
|
self.name = name
|
|
self.description = description
|
|
self.parameters = Schema.object(
|
|
properties: parameters,
|
|
optionalProperties: optionalParameters,
|
|
nullable: false
|
|
)
|
|
}
|
|
}
|
|
|
|
/// A tool that allows the generative model to connect to Google Search to access and incorporate
|
|
/// up-to-date information from the web into its responses.
|
|
///
|
|
/// > Important: When using this feature, you are required to comply with the
|
|
/// "Grounding with Google Search" usage requirements for your chosen API provider:
|
|
/// [Gemini Developer API](https://ai.google.dev/gemini-api/terms#grounding-with-google-search)
|
|
/// or the Agent Platform Gemini API (see
|
|
/// [Service Terms](https://cloud.google.com/terms/service-terms)
|
|
/// section within the Service Specific Terms).
|
|
public struct GoogleSearch: Sendable {
|
|
public init() {}
|
|
}
|
|
|
|
public extension GenerativeModel {
|
|
/// A helper tool that the model may use when generating responses.
|
|
///
|
|
/// 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.
|
|
struct Tool: Sendable {
|
|
/// A list of `FunctionDeclarations` available to the model.
|
|
let functionDeclarations: [FunctionDeclaration]?
|
|
|
|
/// Specifies the Google Search configuration.
|
|
let googleSearch: GoogleSearch?
|
|
|
|
/// Specifies the Google Maps configuration.
|
|
let googleMaps: GoogleMaps?
|
|
|
|
let codeExecution: CodeExecution?
|
|
let urlContext: URLContext?
|
|
|
|
init(functionDeclarations: [FunctionDeclaration]? = nil,
|
|
googleSearch: GoogleSearch? = nil,
|
|
googleMaps: GoogleMaps? = nil,
|
|
urlContext: URLContext? = nil,
|
|
codeExecution: CodeExecution? = nil) {
|
|
self.functionDeclarations = functionDeclarations
|
|
self.googleSearch = googleSearch
|
|
self.googleMaps = googleMaps
|
|
self.urlContext = urlContext
|
|
self.codeExecution = codeExecution
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Configuration for specifying function calling behavior.
|
|
public struct FunctionCallingConfig: Sendable {
|
|
/// Defines the execution behavior for function calling by defining the execution mode.
|
|
enum Mode: String {
|
|
case auto = "AUTO"
|
|
case any = "ANY"
|
|
case none = "NONE"
|
|
}
|
|
|
|
/// Specifies the mode in which function calling should execute.
|
|
let mode: Mode?
|
|
|
|
/// A set of function names that, when provided, limits the functions the model will call.
|
|
let allowedFunctionNames: [String]?
|
|
|
|
init(mode: FunctionCallingConfig.Mode? = nil, allowedFunctionNames: [String]? = nil) {
|
|
self.mode = mode
|
|
self.allowedFunctionNames = allowedFunctionNames
|
|
}
|
|
|
|
/// Creates a function calling config where the model calls functions at its discretion.
|
|
///
|
|
/// > Note: This is the default behavior.
|
|
public static func auto() -> FunctionCallingConfig {
|
|
return FunctionCallingConfig(mode: .auto)
|
|
}
|
|
|
|
/// Creates a function calling config where the model will always call a provided function.
|
|
///
|
|
/// - Parameters:
|
|
/// - allowedFunctionNames: A set of function names that, when provided, limits the functions
|
|
/// that the model will call.
|
|
public static func any(allowedFunctionNames: [String]? = nil) -> FunctionCallingConfig {
|
|
return FunctionCallingConfig(mode: .any, allowedFunctionNames: allowedFunctionNames)
|
|
}
|
|
|
|
/// Creates a function calling config where the model will never call a function.
|
|
///
|
|
/// > Note: This can also be achieved by not passing any ``FunctionDeclaration`` tools when
|
|
/// > instantiating the model.
|
|
public static func none() -> FunctionCallingConfig {
|
|
return FunctionCallingConfig(mode: FunctionCallingConfig.Mode.none)
|
|
}
|
|
}
|
|
|
|
/// Tool configuration for any ``GenerativeModel/Tool`` specified in the request.
|
|
public struct ToolConfig: Sendable {
|
|
let functionCallingConfig: FunctionCallingConfig?
|
|
let retrievalConfig: RetrievalConfig?
|
|
|
|
public init(functionCallingConfig: FunctionCallingConfig? = nil,
|
|
retrievalConfig: RetrievalConfig? = nil) {
|
|
self.functionCallingConfig = functionCallingConfig
|
|
self.retrievalConfig = retrievalConfig
|
|
}
|
|
}
|
|
|
|
/// Configuration for grounding retrieval tools, such as Grounding with Google Maps.
|
|
///
|
|
/// Use `RetrievalConfig` inside ``ToolConfig`` or ``TemplateToolConfig`` to provide optional
|
|
/// location coordinates and language preferences to bias and localize search results.
|
|
public struct RetrievalConfig: Sendable, Encodable {
|
|
/// The geographic location coordinates used to bias the retrieval search.
|
|
let location: CLLocationCoordinate2D?
|
|
/// The BCP 47 language code of the user (for example, `"en_US"`).
|
|
let languageCode: String?
|
|
|
|
/// Constructs a new `RetrievalConfig`.
|
|
///
|
|
/// - Parameters:
|
|
/// - location: Geographic coordinates used to bias results towards the user's location.
|
|
/// - languageCode: A BCP 47 language code to localize responses.
|
|
public init(location: CLLocationCoordinate2D? = nil, languageCode: String? = nil) {
|
|
self.location = location
|
|
self.languageCode = languageCode
|
|
}
|
|
|
|
public func encode(to encoder: Encoder) throws {
|
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
try container.encodeIfPresent(languageCode, forKey: .languageCode)
|
|
if let location = location {
|
|
var latLngContainer = container.nestedContainer(keyedBy: LatLngKeys.self, forKey: .location)
|
|
try latLngContainer.encode(location.latitude, forKey: .latitude)
|
|
try latLngContainer.encode(location.longitude, forKey: .longitude)
|
|
}
|
|
}
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
case location = "latLng"
|
|
case languageCode
|
|
}
|
|
|
|
enum LatLngKeys: String, CodingKey {
|
|
case latitude
|
|
case longitude
|
|
}
|
|
}
|
|
|
|
extension CLLocationCoordinate2D: @retroactive @unchecked Sendable {}
|
|
|
|
// MARK: - Tool Conveniences
|
|
|
|
public extension GenerativeModel.Tool {
|
|
/// Creates a tool that allows the model to perform function calling.
|
|
///
|
|
/// Function calling can be used to provide data to the model that was not known at the time it
|
|
/// was trained (for example, the current date or weather conditions) or to allow it to interact
|
|
/// with external systems (for example, making an API request or querying/updating a database).
|
|
/// For more details and use cases, see [Function calling using the Gemini
|
|
/// API](https://firebase.google.com/docs/ai-logic/function-calling).
|
|
///
|
|
/// - 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 ``FunctionCallPart`` with arguments to the client side for execution. The
|
|
/// model may decide to call none, some or all of the declared functions; this behavior may be
|
|
/// configured by specifying a ``ToolConfig`` when instantiating the model. When a
|
|
/// ``FunctionCallPart`` is received, the next conversation turn may contain a
|
|
/// ``FunctionResponsePart`` in ``ModelContent/parts`` with a ``ModelContent/role`` of
|
|
/// `"user"`; this response contains the result of executing the function on the client,
|
|
/// providing generation context for the model's next turn.
|
|
static func functionDeclarations(_ functionDeclarations: [FunctionDeclaration])
|
|
-> GenerativeModel.Tool {
|
|
return self.init(functionDeclarations: functionDeclarations)
|
|
}
|
|
|
|
/// Creates a tool that allows the model to use Grounding with Google Search.
|
|
///
|
|
/// Grounding with Google Search can be used to allow the model to connect to Google Search to
|
|
/// access and incorporate up-to-date information from the web into it's responses.
|
|
///
|
|
/// > Important: When using this feature, you are required to comply with the
|
|
/// "Grounding with Google Search" usage requirements for your chosen API provider:
|
|
/// [Gemini Developer API](https://ai.google.dev/gemini-api/terms#grounding-with-google-search)
|
|
/// or the Agent Platform Gemini API (see
|
|
/// [Service Terms](https://cloud.google.com/terms/service-terms)
|
|
/// section within the Service Specific Terms).
|
|
///
|
|
/// - Parameters:
|
|
/// - googleSearch: An empty ``GoogleSearch`` object. The presence of this object in the list
|
|
/// of tools enables the model to use Google Search.
|
|
///
|
|
/// - Returns: A ``GenerativeModel/Tool`` configured for Google Search.
|
|
static func googleSearch(_ googleSearch: GoogleSearch = GoogleSearch()) -> GenerativeModel.Tool {
|
|
return self.init(googleSearch: googleSearch)
|
|
}
|
|
|
|
/// Creates a tool that allows the model to use Grounding with Google Maps.
|
|
///
|
|
/// Grounding with Google Maps can be used to allow the model to connect to Google Maps to
|
|
/// access and incorporate up-to-date information from the web into it's responses.
|
|
///
|
|
/// > Important: When using this feature, you are required to comply with the
|
|
/// "Grounding with Google Maps" usage requirements for your chosen API provider.
|
|
///
|
|
/// - Returns: A ``GenerativeModel/Tool`` configured for Google Maps.
|
|
static func googleMaps() -> GenerativeModel.Tool {
|
|
return self.init(googleMaps: GoogleMaps())
|
|
}
|
|
|
|
/// Creates a tool that allows you to provide additional context to the models in the form of
|
|
/// public web URLs.
|
|
///
|
|
/// By including URLs in your request, the Gemini model will access the content from those pages
|
|
/// to inform and enhance its response.
|
|
static func urlContext() -> GenerativeModel.Tool {
|
|
return self.init(urlContext: URLContext())
|
|
}
|
|
|
|
/// Creates a tool that allows the model to execute code.
|
|
///
|
|
/// For more details, see ``CodeExecution``.
|
|
static func codeExecution() -> GenerativeModel.Tool {
|
|
return self.init(codeExecution: CodeExecution())
|
|
}
|
|
}
|
|
|
|
// MARK: - Codable Conformance
|
|
|
|
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.encodeIfPresent(parameters, forKey: .parameters)
|
|
}
|
|
}
|
|
|
|
extension GenerativeModel.Tool: Encodable {}
|
|
|
|
extension FunctionCallingConfig: Encodable {}
|
|
|
|
extension FunctionCallingConfig.Mode: Encodable {}
|
|
|
|
extension GoogleSearch: Encodable {}
|
|
|
|
extension ToolConfig: Encodable {}
|