Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Vertex AI] Add presencePenalty and frequencyPenalty #13899

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions FirebaseVertexAI/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions FirebaseVertexAI/Sources/GenerationConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions FirebaseVertexAI/Tests/Unit/GenerationConfigTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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())
Expand All @@ -66,7 +70,9 @@ final class GenerationConfigTests: XCTestCase {
XCTAssertEqual(json, """
{
"candidateCount" : \(candidateCount),
"frequencyPenalty" : \(frequencyPenalty),
"maxOutputTokens" : \(maxOutputTokens),
"presencePenalty" : \(presencePenalty),
"responseMIMEType" : "\(responseMIMEType)",
"responseSchema" : {
"items" : {
Expand Down
Loading