Skip to content

Commit ba86acb

Browse files
committed
string request
1 parent d70da5a commit ba86acb

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

Sources/OpenAIStructure/OpenAIRequest.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Sources/OpenAIStructure/OpenAIResponse.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ struct TextContainer: Codable, Sendable {
6969
struct Format: Codable, Sendable {
7070
let type: String
7171
let description: String?
72-
let name: String
73-
let schema: AnyCodable
74-
let strict: Bool
72+
let name: String?
73+
let schema: AnyCodable?
74+
let strict: Bool?
7575
}
7676

7777
struct Usage: Codable {

Sources/OpenAIStructureClient/main.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,12 @@ do {
8484
} catch {
8585
print(error)
8686
}
87+
88+
print("-------text-------")
89+
90+
do {
91+
let result = try await OpenAIRequest.request(input: "Talk something about fruits.", instructions: "", model: .gpt4_1_nano, apiKey: "")
92+
print(result)
93+
} catch {
94+
print(error)
95+
}

0 commit comments

Comments
 (0)