@@ -64,18 +64,18 @@ public void Dispose()
6464
6565 /// <inheritdoc />
6666 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 )
6868 {
69- if ( chatMessages is null )
69+ if ( messages is null )
7070 {
71- throw new ArgumentNullException ( nameof ( chatMessages ) ) ;
71+ throw new ArgumentNullException ( nameof ( messages ) ) ;
7272 }
7373
7474 ConverseRequest request = new ( )
7575 {
7676 ModelId = options ? . ModelId ?? _modelId ,
77- Messages = CreateMessages ( chatMessages ) ,
78- System = CreateSystem ( chatMessages ) ,
77+ Messages = CreateMessages ( messages ) ,
78+ System = CreateSystem ( messages ) ,
7979 ToolConfig = CreateToolConfig ( options ) ,
8080 InferenceConfig = CreateInferenceConfiguration ( options ) ,
8181 AdditionalModelRequestFields = CreateAdditionalModelRequestFields ( options ) ,
@@ -138,18 +138,18 @@ public async Task<ChatResponse> GetResponseAsync(
138138
139139 /// <inheritdoc />
140140 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 )
142142 {
143- if ( chatMessages is null )
143+ if ( messages is null )
144144 {
145- throw new ArgumentNullException ( nameof ( chatMessages ) ) ;
145+ throw new ArgumentNullException ( nameof ( messages ) ) ;
146146 }
147147
148148 ConverseStreamRequest request = new ( )
149149 {
150150 ModelId = options ? . ModelId ?? _modelId ,
151- Messages = CreateMessages ( chatMessages ) ,
152- System = CreateSystem ( chatMessages ) ,
151+ Messages = CreateMessages ( messages ) ,
152+ System = CreateSystem ( messages ) ,
153153 ToolConfig = CreateToolConfig ( options ) ,
154154 InferenceConfig = CreateInferenceConfiguration ( options ) ,
155155 AdditionalModelRequestFields = CreateAdditionalModelRequestFields ( options ) ,
@@ -186,11 +186,9 @@ public async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(
186186
187187 if ( contentBlockDelta . Delta . Text is string text )
188188 {
189- yield return new ( )
189+ yield return new ( ChatRole . Assistant , text )
190190 {
191- Role = ChatRole . Assistant ,
192191 FinishReason = finishReason ,
193- Text = text ,
194192 } ;
195193 }
196194 break ;
@@ -281,9 +279,9 @@ private static ChatFinishReason GetChatFinishReason(StopReason stopReason) =>
281279 _ => new ( stopReason . Value ) ,
282280 } ;
283281
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
287285 . Where ( m => m . Role == ChatRole . System && m . Contents . Any ( c => c is TextContent ) )
288286 . Select ( m => new SystemContentBlock ( ) { Text = string . Concat ( m . Contents . OfType < TextContent > ( ) ) } )
289287 . ToList ( ) ;
@@ -308,7 +306,7 @@ private static List<SystemContentBlock> CreateSystem(IList<ChatMessage> chatMess
308306 }
309307
310308 /// <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 )
312310 {
313311 List < Message > messages = [ ] ;
314312
@@ -342,14 +340,14 @@ private static List<ContentBlock> CreateContents(ChatMessage message)
342340 contents . Add ( new ( ) { Text = tc . Text } ) ;
343341 break ;
344342
345- case DataContent dc when dc . Data . HasValue :
343+ case DataContent dc :
346344 if ( GetImageFormat ( dc . MediaType ) is ImageFormat imageFormat )
347345 {
348346 contents . Add ( new ( )
349347 {
350348 Image = new ( )
351349 {
352- Source = new ( ) { Bytes = new ( dc . Data ! . Value . ToArray ( ) ) } ,
350+ Source = new ( ) { Bytes = new ( dc . Data . ToArray ( ) ) } ,
353351 Format = imageFormat ,
354352 }
355353 } ) ;
@@ -360,7 +358,7 @@ private static List<ContentBlock> CreateContents(ChatMessage message)
360358 {
361359 Video = new ( )
362360 {
363- Source = new ( ) { Bytes = new ( dc . Data ! . Value . ToArray ( ) ) } ,
361+ Source = new ( ) { Bytes = new ( dc . Data . ToArray ( ) ) } ,
364362 Format = videoFormat ,
365363 }
366364 } ) ;
@@ -371,7 +369,7 @@ private static List<ContentBlock> CreateContents(ChatMessage message)
371369 {
372370 Document = new ( )
373371 {
374- Source = new ( ) { Bytes = new ( dc . Data ! . Value . ToArray ( ) ) } ,
372+ Source = new ( ) { Bytes = new ( dc . Data . ToArray ( ) ) } ,
375373 Format = docFormat ,
376374 }
377375 } ) ;
@@ -436,19 +434,18 @@ private static List<ContentBlock> CreateContents(ChatMessage message)
436434 } ;
437435
438436 /// <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 ) =>
440438 format ? . Value switch
441439 {
442440 "csv" => "text/csv" ,
443441 "html" => "text/html" ,
444442 "md" => "text/markdown" ,
445- "txt" => "text/plain" ,
446443 "pdf" => "application/pdf" ,
447444 "doc" => "application/msword" ,
448445 "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ,
449446 "xls" => "application/vnd.ms-excel" ,
450447 "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ,
451- _ => null ,
448+ _ => "text/plain" ,
452449 } ;
453450
454451 /// <summary>Gets the <see cref="ImageFormat"/> for the specified MIME type.</summary>
@@ -463,14 +460,13 @@ private static List<ContentBlock> CreateContents(ChatMessage message)
463460 } ;
464461
465462 /// <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 ) =>
467464 format ? . Value switch
468465 {
469- "jpeg" => "image/jpeg" ,
470466 "png" => "image/png" ,
471467 "gif" => "image/gif" ,
472468 "webp" => "image/webp" ,
473- _ => null ,
469+ _ => "image/jpeg" ,
474470 } ;
475471
476472 /// <summary>Gets the <see cref="VideoFormat"/> for the specified MIME type.</summary>
@@ -489,18 +485,17 @@ private static List<ContentBlock> CreateContents(ChatMessage message)
489485 } ;
490486
491487 /// <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 ) =>
493489 format ? . Value switch
494490 {
495491 "flv" => "video/x-flv" ,
496492 "mkv" => "video/x-matroska" ,
497493 "mov" => "video/quicktime" ,
498- "mp4" => "video/mp4" ,
499494 "mpeg" or "mpg" => "video/mpeg" ,
500495 "three_gp" => "video/3gpp" ,
501496 "webm" => "video/webm" ,
502497 "wmv" => "video/x-ms-wmv" ,
503- _ => null ,
498+ _ => "video/mp4" ,
504499 } ;
505500
506501 /// <summary>Converts a <see cref="Dictionary{String, Object}"/> to a <see cref="Document"/>.</summary>
0 commit comments