Skip to content

Commit d47b4cf

Browse files
committed
Cleanup
1 parent c009a90 commit d47b4cf

5 files changed

Lines changed: 123 additions & 116 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Foundation
2+
import Preferences
3+
4+
public protocol ChatGPTConfiguration {
5+
var featureProvider: ChatFeatureProvider { get }
6+
var temperature: Double { get }
7+
var model: String { get }
8+
var endpoint: String { get }
9+
var apiKey: String { get }
10+
var stop: [String] { get }
11+
var maxTokens: Int { get }
12+
var minimumReplyTokens: Int { get }
13+
}
14+
15+
extension ChatGPTConfiguration {
16+
func endpoint(for provider: ChatFeatureProvider) -> String {
17+
switch provider {
18+
case .openAI:
19+
let baseURL = UserDefaults.shared.value(for: \.openAIBaseURL)
20+
if baseURL.isEmpty { return "https://api.openai.com/v1/chat/completions" }
21+
return "\(baseURL)/v1/chat/completions"
22+
case .azureOpenAI:
23+
let baseURL = UserDefaults.shared.value(for: \.azureOpenAIBaseURL)
24+
let deployment = UserDefaults.shared.value(for: \.azureChatGPTDeployment)
25+
let version = "2023-05-15"
26+
if baseURL.isEmpty { return "" }
27+
return "\(baseURL)/openai/deployments/\(deployment)/chat/completions?api-version=\(version)"
28+
}
29+
}
30+
31+
func apiKey(for provider: ChatFeatureProvider) -> String {
32+
switch provider {
33+
case .openAI:
34+
return UserDefaults.shared.value(for: \.openAIAPIKey)
35+
case .azureOpenAI:
36+
return UserDefaults.shared.value(for: \.azureOpenAIAPIKey)
37+
}
38+
}
39+
}

Tool/Sources/OpenAIService/ChatGPTConfiguration.swift renamed to Tool/Sources/OpenAIService/Configuration/UserPreferenceChatGPTConfiguration.swift

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,6 @@
11
import Foundation
22
import Preferences
33

