File tree Expand file tree Collapse file tree
Core/Sources/OpenAIService Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -207,14 +207,16 @@ extension ChatGPTService {
207207 }
208208
209209 func combineHistoryWithSystemPrompt( ) -> [ CompletionRequestBody . Message ] {
210- if history. count > 5 {
211- return [ . init( role: . system, content: systemPrompt) ] +
212- history[ history. endIndex - 5 ..< history. endIndex] . map {
213- . init( role: $0. role, content: $0. content)
214- }
215- }
216- return [ . init( role: . system, content: systemPrompt) ] + history. map {
217- . init( role: $0. role, content: $0. content)
210+ var all : [ CompletionRequestBody . Message ] = [ ]
211+ var count = 0
212+ for message in history. reversed ( ) {
213+ if count >= 5 { break }
214+ if message. content. isEmpty { continue }
215+ all. append ( . init( role: message. role, content: message. content) )
216+ count += 1
218217 }
218+
219+ all. append ( . init( role: . system, content: systemPrompt) )
220+ return all. reversed ( )
219221 }
220222}
You can’t perform that action at this time.
0 commit comments