Skip to content

Commit 4b96944

Browse files
authored
[Vertex AI] Add presencePenalty and frequencyPenalty (#13899)
1 parent 61f2ee3 commit 4b96944

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

FirebaseVertexAI/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
provided alongside the `blockReason`. (#13891)
7777
- [added] Added an optional `publicationDate` property that *may* be provided in
7878
`Citation`. (#13893)
79+
- [added] Added `presencePenalty` and `frequencyPenalty` parameters to
80+
`GenerationConfig`. (#13899)
7981

8082
# 11.3.0
8183
- [added] Added `Decodable` conformance for `FunctionResponse`. (#13606)

FirebaseVertexAI/Sources/GenerationConfig.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,34 @@ public struct GenerationConfig {
5858
/// (unbounded).
5959
public let maxOutputTokens: Int?
6060

61+
/// Controls the likelihood of repeating the same words or phrases already generated in the text.
62+
///
63+
/// Higher values increase the penalty of repetition, resulting in more diverse output. The
64+
/// maximum value for `presencePenalty` is up to, but not including, `2.0`; the minimum value is
65+
/// `-2.0`.
66+
///
67+
/// > Note: While both `presencePenalty` and ``frequencyPenalty`` discourage repetition,
68+
/// > `presencePenalty` applies the same penalty regardless of how many times the word/phrase has
69+
/// > already appeared, whereas `frequencyPenalty` increases the penalty for *each* repetition of
70+
/// > a word/phrase.
71+
///
72+
/// > Important: Supported by `gemini-1.5-pro-002` and` gemini-1.5-flash-002` only.
73+
public let presencePenalty: Float?
74+
75+
/// Controls the likelihood of repeating words, with the penalty increasing for each repetition.
76+
///
77+
/// Higher values increase the penalty of repetition, resulting in more diverse output. The
78+
/// maximum value for `frequencyPenalty` is up to, but not including, `2.0`; the minimum value is
79+
/// `-2.0`.
80+
///
81+
/// > Note: While both `frequencyPenalty` and ``presencePenalty`` discourage repetition,
82+
/// > `frequencyPenalty` increases the penalty for *each* repetition of a word/phrase, whereas
83+
/// > `presencePenalty` applies the same penalty regardless of how many times the word/phrase has
84+
/// > already appeared.
85+
///
86+
/// > Important: Supported by `gemini-1.5-pro-002` and` gemini-1.5-flash-002` only.
87+
public let frequencyPenalty: Float?
88+
6189
/// A set of up to 5 `String`s that will stop output generation. If
6290
/// specified, the API will stop at the first appearance of a stop sequence.
6391
/// The stop sequence will not be included as part of the response.
@@ -88,11 +116,14 @@ public struct GenerationConfig {
88116
/// - Parameter topK: See ``topK``
89117
/// - Parameter candidateCount: See ``candidateCount``
90118
/// - Parameter maxOutputTokens: See ``maxOutputTokens``
119+
/// - Parameter presencePenalty: See ``presencePenalty``
120+
/// - Parameter frequencyPenalty: See ``frequencyPenalty``
91121
/// - Parameter stopSequences: See ``stopSequences``
92122
/// - Parameter responseMIMEType: See ``responseMIMEType``
93123
/// - Parameter responseSchema: See ``responseSchema``
94124
public init(temperature: Float? = nil, topP: Float? = nil, topK: Int? = nil,
95125
candidateCount: Int? = nil, maxOutputTokens: Int? = nil,
126+
presencePenalty: Float? = nil, frequencyPenalty: Float? = nil,
96127
stopSequences: [String]? = nil, responseMIMEType: String? = nil,
97128
responseSchema: Schema? = nil) {
98129
// Explicit init because otherwise if we re-arrange the above variables it changes the API
@@ -102,6 +133,8 @@ public struct GenerationConfig {
102133
self.topK = topK
103134
self.candidateCount = candidateCount
104135
self.maxOutputTokens = maxOutputTokens
136+
self.presencePenalty = presencePenalty
137+
self.frequencyPenalty = frequencyPenalty
105138
self.stopSequences = stopSequences
106139
self.responseMIMEType = responseMIMEType
107140
self.responseSchema = responseSchema

FirebaseVertexAI/Tests/Unit/GenerationConfigTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ final class GenerationConfigTests: XCTestCase {
4747
let topK = 40
4848
let candidateCount = 2
4949
let maxOutputTokens = 256
50+
let presencePenalty: Float = 0.5
51+
let frequencyPenalty: Float = 0.75
5052
let stopSequences = ["END", "DONE"]
5153
let responseMIMEType = "application/json"
5254
let generationConfig = GenerationConfig(
@@ -55,6 +57,8 @@ final class GenerationConfigTests: XCTestCase {
5557
topK: topK,
5658
candidateCount: candidateCount,
5759
maxOutputTokens: maxOutputTokens,
60+
presencePenalty: presencePenalty,
61+
frequencyPenalty: frequencyPenalty,
5862
stopSequences: stopSequences,
5963
responseMIMEType: responseMIMEType,
6064
responseSchema: .array(items: .string())
@@ -66,7 +70,9 @@ final class GenerationConfigTests: XCTestCase {
6670
XCTAssertEqual(json, """
6771
{
6872
"candidateCount" : \(candidateCount),
73+
"frequencyPenalty" : \(frequencyPenalty),
6974
"maxOutputTokens" : \(maxOutputTokens),
75+
"presencePenalty" : \(presencePenalty),
7076
"responseMIMEType" : "\(responseMIMEType)",
7177
"responseSchema" : {
7278
"items" : {

0 commit comments

Comments
 (0)