@@ -139,4 +139,63 @@ public enum OpenAIRequest {
139139 let result = try JSONDecoder ( ) . decode ( T . self, from: text. data ( using: . utf8) !)
140140 return result
141141 }
142+
143+ struct StringPayload : Codable {
144+ let model : String
145+ let input : String
146+ let reasoning : Reasoning
147+ let instructions : String
148+ let text : TextObject
149+ struct Reasoning : Codable {
150+ let effort : String ?
151+ }
152+ struct TextObject : Codable {
153+ let format : Schema
154+ init ( ) {
155+ self . format = . init( )
156+ }
157+ struct Schema : Codable {
158+ var type : String
159+ init ( ) {
160+ self . type = " text "
161+ }
162+ }
163+ }
164+ }
165+
166+ public static func request( input: String , instructions: String , model: OpenAIModel , apiKey: String ) async throws -> String {
167+ let endpoint = URL ( string: " https://api.openai.com/v1/responses " ) !
168+ var req = URLRequest ( url: endpoint)
169+ req. httpMethod = " POST "
170+ req. addValue ( " application/json " , forHTTPHeaderField: " Content-Type " )
171+ req. addValue ( " Bearer \( apiKey) " , forHTTPHeaderField: " Authorization " )
172+ let payload : StringPayload
173+ if model. hasReasoning, case let OpenAIModel . o4_mini( reasoningEffort) = model {
174+ payload = StringPayload (
175+ model: model. modelName,
176+ input: input,
177+ reasoning: . init(
178+ effort: reasoningEffort. rawValue
179+ ) ,
180+ instructions: instructions,
181+ text: . init( )
182+ )
183+ } else {
184+ payload = StringPayload (
185+ model: model. modelName,
186+ input: input,
187+ reasoning: . init( effort: nil ) ,
188+ instructions: instructions,
189+ text: . init( )
190+ )
191+ }
192+ req. httpBody = try JSONEncoder ( ) . encode ( payload)
193+ req. timeoutInterval = 300
194+ let ( data, _) = try await URLSession . shared. data ( for: req)
195+ guard let decoded = try ? JSONDecoder ( ) . decode ( OpenAIResponse . self, from: data) else {
196+ throw OpenAIStructureError ( message: String ( data: data, encoding: . utf8) ?? " error " )
197+ }
198+ let text = decoded. output. last!. content!. last!. text!
199+ return text
200+ }
142201}
0 commit comments