Skip to content

Commit

Permalink
Merge pull request #60 from SunburstEnzo/demoapp-listmodels
Browse files Browse the repository at this point in the history
Add models example to Demo app
  • Loading branch information
Krivoblotsky authored Apr 24, 2023
2 parents eb7f440 + edd80f3 commit f0e1709
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 3 deletions.
9 changes: 8 additions & 1 deletion Demo/App/APIProvidedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import SwiftUI
struct APIProvidedView: View {
@Binding var apiKey: String
@StateObject var chatStore: ChatStore
@StateObject var miscStore: MiscStore
@State var isShowingAPIConfigModal: Bool = true

@Environment(\.idProviderValue) var idProvider
Expand All @@ -28,11 +29,17 @@ struct APIProvidedView: View {
idProvider: idProvider
)
)
self._miscStore = StateObject(
wrappedValue: MiscStore(
openAIClient: OpenAI(apiToken: apiKey.wrappedValue)
)
)
}

var body: some View {
ContentView(
chatStore: chatStore
chatStore: chatStore,
miscStore: miscStore
)
.onChange(of: apiKey) { newApiKey in
chatStore.openAIClient = OpenAI(apiToken: newApiKey)
Expand Down
9 changes: 9 additions & 0 deletions Demo/App/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SwiftUI

struct ContentView: View {
@ObservedObject var chatStore: ChatStore
@ObservedObject var miscStore: MiscStore
@State private var selectedTab = 0
@Environment(\.idProviderValue) var idProvider

Expand All @@ -37,6 +38,14 @@ struct ContentView: View {
Label("Image", systemImage: "photo")
}
.tag(2)

MiscView(
store: miscStore
)
.tabItem {
Label("Misc", systemImage: "ellipsis")
}
.tag(3)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Demo/DemoChat/Sources/ChatStore.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Store.swift
// ChatStore.swift
// DemoChat
//
// Created by Sihao Lu on 3/25/23.
Expand Down
32 changes: 32 additions & 0 deletions Demo/DemoChat/Sources/MiscStore.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// MiscStore.swift
// DemoChat
//
// Created by Aled Samuel on 22/04/2023.
//

import Foundation
import OpenAI

public final class MiscStore: ObservableObject {
public var openAIClient: OpenAIProtocol

@Published var availableModels: [ModelResult] = []

public init(
openAIClient: OpenAIProtocol
) {
self.openAIClient = openAIClient
}

@MainActor
func getModels() async {
do {
let response = try await openAIClient.models()
availableModels = response.data
} catch {
// TODO: Better error handling
print(error.localizedDescription)
}
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion Demo/DemoChat/Sources/UI/Environment/IDProvider.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// IDProvider.swift
//
// DemoChat
//
// Created by Sihao Lu on 4/6/23.
//
Expand Down
27 changes: 27 additions & 0 deletions Demo/DemoChat/Sources/UI/Misc/ListModelsView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// ListModelsView.swift
// DemoChat
//
// Created by Aled Samuel on 22/04/2023.
//

import SwiftUI

public struct ListModelsView: View {
@ObservedObject var store: MiscStore

public var body: some View {
NavigationStack {
List($store.availableModels) { row in
Text(row.id)
}
.listStyle(.insetGrouped)
.navigationTitle("Models")
}
.onAppear {
Task {
await store.getModels()
}
}
}
}
36 changes: 36 additions & 0 deletions Demo/DemoChat/Sources/UI/Misc/MiscView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// MiscView.swift
// DemoChat
//
// Created by Aled Samuel on 22/04/2023.
//

import SwiftUI

public struct MiscView: View {
@ObservedObject var store: MiscStore

public init(store: MiscStore) {
self.store = store
}

public var body: some View {
NavigationStack {
List {
Section(header: Text("Models")) {
NavigationLink("List Models", destination: ListModelsView(store: store))
NavigationLink("Retrieve Model", destination: RetrieveModelView())
}
}
.listStyle(.insetGrouped)
.navigationTitle("Misc")
}
}
}

struct RetrieveModelView: View {
var body: some View {
Text("Retrieve Model: TBD")
.font(.largeTitle)
}
}
2 changes: 2 additions & 0 deletions Sources/OpenAI/Public/Models/Models/ModelResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ public struct ModelResult: Codable, Equatable {
case ownedBy = "owned_by"
}
}

extension ModelResult: Identifiable {}

0 comments on commit f0e1709

Please sign in to comment.