@@ -9,19 +9,88 @@ import Foundation
99
1010// MARK: - Data structures
1111
12- public typealias BedrockModelIdentifier = String
12+ // model enum
13+ public struct BedrockModel : RawRepresentable , Equatable , Hashable {
14+ public var rawValue : String
15+
16+ public init ( rawValue: String ) {
17+ self . rawValue = rawValue
18+ }
19+ }
1320
21+ // Anthropic
22+ public extension BedrockModel {
23+ static var instant : BedrockModel { . init( rawValue: " anthropic.claude-instant-v1 " ) }
24+ static var claudev1 : BedrockModel { . init( rawValue: " anthropic.claude-v1 " ) }
25+ static var claudev2 : BedrockModel { . init( rawValue: " anthropic.claude-v2 " ) }
26+ static var claudev2_1 : BedrockModel { . init( rawValue: " anthropic.claude-v2:1 " ) }
27+ func isAnthropic( ) -> Bool {
28+ switch self {
29+ case . instant, . claudev1, . claudev2, . claudev2_1: return true
30+ default : return false
31+ }
32+ }
33+ }
1434
15- //struct AnyStringKey: CodingKey, Hashable, ExpressibleByStringLiteral {
16- // var stringValue: String
17- // init(stringValue: String) { self.stringValue = stringValue }
18- // init(_ stringValue: String) { self.init(stringValue: stringValue) }
19- // // the three lines below are not used, they are there to comply to `CodingKey` protocol
20- // var intValue: Int?
21- // init?(intValue: Int) { nil }
22- // init(stringLiteral value: String) { self.init(value) }
35+ // Meta
36+ public extension BedrockModel {
37+ static var llama2_13b : BedrockModel { . init( rawValue: " meta.llama2.13b " ) }
38+ static var llama2_70b : BedrockModel { . init( rawValue: " meta.llama2.70b " ) }
39+ }
40+
41+ public extension BedrockModel {
42+ init ? ( from: String ? ) {
43+ guard let model = from else {
44+ return nil
45+ }
46+ self . init ( rawValue: model)
47+ switch self {
48+ case . instant,
49+ . claudev1,
50+ . claudev2,
51+ . claudev2_1,
52+ . llama2_13b: return
53+ default : return nil
54+ }
55+ }
56+ }
57+
58+ //public enum BedrockModel: Hashable {
59+ // case anthropicModel(AnthropicModel)
60+ // case metaModel(MetaModel)
61+ //
62+ // public func id() -> BedrockModelIdentifier {
63+ // switch self {
64+ // case .anthropicModel(let anthropic): return anthropic.rawValue
65+ // case .metaModel(let meta): return meta.rawValue
66+ // }
67+ // }
2368//}
2469
70+ public protocol BedrockResponse : Decodable {
71+ init ( from data: Data ) throws
72+ }
73+
74+ public extension BedrockResponse {
75+ static func decode< T: Decodable > ( _ data: Data ) throws -> T {
76+ let decoder = JSONDecoder ( )
77+ return try decoder. decode ( T . self, from: data)
78+ }
79+ static func decode< T: Decodable > ( json: String ) throws -> T {
80+ let data = json. data ( using: . utf8) !
81+ return try self . decode ( data)
82+ }
83+ }
2584
85+ public protocol BedrockRequest : Encodable {
86+ func encode( ) throws -> Data
87+ }
2688
89+ public extension BedrockRequest {
90+ func encode( ) throws -> Data {
91+ let encoder = JSONEncoder ( )
92+ encoder. keyEncodingStrategy = . convertToSnakeCase
93+ return try encoder. encode ( self )
94+ }
95+ }
2796
0 commit comments