Skip to content

Commit 20aaf77

Browse files
Debug optional
1 parent e6bb9a3 commit 20aaf77

File tree

6 files changed

+273
-201
lines changed

6 files changed

+273
-201
lines changed

Sources/OpenAI/AIProxy/AIProxyService.swift

+69-65
Large diffs are not rendered by default.

Sources/OpenAI/Azure/DefaultOpenAIAzureService.swift

+41-37
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,24 @@ final public class DefaultOpenAIAzureService: OpenAIService {
1212
public init(
1313
azureConfiguration: AzureOpenAIConfiguration,
1414
urlSessionConfiguration: URLSessionConfiguration = .default,
15-
decoder: JSONDecoder = .init())
15+
decoder: JSONDecoder = .init(),
16+
debugEnabled: Bool)
1617
{
1718
session = URLSession(configuration: urlSessionConfiguration)
1819
self.decoder = decoder
1920
AzureOpenAIAPI.azureOpenAIResource = azureConfiguration.resourceName
2021
apiKey = azureConfiguration.openAIAPIKey
2122
extraHeaders = azureConfiguration.extraHeaders
2223
initialQueryItems = [.init(name: "api-version", value: azureConfiguration.apiVersion)]
24+
self.debugEnabled = debugEnabled
2325
}
2426

2527
public let session: URLSession
2628
public let decoder: JSONDecoder
2729
private let apiKey: Authorization
2830
private let initialQueryItems: [URLQueryItem]
31+
/// Set this flag to TRUE if you need to print request events in DEBUG builds.
32+
private let debugEnabled: Bool
2933

3034
// Assistants API
3135
private let extraHeaders: [String: String]?
@@ -52,7 +56,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
5256
method: .post,
5357
params: chatParameters,
5458
queryItems: initialQueryItems)
55-
return try await fetch(type: ChatCompletionObject.self, with: request)
59+
return try await fetch(debugEnabled: debugEnabled, type: ChatCompletionObject.self, with: request)
5660
}
5761

5862
public func startStreamedChat(parameters: ChatCompletionParameters) async throws -> AsyncThrowingStream<ChatCompletionChunkObject, Error> {
@@ -66,7 +70,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
6670
params: chatParameters,
6771
queryItems: initialQueryItems
6872
)
69-
return try await fetchStream(type: ChatCompletionChunkObject.self, with: request)
73+
return try await fetchStream(debugEnabled: debugEnabled, type: ChatCompletionChunkObject.self, with: request)
7074
}
7175

7276
public func createEmbeddings(parameters: EmbeddingParameter) async throws -> OpenAIResponse<EmbeddingObject> {
@@ -155,7 +159,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
155159
betaHeaderField: Self.assistantsBetaV2,
156160
extraHeaders: extraHeaders
157161
)
158-
return try await fetch(type: AssistantObject.self, with: request)
162+
return try await fetch(debugEnabled: debugEnabled, type: AssistantObject.self, with: request)
159163
}
160164

161165
public func retrieveAssistant(id: String) async throws -> AssistantObject {
@@ -167,7 +171,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
167171
betaHeaderField: Self.assistantsBetaV2,
168172
extraHeaders: extraHeaders
169173
)
170-
return try await fetch(type: AssistantObject.self, with: request)
174+
return try await fetch(debugEnabled: debugEnabled, type: AssistantObject.self, with: request)
171175
}
172176

173177
public func modifyAssistant(id: String, parameters: AssistantParameters) async throws -> AssistantObject {
@@ -180,7 +184,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
180184
betaHeaderField: Self.assistantsBetaV2,
181185
extraHeaders: extraHeaders
182186
)
183-
return try await fetch(type: AssistantObject.self, with: request)
187+
return try await fetch(debugEnabled: debugEnabled, type: AssistantObject.self, with: request)
184188
}
185189

186190
public func deleteAssistant(id: String) async throws -> DeletionStatus {
@@ -192,7 +196,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
192196
betaHeaderField: Self.assistantsBetaV2,
193197
extraHeaders: extraHeaders
194198
)
195-
return try await fetch(type: DeletionStatus.self, with: request)
199+
return try await fetch(debugEnabled: debugEnabled, type: DeletionStatus.self, with: request)
196200
}
197201

198202
public func listAssistants(limit: Int?, order: String?, after: String?, before: String?) async throws -> OpenAIResponse<AssistantObject> {
@@ -217,7 +221,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
217221
betaHeaderField: Self.assistantsBetaV2,
218222
extraHeaders: extraHeaders
219223
)
220-
return try await fetch(type: OpenAIResponse<AssistantObject>.self, with: request)
224+
return try await fetch(debugEnabled: debugEnabled, type: OpenAIResponse<AssistantObject>.self, with: request)
221225
}
222226

