Skip to content

Commit

Permalink
Merge branch 'main' into MacPaw.missing_enums
Browse files Browse the repository at this point in the history
  • Loading branch information
ingvarus-bc authored Feb 7, 2024
2 parents bd6c420 + 35afc9a commit aea5766
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 64 deletions.
65 changes: 65 additions & 0 deletions Demo/DemoChat/Sources/Extensions/View.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// View.swift
//
//
// Created by James J Kalafus on 2024-02-03.
//

import SwiftUI

extension View {

@inlinable public func navigationTitle(_ titleKey: LocalizedStringKey, selectedModel: Binding<String>) -> some View {
self
.navigationTitle(titleKey)
.safeAreaInset(edge: .top) {
HStack {
Text(
"Model: \(selectedModel.wrappedValue)"
)
.font(.caption)
.foregroundColor(.secondary)
Spacer()
}
.padding(.horizontal, 16)
.padding(.vertical, 8)
}
}

@inlinable public func modelSelect(selectedModel: Binding<String>, models: [String], showsModelSelectionSheet: Binding<Bool>, help: String) -> some View {
self
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
showsModelSelectionSheet.wrappedValue.toggle()
}) {
Image(systemName: "cpu")
}
}
}
.confirmationDialog(
"Select model",
isPresented: showsModelSelectionSheet,
titleVisibility: .visible,
actions: {
ForEach(models, id: \.self) { (model: String) in
Button {
selectedModel.wrappedValue = model
} label: {
Text(model)
}
}

Button("Cancel", role: .cancel) {
showsModelSelectionSheet.wrappedValue = false
}
},
message: {
Text(
"View \(help) for details"
)
.font(.caption)
}
)
}
}
50 changes: 3 additions & 47 deletions Demo/DemoChat/Sources/UI/DetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct DetailView: View {
@State private var showsModelSelectionSheet = false
@State private var selectedChatModel: Model = .gpt4_0613

private let availableChatModels: [Model] = [.gpt3_5Turbo, .gpt4_0613]
private static let availableChatModels: [Model] = [.gpt3_5Turbo, .gpt4]

let conversation: Conversation
let error: Error?
Expand Down Expand Up @@ -65,52 +65,8 @@ struct DetailView: View {

inputBar(scrollViewProxy: scrollViewProxy)
}
.navigationTitle("Chat")
.safeAreaInset(edge: .top) {
HStack {
Text(
"Model: \(selectedChatModel)"
)
.font(.caption)
.foregroundColor(.secondary)
Spacer()
}
.padding(.horizontal, 16)
.padding(.vertical, 8)
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
showsModelSelectionSheet.toggle()
}) {
Image(systemName: "cpu")
}
}
}
.confirmationDialog(
"Select model",
isPresented: $showsModelSelectionSheet,
titleVisibility: .visible,
actions: {
ForEach(availableChatModels, id: \.self) { model in
Button {
selectedChatModel = model
} label: {
Text(model)
}
}

Button("Cancel", role: .cancel) {
showsModelSelectionSheet = false
}
},
message: {
Text(
"View https://platform.openai.com/docs/models/overview for details"
)
.font(.caption)
}
)
.navigationTitle("Chat", selectedModel: $selectedChatModel)
.modelSelect(selectedModel: $selectedChatModel, models: Self.availableChatModels, showsModelSelectionSheet: $showsModelSelectionSheet, help: "https://platform.openai.com/docs/models/overview")
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions Demo/DemoChat/Sources/UI/TextToSpeechView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ public struct TextToSpeechView: View {

@State private var prompt: String = ""
@State private var voice: AudioSpeechQuery.AudioSpeechVoice = .alloy
@State private var speed: Double = 1
@State private var speed: Double = AudioSpeechQuery.Speed.normal.rawValue
@State private var responseFormat: AudioSpeechQuery.AudioSpeechResponseFormat = .mp3

@State private var showsModelSelectionSheet = false
@State private var selectedSpeechModel: String = Model.tts_1

private static let availableSpeechModels: [String] = [Model.tts_1, Model.tts_1_hd]

public init(store: SpeechStore) {
self.store = store
}
Expand Down Expand Up @@ -56,7 +60,7 @@ public struct TextToSpeechView: View {
HStack {
Text("Speed: ")
Spacer()
Stepper(value: $speed, in: 0.25...4, step: 0.25) {
Stepper(value: $speed, in: AudioSpeechQuery.Speed.min.rawValue...AudioSpeechQuery.Speed.max.rawValue, step: 0.25) {
HStack {
Spacer()
Text("**\(String(format: "%.2f", speed))**")
Expand All @@ -79,7 +83,7 @@ public struct TextToSpeechView: View {
Section {
HStack {
Button("Create Speech") {
let query = AudioSpeechQuery(model: .tts_1,
let query = AudioSpeechQuery(model: selectedSpeechModel,
input: prompt,
voice: voice,
responseFormat: responseFormat,
Expand All @@ -93,6 +97,7 @@ public struct TextToSpeechView: View {
.disabled(prompt.replacingOccurrences(of: " ", with: "").isEmpty)
Spacer()
}
.modelSelect(selectedModel: $selectedSpeechModel, models: Self.availableSpeechModels, showsModelSelectionSheet: $showsModelSelectionSheet, help: "https://platform.openai.com/docs/models/tts")
}
if !$store.audioObjects.wrappedValue.isEmpty {
Section("Click to play, swipe to save:") {
Expand Down Expand Up @@ -129,7 +134,7 @@ public struct TextToSpeechView: View {
}
.listStyle(.insetGrouped)
.scrollDismissesKeyboard(.interactively)
.navigationTitle("Create Speech")
.navigationTitle("Create Speech", selectedModel: $selectedSpeechModel)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAI/OpenAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final public class OpenAI: OpenAIProtocol {
}

private let session: URLSessionProtocol
private var streamingSessions: [NSObject] = []
private var streamingSessions = ArrayWithThreadSafety<NSObject>()

public let configuration: Configuration

Expand Down
25 changes: 14 additions & 11 deletions Sources/OpenAI/Public/Models/AudioSpeechQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ public struct AudioSpeechQuery: Codable, Equatable {
case responseFormat = "response_format"
case speed
}

private enum Constants {
static let normalSpeed = 1.0
static let maxSpeed = 4.0
static let minSpeed = 0.25
}


public init(model: Model, input: String, voice: AudioSpeechVoice, responseFormat: AudioSpeechResponseFormat = .mp3, speed: Double?) {
self.model = AudioSpeechQuery.validateSpeechModel(model)
self.speed = AudioSpeechQuery.normalizeSpeechSpeed(speed)
Expand All @@ -80,13 +74,22 @@ private extension AudioSpeechQuery {
}
return inputModel
}

}

public extension AudioSpeechQuery {

enum Speed: Double {
case normal = 1.0
case max = 4.0
case min = 0.25
}

static func normalizeSpeechSpeed(_ inputSpeed: Double?) -> String {
guard let inputSpeed else { return "\(Constants.normalSpeed)" }
let isSpeedOutOfBounds = inputSpeed >= Constants.maxSpeed && inputSpeed <= Constants.minSpeed
guard let inputSpeed else { return "\(Self.Speed.normal.rawValue)" }
let isSpeedOutOfBounds = inputSpeed <= Self.Speed.min.rawValue || Self.Speed.max.rawValue <= inputSpeed
guard !isSpeedOutOfBounds else {
print("[AudioSpeech] Speed value must be between 0.25 and 4.0. Setting value to closest valid.")
return inputSpeed < Constants.minSpeed ? "\(Constants.minSpeed)" : "\(Constants.maxSpeed)"
return inputSpeed < Self.Speed.min.rawValue ? "\(Self.Speed.min.rawValue)" : "\(Self.Speed.max.rawValue)"
}
return "\(inputSpeed)"
}
Expand Down
26 changes: 26 additions & 0 deletions Sources/OpenAI/Public/Utilities/ArrayWithThreadSafety.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// ArrayWithThreadSafety.swift
//
//
// Created by James J Kalafus on 2024-02-01.
//

import Foundation

internal class ArrayWithThreadSafety<Element> {
private var array = [Element]()
private let queue = DispatchQueue(label: "us.kalaf.OpenAI.threadSafeArray", attributes: .concurrent)

@inlinable public func append(_ element: Element) {
queue.async(flags: .barrier) {
self.array.append(element)
}
}

@inlinable public func removeAll(where shouldBeRemoved: @escaping (Element) throws -> Bool) rethrows {
try queue.sync(flags: .barrier) {
try self.array.removeAll(where: shouldBeRemoved)
}
}
}

24 changes: 24 additions & 0 deletions Tests/OpenAITests/OpenAITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,30 @@ class OpenAITests: XCTestCase {
XCTAssertEqual(inError, apiError)
}

func testAudioSpeechDoesNotNormalize() async throws {
let query = AudioSpeechQuery(model: .tts_1, input: "Hello, world!", voice: .alloy, responseFormat: .mp3, speed: 2.0)

XCTAssertEqual(query.speed, "\(2.0)")
}

func testAudioSpeechNormalizeNil() async throws {
let query = AudioSpeechQuery(model: .tts_1, input: "Hello, world!", voice: .alloy, responseFormat: .mp3, speed: nil)

XCTAssertEqual(query.speed, "\(1.0)")
}

func testAudioSpeechNormalizeLow() async throws {
let query = AudioSpeechQuery(model: .tts_1, input: "Hello, world!", voice: .alloy, responseFormat: .mp3, speed: 0.0)

XCTAssertEqual(query.speed, "\(0.25)")
}

func testAudioSpeechNormalizeHigh() async throws {
let query = AudioSpeechQuery(model: .tts_1, input: "Hello, world!", voice: .alloy, responseFormat: .mp3, speed: 10.0)

XCTAssertEqual(query.speed, "\(4.0)")
}

func testAudioSpeechError() async throws {
let query = AudioSpeechQuery(model: .tts_1, input: "Hello, world!", voice: .alloy, responseFormat: .mp3, speed: 1.0)
let inError = APIError(message: "foo", type: "bar", param: "baz", code: "100")
Expand Down

0 comments on commit aea5766

Please sign in to comment.