Skip to content

Commit

Permalink
be specific and use Encodable for queries, and Decodable for results,…
Browse files Browse the repository at this point in the history
… instead of Codable for everything
  • Loading branch information
James J Kalafus committed Feb 7, 2024
1 parent 4ce648c commit 5aa9b88
Show file tree
Hide file tree
Showing 28 changed files with 64 additions and 60 deletions.
4 changes: 2 additions & 2 deletions Sources/OpenAI/OpenAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ final public class OpenAI: OpenAIProtocol {

extension OpenAI {

func performRequest<ResultType: Codable>(request: any URLRequestBuildable, completion: @escaping (Result<ResultType, Error>) -> Void) {
func performRequest<ResultType: Decodable>(request: any URLRequestBuildable, completion: @escaping (Result<ResultType, Error>) -> Void) {
do {
let request = try request.build(token: configuration.token,
organizationIdentifier: configuration.organizationIdentifier,
Expand All @@ -145,7 +145,7 @@ extension OpenAI {
}
}

func performSteamingRequest<ResultType: Codable>(request: any URLRequestBuildable, onResult: @escaping (Result<ResultType, Error>) -> Void, completion: ((Error?) -> Void)?) {
func performSteamingRequest<ResultType: Decodable>(request: any URLRequestBuildable, onResult: @escaping (Result<ResultType, Error>) -> Void, completion: ((Error?) -> Void)?) {
do {
let request = try request.build(token: configuration.token,
organizationIdentifier: configuration.organizationIdentifier,
Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenAI/Private/JSONRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import FoundationNetworking

final class JSONRequest<ResultType> {

let body: Codable?
let body: Encodable?
let url: URL
let method: String

init(body: Codable? = nil, url: URL, method: String = "POST") {
init(body: Encodable? = nil, url: URL, method: String = "POST") {
self.body = body
self.url = url
self.method = method
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAI/Private/StreamingSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import FoundationNetworking
#endif

final class StreamingSession<ResultType: Codable>: NSObject, Identifiable, URLSessionDelegate, URLSessionDataDelegate {
final class StreamingSession<ResultType: Decodable>: NSObject, Identifiable, URLSessionDelegate, URLSessionDataDelegate {

enum StreamingError: Error {
case unknownContent
Expand Down
6 changes: 3 additions & 3 deletions Sources/OpenAI/Public/Models/AudioSpeechQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import Foundation

/// Learn more: [OpenAI Speech – Documentation](https://platform.openai.com/docs/api-reference/audio/createSpeech)
public struct AudioSpeechQuery: Codable, Equatable {
public struct AudioSpeechQuery: Encodable, Equatable {

/// Encapsulates the voices available for audio generation.
///
/// To get aquinted with each of the voices and listen to the samples visit:
/// [OpenAI Text-to-Speech – Voice Options](https://platform.openai.com/docs/guides/text-to-speech/voice-options)
public enum AudioSpeechVoice: String, Codable, CaseIterable {
public enum AudioSpeechVoice: String, Encodable, CaseIterable {
case alloy
case echo
case fable
Expand All @@ -30,7 +30,7 @@ public struct AudioSpeechQuery: Codable, Equatable {
/// - opus
/// - aac
/// - flac
public enum AudioSpeechResponseFormat: String, Codable, CaseIterable {
public enum AudioSpeechResponseFormat: String, Encodable, CaseIterable {
case mp3
case opus
case aac
Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenAI/Public/Models/AudioTranscriptionQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

import Foundation

public enum AudioResponseFormat: String, Codable, Equatable {
public enum AudioResponseFormat: String, Encodable, Equatable {
case json
case text
case verboseJson = "verbose_json"
case srt
case vtt
}

public struct AudioTranscriptionQuery: Codable, Equatable {
public struct AudioTranscriptionQuery: Encodable, Equatable {
public typealias ResponseFormat = AudioResponseFormat

public let file: Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public struct AudioTranscriptionResult: Codable, Equatable {
public struct AudioTranscriptionResult: Decodable, Equatable {

public let text: String
}
2 changes: 1 addition & 1 deletion Sources/OpenAI/Public/Models/AudioTranslationQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public struct AudioTranslationQuery: Codable, Equatable {
public struct AudioTranslationQuery: Encodable, Equatable {
public typealias ResponseFormat = AudioResponseFormat

public let file: Data
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAI/Public/Models/AudioTranslationResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public struct AudioTranslationResult: Codable, Equatable {
public struct AudioTranslationResult: Decodable, Equatable {

public let text: String
}
30 changes: 17 additions & 13 deletions Sources/OpenAI/Public/Models/ChatQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,31 @@
import Foundation

// See more https://platform.openai.com/docs/guides/text-generation/json-mode
public struct ResponseFormat: Codable, Equatable {
public struct ResponseFormat: Encodable, Equatable {
public static let jsonObject = ResponseFormat(type: .jsonObject)
public static let text = ResponseFormat(type: .text)

public let type: Self.ResponseFormatType

public enum ResponseFormatType: String, Codable, Equatable {
public enum ResponseFormatType: String, Encodable, Equatable {
case jsonObject = "json_object"
case text
}
}

public struct Chat: Codable, Equatable {
extension Chat: Decodable {}
extension Chat.Role: Decodable {}
extension ChatFunctionCall: Decodable {}

public struct Chat: Encodable, Equatable {
public let role: Role
/// The contents of the message. `content` is required for all messages except assistant messages with function calls.
public let content: String?
/// The name of the author of this message. `name` is required if role is `function`, and it should be the name of the function whose response is in the `content`. May contain a-z, A-Z, 0-9, and underscores, with a maximum length of 64 characters.
public let name: String?
public let functionCall: ChatFunctionCall?

public enum Role: String, Codable, Equatable {
public enum Role: String, Encodable, Equatable {
case system
case assistant
case user
Expand Down Expand Up @@ -69,7 +73,7 @@ public struct Chat: Codable, Equatable {
}
}

public struct ChatFunctionCall: Codable, Equatable {
public struct ChatFunctionCall: Encodable, Equatable {
/// The name of the function to call.
public let name: String?
/// The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.
Expand All @@ -83,7 +87,7 @@ public struct ChatFunctionCall: Codable, Equatable {


/// See the [guide](/docs/guides/gpt/function-calling) for examples, and the [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for documentation about the format.
public struct JSONSchema: Codable, Equatable {
public struct JSONSchema: Encodable, Equatable {
public let type: JSONType
public let properties: [String: Property]?
public let required: [String]?
Expand All @@ -100,7 +104,7 @@ public struct JSONSchema: Codable, Equatable {
case multipleOf, minimum, maximum
}

public struct Property: Codable, Equatable {
public struct Property: Encodable, Equatable {
public let type: JSONType
public let description: String?
public let format: String?
Expand Down Expand Up @@ -141,7 +145,7 @@ public struct JSONSchema: Codable, Equatable {
}
}

public enum JSONType: String, Codable {
public enum JSONType: String, Encodable {
case integer = "integer"
case string = "string"
case boolean = "boolean"
Expand All @@ -151,7 +155,7 @@ public struct JSONSchema: Codable, Equatable {
case `null` = "null"
}

public struct Items: Codable, Equatable {
public struct Items: Encodable, Equatable {
public let type: JSONType
public let properties: [String: Property]?
public let pattern: String?
Expand Down Expand Up @@ -198,7 +202,7 @@ public struct JSONSchema: Codable, Equatable {
}
}

public struct ChatFunctionDeclaration: Codable, Equatable {
public struct ChatFunctionDeclaration: Encodable, Equatable {
/// The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
public let name: String

Expand All @@ -215,14 +219,14 @@ public struct ChatFunctionDeclaration: Codable, Equatable {
}
}

public struct ChatQueryFunctionCall: Codable, Equatable {
public struct ChatQueryFunctionCall: Encodable, Equatable {
/// The name of the function to call.
public let name: String?
/// The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.
public let arguments: String?
}

public struct ChatQuery: Equatable, Codable, Streamable {
public struct ChatQuery: Equatable, Encodable, Streamable {
/// ID of the model to use. Currently, only gpt-3.5-turbo and gpt-3.5-turbo-0301 are supported.
public let model: Model
/// An object specifying the format that the model must output.
Expand Down Expand Up @@ -254,7 +258,7 @@ public struct ChatQuery: Equatable, Codable, Streamable {

var stream: Bool = false

public enum FunctionCall: Codable, Equatable {
public enum FunctionCall: Encodable, Equatable {
case none
case auto
case function(String)
Expand Down
6 changes: 3 additions & 3 deletions Sources/OpenAI/Public/Models/ChatResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import Foundation

public struct ChatResult: Codable, Equatable {
public struct ChatResult: Decodable, Equatable {

public struct Choice: Codable, Equatable {
public struct Choice: Decodable, Equatable {

public let index: Int
/// Exists only if it is a complete message.
Expand All @@ -24,7 +24,7 @@ public struct ChatResult: Codable, Equatable {
}
}

public struct Usage: Codable, Equatable {
public struct Usage: Decodable, Equatable {
public let promptTokens: Int
public let completionTokens: Int
public let totalTokens: Int
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 @@ -7,10 +7,10 @@

import Foundation

public struct ChatStreamResult: Codable, Equatable {
public struct ChatStreamResult: Decodable, Equatable {

public struct Choice: Codable, Equatable {
public struct Delta: Codable, Equatable {
public struct Choice: Decodable, Equatable {
public struct Delta: Decodable, Equatable {
public let content: String?
public let role: Chat.Role?
/// The name of the author of this message. `name` is required if role is `function`, and it should be the name of the function whose response is in the `content`. May contain a-z, A-Z, 0-9, and underscores, with a maximum length of 64 characters.
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAI/Public/Models/CompletionsQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public struct CompletionsQuery: Codable, Streamable {
public struct CompletionsQuery: Encodable, Streamable {
/// ID of the model to use.
public let model: Model
/// The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.
Expand Down
6 changes: 3 additions & 3 deletions Sources/OpenAI/Public/Models/CompletionsResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import Foundation

public struct CompletionsResult: Codable, Equatable {
public struct CompletionsResult: Decodable, Equatable {

public struct Usage: Codable, Equatable {
public struct Usage: Decodable, Equatable {
public let promptTokens: Int
public let completionTokens: Int
public let totalTokens: Int
Expand All @@ -21,7 +21,7 @@ public struct CompletionsResult: Codable, Equatable {
}
}

public struct Choice: Codable, Equatable {
public struct Choice: Decodable, Equatable {
public let text: String
public let index: Int
public let finishReason: String?
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAI/Public/Models/EditsQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public struct EditsQuery: Codable {
public struct EditsQuery: Encodable {
/// ID of the model to use.
public let model: Model
/// Input text to get embeddings for.
Expand Down
6 changes: 3 additions & 3 deletions Sources/OpenAI/Public/Models/EditsResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

import Foundation

public struct EditsResult: Codable, Equatable {
public struct EditsResult: Decodable, Equatable {

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

public struct Usage: Codable, Equatable {
public struct Usage: Decodable, Equatable {
public let promptTokens: Int
public let completionTokens: Int
public let totalTokens: Int
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAI/Public/Models/EmbeddingsQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public struct EmbeddingsQuery: Codable {
public struct EmbeddingsQuery: Encodable {
/// ID of the model to use.
public let model: Model
/// Input text to get embeddings for.
Expand Down
6 changes: 3 additions & 3 deletions Sources/OpenAI/Public/Models/EmbeddingsResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

import Foundation

public struct EmbeddingsResult: Codable, Equatable {
public struct EmbeddingsResult: Decodable, Equatable {

public struct Embedding: Codable, Equatable {
public struct Embedding: Decodable, Equatable {
public let object: String
public let embedding: [Double]
public let index: Int
}

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

Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAI/Public/Models/ImageEditsQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public struct ImageEditsQuery: Codable {
public struct ImageEditsQuery: Encodable {
/// The image to edit. Must be a valid PNG file, less than 4MB, and square. If mask is not provided, image must have transparency, which will be used as the mask.
public let image: Data
public let fileName: String
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAI/Public/Models/ImageVariationsQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public struct ImageVariationsQuery: Codable {
public struct ImageVariationsQuery: Encodable {
/// The image to edit. Must be a valid PNG file, less than 4MB, and square.
public let image: Data
public let fileName: String
Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenAI/Public/Models/ImagesQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import Foundation


public enum ImageResponseFormat: String, Codable, Equatable {
public enum ImageResponseFormat: String, Encodable, Equatable {
case url
case b64_json
}

public struct ImagesQuery: Codable {
public struct ImagesQuery: Encodable {
public typealias ResponseFormat = ImageResponseFormat

/// A text description of the desired image(s). The maximum length is 1000 characters.
Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenAI/Public/Models/ImagesResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import Foundation

public struct ImagesResult: Codable, Equatable {
public struct ImagesResult: Decodable, Equatable {

public struct URLResult: Codable, Equatable {
public struct URLResult: Decodable, Equatable {
public let url: String?
public let b64_json: String?
}
Expand Down
Loading

0 comments on commit 5aa9b88

Please sign in to comment.