@@ -205,17 +205,18 @@ fn get_image_content_block_from_url(url: &str) -> Result<bedrock::types::Content
205
205
206
206
let kind = infer:: get ( & bytes) ;
207
207
208
- if kind. is_none ( ) {
209
- return Err ( custom_error (
210
- llm:: ErrorCode :: InvalidRequest ,
211
- format ! (
212
- "Could not infer the mime type of the image downloaded from url: {}" ,
213
- url
214
- ) ,
215
- ) ) ;
216
- }
217
-
218
- let mime = str_to_bedrock_mime_type ( kind. unwrap ( ) . mime_type ( ) ) ?;
208
+ let mime = match kind {
209
+ Some ( kind) => str_to_bedrock_mime_type ( kind. mime_type ( ) ) ?,
210
+ None => {
211
+ return Err ( custom_error (
212
+ llm:: ErrorCode :: InvalidRequest ,
213
+ format ! (
214
+ "Could not infer the mime type of the image downloaded from url: {}" ,
215
+ url
216
+ ) ,
217
+ ) ) ;
218
+ }
219
+ } ;
219
220
220
221
Ok ( bedrock:: types:: ContentBlock :: Image (
221
222
ImageBlock :: builder ( )
@@ -280,24 +281,22 @@ pub fn converse_output_to_tool_calls(
280
281
. to_owned ( ) ,
281
282
) ) ?;
282
283
283
- if ! output. is_message ( ) {
284
- return Err ( custom_error (
284
+ match output. as_message ( ) {
285
+ Err ( _ ) => Err ( custom_error (
285
286
llm:: ErrorCode :: InternalError ,
286
287
"An error occurred while converting to tool calls: expected output to be a Message"
287
288
. to_owned ( ) ,
288
- ) ) ;
289
- }
290
-
291
- let message = output . as_message ( ) . unwrap ( ) . clone ( ) ;
292
- let mut tool_calls : Vec < llm :: ToolCall > = vec ! [ ] ;
293
-
294
- for block in message . content {
295
- if let bedrock :: types :: ContentBlock :: ToolUse ( tool ) = block {
296
- tool_calls . push ( bedrock_tool_use_to_llm_tool_call ( tool ) ? ) ;
289
+ ) ) ,
290
+ Ok ( message ) => {
291
+ let mut tool_calls : Vec < llm :: ToolCall > = vec ! [ ] ;
292
+ for block in message . content . clone ( ) {
293
+ if let bedrock :: types :: ContentBlock :: ToolUse ( tool ) = block {
294
+ tool_calls . push ( bedrock_tool_use_to_llm_tool_call ( tool ) ? ) ;
295
+ }
296
+ }
297
+ Ok ( tool_calls )
297
298
}
298
299
}
299
-
300
- Ok ( tool_calls)
301
300
}
302
301
303
302
pub fn converse_output_to_complete_response (
@@ -309,44 +308,46 @@ pub fn converse_output_to_complete_response(
309
308
. to_owned ( ) ,
310
309
) ) ?;
311
310
312
- if ! output. is_message ( ) {
313
- return Err ( custom_error (
311
+ match output. as_message ( ) {
312
+ Err ( _ ) => Err ( custom_error (
314
313
llm:: ErrorCode :: InternalError ,
315
- "An error occurred while converting to complete response: expected output to be a Message" . to_owned ( ) ,
316
- ) ) ;
317
- }
318
-
319
- let message = output. as_message ( ) . unwrap ( ) . clone ( ) ;
320
-
321
- let mut content_parts: Vec < llm:: ContentPart > = vec ! [ ] ;
322
- let mut tool_calls: Vec < llm:: ToolCall > = vec ! [ ] ;
323
- for block in message. content {
324
- match block {
325
- bedrock:: types:: ContentBlock :: Text ( text) => {
326
- content_parts. push ( llm:: ContentPart :: Text ( text. to_owned ( ) ) ) ;
327
- }
328
- bedrock:: types:: ContentBlock :: Image ( image) => {
329
- content_parts. push ( bedrock_image_to_llm_content_part ( image) ) ;
330
- }
331
- bedrock:: types:: ContentBlock :: ToolUse ( tool) => {
332
- tool_calls. push ( bedrock_tool_use_to_llm_tool_call ( tool) ?) ;
314
+ "An error occurred while converting to complete response: expected output to be a Message"
315
+ . to_owned ( ) ,
316
+ ) ) ,
317
+ Ok ( message) => {
318
+ let mut content_parts: Vec < llm:: ContentPart > = vec ! [ ] ;
319
+ let mut tool_calls: Vec < llm:: ToolCall > = vec ! [ ] ;
320
+ for block in message. content . clone ( ) {
321
+ match block {
322
+ bedrock:: types:: ContentBlock :: Text ( text) => {
323
+ content_parts. push ( llm:: ContentPart :: Text ( text. to_owned ( ) ) ) ;
324
+ }
325
+ bedrock:: types:: ContentBlock :: Image ( image) => {
326
+ content_parts. push ( bedrock_image_to_llm_content_part ( image) ) ;
327
+ }
328
+ bedrock:: types:: ContentBlock :: ToolUse ( tool) => {
329
+ tool_calls. push ( bedrock_tool_use_to_llm_tool_call ( tool) ?) ;
330
+ }
331
+ _ => { }
332
+ }
333
333
}
334
- _ => { }
334
+
335
+ let metadata = converse_output_to_response_metadata ( & response) ;
336
+
337
+ Ok ( llm:: CompleteResponse {
338
+ // bedrock does not return an id as part of the response struct.
339
+ // there may be one present in `additional_model_response_fields`
340
+ // but the schema varies depending on the model being invoked. Leaving it empty for now
341
+ // until we have a better solution for this.
342
+ id : "" . to_owned ( ) ,
343
+ content : content_parts,
344
+ tool_calls,
345
+ metadata,
346
+ } )
347
+
335
348
}
336
349
}
337
350
338
- let metadata = converse_output_to_response_metadata ( & response) ;
339
-
340
- Ok ( llm:: CompleteResponse {
341
- // bedrock does not return an id as part of the response struct.
342
- // there may be one present in `additional_model_response_fields`
343
- // but the schema varies depending on the model being invoked. Leaving it empty for now
344
- // until we have a better solution for this.
345
- id : "" . to_owned ( ) ,
346
- content : content_parts,
347
- tool_calls,
348
- metadata,
349
- } )
350
351
}
351
352
352
353
fn bedrock_tool_use_to_llm_tool_call ( tool : ToolUseBlock ) -> Result < llm:: ToolCall , llm:: Error > {
@@ -445,13 +446,12 @@ pub fn converse_stream_output_to_stream_event(
445
446
446
447
fn process_content_block_start_event ( block : ContentBlockStartEvent ) -> Option < llm:: StreamEvent > {
447
448
if let Some ( start_info) = block. start {
448
- if start_info. is_tool_use ( ) {
449
- let tool_use = start_info. as_tool_use ( ) . unwrap ( ) . clone ( ) ;
449
+ if let Ok ( tool_use) = start_info. as_tool_use ( ) {
450
450
return Some ( llm:: StreamEvent :: Delta ( llm:: StreamDelta {
451
451
content : None ,
452
452
tool_calls : Some ( vec ! [ llm:: ToolCall {
453
- id: tool_use. tool_use_id,
454
- name: tool_use. name,
453
+ id: tool_use. tool_use_id. clone ( ) ,
454
+ name: tool_use. name. clone ( ) ,
455
455
arguments_json: "" . to_owned( ) ,
456
456
} ] ) ,
457
457
} ) ) ;
@@ -462,20 +462,18 @@ fn process_content_block_start_event(block: ContentBlockStartEvent) -> Option<ll
462
462
463
463
fn process_content_block_delta_event ( block : ContentBlockDeltaEvent ) -> Option < llm:: StreamEvent > {
464
464
if let Some ( block_info) = block. delta {
465
- if block_info. is_tool_use ( ) {
466
- let tool_use = block_info. as_tool_use ( ) . unwrap ( ) . clone ( ) ;
465
+ if let Ok ( tool_use) = block_info. as_tool_use ( ) {
467
466
return Some ( llm:: StreamEvent :: Delta ( llm:: StreamDelta {
468
467
content : None ,
469
468
tool_calls : Some ( vec ! [ llm:: ToolCall {
470
469
id: "" . to_owned( ) ,
471
470
name: "" . to_owned( ) ,
472
- arguments_json: tool_use. input,
471
+ arguments_json: tool_use. input. clone ( ) ,
473
472
} ] ) ,
474
473
} ) ) ;
475
- } else if block_info. is_text ( ) {
476
- let text = block_info. as_text ( ) . unwrap ( ) . clone ( ) ;
474
+ } else if let Ok ( text) = block_info. as_text ( ) {
477
475
return Some ( llm:: StreamEvent :: Delta ( llm:: StreamDelta {
478
- content : Some ( vec ! [ llm:: ContentPart :: Text ( text) ] ) ,
476
+ content : Some ( vec ! [ llm:: ContentPart :: Text ( text. clone ( ) ) ] ) ,
479
477
tool_calls : None ,
480
478
} ) ) ;
481
479
}
0 commit comments