From be88f6a1681f6aa7aa00538e3b677fdb442c7bfe Mon Sep 17 00:00:00 2001 From: Frank Buss Date: Fri, 8 Nov 2024 21:33:57 +0100 Subject: [PATCH] added default inits for the new AudioTranscriptionResult and added a test case --- .../Models/AudioTranscriptionResult.swift | 16 ++++++++- Tests/OpenAITests/OpenAITests.swift | 33 ++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/Sources/OpenAI/Public/Models/AudioTranscriptionResult.swift b/Sources/OpenAI/Public/Models/AudioTranscriptionResult.swift index bfdda768..d33d02e2 100644 --- a/Sources/OpenAI/Public/Models/AudioTranscriptionResult.swift +++ b/Sources/OpenAI/Public/Models/AudioTranscriptionResult.swift @@ -18,7 +18,21 @@ public struct AudioTranscriptionResult: Codable, Equatable { public let text: String /// The segments containing detailed information (only present in verbose_json format) public let segments: [Segment]? - + + public init( + task: String? = nil, + language: String? = nil, + duration: Double? = nil, + text: String, + segments: [Segment]? = nil + ) { + self.task = task + self.language = language + self.duration = duration + self.text = text + self.segments = segments + } + public struct Segment: Codable, Equatable { public let id: Int public let seek: Int diff --git a/Tests/OpenAITests/OpenAITests.swift b/Tests/OpenAITests/OpenAITests.swift index bae3e015..1f28727b 100644 --- a/Tests/OpenAITests/OpenAITests.swift +++ b/Tests/OpenAITests/OpenAITests.swift @@ -320,7 +320,38 @@ class OpenAITests: XCTestCase { let result = try await openAI.audioTranscriptions(query: query) XCTAssertEqual(result, transcriptionResult) } - + + func testVerboseJsonAudioTranscriptions() async throws { + let data = Data() + let query = AudioTranscriptionQuery(file: data, fileType: .m4a, model: .whisper_1, responseFormat: .verboseJson) + + let transcriptionResult = AudioTranscriptionResult( + task: "transcribe", + language: "english", + duration: 3.759999990463257, + text: "This is a test.", + segments: [ + AudioTranscriptionResult.Segment( + id: 0, + seek: 0, + start: 0, + end: 3.759999990463257, + text: " This is a test.", + tokens: [50364, 639, 307, 257, 1500, 13, 50552], + temperature: 0, + avg_logprob: -0.5153926610946655, + compression_ratio: 0.7142857313156128, + no_speech_prob: 0.08552933484315872 + ) + ] + ) + + try self.stub(result: transcriptionResult) + + let result = try await openAI.audioTranscriptions(query: query) + XCTAssertEqual(result, transcriptionResult) + } + func testAudioTranscriptionsError() async throws { let data = Data() let query = AudioTranscriptionQuery(file: data, fileType: .m4a, model: .whisper_1)