Skip to content

Commit 3675e8a

Browse files
committed
Pass configuration to DynamicContextController
1 parent 192d3be commit 3675e8a

File tree

8 files changed

+27
-11
lines changed

8 files changed

+27
-11
lines changed

Core/Sources/ChatContextCollector/ChatContextCollector.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public protocol ChatContextCollector {
1414
func generateContext(
1515
history: [ChatMessage],
1616
scopes: Set<String>,
17-
content: String
17+
content: String,
18+
configuration: ChatGPTConfiguration
1819
) -> ChatContext?
1920
}
2021

Core/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/ActiveDocumentChatContextCollector.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
1515
public func generateContext(
1616
history: [ChatMessage],
1717
scopes: Set<String>,
18-
content: String
18+
content: String,
19+
configuration: ChatGPTConfiguration
1920
) -> ChatContext? {
2021
guard let info = getEditorInformation() else { return nil }
2122
let context = getActiveDocumentContext(info)

Core/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/LegacyActiveDocumentChatContextCollector.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public struct LegacyActiveDocumentChatContextCollector: ChatContextCollector {
1111
public func generateContext(
1212
history: [ChatMessage],
1313
scopes: Set<String>,
14-
content: String
14+
content: String,
15+
configuration: ChatGPTConfiguration
1516
) -> ChatContext? {
1617
guard let content = getEditorInformation() else { return nil }
1718
let relativePath = content.relativePath

Core/Sources/ChatContextCollectors/SystemInfoChatContextCollector/SystemInfoChatContextCollector.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public final class SystemInfoChatContextCollector: ChatContextCollector {
1414
public func generateContext(
1515
history: [ChatMessage],
1616
scopes: Set<String>,
17-
content: String
17+
content: String,
18+
configuration: ChatGPTConfiguration
1819
) -> ChatContext? {
1920
return .init(
2021
systemPrompt: """

Core/Sources/ChatContextCollectors/WebChatContextCollector/SearchFunction.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ struct SearchFunction: ChatGPTFunction {
2828
}.joined(separator: "\n")
2929
}
3030
}
31+
32+
let maxTokens: Int
3133

3234
var reportProgress: (String) async -> Void = { _ in }
3335

@@ -73,11 +75,9 @@ struct SearchFunction: ChatGPTFunction {
7375
searchURL: UserDefaults.shared.value(for: \.bingSearchEndpoint)
7476
)
7577

76-
#warning("request chat service to pass in the token length")
77-
7878
let result = try await bingSearch.search(
7979
query: arguments.query,
80-
numberOfResult: UserDefaults.shared.value(for: \.chatGPTMaxToken) > 5000 ? 5 : 3,
80+
numberOfResult: maxTokens > 5000 ? 5 : 3,
8181
freshness: arguments.freshness
8282
)
8383

Core/Sources/ChatContextCollectors/WebChatContextCollector/WebChatContextCollector.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ public final class WebChatContextCollector: ChatContextCollector {
1010
public func generateContext(
1111
history: [ChatMessage],
1212
scopes: Set<String>,
13-
content: String
13+
content: String,
14+
configuration: ChatGPTConfiguration
1415
) -> ChatContext? {
1516
guard scopes.contains("web") || scopes.contains("w") else { return nil }
1617
let links = Self.detectLinks(from: history) + Self.detectLinks(from: content)
1718
let functions: [(any ChatGPTFunction)?] = [
18-
SearchFunction(),
19+
SearchFunction(maxTokens: configuration.maxTokens),
1920
// allow this function only when there is a link in the memory.
2021
links.isEmpty ? nil : QueryWebsiteFunction(),
2122
]

Core/Sources/ChatService/ContextAwareAutoManagedChatGPTMemory.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public final class ContextAwareAutoManagedChatGPTMemory: ChatGPTMemory {
3535
contextController = DynamicContextController(
3636
memory: memory,
3737
functionProvider: functionProvider,
38+
configuration: configuration,
3839
contextCollectors: allContextCollectors
3940
)
4041
self.functionProvider = functionProvider

Core/Sources/ChatService/DynamicContextController.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,50 @@ final class DynamicContextController {
99
let contextCollectors: [ChatContextCollector]
1010
let memory: AutoManagedChatGPTMemory
1111
let functionProvider: ChatFunctionProvider
12+
let configuration: ChatGPTConfiguration
1213
var defaultScopes = [] as Set<String>
1314

1415
convenience init(
1516
memory: AutoManagedChatGPTMemory,
1617
functionProvider: ChatFunctionProvider,
18+
configuration: ChatGPTConfiguration,
1719
contextCollectors: ChatContextCollector...
1820
) {
1921
self.init(
2022
memory: memory,
2123
functionProvider: functionProvider,
24+
configuration: configuration,
2225
contextCollectors: contextCollectors
2326
)
2427
}
2528

2629
init(
2730
memory: AutoManagedChatGPTMemory,
2831
functionProvider: ChatFunctionProvider,
32+
configuration: ChatGPTConfiguration,
2933
contextCollectors: [ChatContextCollector]
3034
) {
3135
self.memory = memory
3236
self.functionProvider = functionProvider
37+
self.configuration = configuration
3338
self.contextCollectors = contextCollectors
3439
}
3540

3641
func updatePromptToMatchContent(systemPrompt: String, content: String) async throws {
3742
var content = content
3843
var scopes = Self.parseScopes(&content)
3944
scopes.formUnion(defaultScopes)
40-
45+
4146
functionProvider.removeAll()
4247
let language = UserDefaults.shared.value(for: \.chatGPTLanguage)
4348
let oldMessages = await memory.history
4449
let contexts = contextCollectors.compactMap {
45-
$0.generateContext(history: oldMessages, scopes: scopes, content: content)
50+
$0.generateContext(
51+
history: oldMessages,
52+
scopes: scopes,
53+
content: content,
54+
configuration: configuration
55+
)
4656
}
4757
let contextualSystemPrompt = """
4858
\(language.isEmpty ? "" : "You must always reply in \(language)")

0 commit comments

Comments
 (0)