Skip to content

Commit b088572

Browse files
committed
Add hard limit for retrieved content to use no more than half of max tokens
1 parent 8f7f16a commit b088572

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

Tool/Sources/OpenAIService/Memory/AutoManagedChatGPTMemory.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ public actor AutoManagedChatGPTMemory: ChatGPTMemory {
113113
}
114114

115115
/// the available tokens count for retrieved content
116-
let availableTokenCountForRetrievedContent = availableTokenCountForMessages
117-
- messageTokenCount
116+
let availableTokenCountForRetrievedContent = min(
117+
availableTokenCountForMessages - messageTokenCount,
118+
configuration.maxTokens / 2
119+
)
118120
var retrievedContentTokenCount = 0
119121

120122
let separator = String(repeating: "=", count: 32) // only 1 token
@@ -138,7 +140,7 @@ public actor AutoManagedChatGPTMemory: ChatGPTMemory {
138140
139141
""") { break }
140142
} else {
141-
if !appendToSystemPrompt(separator) { break }
143+
if !appendToSystemPrompt("\n\(separator)\n") { break }
142144
}
143145

144146
if !appendToSystemPrompt(content) { break }
@@ -152,7 +154,7 @@ public actor AutoManagedChatGPTMemory: ChatGPTMemory {
152154
#if DEBUG
153155
Logger.service.info("""
154156
Sending tokens count
155-
- system prompt: \(smallestSystemPromptMessage)
157+
- system prompt: \(smallestSystemMessageTokenCount)
156158
- functions: \(functionTokenCount)
157159
- messages: \(messageTokenCount)
158160
- retrieved content: \(retrievedContentTokenCount)
@@ -162,7 +164,6 @@ public actor AutoManagedChatGPTMemory: ChatGPTMemory {
162164
+ messageTokenCount
163165
+ retrievedContentTokenCount
164166
)
165-
166167
""")
167168
#endif
168169

0 commit comments

Comments
 (0)