@@ -64,18 +64,18 @@ public void Dispose()
64
64
65
65
/// <inheritdoc />
66
66
public async Task < ChatResponse > GetResponseAsync (
67
- IList < ChatMessage > chatMessages , ChatOptions ? options = null , CancellationToken cancellationToken = default )
67
+ IEnumerable < ChatMessage > messages , ChatOptions ? options = null , CancellationToken cancellationToken = default )
68
68
{
69
- if ( chatMessages is null )
69
+ if ( messages is null )
70
70
{
71
- throw new ArgumentNullException ( nameof ( chatMessages ) ) ;
71
+ throw new ArgumentNullException ( nameof ( messages ) ) ;
72
72
}
73
73
74
74
ConverseRequest request = new ( )
75
75
{
76
76
ModelId = options ? . ModelId ?? _modelId ,
77
- Messages = CreateMessages ( chatMessages ) ,
78
- System = CreateSystem ( chatMessages ) ,
77
+ Messages = CreateMessages ( messages ) ,
78
+ System = CreateSystem ( messages ) ,
79
79
ToolConfig = CreateToolConfig ( options ) ,
80
80
InferenceConfig = CreateInferenceConfiguration ( options ) ,
81
81
AdditionalModelRequestFields = CreateAdditionalModelRequestFields ( options ) ,
@@ -138,18 +138,18 @@ public async Task<ChatResponse> GetResponseAsync(
138
138
139
139
/// <inheritdoc />
140
140
public async IAsyncEnumerable < ChatResponseUpdate > GetStreamingResponseAsync (
141
- IList < ChatMessage > chatMessages , ChatOptions ? options = null , [ EnumeratorCancellation ] CancellationToken cancellationToken = default )
141
+ IEnumerable < ChatMessage > messages , ChatOptions ? options = null , [ EnumeratorCancellation ] CancellationToken cancellationToken = default )
142
142
{
143
- if ( chatMessages is null )
143
+ if ( messages is null )
144
144
{
145
- throw new ArgumentNullException ( nameof ( chatMessages ) ) ;
145
+ throw new ArgumentNullException ( nameof ( messages ) ) ;
146
146
}
147
147
148
148
ConverseStreamRequest request = new ( )
149
149
{
150
150
ModelId = options ? . ModelId ?? _modelId ,
151
- Messages = CreateMessages ( chatMessages ) ,
152
- System = CreateSystem ( chatMessages ) ,
151
+ Messages = CreateMessages ( messages ) ,
152
+ System = CreateSystem ( messages ) ,
153
153
ToolConfig = CreateToolConfig ( options ) ,
154
154
InferenceConfig = CreateInferenceConfiguration ( options ) ,
155
155
AdditionalModelRequestFields = CreateAdditionalModelRequestFields ( options ) ,
@@ -186,11 +186,9 @@ public async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(
186
186
187
187
if ( contentBlockDelta . Delta . Text is string text )
188
188
{
189
- yield return new ( )
189
+ yield return new ( ChatRole . Assistant , text )
190
190
{
191
- Role = ChatRole . Assistant ,
192
191
FinishReason = finishReason ,
193
- Text = text ,
194
192
} ;
195
193
}
196
194
break ;
@@ -281,9 +279,9 @@ private static ChatFinishReason GetChatFinishReason(StopReason stopReason) =>
281
279
_ => new ( stopReason . Value ) ,
282
280
} ;
283
281
284
- /// <summary>Creates a list of <see cref="SystemContentBlock"/> from the system messages in the provided <paramref name="chatMessages "/>.</summary>
285
- private static List < SystemContentBlock > CreateSystem ( IList < ChatMessage > chatMessages ) =>
286
- chatMessages
282
+ /// <summary>Creates a list of <see cref="SystemContentBlock"/> from the system messages in the provided <paramref name="messages "/>.</summary>
283
+ private static List < SystemContentBlock > CreateSystem ( IEnumerable < ChatMessage > messages ) =>
284
+ messages
287
285
. Where ( m => m . Role == ChatRole . System && m . Contents . Any ( c => c is TextContent ) )
288
286
. Select ( m => new SystemContentBlock ( ) { Text = string . Concat ( m . Contents . OfType < TextContent > ( ) ) } )
289
287
. ToList ( ) ;
@@ -308,7 +306,7 @@ private static List<SystemContentBlock> CreateSystem(IList<ChatMessage> chatMess
308
306
}
309
307
310
308
/// <summary>Creates a list of <see cref="Message"/> from the provided <paramref name="chatMessages"/>.</summary>
311
- private static List < Message > CreateMessages ( IList < ChatMessage > chatMessages )
309
+ private static List < Message > CreateMessages ( IEnumerable < ChatMessage > chatMessages )
312
310
{
313
311
List < Message > messages = [ ] ;
314
312
@@ -342,14 +340,14 @@ private static List<ContentBlock> CreateContents(ChatMessage message)
342
340
contents . Add ( new ( ) { Text = tc . Text } ) ;
343
341
break ;
344
342
345
- case DataContent dc when dc . Data . HasValue :
343
+ case DataContent dc :
346
344
if ( GetImageFormat ( dc . MediaType ) is ImageFormat imageFormat )
347
345
{
348
346
contents . Add ( new ( )
349
347
{
350
348
Image = new ( )
351
349
{
352
- Source = new ( ) { Bytes = new ( dc . Data ! . Value . ToArray ( ) ) } ,
350
+ Source = new ( ) { Bytes = new ( dc . Data . ToArray ( ) ) } ,
353
351
Format = imageFormat ,
354
352
}
355
353
} ) ;
@@ -360,7 +358,7 @@ private static List<ContentBlock> CreateContents(ChatMessage message)
360
358
{
361
359
Video = new ( )
362
360
{
363
- Source = new ( ) { Bytes = new ( dc . Data ! . Value . ToArray ( ) ) } ,
361
+ Source = new ( ) { Bytes = new ( dc . Data . ToArray ( ) ) } ,
364
362
Format = videoFormat ,
365
363
}
366
364
} ) ;
@@ -371,7 +369,7 @@ private static List<ContentBlock> CreateContents(ChatMessage message)
371
369
{
372
370
Document = new ( )
373
371
{
374
- Source = new ( ) { Bytes = new ( dc . Data ! . Value . ToArray ( ) ) } ,
372
+ Source = new ( ) { Bytes = new ( dc . Data . ToArray ( ) ) } ,
375
373
Format = docFormat ,
376
374
}
377
375
} ) ;
@@ -436,19 +434,18 @@ private static List<ContentBlock> CreateContents(ChatMessage message)
436
434
} ;
437
435
438
436
/// <summary>Gets the MIME type for a <see cref="DocumentFormat"/>.</summary>
439
- private static string ? GetMimeType ( DocumentFormat ? format ) =>
437
+ private static string GetMimeType ( DocumentFormat ? format ) =>
440
438
format ? . Value switch
441
439
{
442
440
"csv" => "text/csv" ,
443
441
"html" => "text/html" ,
444
442
"md" => "text/markdown" ,
445
- "txt" => "text/plain" ,
446
443
"pdf" => "application/pdf" ,
447
444
"doc" => "application/msword" ,
448
445
"docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ,
449
446
"xls" => "application/vnd.ms-excel" ,
450
447
"xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ,
451
- _ => null ,
448
+ _ => "text/plain" ,
452
449
} ;
453
450
454
451
/// <summary>Gets the <see cref="ImageFormat"/> for the specified MIME type.</summary>
@@ -463,14 +460,13 @@ private static List<ContentBlock> CreateContents(ChatMessage message)
463
460
} ;
464
461
465
462
/// <summary>Gets the MIME type for a <see cref="ImageFormat"/>.</summary>
466
- private static string ? GetMimeType ( ImageFormat ? format ) =>
463
+ private static string GetMimeType ( ImageFormat ? format ) =>
467
464
format ? . Value switch
468
465
{
469
- "jpeg" => "image/jpeg" ,
470
466
"png" => "image/png" ,
471
467
"gif" => "image/gif" ,
472
468
"webp" => "image/webp" ,
473
- _ => null ,
469
+ _ => "image/jpeg" ,
474
470
} ;
475
471
476
472
/// <summary>Gets the <see cref="VideoFormat"/> for the specified MIME type.</summary>
@@ -489,18 +485,17 @@ private static List<ContentBlock> CreateContents(ChatMessage message)
489
485
} ;
490
486
491
487
/// <summary>Gets the MIME type for a <see cref="VideoFormat"/>.</summary>
492
- private static string ? GetMimeType ( VideoFormat ? format ) =>
488
+ private static string GetMimeType ( VideoFormat ? format ) =>
493
489
format ? . Value switch
494
490
{
495
491
"flv" => "video/x-flv" ,
496
492
"mkv" => "video/x-matroska" ,
497
493
"mov" => "video/quicktime" ,
498
- "mp4" => "video/mp4" ,
499
494
"mpeg" or "mpg" => "video/mpeg" ,
500
495
"three_gp" => "video/3gpp" ,
501
496
"webm" => "video/webm" ,
502
497
"wmv" => "video/x-ms-wmv" ,
503
- _ => null ,
498
+ _ => "video/mp4" ,
504
499
} ;
505
500
506
501
/// <summary>Converts a <see cref="Dictionary{String, Object}"/> to a <see cref="Document"/>.</summary>
0 commit comments