@@ -128,7 +128,7 @@ public async Task<SKContext> ExecutePlanAsync(
128
128
throw new SKException ( "ChatHistory is null." ) ;
129
129
}
130
130
131
- var startingMessageCount = chatHistory . Messages . Count ;
131
+ var startingMessageCount = chatHistory . Count ;
132
132
133
133
var stepsTaken = new List < SystemStep > ( ) ;
134
134
SystemStep ? lastStep = null ;
@@ -205,9 +205,9 @@ public async Task<SKContext> ExecutePlanAsync(
205
205
206
206
lastStep . OriginalResponse += step . OriginalResponse ;
207
207
step = lastStep ;
208
- if ( chatHistory . Messages . Count > startingMessageCount )
208
+ if ( chatHistory . Count > startingMessageCount )
209
209
{
210
- chatHistory . Messages . RemoveAt ( chatHistory . Messages . Count - 1 ) ;
210
+ chatHistory . RemoveAt ( chatHistory . Count - 1 ) ;
211
211
}
212
212
}
213
213
else
@@ -382,34 +382,28 @@ private Task<string> GetSystemMessage(SKContext context)
382
382
383
383
private Task < string > GetNextStepCompletion ( List < SystemStep > stepsTaken , ChatHistory chatHistory , IAIService aiService , int startingMessageCount , CancellationToken token )
384
384
{
385
- var tokenCount = this . GetChatHistoryTokens ( chatHistory ) ;
385
+ var skipStart = startingMessageCount ;
386
+ var skipCount = 0 ;
386
387
387
- var preserveFirstNSteps = 0 ;
388
- var removalIndex = ( startingMessageCount ) + preserveFirstNSteps ;
389
- var messagesRemoved = 0 ;
390
388
string ? originalThought = null ;
391
- while ( tokenCount >= this . Config . MaxTokens && chatHistory . Messages . Count > removalIndex )
392
- {
393
- // something needs to be removed.
394
- if ( string . IsNullOrEmpty ( originalThought ) )
395
- {
396
- originalThought = stepsTaken [ 0 ] . Thought ;
397
- }
398
389
399
- // Update message history
400
- chatHistory . AddAssistantMessage ( $ "{ Thought } { originalThought } ") ;
401
- preserveFirstNSteps ++ ;
402
- chatHistory . AddAssistantMessage ( "... I've removed some of my previous work to make room for the new stuff ..." ) ;
403
- preserveFirstNSteps ++ ;
390
+ var tokenCount = chatHistory . GetTokenCount ( ) ;
391
+ while ( tokenCount >= this . Config . MaxTokens && chatHistory . Count > skipStart )
392
+ {
393
+ originalThought = $ "{ Thought } { stepsTaken . FirstOrDefault ( ) ? . Thought } ";
394
+ tokenCount = chatHistory . GetTokenCount ( $ "{ originalThought } \n { TrimMessage } ", skipStart , ++ skipCount ) ;
395
+ }
404
396
405
- removalIndex = ( startingMessageCount ) + preserveFirstNSteps ;
397
+ var reducedChatHistory = new ChatHistory ( ) ;
398
+ reducedChatHistory . AddRange ( chatHistory . Where ( ( m , i ) => i < skipStart || i >= skipStart + skipCount ) ) ;
406
399
407
- chatHistory . Messages . RemoveAt ( removalIndex ) ;
408
- tokenCount = this . GetChatHistoryTokens ( chatHistory ) ;
409
- messagesRemoved ++ ;
400
+ if ( skipCount > 0 && originalThought is not null )
401
+ {
402
+ reducedChatHistory . InsertMessage ( skipStart , AuthorRole . Assistant , TrimMessage ) ;
403
+ reducedChatHistory . InsertMessage ( skipStart , AuthorRole . Assistant , originalThought ) ;
410
404
}
411
405
412
- return this . GetCompletionAsync ( aiService , chatHistory , stepsTaken . Count == 0 , token ) ;
406
+ return this . GetCompletionAsync ( aiService , reducedChatHistory , stepsTaken . Count == 0 , token ) ;
413
407
}
414
408
415
409
private async Task < string > GetCompletionAsync ( IAIService aiService , ChatHistory chatHistory , bool addThought , CancellationToken token )
@@ -421,7 +415,7 @@ private async Task<string> GetCompletionAsync(IAIService aiService, ChatHistory
421
415
}
422
416
else if ( aiService is ITextCompletion textCompletion )
423
417
{
424
- var thoughtProcess = string . Join ( "\n " , chatHistory . Messages . Select ( m => m . Content ) ) ;
418
+ var thoughtProcess = string . Join ( "\n " , chatHistory . Select ( m => m . Content ) ) ;
425
419
426
420
// Add Thought to the thought process at the start of the first iteration
427
421
if ( addThought )
@@ -444,13 +438,6 @@ private async Task<string> GetCompletionAsync(IAIService aiService, ChatHistory
444
438
throw new SKException ( "No AIService available for getting completions." ) ;
445
439
}
446
440
447
- private int GetChatHistoryTokens ( ChatHistory chatHistory )
448
- {
449
- var messages = string . Join ( "\n " , chatHistory . Messages ) ;
450
- var tokenCount = messages . Length / 4 ;
451
- return tokenCount ;
452
- }
453
-
454
441
/// <summary>
455
442
/// Parse LLM response into a SystemStep during execution
456
443
/// </summary>
@@ -749,6 +736,11 @@ private static string ToFullyQualifiedName(FunctionView function)
749
736
/// </summary>
750
737
private const string Observation = "[OBSERVATION]" ;
751
738
739
+ /// <summary>
740
+ /// The chat message to include when trimming thought process history
741
+ /// </summary>
742
+ private const string TrimMessage = "... I've removed some of my previous work to make room for the new stuff ..." ;
743
+
752
744
/// <summary>
753
745
/// The regex for parsing the thought response
754
746
/// </summary>
0 commit comments