Skip to content

Commit

Permalink
Add apiVersion to RequestOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheard committed Jan 31, 2024
1 parent ccf2c94 commit b93ba63
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Sources/GoogleAI/CountTokensRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension CountTokensRequest: GenerativeAIRequest {
typealias Response = CountTokensResponse

var url: URL {
URL(string: "\(GenerativeAISwift.baseURL)/\(model):countTokens")!
URL(string: "\(GenerativeAISwift.baseURL)/\(options.apiVersion)/\(model):countTokens")!
}
}

Expand Down
5 changes: 3 additions & 2 deletions Sources/GoogleAI/GenerateContentRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ extension GenerateContentRequest: GenerativeAIRequest {
typealias Response = GenerateContentResponse

var url: URL {
let modelURL = "\(GenerativeAISwift.baseURL)/\(options.apiVersion)/\(model)"
if isStreaming {
URL(string: "\(GenerativeAISwift.baseURL)/\(model):streamGenerateContent?alt=sse")!
return URL(string: "\(modelURL):streamGenerateContent?alt=sse")!
} else {
URL(string: "\(GenerativeAISwift.baseURL)/\(model):generateContent")!
return URL(string: "\(modelURL):generateContent")!
}
}
}
12 changes: 9 additions & 3 deletions Sources/GoogleAI/GenerativeAIRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ public struct RequestOptions {
/// `URLRequest`.
let timeout: TimeInterval?

/// The API version to use in requests to the backend.
let apiVersion: String

/// Initializes a request options object.
///
/// - Parameter timeout The request’s timeout interval in seconds; if not specified uses the
/// default value for a `URLRequest`.
public init(timeout: TimeInterval? = nil) {
/// - Parameters:
/// - timeout The request’s timeout interval in seconds; if not specified uses the default value
/// for a `URLRequest`.
/// - apiVersion The API version to use in requests to the backend; defaults to "v1".
public init(timeout: TimeInterval? = nil, apiVersion: String = "v1") {
self.timeout = timeout
self.apiVersion = apiVersion
}
}
2 changes: 1 addition & 1 deletion Sources/GoogleAI/GenerativeAISwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import Foundation
public enum GenerativeAISwift {
/// String value of the SDK version
public static let version = "0.4.7"
static let baseURL = "https://generativelanguage.googleapis.com/v1"
static let baseURL = "https://generativelanguage.googleapis.com"
}

0 comments on commit b93ba63

Please sign in to comment.