-
Notifications
You must be signed in to change notification settings - Fork 4
/
ChatModel.swift
35 lines (30 loc) · 968 Bytes
/
ChatModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public enum ChatModel: Codable {
case gpt35Turbo
case gpt4
case specific(String)
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let modelString = try container.decode(String.self)
switch modelString {
case _ where modelString.starts(with: "gpt-3.5"):
self = .gpt35Turbo
case _ where modelString.starts(with: "gpt-4"):
self = .gpt4
default:
self = .specific(modelString)
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
let modelString: String
switch self {
case .gpt35Turbo:
modelString = "gpt-3.5-turbo"
case .gpt4:
modelString = "gpt-4"
case .specific(let specificString):
modelString = specificString
}
try container.encode(modelString)
}
}