3
3
using System . IO ;
4
4
using System . Linq ;
5
5
using System . Runtime . CompilerServices ;
6
+ using System . Text ;
6
7
using System . Text . Json ;
7
8
using System . Threading ;
8
9
using System . Threading . Tasks ;
@@ -171,7 +172,7 @@ public SessionState GetSessionState()
171
172
{
172
173
var executorState = ( ( StatefulExecutorBase ) Executor ) . GetStateData ( ) ;
173
174
return new SessionState (
174
- executorState . PastTokensCount > 0
175
+ executorState . PastTokensCount > 0
175
176
? Executor . Context . GetState ( ) : null ,
176
177
executorState ,
177
178
History ,
@@ -227,7 +228,7 @@ public void LoadSession(string path, bool loadTransforms = true)
227
228
if ( state . ExecutorState is null )
228
229
{
229
230
var executorPath = Path . Combine ( path , EXECUTOR_STATE_FILENAME ) ;
230
- ( ( StatefulExecutorBase ) Executor ) . LoadState ( filename : executorPath ) ;
231
+ ( ( StatefulExecutorBase ) Executor ) . LoadState ( filename : executorPath ) ;
231
232
}
232
233
LoadSession ( state , loadTransforms ) ;
233
234
}
@@ -441,21 +442,21 @@ public async IAsyncEnumerable<string> ChatAsync(
441
442
prompt = HistoryTransform . HistoryToText ( singleMessageHistory ) ;
442
443
}
443
444
444
- string assistantMessage = string . Empty ;
445
+ StringBuilder assistantMessage = new ( ) ;
445
446
446
- await foreach (
447
- string textToken
448
- in ChatAsyncInternal (
449
- prompt ,
450
- inferenceParams ,
451
- cancellationToken ) )
447
+ try
452
448
{
453
- assistantMessage += textToken ;
454
- yield return textToken ;
449
+ await foreach ( var textToken in ChatAsyncInternal ( prompt , inferenceParams , cancellationToken ) )
450
+ {
451
+ assistantMessage . Append ( textToken ) ;
452
+ yield return textToken ;
453
+ }
454
+ }
455
+ finally
456
+ {
457
+ // Add the assistant message to the history
458
+ AddAssistantMessage ( assistantMessage . ToString ( ) ) ;
455
459
}
456
-
457
- // Add the assistant message to the history
458
- AddAssistantMessage ( assistantMessage ) ;
459
460
}
460
461
461
462
/// <summary>
@@ -624,7 +625,7 @@ public record SessionState
624
625
/// <summary>
625
626
/// The input transform pipeline used in this session.
626
627
/// </summary>
627
- public ITextTransform [ ] InputTransformPipeline { get ; set ; } = [ ] ;
628
+ public ITextTransform [ ] InputTransformPipeline { get ; set ; } = [ ] ;
628
629
629
630
/// <summary>
630
631
/// The output transform used in this session.
@@ -635,11 +636,11 @@ public record SessionState
635
636
/// The history transform used in this session.
636
637
/// </summary>
637
638
public IHistoryTransform HistoryTransform { get ; set ; } = new LLamaTransforms . DefaultHistoryTransform ( ) ;
638
-
639
+
639
640
/// <summary>
640
641
/// The chat history messages for this session.
641
642
/// </summary>
642
- public ChatHistory . Message [ ] History { get ; set ; } = [ ] ;
643
+ public ChatHistory . Message [ ] History { get ; set ; } = [ ] ;
643
644
644
645
/// <summary>
645
646
/// Create a new session state.
@@ -651,7 +652,7 @@ public record SessionState
651
652
/// <param name="outputTransform"></param>
652
653
/// <param name="historyTransform"></param>
653
654
public SessionState (
654
- State ? contextState , ExecutorBaseState executorState ,
655
+ State ? contextState , ExecutorBaseState executorState ,
655
656
ChatHistory history , List < ITextTransform > inputTransformPipeline ,
656
657
ITextStreamTransform outputTransform , IHistoryTransform historyTransform )
657
658
{
@@ -738,22 +739,22 @@ public static SessionState Load(string path)
738
739
ITextTransform [ ] inputTransforms ;
739
740
try
740
741
{
741
- inputTransforms = File . Exists ( inputTransformFilepath ) ?
742
+ inputTransforms = File . Exists ( inputTransformFilepath ) ?
742
743
( JsonSerializer . Deserialize < ITextTransform [ ] > ( File . ReadAllText ( inputTransformFilepath ) )
743
744
?? throw new ArgumentException ( "Input transform file is invalid" , nameof ( path ) ) )
744
- : [ ] ;
745
+ : [ ] ;
745
746
}
746
747
catch ( JsonException )
747
748
{
748
749
throw new ArgumentException ( "Input transform file is invalid" , nameof ( path ) ) ;
749
750
}
750
751
751
752
string outputTransformFilepath = Path . Combine ( path , ChatSession . OUTPUT_TRANSFORM_FILENAME ) ;
752
-
753
+
753
754
ITextStreamTransform outputTransform ;
754
755
try
755
756
{
756
- outputTransform = File . Exists ( outputTransformFilepath ) ?
757
+ outputTransform = File . Exists ( outputTransformFilepath ) ?
757
758
( JsonSerializer . Deserialize < ITextStreamTransform > ( File . ReadAllText ( outputTransformFilepath ) )
758
759
?? throw new ArgumentException ( "Output transform file is invalid" , nameof ( path ) ) )
759
760
: new LLamaTransforms . EmptyTextOutputStreamTransform ( ) ;
@@ -767,7 +768,7 @@ public static SessionState Load(string path)
767
768
IHistoryTransform historyTransform ;
768
769
try
769
770
{
770
- historyTransform = File . Exists ( historyTransformFilepath ) ?
771
+ historyTransform = File . Exists ( historyTransformFilepath ) ?
771
772
( JsonSerializer . Deserialize < IHistoryTransform > ( File . ReadAllText ( historyTransformFilepath ) )
772
773
?? throw new ArgumentException ( "History transform file is invalid" , nameof ( path ) ) )
773
774
: new LLamaTransforms . DefaultHistoryTransform ( ) ;
0 commit comments