4-
public protocol ChatGPTConfiguration {
5-
var featureProvider: ChatFeatureProvider { get }
6-
var temperature: Double { get }
7-
var model: String { get }
8-
var endpoint: String { get }
9-
var apiKey: String { get }
10-
var stop: [String] { get }
11-
var maxTokens: Int { get }
12-
var minimumReplyTokens: Int { get }
13-
}
14-
15-
extension ChatGPTConfiguration {
16-
func endpoint(for provider: ChatFeatureProvider) -> String {
17-
switch provider {
18-
case .openAI:
19-
let baseURL = UserDefaults.shared.value(for: \.openAIBaseURL)
20-
if baseURL.isEmpty { return "https://api.openai.com/v1/chat/completions" }
21-
return "\(baseURL)/v1/chat/completions"
22-
case .azureOpenAI:
23-
let baseURL = UserDefaults.shared.value(for: \.azureOpenAIBaseURL)
24-
let deployment = UserDefaults.shared.value(for: \.azureChatGPTDeployment)
25-
let version = "2023-05-15"
26-
if baseURL.isEmpty { return "" }
27-
return "\(baseURL)/openai/deployments/\(deployment)/chat/completions?api-version=\(version)"
28-
}
29-
}
30-
31-
func apiKey(for provider: ChatFeatureProvider) -> String {
32-
switch provider {
33-
case .openAI:
34-
return UserDefaults.shared.value(for: \.openAIAPIKey)
35-
case .azureOpenAI:
36-
return UserDefaults.shared.value(for: \.azureOpenAIAPIKey)
37-
}
38-
}
39-
}
40-
414
public struct UserPreferenceChatGPTConfiguration: ChatGPTConfiguration {
425
public var featureProvider: ChatFeatureProvider {
436
UserDefaults.shared.value(for: \.chatFeatureProvider)

Tool/Sources/OpenAIService/ChatGPTMemory.swift renamed to Tool/Sources/OpenAIService/Memory/AutoManagedChatGPTMemory.swift

Lines changed: 8 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,6 @@
11
import Foundation
22
import GPTEncoder
3-
4-
public protocol ChatGPTMemory {
5-
/// The visible messages to the ChatGPT service.
6-
var messages: [ChatMessage] { get async }
7-
/// The remaining tokens available for the reply.
8-
var remainingTokens: Int? { get async }
9-
/// Update the message history.
10-
func mutateHistory(_ update: (inout [ChatMessage]) -> Void) async
11-
}
12-
13-
public extension ChatGPTMemory {
14-
/// Append a message to the history.
15-
func appendMessage(_ message: ChatMessage) async {
16-
await mutateHistory {
17-
$0.append(message)
18-
}
19-
}
20-
21-
/// Update a message in the history.
22-
func updateMessage(id: String, _ update: (inout ChatMessage) -> Void) async {
23-
await mutateHistory { history in
24-
if let index = history.firstIndex(where: { $0.id == id }) {
25-
update(&history[index])
26-
}
27-
}
28-
}
29-
30-
/// Remove a message from the history.
31-
func removeMessage(_ id: String) async {
32-
await mutateHistory {
33-
$0.removeAll { $0.id == id }
34-
}
35-
}
36-
37-
/// Stream a message to the history.
38-
func streamMessage(id: String, role: ChatMessage.Role?, content: String?) async {
39-
await mutateHistory { history in
40-
if let index = history.firstIndex(where: { $0.id == id }) {
41-
if let content {
42-
history[index].content.append(content)
43-
}
44-
if let role {
45-
history[index].role = role
46-
}
47-
} else {
48-
history.append(.init(
49-
id: id,
50-
role: role ?? .system,
51-
content: content ?? ""
52-
))
53-
}
54-
}
55-
}
56-
57-
/// Clear the history.
58-
func clearHistory() async {
59-
await mutateHistory { $0.removeAll() }
60-
}
61-
}
62-
63-
public actor ConversationChatGPTMemory: ChatGPTMemory {
64-
public var messages: [ChatMessage] = []
65-
public var remainingTokens: Int? { nil }
66-
67-
public init(systemPrompt: String) {
68-
messages.append(.init(role: .system, content: systemPrompt))
69-
}
70-
71-
public func mutateHistory(_ update: (inout [ChatMessage]) -> Void) {
72-
update(&messages)
73-
}
74-
}
3+
import Preferences
754

765
/// A memory that automatically manages the history according to max tokens and max message count.
776
public actor AutoManagedChatGPTMemory: ChatGPTMemory {
@@ -101,6 +30,13 @@ public actor AutoManagedChatGPTMemory: ChatGPTMemory {
10130
public func mutateSystemPrompt(_ newPrompt: String) {
10231
systemPrompt.content = newPrompt
10332
}
33+
34+
public nonisolated
35+
func observeHistoryChange(_ onChange: @escaping () -> Void) {
36+
Task {
37+
await setOnHistoryChangeBlock(onChange)
38+
}
39+
}
10440

10541
func generateSendingHistory(
10642
maxNumberOfMessages: Int = UserDefaults.shared.value(for: \.chatGPTMaxMessageCount),
@@ -146,13 +82,6 @@ public actor AutoManagedChatGPTMemory: ChatGPTMemory {
14682
return max(configuration.minimumReplyTokens, configuration.maxTokens - tokensCount)
14783
}
14884

149-
public nonisolated
150-
func observeHistoryChange(_ onChange: @escaping () -> Void) {
151-
Task {
152-
await setOnHistoryChangeBlock(onChange)
153-
}
154-
}
155-
15685
func setOnHistoryChangeBlock(_ onChange: @escaping () -> Void) {
15786
onHistoryChange = onChange
15887
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import Foundation
2+
import GPTEncoder
3+
4+
public protocol ChatGPTMemory {
5+
/// The visible messages to the ChatGPT service.
6+
var messages: [ChatMessage] { get async }
7+
/// The remaining tokens available for the reply.
8+
var remainingTokens: Int? { get async }
9+
/// Update the message history.
10+
func mutateHistory(_ update: (inout [ChatMessage]) -> Void) async
11+
}
12+
13+
public extension ChatGPTMemory {
14+
/// Append a message to the history.
15+
func appendMessage(_ message: ChatMessage) async {
16+
await mutateHistory {
17+
$0.append(message)
18+
}
19+
}
20+
21+
/// Update a message in the history.
22+
func updateMessage(id: String, _ update: (inout ChatMessage) -> Void) async {
23+
await mutateHistory { history in
24+
if let index = history.firstIndex(where: { $0.id == id }) {
25+
update(&history[index])
26+
}
27+
}
28+
}
29+
30+
/// Remove a message from the history.
31+
func removeMessage(_ id: String) async {
32+
await mutateHistory {
33+
$0.removeAll { $0.id == id }
34+
}
35+
}
36+
37+
/// Stream a message to the history.
38+
func streamMessage(id: String, role: ChatMessage.Role?, content: String?) async {
39+
await mutateHistory { history in
40+
if let index = history.firstIndex(where: { $0.id == id }) {
41+
if let content {
42+
history[index].content.append(content)
43+
}
44+
if let role {
45+
history[index].role = role
46+
}
47+
} else {
48+
history.append(.init(
49+
id: id,
50+
role: role ?? .system,
51+
content: content ?? ""
52+
))
53+
}
54+
}
55+
}
56+
57+
/// Clear the history.
58+
func clearHistory() async {
59+
await mutateHistory { $0.removeAll() }
60+
}
61+
}
62+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Foundation
2+
3+
public actor ConversationChatGPTMemory: ChatGPTMemory {
4+
public var messages: [ChatMessage] = []
5+
public var remainingTokens: Int? { nil }
6+
7+
public init(systemPrompt: String) {
8+
messages.append(.init(role: .system, content: systemPrompt))
9+
}
10+
11+
public func mutateHistory(_ update: (inout [ChatMessage]) -> Void) {
12+
update(&messages)
13+
}
14+
}

0 commit comments

Comments
 (0)