Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 2 additions & 38 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

142 changes: 142 additions & 0 deletions Sources/ExyteOpenAI/Endpoint Configurations/Audio.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//
// Audio.swift
//
// Copyright (c) 2024 Exyte
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation

enum Audio {
case createTranscription(payload: CreateTranscriptionPayload)
case createTranslation(payload: CreateTranslationPayload)
case createSpeech(payload: CreateSpeechPayload, destination: URL)
}

extension Audio: EndpointConfiguration {

var method: HTTPRequestMethod {
return .post
}

var path: String {
switch self {
case .createTranscription:
return "/audio/transcriptions"
case .createTranslation:
return "/audio/translations"
case .createSpeech:
return "/audio/speech"
}
}

var task: RequestTask {
switch self {
case .createTranscription(let payload):
var data: [FormBodyPart] = [
FormBodyPart(
name: "file",
value: .fileURL(payload.file),
fileName: payload.file.lastPathComponent,
mimeType: payload.file.pathExtension
),
FormBodyPart(
name: "model",
value: .plainText(payload.model.rawValue)
),
FormBodyPart(
name: "response_format",
value: .plainText(payload.responseFormat?.rawValue ?? TextResponseFormat.json.rawValue)
)
]
if let temperature = payload.temperature {
data.append(
FormBodyPart(
name: "temperature",
value: .floatingPoint(temperature)
)
)
}
if let prompt = payload.prompt {
data.append(
FormBodyPart(
name: "prompt",
value: .plainText(prompt)
)
)
}
if let language = payload.language {
data.append(
FormBodyPart(
name: "language",
value: .plainText(language))
)
}
if let timestampGranularities = payload.timestampGranularities,
payload.responseFormat == .verboseJson {
let timestampGranularitiesData = withUnsafeBytes(of: timestampGranularities) { Data($0) }
data.append(
FormBodyPart(
name: "timestamp_granularities",
value: .data(timestampGranularitiesData)
)
)
}
return .uploadMultipart(data)
case .createTranslation(let payload):
var data: [FormBodyPart] = [
FormBodyPart(
name: "file",
value: .fileURL(payload.file),
fileName: payload.file.lastPathComponent,
mimeType: payload.file.pathExtension
),
FormBodyPart(
name: "model",
value: .plainText(payload.model.rawValue)
),
FormBodyPart(
name: "response_format",
value: .plainText(payload.responseFormat?.rawValue ?? TextResponseFormat.json.rawValue)
)
]
if let prompt = payload.prompt {
data.append(
FormBodyPart(
name: "prompt",
value: .plainText(prompt)
)
)
}
if let temperature = payload.temperature {
data.append(
FormBodyPart(
name: "temperature",
value: .floatingPoint(temperature)
)
)
}
return .uploadMultipart(data)
case .createSpeech(let payload, let destination):
return .download(payload, destination)
}
}

}
2 changes: 1 addition & 1 deletion Sources/ExyteOpenAI/Endpoint Configurations/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ extension Files: EndpointConfiguration {
case .listFiles, .retrieveFile, .deleteFile:
return .plain
case .retrieveFileContent(_, let destination):
return .download(destination)
return .download(nil, destination)
}
}

Expand Down
49 changes: 49 additions & 0 deletions Sources/ExyteOpenAI/Models/Audio/Transcription.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Transcription.swift
//
// Copyright (c) 2024 Exyte
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation

public struct Transcription: Codable {

let text: String
let language: String?
let duration: Double?
let words: String?
let segments: [TranscriptionSegment]?

public init(
text: String,
language: String? = nil,
duration: Double? = nil,
words: String? = nil,
segments: [TranscriptionSegment]? = nil
) {
self.text = text
self.language = language
self.duration = duration
self.words = words
self.segments = segments
}

}
40 changes: 40 additions & 0 deletions Sources/ExyteOpenAI/Models/Audio/TranscriptionSegment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// TranscriptionSegment.swift
//
// Copyright (c) 2024 Exyte
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation

public struct TranscriptionSegment: Codable {

let id: Int
let seek: Int
let start: Double
let end: Double
let text: String
let tokens: [Int]
let temperature: Double
let avgLogprob: Double?
let compressionRatio: Double?
let noSpeechProb: Double?

}
35 changes: 35 additions & 0 deletions Sources/ExyteOpenAI/Models/Audio/Translation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Translation.swift
//
// Copyright (c) 2024 Exyte
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation

public struct Translation: Codable {

let text: String

public init(text: String) {
self.text = text
}

}
36 changes: 36 additions & 0 deletions Sources/ExyteOpenAI/Models/Enums/AudioResponseFormat.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// AudioResponseFormat.swift
//
// Copyright (c) 2024 Exyte
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation

public enum AudioResponseFormat: String, Codable {

case mp3 = "mp3"
case opus = "opus"
case aac = "aac"
case flac = "flac"
case wav = "wav"
case pcm = "pcm"

}
29 changes: 29 additions & 0 deletions Sources/ExyteOpenAI/Models/Enums/STTModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// STTModel.swift
//
// Copyright (c) 2024 Exyte
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation

public enum STTModel: String, Codable {
case whisper1 = "whisper-1"
}
Loading