Skip to content

Commit

Permalink
Encodable extensions for use in testing stubs.
Browse files Browse the repository at this point in the history
  • Loading branch information
James J Kalafus committed Feb 15, 2024
1 parent 0c60094 commit c82f4ec
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Sources/OpenAI/Public/Errors/APIError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public struct APIError: Error, Decodable, Equatable {
self.code = code
}

enum CodingKeys: CodingKey {
public enum CodingKeys: CodingKey {
case message
case type
case param
Expand Down
4 changes: 4 additions & 0 deletions Sources/OpenAI/Public/Models/AudioTranscriptionResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ import Foundation
public struct AudioTranscriptionResult: Decodable, Equatable {

public let text: String

public enum CodingKeys: CodingKey {
case text
}
}
4 changes: 4 additions & 0 deletions Sources/OpenAI/Public/Models/AudioTranslationResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ import Foundation
public struct AudioTranslationResult: Decodable, Equatable {

public let text: String

public enum CodingKeys: CodingKey {
case text
}
}
6 changes: 3 additions & 3 deletions Sources/OpenAI/Public/Models/ChatResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct ChatResult: Decodable, Equatable {
/// Exists only if it is a complete message.
public let finishReason: String?

enum CodingKeys: String, CodingKey {
public enum CodingKeys: String, CodingKey {
case index
case message
case finishReason = "finish_reason"
Expand All @@ -29,7 +29,7 @@ public struct ChatResult: Decodable, Equatable {
public let completionTokens: Int
public let totalTokens: Int

enum CodingKeys: String, CodingKey {
public enum CodingKeys: String, CodingKey {
case promptTokens = "prompt_tokens"
case completionTokens = "completion_tokens"
case totalTokens = "total_tokens"
Expand All @@ -43,7 +43,7 @@ public struct ChatResult: Decodable, Equatable {
public let choices: [Choice]
public let usage: Usage?

enum CodingKeys: String, CodingKey {
public enum CodingKeys: String, CodingKey {
case id
case object
case created
Expand Down
6 changes: 3 additions & 3 deletions Sources/OpenAI/Public/Models/ChatStreamResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct ChatStreamResult: Decodable, Equatable {
public let name: String?
public let functionCall: ChatFunctionCall?

enum CodingKeys: String, CodingKey {
public enum CodingKeys: String, CodingKey {
case role
case content
case name
Expand All @@ -29,7 +29,7 @@ public struct ChatStreamResult: Decodable, Equatable {
public let delta: Delta
public let finishReason: String?

enum CodingKeys: String, CodingKey {
public enum CodingKeys: String, CodingKey {
case index
case delta
case finishReason = "finish_reason"
Expand All @@ -42,7 +42,7 @@ public struct ChatStreamResult: Decodable, Equatable {
public let model: Model
public let choices: [Choice]

enum CodingKeys: String, CodingKey {
public enum CodingKeys: String, CodingKey {
case id
case object
case created
Expand Down
21 changes: 15 additions & 6 deletions Sources/OpenAI/Public/Models/CompletionsResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@
import Foundation

public struct CompletionsResult: Decodable, Equatable {

public struct Usage: Decodable, Equatable {
public let promptTokens: Int
public let completionTokens: Int
public let totalTokens: Int
enum CodingKeys: String, CodingKey {

public enum CodingKeys: String, CodingKey {
case promptTokens = "prompt_tokens"
case completionTokens = "completion_tokens"
case totalTokens = "total_tokens"
}
}

public struct Choice: Decodable, Equatable {
public let text: String
public let index: Int
public let finishReason: String?
enum CodingKeys: String, CodingKey {

public enum CodingKeys: String, CodingKey {
case text
case index
case finishReason = "finish_reason"
Expand All @@ -39,4 +39,13 @@ public struct CompletionsResult: Decodable, Equatable {
public let model: Model
public let choices: [Choice]
public let usage: Usage?

public enum CodingKeys: CodingKey {
case id
case object
case created
case model
case choices
case usage
}
}
20 changes: 16 additions & 4 deletions Sources/OpenAI/Public/Models/EditsResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,38 @@
import Foundation

public struct EditsResult: Decodable, Equatable {

public struct Choice: Decodable, Equatable {
public let text: String
public let index: Int

public enum CodingKeys: CodingKey {
case text
case index
}
}

public struct Usage: Decodable, Equatable {
public let promptTokens: Int
public let completionTokens: Int
public let totalTokens: Int
enum CodingKeys: String, CodingKey {

public enum CodingKeys: String, CodingKey {
case promptTokens = "prompt_tokens"
case completionTokens = "completion_tokens"
case totalTokens = "total_tokens"
}
}

public let object: String
public let created: TimeInterval
public let choices: [Choice]
public let usage: Usage

public enum CodingKeys: CodingKey {
case object
case created
case choices
case usage
}
}
14 changes: 13 additions & 1 deletion Sources/OpenAI/Public/Models/EmbeddingsResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ public struct EmbeddingsResult: Decodable, Equatable {
public let object: String
public let embedding: [Double]
public let index: Int

public enum CodingKeys: CodingKey {
case object
case embedding
case index
}
}

public struct Usage: Decodable, Equatable {
public let promptTokens: Int
public let totalTokens: Int

enum CodingKeys: String, CodingKey {
public enum CodingKeys: String, CodingKey {
case promptTokens = "prompt_tokens"
case totalTokens = "total_tokens"
}
Expand All @@ -28,4 +34,10 @@ public struct EmbeddingsResult: Decodable, Equatable {
public let data: [Embedding]
public let model: Model
public let usage: Usage

public enum CodingKeys: CodingKey {
case data
case model
case usage
}
}
14 changes: 12 additions & 2 deletions Sources/OpenAI/Public/Models/ImagesResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@
import Foundation

public struct ImagesResult: Decodable, Equatable {

public struct URLResult: Decodable, Equatable {
public let url: String?
public let b64_json: String?

public enum CodingKeys: CodingKey {
case url
case b64_json
}
}

public let created: TimeInterval
public let data: [URLResult]

public enum CodingKeys: CodingKey {
case created
case data
}
}

extension ImagesResult.URLResult: Hashable { }
2 changes: 1 addition & 1 deletion Sources/OpenAI/Public/Models/Models/ModelResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public struct ModelResult: Decodable, Equatable {
public let object: String
public let ownedBy: String

enum CodingKeys: String, CodingKey {
public enum CodingKeys: String, CodingKey {
case id
case object
case ownedBy = "owned_by"
Expand Down
5 changes: 5 additions & 0 deletions Sources/OpenAI/Public/Models/Models/ModelsResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ public struct ModelsResult: Decodable, Equatable {

public let data: [ModelResult]
public let object: String

public enum CodingKeys: CodingKey {
case data
case object
}
}
12 changes: 9 additions & 3 deletions Sources/OpenAI/Public/Models/ModerationsResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public struct ModerationsResult: Decodable, Equatable {
/// Violent content that depicts death, violence, or serious physical injury in extreme graphic detail.
public let violenceGraphic: Bool

enum CodingKeys: String, CodingKey {
public enum CodingKeys: String, CodingKey {
case hate
case hateThreatening = "hate/threatening"
case selfHarm = "self-harm"
Expand All @@ -54,7 +54,7 @@ public struct ModerationsResult: Decodable, Equatable {
/// Violent content that depicts death, violence, or serious physical injury in extreme graphic detail.
public let violenceGraphic: Double

enum CodingKeys: String, CodingKey {
public enum CodingKeys: String, CodingKey {
case hate
case hateThreatening = "hate/threatening"
case selfHarm = "self-harm"
Expand All @@ -72,7 +72,7 @@ public struct ModerationsResult: Decodable, Equatable {
/// True if the model classifies the content as violating OpenAI's usage policies, false otherwise.
public let flagged: Bool

enum CodingKeys: String, CodingKey {
public enum CodingKeys: String, CodingKey {
case categories
case categoryScores = "category_scores"
case flagged
Expand All @@ -82,4 +82,10 @@ public struct ModerationsResult: Decodable, Equatable {
public let id: String
public let model: Model
public let results: [CategoryResult]

public enum CodingKeys: CodingKey {
case id
case model
case results
}
}
Loading

0 comments on commit c82f4ec

Please sign in to comment.