Skip to content

Commit 1642db9

Browse files
committed
* Fixed type for Completions.Request.n.
* Removed redundant `Equatable` conformance for `Identifier` * Renamed `Utils` to `JSON`, improved parsing * Added proper `Codeable` support for `Prompt`
1 parent 01b87b4 commit 1642db9

File tree

5 files changed

+58
-6
lines changed

5 files changed

+58
-6
lines changed

Sources/OpenAIGPT3/Completions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension Completions {
1212
public let maxTokens: Int?
1313
public let temperature: Percentage?
1414
public let topP: Percentage?
15-
public let n: Percentage?
15+
public let n: Int?
1616
public let stream: Bool?
1717
public let logprobs: Int?
1818
public let echo: Bool?
@@ -30,7 +30,7 @@ extension Completions {
3030
maxTokens: Int? = nil,
3131
temperature: Percentage? = nil,
3232
topP: Percentage? = nil,
33-
n: Percentage? = nil,
33+
n: Int? = nil,
3434
stream: Bool? = nil,
3535
logprobs: Int? = nil,
3636
echo: Bool? = nil,

Sources/OpenAIGPT3/Identifier.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import Foundation
44

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

99
init(value: String)
File renamed without changes.

Sources/OpenAIGPT3/Prompt.swift

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import Foundation
22

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

1213
extension Prompt: ExpressibleByStringLiteral, ExpressibleByStringInterpolation {
@@ -15,3 +16,26 @@ extension Prompt: ExpressibleByStringLiteral, ExpressibleByStringInterpolation {
1516
self = .string(value)
1617
}
1718
}
19+
20+
extension Prompt: Codable {
21+
public init(from decoder: Decoder) throws {
22+
let container = try decoder.singleValueContainer()
23+
do {
24+
self = .string(try container.decode(String.self))
25+
return
26+
} catch {}
27+
28+
self = .strings(try container.decode([String].self))
29+
}
30+
31+
public func encode(to encoder: Encoder) throws {
32+
switch self {
33+
case .string(let value):
34+
var container = encoder.singleValueContainer()
35+
try container.encode(value)
36+
case .strings(let values):
37+
var container = encoder.singleValueContainer()
38+
try container.encode(values)
39+
}
40+
}
41+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import XCTest
2+
@testable import OpenAIGPT3
3+
4+
final class PromptTests: XCTestCase {
5+
6+
func testEncodeStringToJSON() throws {
7+
let prompt: Prompt = .string("alpha")
8+
let json = try jsonEncode(prompt)
9+
XCTAssertEqual("\"alpha\"", json)
10+
}
11+
12+
func testEncodeStringsToJSON() throws {
13+
let prompt: Prompt = .strings(["alpha","beta"])
14+
let json = try jsonEncode(prompt)
15+
XCTAssertEqual("[\"alpha\",\"beta\"]", json)
16+
}
17+
18+
func testDecodeStringFromJSON() throws {
19+
let result: Prompt = try jsonDecode("\"gamma\"")
20+
XCTAssertEqual(.string("gamma"), result)
21+
}
22+
23+
func testDecodeStringsFromJSON() throws {
24+
let result: Prompt = try jsonDecode("[\"gamma\",\"delta\"]")
25+
XCTAssertEqual(.strings(["gamma", "delta"]), result)
26+
}
27+
28+
}

0 commit comments

Comments
 (0)