Skip to content

Commit eefb14b

Browse files
authored
Merge pull request #66 from SunburstEnzo/demoapp_moderations
Add Moderations chat example to Demo app
2 parents f0e1709 + 11a3193 commit eefb14b

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

Demo/DemoChat/Sources/MiscStore.swift

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public final class MiscStore: ObservableObject {
1919
self.openAIClient = openAIClient
2020
}
2121

22+
// MARK: Models
23+
2224
@MainActor
2325
func getModels() async {
2426
do {
@@ -29,4 +31,64 @@ public final class MiscStore: ObservableObject {
2931
print(error.localizedDescription)
3032
}
3133
}
34+
35+
// MARK: Moderations
36+
37+
@Published var moderationConversation = Conversation(id: "", messages: [])
38+
@Published var moderationConversationError: Error?
39+
40+
@MainActor
41+
func sendModerationMessage(_ message: Message) async {
42+
moderationConversation.messages.append(message)
43+
await completeModerationChat(message: message)
44+
}
45+
46+
@MainActor
47+
func completeModerationChat(message: Message) async {
48+
49+
moderationConversationError = nil
50+
51+
do {
52+
let response = try await openAIClient.moderations(
53+
query: ModerationsQuery(
54+
input: message.content,
55+
model: .textModerationLatest
56+
)
57+
)
58+
59+
let categoryResults = response.results
60+
61+
let existingMessages = moderationConversation.messages
62+
63+
func circleEmoji(for resultType: Bool) -> String {
64+
resultType ? "🔴" : "🟢"
65+
}
66+
67+
for result in categoryResults {
68+
let content = """
69+
\(circleEmoji(for: result.categories.hate)) Hate
70+
\(circleEmoji(for: result.categories.hateThreatening)) Hate/Threatening
71+
\(circleEmoji(for: result.categories.selfHarm)) Self-harm
72+
\(circleEmoji(for: result.categories.sexual)) Sexual
73+
\(circleEmoji(for: result.categories.sexualMinors)) Sexual/Minors
74+
\(circleEmoji(for: result.categories.violence)) Violence
75+
\(circleEmoji(for: result.categories.violenceGraphic)) Violence/Graphic
76+
"""
77+
78+
let message = Message(
79+
id: response.id,
80+
role: .assistant,
81+
content: content,
82+
createdAt: message.createdAt)
83+
84+
if existingMessages.contains(message) {
85+
continue
86+
}
87+
moderationConversation.messages.append(message)
88+
}
89+
90+
} catch {
91+
moderationConversationError = error
92+
}
93+
}
3294
}

Demo/DemoChat/Sources/UI/Misc/MiscView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public struct MiscView: View {
2121
NavigationLink("List Models", destination: ListModelsView(store: store))
2222
NavigationLink("Retrieve Model", destination: RetrieveModelView())
2323
}
24+
Section(header: Text("Moderations")) {
25+
NavigationLink("Moderation Chat", destination: ModerationChatView(store: store))
26+
}
2427
}
2528
.listStyle(.insetGrouped)
2629
.navigationTitle("Misc")
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// ModerationChatView.swift
3+
// DemoChat
4+
//
5+
// Created by Aled Samuel on 26/04/2023.
6+
//
7+
8+
import SwiftUI
9+
10+
public struct ModerationChatView: View {
11+
@ObservedObject var store: MiscStore
12+
13+
@Environment(\.dateProviderValue) var dateProvider
14+
@Environment(\.idProviderValue) var idProvider
15+
16+
public init(store: MiscStore) {
17+
self.store = store
18+
}
19+
20+
public var body: some View {
21+
DetailView(
22+
conversation: store.moderationConversation,
23+
error: store.moderationConversationError,
24+
sendMessage: { message in
25+
Task {
26+
await store.sendModerationMessage(
27+
Message(
28+
id: idProvider(),
29+
role: .user,
30+
content: message,
31+
createdAt: dateProvider()
32+
)
33+
)
34+
}
35+
}
36+
)
37+
}
38+
}

Sources/OpenAI/Public/Models/Models/Models.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public extension Model {
6969

7070
/// Almost as capable as the latest model, but slightly older.
7171
static let textModerationStable = "text-moderation-stable"
72-
/// Most capable moderation model. Accuracy will be slighlty higher than the stable model.
72+
/// Most capable moderation model. Accuracy will be slightly higher than the stable model.
7373
static let textModerationLatest = "text-moderation-latest"
7474
static let moderation = "text-moderation-001"
7575
}

0 commit comments

Comments
 (0)