Skip to content

Commit

Permalink
Used watchOS 4.0 as a baseline and updated function requirements as such
Browse files Browse the repository at this point in the history
  • Loading branch information
rin-senpai committed Jun 30, 2024
1 parent 646da8e commit 9a5df1b
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 64 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let package = Package(
.iOS(.v11),
.macOS(.v10_13),
.macCatalyst(.v13),
.watchOS(.v8)
.watchOS(.v4)
],
products: [
.library(
Expand Down
6 changes: 3 additions & 3 deletions Sources/GoogleAI/Chat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class Chat {
/// and response will be added to the history. If unsuccessful, history will remain unchanged.
/// - Parameter parts: The new content to send as a single chat message.
/// - Returns: A stream containing the model's response or an error if an error occurred.
@available(macOS 12.0, *)
@available(macOS 12.0, watchOS 8.0, *)
public func sendMessageStream(_ parts: any ThrowingPartsRepresentable...)
-> AsyncThrowingStream<GenerateContentResponse, Error> {
return try sendMessageStream([ModelContent(parts: parts)])
Expand All @@ -94,7 +94,7 @@ public class Chat {
/// and response will be added to the history. If unsuccessful, history will remain unchanged.
/// - Parameter content: The new content to send as a single chat message.
/// - Returns: A stream containing the model's response or an error if an error occurred.
@available(macOS 12.0, *)
@available(macOS 12.0, watchOS 8.0, *)
public func sendMessageStream(_ content: @autoclosure () throws -> [ModelContent])
-> AsyncThrowingStream<GenerateContentResponse, Error> {
let resolvedContent: [ModelContent]
Expand Down Expand Up @@ -188,4 +188,4 @@ public class Chat {
return ModelContent(role: "user", parts: content.parts)
}
}
}
}
12 changes: 6 additions & 6 deletions Sources/GoogleAI/CountTokensRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

import Foundation

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
struct CountTokensRequest {
let model: String
let generateContentRequest: GenerateContentRequest
let options: RequestOptions
}

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
extension CountTokensRequest: GenerativeAIRequest {
typealias Response = CountTokensResponse

Expand All @@ -31,20 +31,20 @@ extension CountTokensRequest: GenerativeAIRequest {
}

/// The model's response to a count tokens request.
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
public struct CountTokensResponse {
/// The total number of tokens in the input given to the model as a prompt.
public let totalTokens: Int
}

// MARK: - Codable Conformances

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
extension CountTokensRequest: Encodable {
enum CodingKeys: CodingKey {
case generateContentRequest
}
}

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
extension CountTokensResponse: Decodable {}
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
extension CountTokensResponse: Decodable {}
2 changes: 1 addition & 1 deletion Sources/GoogleAI/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,4 @@ enum RPCErrorMessage: String {
enum InvalidCandidateError: Error {
case emptyContent(underlyingError: Error)
case malformedContent(underlyingError: Error)
}
}
2 changes: 1 addition & 1 deletion Sources/GoogleAI/FunctionCalling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,4 @@ extension FunctionCallingConfig.Mode: Encodable {}

extension ToolConfig: Encodable {}

extension FunctionResponse: Encodable {}
extension FunctionResponse: Encodable {}
2 changes: 1 addition & 1 deletion Sources/GoogleAI/GenerateContentError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ public enum GenerateContentError: Error {
/// - Important: The API is only available in
/// [specific regions](https://ai.google.dev/available_regions#available_regions).
case unsupportedUserLocation
}
}
6 changes: 3 additions & 3 deletions Sources/GoogleAI/GenerateContentRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import Foundation

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
struct GenerateContentRequest {
/// Model name.
let model: String
Expand All @@ -28,7 +28,7 @@ struct GenerateContentRequest {
let options: RequestOptions
}

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
extension GenerateContentRequest: Encodable {
enum CodingKeys: String, CodingKey {
case model
Expand All @@ -53,4 +53,4 @@ extension GenerateContentRequest: GenerativeAIRequest {
return URL(string: "\(modelURL):generateContent")!
}
}
}
}
20 changes: 10 additions & 10 deletions Sources/GoogleAI/GenerateContentResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public struct GenerateContentResponse {

/// A struct representing a possible reply to a content generation prompt. Each content generation
/// prompt may produce multiple candidate responses.
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
public struct CandidateResponse {
/// The response's content.
public let content: ModelContent
Expand All @@ -108,14 +108,14 @@ public struct CandidateResponse {
}

/// A collection of source attributions for a piece of content.
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
public struct CitationMetadata {
/// A list of individual cited sources and the parts of the content to which they apply.
public let citationSources: [Citation]
}

/// A struct describing a source attribution.
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
public struct Citation {
/// The inclusive beginning of a sequence in a model response that derives from a cited source.
public let startIndex: Int
Expand All @@ -131,7 +131,7 @@ public struct Citation {
}

/// A value enumerating possible reasons for a model to terminate a content generation request.
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
public enum FinishReason: String {
case unknown = "FINISH_REASON_UNKNOWN"

Expand All @@ -156,7 +156,7 @@ public enum FinishReason: String {
}

/// A metadata struct containing any feedback the model had on the prompt it was provided.
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
public struct PromptFeedback {
/// A type describing possible reasons to block a prompt.
public enum BlockReason: String {
Expand Down Expand Up @@ -239,7 +239,7 @@ extension GenerateContentResponse.UsageMetadata: Decodable {
}
}

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
extension CandidateResponse: Decodable {
enum CodingKeys: CodingKey {
case content
Expand Down Expand Up @@ -286,10 +286,10 @@ extension CandidateResponse: Decodable {
}
}

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
extension CitationMetadata: Decodable {}

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
extension Citation: Decodable {
enum CodingKeys: CodingKey {
case startIndex
Expand Down Expand Up @@ -342,7 +342,7 @@ extension PromptFeedback.BlockReason: Decodable {
}
}

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
extension PromptFeedback: Decodable {
enum CodingKeys: CodingKey {
case blockReason
Expand All @@ -364,4 +364,4 @@ extension PromptFeedback: Decodable {
safetyRatings = []
}
}
}
}
6 changes: 3 additions & 3 deletions Sources/GoogleAI/GenerationConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Foundation

/// A struct defining model parameters to be used when sending generative AI
/// requests to the backend model.
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
public struct GenerationConfig {
/// A parameter controlling the degree of randomness in token selection. A
/// temperature of zero is deterministic, always choosing the
Expand Down Expand Up @@ -106,5 +106,5 @@ public struct GenerationConfig {

// MARK: - Codable Conformances

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
extension GenerationConfig: Encodable {}
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
extension GenerationConfig: Encodable {}
6 changes: 3 additions & 3 deletions Sources/GoogleAI/GenerativeAIRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import Foundation

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
protocol GenerativeAIRequest: Encodable {
associatedtype Response: Decodable

Expand All @@ -24,7 +24,7 @@ protocol GenerativeAIRequest: Encodable {
}

/// Configuration parameters for sending requests to the backend.
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
public struct RequestOptions {
/// The request’s timeout interval in seconds.
let timeout: TimeInterval
Expand All @@ -41,4 +41,4 @@ public struct RequestOptions {
self.timeout = timeout
self.apiVersion = apiVersion
}
}
}
7 changes: 5 additions & 2 deletions Sources/GoogleAI/GenerativeAIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct GenerativeAIService {
return try parseResponse(T.Response.self, from: data)
}

@available(macOS 12.0, *)
@available(macOS 12.0, watchOS 8.0, *)
func loadRequestStream<T: GenerativeAIRequest>(request: T)
-> AsyncThrowingStream<T.Response, Error> {
return AsyncThrowingStream { continuation in
Expand Down Expand Up @@ -161,6 +161,7 @@ struct GenerativeAIService {
return urlRequest
}

@available(watchOS 7.0, *)
private func httpResponse(urlResponse: URLResponse) throws -> HTTPURLResponse {
// Verify the status code is 200
guard let response = urlResponse as? HTTPURLResponse else {
Expand Down Expand Up @@ -209,6 +210,7 @@ struct GenerativeAIService {
}
}

@available(watchOS 7.0, *)
private func parseResponse<T: Decodable>(_ type: T.Type, from data: Data) throws -> T {
do {
return try JSONDecoder().decode(type, from: data)
Expand All @@ -222,6 +224,7 @@ struct GenerativeAIService {
}

#if DEBUG
@available(watchOS 7.0, *)
private func cURLCommand(from request: URLRequest) -> String {
var returnValue = "curl "
if let allHeaders = request.allHTTPHeaderFields {
Expand Down Expand Up @@ -251,4 +254,4 @@ struct GenerativeAIService {
""")
}
#endif // DEBUG
}
}
4 changes: 2 additions & 2 deletions Sources/GoogleAI/GenerativeAISwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import Foundation
#endif

/// Constants associated with the GenerativeAISwift SDK.
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
public enum GenerativeAISwift {
/// String value of the SDK version
public static let version = "0.5.4"
/// The Google AI backend endpoint URL.
static let baseURL = "https://generativelanguage.googleapis.com"
}
}
8 changes: 4 additions & 4 deletions Sources/GoogleAI/GenerativeModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public final class GenerativeModel {
/// for conforming types).
/// - Returns: A stream wrapping content generated by the model or a ``GenerateContentError``
/// error if an error occurred.
@available(macOS 12.0, *)
@available(macOS 12.0, watchOS 8.0, *)
public func generateContentStream(_ parts: any ThrowingPartsRepresentable...)
-> AsyncThrowingStream<GenerateContentResponse, Error> {
return try generateContentStream([ModelContent(parts: parts)])
Expand All @@ -231,7 +231,7 @@ public final class GenerativeModel {
/// - Parameter content: The input(s) given to the model as a prompt.
/// - Returns: A stream wrapping content generated by the model or a ``GenerateContentError``
/// error if an error occurred.
@available(macOS 12.0, *)
@available(macOS 12.0, watchOS 8.0, *)
public func generateContentStream(_ content: @autoclosure () throws -> [ModelContent])
-> AsyncThrowingStream<GenerateContentResponse, Error> {
let evaluatedContent: [ModelContent]
Expand Down Expand Up @@ -370,7 +370,7 @@ public final class GenerativeModel {
}

/// An error thrown in `GenerativeModel.countTokens(_:)`.
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
public enum CountTokensError: Error {
case internalError(underlying: Error)
}
}
2 changes: 1 addition & 1 deletion Sources/GoogleAI/JSONValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ extension JSONValue: Encodable {
}
}

extension JSONValue: Equatable {}
extension JSONValue: Equatable {}
2 changes: 1 addition & 1 deletion Sources/GoogleAI/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ struct Logging {
return Logger(.disabled)
}
}()
}
}
8 changes: 4 additions & 4 deletions Sources/GoogleAI/ModelContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Foundation
/// A type describing data in media formats interpretable by an AI model. Each generative AI
/// request or response contains an `Array` of ``ModelContent``s, and each ``ModelContent`` value
/// may comprise multiple heterogeneous ``ModelContent/Part``s.
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
public struct ModelContent: Equatable {
/// A discrete piece of data in a media format intepretable by an AI model. Within a single value
/// of ``Part``, different data types may not mix.
Expand Down Expand Up @@ -118,10 +118,10 @@ public struct ModelContent: Equatable {

// MARK: Codable Conformances

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
extension ModelContent: Codable {}

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
extension ModelContent.Part: Codable {
enum CodingKeys: String, CodingKey {
case text
Expand Down Expand Up @@ -188,4 +188,4 @@ extension ModelContent.Part: Codable {
))
}
}
}
}
Loading

0 comments on commit 9a5df1b

Please sign in to comment.