forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContextAwareAutoManagedChatGPTMemory.swift
More file actions
53 lines (46 loc) · 1.7 KB
/
ContextAwareAutoManagedChatGPTMemory.swift
File metadata and controls
53 lines (46 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import Foundation
import OpenAIService
public final class ContextAwareAutoManagedChatGPTMemory: ChatGPTMemory {
private let memory: AutoManagedChatGPTMemory
let contextController: DynamicContextController
let functionProvider: ChatFunctionProvider
weak var chatService: ChatService?
public var history: [ChatMessage] {
get async { await memory.history }
}
func observeHistoryChange(_ observer: @escaping () -> Void) {
memory.observeHistoryChange(observer)
}
init(
configuration: OverridingChatGPTConfiguration,
functionProvider: ChatFunctionProvider
) {
memory = AutoManagedChatGPTMemory(
systemPrompt: "",
configuration: configuration,
functionProvider: functionProvider
)
contextController = DynamicContextController(
memory: memory,
functionProvider: functionProvider,
configuration: configuration,
contextCollectors: allContextCollectors
)
self.functionProvider = functionProvider
}
public func mutateHistory(_ update: (inout [ChatMessage]) -> Void) async {
await memory.mutateHistory(update)
}
public func generatePrompt() async -> ChatGPTPrompt {
let content = (await memory.history)
.last(where: { $0.role == .user })?.content
try? await contextController.collectContextInformation(
systemPrompt: """
\(chatService?.systemPrompt ?? "")
\(chatService?.extraSystemPrompt ?? "")
""".trimmingCharacters(in: .whitespacesAndNewlines),
content: content ?? ""
)
return await memory.generatePrompt()
}
}