Skip to content

Commit

Permalink
* Fixed type for Completions.Request.n.
Browse files Browse the repository at this point in the history
* Removed redundant `Equatable` conformance for `Identifier`
* Renamed `Utils` to `JSON`, improved parsing
* Added proper `Codeable` support for `Prompt`
  • Loading branch information
randomeizer committed Jul 17, 2022
1 parent 01b87b4 commit 1642db9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Sources/OpenAIGPT3/Completions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension Completions {
public let maxTokens: Int?
public let temperature: Percentage?
public let topP: Percentage?
public let n: Percentage?
public let n: Int?
public let stream: Bool?
public let logprobs: Int?
public let echo: Bool?
Expand All @@ -30,7 +30,7 @@ extension Completions {
maxTokens: Int? = nil,
temperature: Percentage? = nil,
topP: Percentage? = nil,
n: Percentage? = nil,
n: Int? = nil,
stream: Bool? = nil,
logprobs: Int? = nil,
echo: Bool? = nil,
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAIGPT3/Identifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Foundation

/// Describes a value intended to be a single ``String`` that is ``Codable`` and can be created via a hard-coded ``String``
public protocol Identifier: Hashable, Equatable, Codable, ExpressibleByStringLiteral, CustomStringConvertible {
public protocol Identifier: Hashable, Codable, ExpressibleByStringLiteral, CustomStringConvertible {
var value: String { get }

init(value: String)
Expand Down
File renamed without changes.
30 changes: 27 additions & 3 deletions Sources/OpenAIGPT3/Prompt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import Foundation

/// A ``Prompt`` can be a single ``String``, an array of ``String``s, a ``Token`` array, or an array of ``Token`` arrays.
/// You can also assign it directly with ``String`` literal value, which will result in a ``Prompt/string(_:)`` value.
public enum Prompt: Equatable, Codable {
public enum Prompt: Equatable {
case string(String)
case strings([String])
case tokenArray([Token])
case tokenArrays([[Token]])
// TODO: Figure out tokens.
// case tokenArray([Token])
// case tokenArrays([[Token]])
}

extension Prompt: ExpressibleByStringLiteral, ExpressibleByStringInterpolation {
Expand All @@ -15,3 +16,26 @@ extension Prompt: ExpressibleByStringLiteral, ExpressibleByStringInterpolation {
self = .string(value)
}
}

extension Prompt: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
do {
self = .string(try container.decode(String.self))
return
} catch {}

self = .strings(try container.decode([String].self))
}

public func encode(to encoder: Encoder) throws {
switch self {
case .string(let value):
var container = encoder.singleValueContainer()
try container.encode(value)
case .strings(let values):
var container = encoder.singleValueContainer()
try container.encode(values)
}
}
}
28 changes: 28 additions & 0 deletions Tests/OpenAIGPT3Tests/PromptTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import XCTest
@testable import OpenAIGPT3

final class PromptTests: XCTestCase {

func testEncodeStringToJSON() throws {
let prompt: Prompt = .string("alpha")
let json = try jsonEncode(prompt)
XCTAssertEqual("\"alpha\"", json)
}

func testEncodeStringsToJSON() throws {
let prompt: Prompt = .strings(["alpha","beta"])
let json = try jsonEncode(prompt)
XCTAssertEqual("[\"alpha\",\"beta\"]", json)
}

func testDecodeStringFromJSON() throws {
let result: Prompt = try jsonDecode("\"gamma\"")
XCTAssertEqual(.string("gamma"), result)
}

func testDecodeStringsFromJSON() throws {
let result: Prompt = try jsonDecode("[\"gamma\",\"delta\"]")
XCTAssertEqual(.strings(["gamma", "delta"]), result)
}

}

0 comments on commit 1642db9

Please sign in to comment.