Skip to content

Commit

Permalink
refactor: remove update model
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Sep 2, 2023
1 parent 4e38e44 commit ee3a9ed
Showing 1 changed file with 33 additions and 56 deletions.
89 changes: 33 additions & 56 deletions PlantUML/PlantUML+OpenAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ class OpenAIService : ObservableObject {
case Error( String )
case Editing
}
let models = ["text-davinci-edit-001", "code-davinci-edit-001"]

// let models = ["text-davinci-edit-001", "code-davinci-edit-001"]

@Published public var status: Status = .Ready
@Published public var inputApiKey = ""
@Published public var inputOrgId = ""
@Published public var inputModel:String
// @Published public var inputModel:String


@AppStorage("openaiModel") private var openAIModel:String?
@AppStorage("openaiModel") private var openAIModel:String = "text-davinci-edit-001"
@AppSecureStorage("openaikey") private var openAIKey:String?
@AppSecureStorage("openaiorg") private var openAIOrg:String?

Expand All @@ -109,23 +109,21 @@ class OpenAIService : ObservableObject {

init() {

inputModel = models[0]

if let apiKey = Bundle.main.object(forInfoDictionaryKey: "OPENAI_API_KEY") as? String, !apiKey.isEmpty {
openAIKey = apiKey
}
if let orgId = Bundle.main.object(forInfoDictionaryKey: "OPENAI_ORG_ID") as? String, !orgId.isEmpty {
openAIOrg = orgId
}


inputApiKey = openAIKey ?? ""
inputOrgId = openAIOrg ?? ""

if let openAIModel {
inputModel = openAIModel
}

// inputModel = models[0]

// if let openAIModel {
// inputModel = openAIModel
// }

}

Expand All @@ -135,41 +133,24 @@ class OpenAIService : ObservableObject {
}
openAIKey = inputApiKey
openAIOrg = inputOrgId
openAIModel = inputModel
// focobjectWillChange.send()
// openAIModel = inputModel
}

func resetSettings() {
// inputModel = models[0]
inputApiKey = ""
inputOrgId = ""
inputModel = models[0]
openAIKey = nil
openAIOrg = nil
}


var isSettingsValid:Bool {
guard let openAIKey, !openAIKey.isEmpty, let openAIOrg, !openAIOrg.isEmpty else {
return false
}
return true
}

// lazy var openAI: OpenAI? = {
//
// guard let apiKey = Bundle.main.object(forInfoDictionaryKey: "OPENAI_API_KEY") as? String, !apiKey.isEmpty else {
// status = .Error("api key not found!")
// return nil
// }
// guard let orgId = Bundle.main.object(forInfoDictionaryKey: "OPENAI_ORG_ID") as? String, !orgId.isEmpty else {
// status = .Error("org id not found!")
// return nil
// }
//
// return OpenAI( Configuration(organizationId: orgId, apiKey: apiKey))
//
// }()

var openAI: OpenAI? {

guard let openAIKey else {
Expand All @@ -189,7 +170,7 @@ class OpenAIService : ObservableObject {
@MainActor
func generateEdit( input: String, instruction: String ) async -> String? {

guard let openAI, let openAIModel, case .Ready = status else {
guard let openAI /*, let openAIModel */, case .Ready = status else {
return nil
}

Expand Down Expand Up @@ -237,16 +218,13 @@ struct OpenAIView : View {

@FocusState private var promptInFocus: Bool



var isEditing:Bool {
if case .Editing = service.status {
return true
}
return false
}


var body: some View {

VStack(spacing:0) {
Expand Down Expand Up @@ -373,7 +351,6 @@ extension OpenAIView {
.split( whereSeparator: \.isNewline )
.filter { $0 != "@startuml" && $0 != "@enduml" }
.joined(separator: "\n" )

}
}
},
Expand Down Expand Up @@ -452,8 +429,8 @@ extension OpenAIView {
HStack {
Text("OpenAI Secrets")
HideToggleButton(hidden: $hideOpenAISecrets)
Divider()
Button( action: { p.scrollTo("openai-settings", anchor: .top) }, label: { Text("More .....").font(.footnote) } )
// Divider()
// Button( action: { p.scrollTo("openai-settings", anchor: .top) }, label: { Text("More .....").font(.footnote) } )
}
.id( "openai-secret")

Expand All @@ -466,25 +443,25 @@ extension OpenAIView {
}
}

Section {
Picker("Model", selection: $service.inputModel) {
ForEach(service.models, id: \.self) {
Text($0)
}
}
}
header: {
HStack {
Text("OpenAI Extra settings")
Divider()
Button( action: { p.scrollTo("openai-secret", anchor: .bottom) }, label: { Text("Back ...").font(.footnote) } )
}
.id( "openai-settings")
}
footer: {
Rectangle().fill(Color.clear).frame(height: 65)
}
// Section {
// Picker("Model", selection: $service.inputModel) {
// ForEach(service.models, id: \.self) {
// Text($0)
// }
// }
// }
// header: {
// HStack {
// Text("OpenAI Extra settings")
// Divider()
// Button( action: { p.scrollTo("openai-secret", anchor: .bottom) }, label: { Text("Back ...").font(.footnote) } )
// }
// .id( "openai-settings")
// }
// footer: {
// Rectangle().fill(Color.clear).frame(height: 65)
//
// }

}
}
Expand Down

0 comments on commit ee3a9ed

Please sign in to comment.