223227
public func createThread(parameters: CreateThreadParameters) async throws -> ThreadObject {
@@ -229,7 +233,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
229233
queryItems: initialQueryItems,
230234
betaHeaderField: Self.assistantsBetaV2,
231235
extraHeaders: extraHeaders)
232-
return try await fetch(type: ThreadObject.self, with: request)
236+
return try await fetch(debugEnabled: debugEnabled, type: ThreadObject.self, with: request)
233237
}
234238

235239
public func retrieveThread(id: String) async throws -> ThreadObject {
@@ -240,7 +244,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
240244
queryItems: initialQueryItems,
241245
betaHeaderField: Self.assistantsBetaV2,
242246
extraHeaders: extraHeaders)
243-
return try await fetch(type: ThreadObject.self, with: request)
247+
return try await fetch(debugEnabled: debugEnabled, type: ThreadObject.self, with: request)
244248
}
245249

246250
public func modifyThread(id: String, parameters: ModifyThreadParameters) async throws -> ThreadObject {
@@ -252,7 +256,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
252256
queryItems: initialQueryItems,
253257
betaHeaderField: Self.assistantsBetaV2,
254258
extraHeaders: extraHeaders)
255-
return try await fetch(type: ThreadObject.self, with: request)
259+
return try await fetch(debugEnabled: debugEnabled, type: ThreadObject.self, with: request)
256260
}
257261

258262
public func deleteThread(id: String) async throws -> DeletionStatus {
@@ -263,7 +267,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
263267
queryItems: initialQueryItems,
264268
betaHeaderField: Self.assistantsBetaV2,
265269
extraHeaders: extraHeaders)
266-
return try await fetch(type: DeletionStatus.self, with: request)
270+
return try await fetch(debugEnabled: debugEnabled, type: DeletionStatus.self, with: request)
267271
}
268272

269273
public func createMessage(threadID: String, parameters: MessageParameter) async throws -> MessageObject {
@@ -275,7 +279,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
275279
queryItems: initialQueryItems,
276280
betaHeaderField: Self.assistantsBetaV2,
277281
extraHeaders: extraHeaders)
278-
return try await fetch(type: MessageObject.self, with: request)
282+
return try await fetch(debugEnabled: debugEnabled, type: MessageObject.self, with: request)
279283
}
280284

281285
public func retrieveMessage(threadID: String, messageID: String) async throws -> MessageObject {
@@ -286,7 +290,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
286290
queryItems: initialQueryItems,
287291
betaHeaderField: Self.assistantsBetaV2,
288292
extraHeaders: extraHeaders)
289-
return try await fetch(type: MessageObject.self, with: request)
293+
return try await fetch(debugEnabled: debugEnabled, type: MessageObject.self, with: request)
290294
}
291295

292296
public func modifyMessage(threadID: String, messageID: String, parameters: ModifyMessageParameters) async throws -> MessageObject {
@@ -298,7 +302,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
298302
queryItems: initialQueryItems,
299303
betaHeaderField: Self.assistantsBetaV2,
300304
extraHeaders: extraHeaders)
301-
return try await fetch(type: MessageObject.self, with: request)
305+
return try await fetch(debugEnabled: debugEnabled, type: MessageObject.self, with: request)
302306
}
303307

304308
public func listMessages(threadID: String, limit: Int?, order: String?, after: String?, before: String?, runID: String?) async throws -> OpenAIResponse<MessageObject> {
@@ -325,7 +329,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
325329
queryItems: queryItems,
326330
betaHeaderField: Self.assistantsBetaV2,
327331
extraHeaders: extraHeaders)
328-
return try await fetch(type: OpenAIResponse<MessageObject>.self, with: request)
332+
return try await fetch(debugEnabled: debugEnabled, type: OpenAIResponse<MessageObject>.self, with: request)
329333
}
330334

331335
public func createRun(threadID: String, parameters: RunParameter) async throws -> RunObject {
@@ -337,7 +341,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
337341
queryItems: initialQueryItems,
338342
betaHeaderField: Self.assistantsBetaV2,
339343
extraHeaders: extraHeaders)
340-
return try await fetch(type: RunObject.self, with: request)
344+
return try await fetch(debugEnabled: debugEnabled, type: RunObject.self, with: request)
341345
}
342346

343347
public func retrieveRun(threadID: String, runID: String) async throws -> RunObject {
@@ -348,7 +352,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
348352
queryItems: initialQueryItems,
349353
betaHeaderField: Self.assistantsBetaV2,
350354
extraHeaders: extraHeaders)
351-
return try await fetch(type: RunObject.self, with: request)
355+
return try await fetch(debugEnabled: debugEnabled, type: RunObject.self, with: request)
352356
}
353357

