@@ -117,36 +117,38 @@ async Task<ChatResponse> IChatClient.GetResponseAsync(
117
117
async IAsyncEnumerable < ChatResponseUpdate > IChatClient . GetStreamingResponseAsync (
118
118
IList < ChatMessage > chatMessages , ChatOptions ? options , [ EnumeratorCancellation ] CancellationToken cancellationToken )
119
119
{
120
- // TODO: Implement full streaming support. For now, it just yields the CompleteAsync result.
121
-
122
- ChatResponse response = await ( ( IChatClient ) this ) . GetResponseAsync ( chatMessages , options , cancellationToken ) . ConfigureAwait ( false ) ;
120
+ CreateMessageParams request = CreateRequest ( chatMessages , options ) ;
121
+
122
+ IAsyncEnumerable < MessageStreamEvent > enumerable =
123
+ CreateMessageAsStreamAsync ( request , anthropicVersion : "2023-06-01" , cancellationToken ) ;
123
124
124
- for ( int i = 0 ; i < response . Choices . Count ; i ++ )
125
+ await foreach ( var response in enumerable . ConfigureAwait ( false ) )
125
126
{
126
- ChatMessage choice = response . Choices [ i ] ;
127
-
128
- yield return new ( )
127
+ var chatResponseUpdate = new ChatResponseUpdate ( ) ;
128
+ if ( response . IsContentBlockDelta )
129
129
{
130
- AdditionalProperties = choice . AdditionalProperties ,
131
- AuthorName = choice . AuthorName ,
132
- ChatThreadId = response . ChatThreadId ,
133
- ChoiceIndex = i ,
134
- ResponseId = response . ResponseId ,
135
- Contents = choice . Contents ,
136
- CreatedAt = response . CreatedAt ,
137
- FinishReason = response . FinishReason ,
138
- ModelId = response . ModelId ,
139
- RawRepresentation = choice . RawRepresentation ,
140
- Role = choice . Role ,
141
- } ;
142
- }
130
+ var delta = response . ContentBlockDelta ! . Delta ;
131
+ if ( delta . IsTextDelta )
132
+ {
133
+ chatResponseUpdate . Contents . Add ( new TextContent ( delta . TextDelta ! . Text ) { RawRepresentation = delta . TextDelta ! . Text } ) ;
134
+ }
135
+ }
143
136
144
- if ( response . Usage is not null )
145
- {
146
- yield return new ( )
147
- {
148
- Contents = [ new UsageContent ( response . Usage ) ] ,
149
- } ;
137
+ yield return chatResponseUpdate ;
138
+ // yield return new ChatResponseUpdate
139
+ // {
140
+ // //AdditionalProperties = response.AdditionalProperties,
141
+ // //AuthorName = choice.AuthorName,
142
+ // ChatThreadId = response.ChatThreadId,
143
+ // ChoiceIndex = i,
144
+ // ResponseId = response.ResponseId,
145
+ // Contents = choice.Contents,
146
+ // CreatedAt = response.CreatedAt,
147
+ // FinishReason = response.FinishReason,
148
+ // ModelId = response.ModelId,
149
+ // RawRepresentation = choice.RawRepresentation,
150
+ // Role = choice.Role,
151
+ // };
150
152
}
151
153
}
152
154
0 commit comments