Skip to content

Commit 60f4ff5

Browse files
committed
Adjust ChatContext
1 parent 81f723c commit 60f4ff5

File tree

7 files changed

+33
-27
lines changed

7 files changed

+33
-27
lines changed

Core/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/ActiveDocumentChatContextCollector.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
2727
var removedCode = context
2828
removedCode.focusedContext = nil
2929
return .init(
30-
systemPrompt: [
30+
systemPrompt: "",
31+
retrievedContent: [
3132
.init(
3233
content: extractSystemPrompt(removedCode),
3334
priority: .high
@@ -70,7 +71,8 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
7071
}
7172

7273
return .init(
73-
systemPrompt: [
74+
systemPrompt: "",
75+
retrievedContent: [
7476
.init(content: extractSystemPrompt(context), priority: .high)
7577
],
7678
functions: functions

Core/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/LegacyActiveDocumentChatContextCollector.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public struct LegacyActiveDocumentChatContextCollector: ChatContextCollector {
7878
}()
7979

8080
return .init(
81-
systemPrompt: [
81+
systemPrompt: "",
82+
retrievedContent: [
8283
.init(content: """
8384
Active Document Context:###
8485
Document Relative Path: \(relativePath)

Core/Sources/ChatContextCollectors/SystemInfoChatContextCollector/SystemInfoChatContextCollector.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,12 @@ public final class SystemInfoChatContextCollector: ChatContextCollector {
1818
configuration: ChatGPTConfiguration
1919
) -> ChatContext {
2020
return .init(
21-
systemPrompt: [
22-
.init(
23-
content: """
21+
systemPrompt: """
2422
Current Time: \(
2523
Self.dateFormatter.string(from: Date())
2624
) (You can use it to calculate time in another time zone)
2725
""",
28-
priority: .custom(999999)
29-
),
30-
],
26+
retrievedContent: [],
3127
functions: []
3228
)
3329
}

Core/Sources/ChatContextCollectors/WebChatContextCollector/WebChatContextCollector.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ public final class WebChatContextCollector: ChatContextCollector {
2121
links.isEmpty ? nil : QueryWebsiteFunction(),
2222
]
2323
return .init(
24-
systemPrompt: [
25-
.init(
26-
content: "You prefer to answer questions with latest content on the internet.",
27-
priority: .low
28-
),
29-
],
24+
systemPrompt: "You prefer to answer questions with latest content on the internet.",
25+
retrievedContent: [],
3026
functions: functions.compactMap { $0 }
3127
)
3228
}

Core/Sources/ChatService/DynamicContextController.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,19 @@ final class DynamicContextController {
6666
return contexts
6767
}
6868

69+
let extraSystemPrompt = contexts
70+
.map(\.systemPrompt)
71+
.filter { !$0.isEmpty }
72+
.joined(separator: "\n")
73+
6974
let contextPrompts = contexts
70-
.flatMap(\.systemPrompt)
75+
.flatMap(\.retrievedContent)
7176
.filter { !$0.content.isEmpty }
7277
.sorted { $0.priority > $1.priority }
7378

7479
let contextualSystemPrompt = """
7580
\(language.isEmpty ? "" : "You must always reply in \(language)")
76-
\(systemPrompt)
81+
\(systemPrompt)\(extraSystemPrompt.isEmpty ? "" : "\n\(extraSystemPrompt)")
7782
"""
7883
await memory.mutateSystemPrompt(contextualSystemPrompt)
7984
await memory.mutateRetrievedContent(contextPrompts.map(\.content))

Pro

Submodule Pro updated from 9b5986f to 8dc9244

Tool/Sources/ChatContextCollector/ChatContextCollector.swift

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import OpenAIService
33

44
public struct ChatContext {
5-
public struct RetrievedPrompt {
5+
public struct RetrievedContent {
66
public enum Priority: Equatable, Comparable {
77
case low
88
case medium
@@ -40,29 +40,35 @@ public struct ChatContext {
4040
}
4141
}
4242

43-
public var systemPrompt: [RetrievedPrompt]
43+
public var systemPrompt: String
44+
public var retrievedContent: [RetrievedContent]
4445
public var functions: [any ChatGPTFunction]
45-
public init(systemPrompt: [RetrievedPrompt], functions: [any ChatGPTFunction]) {
46+
public init(
47+
systemPrompt: String,
48+
retrievedContent: [RetrievedContent],
49+
functions: [any ChatGPTFunction]
50+
) {
4651
self.systemPrompt = systemPrompt
52+
self.retrievedContent = retrievedContent
4753
self.functions = functions
4854
}
49-
55+
5056
public static var empty: Self {
51-
.init(systemPrompt: [], functions: [])
57+
.init(systemPrompt: "", retrievedContent: [], functions: [])
5258
}
5359
}
5460

5561
public func + (
56-
lhs: ChatContext.RetrievedPrompt.Priority,
62+
lhs: ChatContext.RetrievedContent.Priority,
5763
rhs: Int
58-
) -> ChatContext.RetrievedPrompt.Priority {
64+
) -> ChatContext.RetrievedContent.Priority {
5965
.custom(lhs.rawValue + rhs)
6066
}
6167

6268
public func - (
63-
lhs: ChatContext.RetrievedPrompt.Priority,
69+
lhs: ChatContext.RetrievedContent.Priority,
6470
rhs: Int
65-
) -> ChatContext.RetrievedPrompt.Priority {
71+
) -> ChatContext.RetrievedContent.Priority {
6672
.custom(lhs.rawValue - rhs)
6773
}
6874

0 commit comments

Comments
 (0)