diff --git a/FirebaseVertexAI/CHANGELOG.md b/FirebaseVertexAI/CHANGELOG.md index cb4ef58083a..95d70ca6c0a 100644 --- a/FirebaseVertexAI/CHANGELOG.md +++ b/FirebaseVertexAI/CHANGELOG.md @@ -76,6 +76,8 @@ provided alongside the `blockReason`. (#13891) - [added] Added an optional `publicationDate` property that *may* be provided in `Citation`. (#13893) +- [added] Added `presencePenalty` and `frequencyPenalty` parameters to + `GenerationConfig`. (#13899) # 11.3.0 - [added] Added `Decodable` conformance for `FunctionResponse`. (#13606) diff --git a/FirebaseVertexAI/Sources/GenerationConfig.swift b/FirebaseVertexAI/Sources/GenerationConfig.swift index 3f3a4b6f214..8598624aea0 100644 --- a/FirebaseVertexAI/Sources/GenerationConfig.swift +++ b/FirebaseVertexAI/Sources/GenerationConfig.swift @@ -58,6 +58,34 @@ public struct GenerationConfig { /// (unbounded). public let maxOutputTokens: Int? + /// Controls the likelihood of repeating the same words or phrases already generated in the text. + /// + /// Higher values increase the penalty of repetition, resulting in more diverse output. The + /// maximum value for `presencePenalty` is up to, but not including, `2.0`; the minimum value is + /// `-2.0`. + /// + /// > Note: While both `presencePenalty` and ``frequencyPenalty`` discourage repetition, + /// > `presencePenalty` applies the same penalty regardless of how many times the word/phrase has + /// > already appeared, whereas `frequencyPenalty` increases the penalty for *each* repetition of + /// > a word/phrase. + /// + /// > Important: Supported by `gemini-1.5-pro-002` and` gemini-1.5-flash-002` only. + public let presencePenalty: Float? + + /// Controls the likelihood of repeating words, with the penalty increasing for each repetition. + /// + /// Higher values increase the penalty of repetition, resulting in more diverse output. The + /// maximum value for `frequencyPenalty` is up to, but not including, `2.0`; the minimum value is + /// `-2.0`. + /// + /// > Note: While both `frequencyPenalty` and ``presencePenalty`` discourage repetition, + /// > `frequencyPenalty` increases the penalty for *each* repetition of a word/phrase, whereas + /// > `presencePenalty` applies the same penalty regardless of how many times the word/phrase has + /// > already appeared. + /// + /// > Important: Supported by `gemini-1.5-pro-002` and` gemini-1.5-flash-002` only. + public let frequencyPenalty: Float? + /// A set of up to 5 `String`s that will stop output generation. If /// specified, the API will stop at the first appearance of a stop sequence. /// The stop sequence will not be included as part of the response. @@ -88,11 +116,14 @@ public struct GenerationConfig { /// - Parameter topK: See ``topK`` /// - Parameter candidateCount: See ``candidateCount`` /// - Parameter maxOutputTokens: See ``maxOutputTokens`` + /// - Parameter presencePenalty: See ``presencePenalty`` + /// - Parameter frequencyPenalty: See ``frequencyPenalty`` /// - Parameter stopSequences: See ``stopSequences`` /// - Parameter responseMIMEType: See ``responseMIMEType`` /// - Parameter responseSchema: See ``responseSchema`` public init(temperature: Float? = nil, topP: Float? = nil, topK: Int? = nil, candidateCount: Int? = nil, maxOutputTokens: Int? = nil, + presencePenalty: Float? = nil, frequencyPenalty: Float? = nil, stopSequences: [String]? = nil, responseMIMEType: String? = nil, responseSchema: Schema? = nil) { // Explicit init because otherwise if we re-arrange the above variables it changes the API @@ -102,6 +133,8 @@ public struct GenerationConfig { self.topK = topK self.candidateCount = candidateCount self.maxOutputTokens = maxOutputTokens + self.presencePenalty = presencePenalty + self.frequencyPenalty = frequencyPenalty self.stopSequences = stopSequences self.responseMIMEType = responseMIMEType self.responseSchema = responseSchema diff --git a/FirebaseVertexAI/Tests/Unit/GenerationConfigTests.swift b/FirebaseVertexAI/Tests/Unit/GenerationConfigTests.swift index 43cbfe6dd96..232807d08da 100644 --- a/FirebaseVertexAI/Tests/Unit/GenerationConfigTests.swift +++ b/FirebaseVertexAI/Tests/Unit/GenerationConfigTests.swift @@ -47,6 +47,8 @@ final class GenerationConfigTests: XCTestCase { let topK = 40 let candidateCount = 2 let maxOutputTokens = 256 + let presencePenalty: Float = 0.5 + let frequencyPenalty: Float = 0.75 let stopSequences = ["END", "DONE"] let responseMIMEType = "application/json" let generationConfig = GenerationConfig( @@ -55,6 +57,8 @@ final class GenerationConfigTests: XCTestCase { topK: topK, candidateCount: candidateCount, maxOutputTokens: maxOutputTokens, + presencePenalty: presencePenalty, + frequencyPenalty: frequencyPenalty, stopSequences: stopSequences, responseMIMEType: responseMIMEType, responseSchema: .array(items: .string()) @@ -66,7 +70,9 @@ final class GenerationConfigTests: XCTestCase { XCTAssertEqual(json, """ { "candidateCount" : \(candidateCount), + "frequencyPenalty" : \(frequencyPenalty), "maxOutputTokens" : \(maxOutputTokens), + "presencePenalty" : \(presencePenalty), "responseMIMEType" : "\(responseMIMEType)", "responseSchema" : { "items" : {