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

Remove Codable conformance for request or response-only types #34

Merged
merged 1 commit into from
Dec 12, 2023
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: 1 addition & 1 deletion Sources/GoogleAI/CountTokensRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ extension CountTokensRequest: GenerativeAIRequest {
}
}

public struct CountTokensResponse: Codable {
public struct CountTokensResponse: Decodable {
public let totalTokens: Int
}
24 changes: 17 additions & 7 deletions Sources/GoogleAI/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@

import Foundation

struct RPCError: Error, Decodable {
struct RPCError: Error {
let httpResponseCode: Int32
let message: String
let status: RPCStatus

enum CodingKeys: CodingKey {
case error
}

init(httpResponseCode: Int32, message: String, status: RPCStatus) {
self.httpResponseCode = httpResponseCode
self.message = message
self.status = status
}
}

extension RPCError: Decodable {
enum CodingKeys: CodingKey {
case error
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
Expand All @@ -53,10 +55,18 @@ struct RPCError: Error, Decodable {
}
}

struct ErrorStatus: Codable {
struct ErrorStatus {
let code: Int32?
let message: String?
let status: RPCStatus?
}

extension ErrorStatus: Decodable {
enum CodingKeys: CodingKey {
case code
case message
case status
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
Expand All @@ -70,7 +80,7 @@ struct ErrorStatus: Codable {
}
}

enum RPCStatus: String, Codable {
enum RPCStatus: String, Decodable {
// Not an error; returned on success.
case ok = "OK"

Expand Down
2 changes: 1 addition & 1 deletion Sources/GoogleAI/GenerateContentRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct GenerateContentRequest {
}

extension GenerateContentRequest: Encodable {
private enum CodingKeys: String, CodingKey {
enum CodingKeys: String, CodingKey {
case contents
case generationConfig
case safetySettings
Expand Down
41 changes: 34 additions & 7 deletions Sources/GoogleAI/GenerateContentResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import Foundation

public struct GenerateContentResponse: Codable {
public struct GenerateContentResponse {
public let candidates: [CandidateResponse]

public let promptFeedback: PromptFeedback?
Expand All @@ -36,6 +36,13 @@ public struct GenerateContentResponse: Codable {
self.candidates = candidates
self.promptFeedback = promptFeedback
}
}

extension GenerateContentResponse: Decodable {
enum CodingKeys: CodingKey {
case candidates
case promptFeedback
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
Expand All @@ -62,7 +69,7 @@ public struct GenerateContentResponse: Codable {
}
}

public struct CandidateResponse: Codable {
public struct CandidateResponse {
public let content: ModelContent
public let safetyRatings: [SafetyRating]

Expand All @@ -78,6 +85,16 @@ public struct CandidateResponse: Codable {
self.finishReason = finishReason
self.citationMetadata = citationMetadata
}
}

extension CandidateResponse: Decodable {
enum CodingKeys: CodingKey {
case content
case safetyRatings
case finishReason
case finishMessage
case citationMetadata
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
Expand Down Expand Up @@ -117,18 +134,18 @@ public struct CandidateResponse: Codable {
}

/// A collection of source attributions for a piece of content.
public struct CitationMetadata: Codable {
public struct CitationMetadata: Decodable {
public let citationSources: [Citation]
}

public struct Citation: Codable {
public struct Citation: Decodable {
public let startIndex: Int
public let endIndex: Int
public let uri: String
public let license: String
}

public enum FinishReason: String, Codable {
public enum FinishReason: String {
case unknown = "FINISH_REASON_UNKNOWN"

case unspecified = "FINISH_REASON_UNSPECIFIED"
Expand All @@ -145,7 +162,9 @@ public enum FinishReason: String, Codable {
case safety = "SAFETY"
case recitation = "RECITATION"
case other = "OTHER"
}

extension FinishReason: Decodable {
/// Do not explicitly use. Initializer required for Decodable conformance.
public init(from decoder: Decoder) throws {
let value = try decoder.singleValueContainer().decode(String.self)
Expand All @@ -160,8 +179,8 @@ public enum FinishReason: String, Codable {
}
}

public struct PromptFeedback: Codable {
public enum BlockReason: String, Codable {
public struct PromptFeedback {
public enum BlockReason: String, Decodable {
case unknown = "UNKNOWN"
case unspecified = "BLOCK_REASON_UNSPECIFIED"
case safety = "SAFETY"
Expand Down Expand Up @@ -189,7 +208,15 @@ public struct PromptFeedback: Codable {
self.blockReason = blockReason
self.safetyRatings = safetyRatings
}
}

extension PromptFeedback: Decodable {
enum CodingKeys: CodingKey {
case blockReason
case safetyRatings
}

/// Do not explicitly use. Initializer required for Decodable conformance.
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
blockReason = try container.decodeIfPresent(
Expand Down
2 changes: 1 addition & 1 deletion 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.
public struct GenerationConfig: Codable {
public struct GenerationConfig: Encodable {
/// A parameter controlling the degree of randomness in token selection. A
/// temperature of zero is deterministic, always choosing the
/// highest-probability response. Typical values are between 0 and 1
Expand Down
4 changes: 2 additions & 2 deletions Sources/GoogleAI/Safety.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Foundation
/// A type defining potentially harmful media categories and their model-assigned ratings. A value
/// of this type may be assigned to a category for every model-generated response, not just
/// responses that exceed a certain threshold.
public struct SafetyRating: Codable, Equatable {
public struct SafetyRating: Decodable, Equatable {
/// The category describing the potential harm a piece of content may pose. See
/// ``SafetySetting.HarmCategory`` for a list of possible values.
public let category: SafetySetting.HarmCategory
Expand Down Expand Up @@ -74,7 +74,7 @@ public struct SafetyRating: Codable, Equatable {
}

/// Safety feedback for an entire request.
public struct SafetyFeedback: Codable {
public struct SafetyFeedback: Decodable {
/// Safety rating evaluated from content.
public let rating: SafetyRating

Expand Down
Loading