Skip to content

Commit 050aeb7

Browse files
committed
Add ChatFunctionProvider
1 parent 959cd37 commit 050aeb7

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Foundation
2+
import OpenAIService
3+
4+
final class ChatFunctionProvider {
5+
var functions: [any ChatGPTFunction] = []
6+
7+
init() {}
8+
9+
func removeAll() {
10+
functions = []
11+
}
12+
13+
func append(functions others: [any ChatGPTFunction]) {
14+
functions.append(contentsOf: others)
15+
}
16+
}
17+
18+
extension ChatFunctionProvider: ChatGPTFunctionProvider {}
19+

Core/Sources/ChatService/ChatService.swift

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,51 @@ public final class ChatService: ObservableObject {
1616

1717
let pluginController: ChatPluginController
1818
let contextController: DynamicContextController
19+
let functionProvider: ChatFunctionProvider
1920
var cancellable = Set<AnyCancellable>()
2021

2122
init<T: ChatGPTServiceType>(
2223
memory: AutoManagedChatGPTMemory,
2324
configuration: OverridingChatGPTConfiguration<UserPreferenceChatGPTConfiguration>,
25+
functionProvider: ChatFunctionProvider,
2426
chatGPTService: T
2527
) {
2628
self.memory = memory
2729
self.configuration = configuration
2830
self.chatGPTService = chatGPTService
29-
pluginController = ChatPluginController(chatGPTService: chatGPTService, plugins: allPlugins)
31+
self.functionProvider = functionProvider
32+
pluginController = ChatPluginController(
33+
chatGPTService: chatGPTService,
34+
plugins: allPlugins
35+
)
3036
contextController = DynamicContextController(
3137
memory: memory,
32-
contextCollectors: ActiveDocumentChatContextCollector()
38+
functionProvider: functionProvider,
39+
contextCollectors: allContextCollectors
3340
)
3441

3542
pluginController.chatService = self
3643
}
3744

38-
public init() {
39-
configuration = UserPreferenceChatGPTConfiguration().overriding()
40-
memory = AutoManagedChatGPTMemory(systemPrompt: "", configuration: configuration)
41-
chatGPTService = ChatGPTService(memory: memory, configuration: configuration)
42-
pluginController = ChatPluginController(chatGPTService: chatGPTService, plugins: allPlugins)
43-
contextController = DynamicContextController(
45+
public convenience init() {
46+
let configuration = UserPreferenceChatGPTConfiguration().overriding()
47+
let functionProvider = ChatFunctionProvider()
48+
let memory = AutoManagedChatGPTMemory(
49+
systemPrompt: "",
50+
configuration: configuration,
51+
functionProvider: functionProvider
52+
)
53+
self.init(
4454
memory: memory,
45-
contextCollectors: ActiveDocumentChatContextCollector()
55+
configuration: configuration,
56+
functionProvider: functionProvider,
57+
chatGPTService: ChatGPTService(
58+
memory: memory,
59+
configuration: configuration,
60+
functionProvider: functionProvider
61+
)
4662
)
4763

48-
pluginController.chatService = self
4964
memory.observeHistoryChange { [weak self] in
5065
self?.objectWillChange.send()
5166
}

0 commit comments

Comments
 (0)