-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
246 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// | ||
// 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 | ||
} | ||
|
||
// MARK: Models | ||
|
||
@MainActor | ||
func getModels() async { | ||
do { | ||
let response = try await openAIClient.models() | ||
availableModels = response.data | ||
} catch { | ||
// TODO: Better error handling | ||
print(error.localizedDescription) | ||
} | ||
} | ||
|
||
// MARK: Moderations | ||
|
||
@Published var moderationConversation = Conversation(id: "", messages: []) | ||
@Published var moderationConversationError: Error? | ||
|
||
@MainActor | ||
func sendModerationMessage(_ message: Message) async { | ||
moderationConversation.messages.append(message) | ||
await completeModerationChat(message: message) | ||
} | ||
|
||
@MainActor | ||
func completeModerationChat(message: Message) async { | ||
|
||
moderationConversationError = nil | ||
|
||
do { | ||
let response = try await openAIClient.moderations( | ||
query: ModerationsQuery( | ||
input: message.content, | ||
model: .textModerationLatest | ||
) | ||
) | ||
|
||
let categoryResults = response.results | ||
|
||
let existingMessages = moderationConversation.messages | ||
|
||
func circleEmoji(for resultType: Bool) -> String { | ||
resultType ? "🔴" : "🟢" | ||
} | ||
|
||
for result in categoryResults { | ||
let content = """ | ||
\(circleEmoji(for: result.categories.hate)) Hate | ||
\(circleEmoji(for: result.categories.hateThreatening)) Hate/Threatening | ||
\(circleEmoji(for: result.categories.selfHarm)) Self-harm | ||
\(circleEmoji(for: result.categories.sexual)) Sexual | ||
\(circleEmoji(for: result.categories.sexualMinors)) Sexual/Minors | ||
\(circleEmoji(for: result.categories.violence)) Violence | ||
\(circleEmoji(for: result.categories.violenceGraphic)) Violence/Graphic | ||
""" | ||
|
||
let message = Message( | ||
id: response.id, | ||
role: .assistant, | ||
content: content, | ||
createdAt: message.createdAt) | ||
|
||
if existingMessages.contains(message) { | ||
continue | ||
} | ||
moderationConversation.messages.append(message) | ||
} | ||
|
||
} catch { | ||
moderationConversationError = error | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
// | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// 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()) | ||
} | ||
Section(header: Text("Moderations")) { | ||
NavigationLink("Moderation Chat", destination: ModerationChatView(store: store)) | ||
} | ||
} | ||
.listStyle(.insetGrouped) | ||
.navigationTitle("Misc") | ||
} | ||
} | ||
} | ||
|
||
struct RetrieveModelView: View { | ||
var body: some View { | ||
Text("Retrieve Model: TBD") | ||
.font(.largeTitle) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// ModerationChatView.swift | ||
// DemoChat | ||
// | ||
// Created by Aled Samuel on 26/04/2023. | ||
// | ||
|
||
import SwiftUI | ||
|
||
public struct ModerationChatView: View { | ||
@ObservedObject var store: MiscStore | ||
|
||
@Environment(\.dateProviderValue) var dateProvider | ||
@Environment(\.idProviderValue) var idProvider | ||
|
||
public init(store: MiscStore) { | ||
self.store = store | ||
} | ||
|
||
public var body: some View { | ||
DetailView( | ||
conversation: store.moderationConversation, | ||
error: store.moderationConversationError, | ||
sendMessage: { message in | ||
Task { | ||
await store.sendModerationMessage( | ||
Message( | ||
id: idProvider(), | ||
role: .user, | ||
content: message, | ||
createdAt: dateProvider() | ||
) | ||
) | ||
} | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.