1
1
//
2
2
// ChatQuery.swift
3
- //
3
+ //
4
4
//
5
5
// Created by Sergii Kryvoblotskyi on 02/04/2023.
6
6
//
@@ -819,10 +819,12 @@ public struct ChatQuery: Equatable, Codable, Streamable, Sendable {
819
819
case text
820
820
case jsonObject
821
821
case jsonSchema( name: String , type: StructuredOutput . Type )
822
+ case dynamicJsonSchema( DynamicJSONSchema )
822
823
823
824
enum CodingKeys : String , CodingKey {
824
825
case type
825
826
case jsonSchema = " json_schema "
827
+ case dynamicJsonSchema
826
828
}
827
829
828
830
public func encode( to encoder: any Encoder ) throws {
@@ -836,6 +838,9 @@ public struct ChatQuery: Equatable, Codable, Streamable, Sendable {
836
838
try container. encode ( " json_schema " , forKey: . type)
837
839
let schema = JSONSchema ( name: name, schema: type. example)
838
840
try container. encode ( schema, forKey: . jsonSchema)
841
+ case . dynamicJsonSchema( let dynamicJSONSchema) :
842
+ try container. encode ( " json_schema " , forKey: . type)
843
+ try container. encode ( dynamicJSONSchema, forKey: . jsonSchema)
839
844
}
840
845
}
841
846
@@ -845,6 +850,8 @@ public struct ChatQuery: Equatable, Codable, Streamable, Sendable {
845
850
case ( . jsonObject, . jsonObject) : return true
846
851
case ( . jsonSchema( let lhsName, let lhsType) , . jsonSchema( let rhsName, let rhsType) ) :
847
852
return lhsName == rhsName && lhsType == rhsType
853
+ case ( . dynamicJsonSchema( let lhsSchema) , . dynamicJsonSchema( let rhsSchema) ) :
854
+ return lhsSchema == rhsSchema
848
855
default :
849
856
return false
850
857
}
@@ -1072,6 +1079,53 @@ public struct ChatQuery: Equatable, Codable, Streamable, Sendable {
1072
1079
}
1073
1080
}
1074
1081
}
1082
+
1083
+ public struct DynamicJSONSchema : Encodable , Sendable , Equatable {
1084
+ let name : String
1085
+ let description : String ?
1086
+ let schema : Encodable & Sendable
1087
+ let strict : Bool ?
1088
+
1089
+ enum CodingKeys : String , CodingKey {
1090
+ case name
1091
+ case description
1092
+ case schema
1093
+ case strict
1094
+ }
1095
+
1096
+ public init (
1097
+ name: String ,
1098
+ description: String ? = nil ,
1099
+ schema: Encodable & Sendable ,
1100
+ strict: Bool ? = nil
1101
+ ) {
1102
+ self . name = name
1103
+ self . description = description
1104
+ self . schema = schema
1105
+ self . strict = strict
1106
+ }
1107
+
1108
+ public func encode( to encoder: any Encoder ) throws {
1109
+ var container = encoder. container ( keyedBy: CodingKeys . self)
1110
+ try container. encode ( name, forKey: . name)
1111
+ if let description {
1112
+ try container. encode ( description, forKey: . description)
1113
+ }
1114
+ try container. encode ( schema, forKey: . schema)
1115
+ if let strict {
1116
+ try container. encode ( strict, forKey: . strict)
1117
+ }
1118
+ }
1119
+
1120
+ public static func == ( lhs: DynamicJSONSchema , rhs: DynamicJSONSchema ) -> Bool {
1121
+ guard lhs. name == rhs. name else { return false }
1122
+ guard lhs. description == rhs. description else { return false }
1123
+ guard lhs. strict == rhs. strict else { return false }
1124
+ let lhsData = try ? JSONEncoder ( ) . encode ( lhs. schema)
1125
+ let rhsData = try ? JSONEncoder ( ) . encode ( rhs. schema)
1126
+ return lhsData == rhsData
1127
+ }
1128
+ }
1075
1129
1076
1130
public enum ChatCompletionFunctionCallOptionParam : Codable , Equatable , Sendable {
1077
1131
case none
0 commit comments