354358
public func modifyRun(threadID: String, runID: String, parameters: ModifyRunParameters) async throws -> RunObject {
@@ -360,7 +364,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
360364
queryItems: initialQueryItems,
361365
betaHeaderField: Self.assistantsBetaV2,
362366
extraHeaders: extraHeaders)
363-
return try await fetch(type: RunObject.self, with: request)
367+
return try await fetch(debugEnabled: debugEnabled, type: RunObject.self, with: request)
364368
}
365369

366370
public func listRuns(threadID: String, limit: Int?, order: String?, after: String?, before: String?) async throws -> OpenAIResponse<RunObject> {
@@ -384,7 +388,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
384388
queryItems: queryItems,
385389
betaHeaderField: Self.assistantsBetaV2,
386390
extraHeaders: extraHeaders)
387-
return try await fetch(type: OpenAIResponse<RunObject>.self, with: request)
391+
return try await fetch(debugEnabled: debugEnabled, type: OpenAIResponse<RunObject>.self, with: request)
388392
}
389393

390394
public func cancelRun(threadID: String, runID: String) async throws -> RunObject {
@@ -395,7 +399,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
395399
queryItems: initialQueryItems,
396400
betaHeaderField: Self.assistantsBetaV2,
397401
extraHeaders: extraHeaders)
398-
return try await fetch(type: RunObject.self, with: request)
402+
return try await fetch(debugEnabled: debugEnabled, type: RunObject.self, with: request)
399403
}
400404

401405
public func submitToolOutputsToRun(threadID: String, runID: String, parameters: RunToolsOutputParameter) async throws -> RunObject {
@@ -407,7 +411,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
407411
queryItems: initialQueryItems,
408412
betaHeaderField: Self.assistantsBetaV2,
409413
extraHeaders: extraHeaders)
410-
return try await fetch(type: RunObject.self, with: request)
414+
return try await fetch(debugEnabled: debugEnabled, type: RunObject.self, with: request)
411415
}
412416

413417
public func createThreadAndRun(parameters: CreateThreadAndRunParameter) async throws -> RunObject {
@@ -419,7 +423,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
419423
queryItems: initialQueryItems,
420424
betaHeaderField: Self.assistantsBetaV2,
421425
extraHeaders: extraHeaders)
422-
return try await fetch(type: RunObject.self, with: request)
426+
return try await fetch(debugEnabled: debugEnabled, type: RunObject.self, with: request)
423427
}
424428

425429
public func retrieveRunstep(threadID: String, runID: String, stepID: String) async throws -> RunStepObject {
@@ -430,7 +434,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
430434
queryItems: initialQueryItems,
431435
betaHeaderField: Self.assistantsBetaV2,
432436
extraHeaders: extraHeaders)
433-
return try await fetch(type: RunStepObject.self, with: request)
437+
return try await fetch(debugEnabled: debugEnabled, type: RunStepObject.self, with: request)
434438
}
435439

436440
public func listRunSteps(threadID: String, runID: String, limit: Int?, order: String?, after: String?, before: String?) async throws -> OpenAIResponse<RunStepObject> {
@@ -454,7 +458,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
454458
queryItems: queryItems,
455459
betaHeaderField: Self.assistantsBetaV2,
456460
extraHeaders: extraHeaders)
457-
return try await fetch(type: OpenAIResponse<RunStepObject>.self, with: request)
461+
return try await fetch(debugEnabled: debugEnabled, type: OpenAIResponse<RunStepObject>.self, with: request)
458462
}
459463

460464
public func createThreadAndRunStream(
@@ -471,7 +475,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
471475
queryItems: initialQueryItems,
472476
betaHeaderField: Self.assistantsBetaV2,
473477
extraHeaders: extraHeaders)
474-
return try await fetchAssistantStreamEvents(with: request)
478+
return try await fetchAssistantStreamEvents(with: request, debugEnabled: debugEnabled)
475479
}
476480

477481
public func createRunStream(
@@ -489,7 +493,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
489493
queryItems: initialQueryItems,
490494
betaHeaderField: Self.assistantsBetaV2,
491495
extraHeaders: extraHeaders)
492-
return try await fetchAssistantStreamEvents(with: request)
496+
return try await fetchAssistantStreamEvents(with: request, debugEnabled: debugEnabled)
493497
}
494498

