Skip to content

Commit

Permalink
Add translation and transcription endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitabumblebee committed Aug 21, 2024
1 parent 3a83729 commit 1547a1e
Show file tree
Hide file tree
Showing 18 changed files with 712 additions and 38 deletions.
40 changes: 2 additions & 38 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,48 +1,12 @@
{
"pins" : [
{
"identity" : "alamofire",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Alamofire/Alamofire.git",
"state" : {
"revision" : "f455c2975872ccd2d9c81594c658af65716e9b9a",
"version" : "5.9.1"
}
},
{
"identity" : "eventsourcehttpbody",
"kind" : "remoteSourceControl",
"location" : "https://github.com/exyte/EventSourceHttpBody.git",
"state" : {
"revision" : "9b68240460bae09faa0c5a9afbbccf5e18890e0c",
"version" : "0.1.3"
}
},
{
"identity" : "moya",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Moya/Moya.git",
"state" : {
"revision" : "c263811c1f3dbf002be9bd83107f7cdc38992b26",
"version" : "15.0.3"
}
},
{
"identity" : "reactiveswift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ReactiveCocoa/ReactiveSwift.git",
"state" : {
"revision" : "c43bae3dac73fdd3cb906bd5a1914686ca71ed3c",
"version" : "6.7.0"
}
},
{
"identity" : "rxswift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ReactiveX/RxSwift.git",
"state" : {
"revision" : "b06a8c8596e4c3e8e7788e08e720e3248563ce6a",
"version" : "6.7.1"
"revision" : "b000e62b83206dd6e00f2066cf08c96f232a4168",
"version" : "0.1.5"
}
}
],
Expand Down
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 createTranscriptionPayload(payload: CreateTranscriptionPayload)
case createTranslationPayload(payload: CreateTranslationPayload)
case createSpeechPayload(payload: CreateSpeechPayload, destination: URL)
}

extension Audio: EndpointConfiguration {

var method: HTTPRequestMethod {
return .post
}

var path: String {
switch self {
case .createTranscriptionPayload:
return "/audio/transcriptions"
case .createTranslationPayload:
return "/audio/translations"
case .createSpeechPayload:
return "/audio/speech"
}
}

var task: RequestTask {
switch self {
case .createTranscriptionPayload(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(Float(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 .createTranslationPayload(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(Float(temperature))
)
)
}
return .uploadMultipart(data)
case .createSpeechPayload(let payload, let destination):
return .download(destination)
}
}

}
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

0 comments on commit 1547a1e

Please sign in to comment.