495499
public func submitToolOutputsToRunStream(
@@ -508,7 +512,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
508512
queryItems: initialQueryItems,
509513
betaHeaderField: Self.assistantsBetaV2,
510514
extraHeaders: extraHeaders)
511-
return try await fetchAssistantStreamEvents(with: request)
515+
return try await fetchAssistantStreamEvents(with: request, debugEnabled: debugEnabled)
512516
}
513517

514518
// MARK: Batch
@@ -555,7 +559,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
555559
queryItems: initialQueryItems,
556560
betaHeaderField: Self.assistantsBetaV2,
557561
extraHeaders: extraHeaders)
558-
return try await fetch(type: VectorStoreObject.self, with: request)
562+
return try await fetch(debugEnabled: debugEnabled, type: VectorStoreObject.self, with: request)
559563
}
560564

561565
public func listVectorStores(
@@ -585,7 +589,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
585589
queryItems: queryItems,
586590
betaHeaderField: Self.assistantsBetaV2,
587591
extraHeaders: extraHeaders)
588-
return try await fetch(type: OpenAIResponse<VectorStoreObject>.self, with: request)
592+
return try await fetch(debugEnabled: debugEnabled, type: OpenAIResponse<VectorStoreObject>.self, with: request)
589593
}
590594

591595
public func retrieveVectorStore(
@@ -599,7 +603,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
599603
queryItems: initialQueryItems,
600604
betaHeaderField: Self.assistantsBetaV2,
601605
extraHeaders: extraHeaders)
602-
return try await fetch(type: VectorStoreObject.self, with: request)
606+
return try await fetch(debugEnabled: debugEnabled, type: VectorStoreObject.self, with: request)
603607
}
604608

605609
public func modifyVectorStore(
@@ -615,7 +619,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
615619
queryItems: initialQueryItems,
616620
betaHeaderField: Self.assistantsBetaV2,
617621
extraHeaders: extraHeaders)
618-
return try await fetch(type: VectorStoreObject.self, with: request)
622+
return try await fetch(debugEnabled: debugEnabled, type: VectorStoreObject.self, with: request)
619623
}
620624

621625
public func deleteVectorStore(
@@ -629,7 +633,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
629633
queryItems: initialQueryItems,
630634
betaHeaderField: Self.assistantsBetaV2,
631635
extraHeaders: extraHeaders)
632-
return try await fetch(type: DeletionStatus.self, with: request)
636+
return try await fetch(debugEnabled: debugEnabled, type: DeletionStatus.self, with: request)
633637
}
634638

635639
// MARK: Vector Store Files
@@ -643,7 +647,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
643647
queryItems: initialQueryItems,
644648
betaHeaderField: Self.assistantsBetaV2,
645649
extraHeaders: extraHeaders)
646-
return try await fetch(type: VectorStoreFileObject.self, with: request)
650+
return try await fetch(debugEnabled: debugEnabled, type: VectorStoreFileObject.self, with: request)
647651
}
648652

649653
public func listVectorStoreFiles(vectorStoreID: String, limit: Int?, order: String?, after: String?, before: String?, filter: String?) async throws -> OpenAIResponse<VectorStoreFileObject> {
@@ -670,7 +674,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
670674
queryItems: queryItems,
671675
betaHeaderField: Self.assistantsBetaV2,
672676
extraHeaders: extraHeaders)
673-
return try await fetch(type: OpenAIResponse<VectorStoreFileObject>.self, with: request)
677+
return try await fetch(debugEnabled: debugEnabled, type: OpenAIResponse<VectorStoreFileObject>.self, with: request)
674678
}
675679

676680
public func retrieveVectorStoreFile(vectorStoreID: String, fileID: String) async throws -> VectorStoreFileObject {
@@ -681,7 +685,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
681685
queryItems: initialQueryItems,
682686
betaHeaderField: Self.assistantsBetaV2,
683687
extraHeaders: extraHeaders)
684-
return try await fetch(type: VectorStoreFileObject.self, with: request)
688+
return try await fetch(debugEnabled: debugEnabled, type: VectorStoreFileObject.self, with: request)
685689
}
686690

687691
public func deleteVectorStoreFile(vectorStoreID: String, fileID: String) async throws -> DeletionStatus {
@@ -692,7 +696,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
692696
queryItems: initialQueryItems,
693697
betaHeaderField: Self.assistantsBetaV2,
694698
extraHeaders: extraHeaders)
695-
return try await fetch(type: DeletionStatus.self, with: request)
699+
return try await fetch(debugEnabled: debugEnabled, type: DeletionStatus.self, with: request)
696700
}
697701

698702
public func createVectorStoreFileBatch(vectorStoreID: String, parameters: VectorStoreFileBatchParameter) async throws -> VectorStoreFileBatchObject {

0 commit comments

Comments
 (0)