llama-index-llms-bedrock-converse 0.10.6__py3-none-any.whl → 0.11.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- llama_index/llms/bedrock_converse/base.py +100 -35
- llama_index/llms/bedrock_converse/utils.py +68 -21
- {llama_index_llms_bedrock_converse-0.10.6.dist-info → llama_index_llms_bedrock_converse-0.11.0.dist-info}/METADATA +2 -2
- llama_index_llms_bedrock_converse-0.11.0.dist-info/RECORD +7 -0
- llama_index_llms_bedrock_converse-0.10.6.dist-info/RECORD +0 -7
- {llama_index_llms_bedrock_converse-0.10.6.dist-info → llama_index_llms_bedrock_converse-0.11.0.dist-info}/WHEEL +0 -0
- {llama_index_llms_bedrock_converse-0.10.6.dist-info → llama_index_llms_bedrock_converse-0.11.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -4,6 +4,7 @@ from typing import (
|
|
|
4
4
|
Callable,
|
|
5
5
|
Dict,
|
|
6
6
|
List,
|
|
7
|
+
Literal,
|
|
7
8
|
Optional,
|
|
8
9
|
Sequence,
|
|
9
10
|
Tuple,
|
|
@@ -23,6 +24,7 @@ from llama_index.core.base.llms.types import (
|
|
|
23
24
|
MessageRole,
|
|
24
25
|
TextBlock,
|
|
25
26
|
ThinkingBlock,
|
|
27
|
+
ToolCallBlock,
|
|
26
28
|
)
|
|
27
29
|
from llama_index.core.bridge.pydantic import Field, PrivateAttr
|
|
28
30
|
from llama_index.core.callbacks import CallbackManager
|
|
@@ -157,6 +159,13 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
157
159
|
guardrail_version: Optional[str] = Field(
|
|
158
160
|
description="The version number for the guardrail. The value can also be DRAFT"
|
|
159
161
|
)
|
|
162
|
+
guardrail_stream_processing_mode: Optional[Literal["sync", "async"]] = Field(
|
|
163
|
+
description=(
|
|
164
|
+
"The stream processing mode to use when leveraging a guardrail in a streaming request (ConverseStream). "
|
|
165
|
+
"If set, the specified mode will be included in the request's guardrail configuration object, altering the streaming response behavior. "
|
|
166
|
+
"If a value is not provided, no mode will be explicitly included in the request's guardrail configuration object, and thus Amazon Bedrock's default, Synchronous Mode, will be used."
|
|
167
|
+
)
|
|
168
|
+
)
|
|
160
169
|
application_inference_profile_arn: Optional[str] = Field(
|
|
161
170
|
description="The ARN of an application inference profile to invoke in place of the model. If provided, make sure the model argument refers to the same one underlying the application inference profile."
|
|
162
171
|
)
|
|
@@ -211,6 +220,7 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
211
220
|
output_parser: Optional[BaseOutputParser] = None,
|
|
212
221
|
guardrail_identifier: Optional[str] = None,
|
|
213
222
|
guardrail_version: Optional[str] = None,
|
|
223
|
+
guardrail_stream_processing_mode: Optional[Literal["sync", "async"]] = None,
|
|
214
224
|
application_inference_profile_arn: Optional[str] = None,
|
|
215
225
|
trace: Optional[str] = None,
|
|
216
226
|
thinking: Optional[ThinkingDict] = None,
|
|
@@ -263,6 +273,7 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
263
273
|
botocore_config=botocore_config,
|
|
264
274
|
guardrail_identifier=guardrail_identifier,
|
|
265
275
|
guardrail_version=guardrail_version,
|
|
276
|
+
guardrail_stream_processing_mode=guardrail_stream_processing_mode,
|
|
266
277
|
application_inference_profile_arn=application_inference_profile_arn,
|
|
267
278
|
trace=trace,
|
|
268
279
|
thinking=thinking,
|
|
@@ -355,7 +366,7 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
355
366
|
def _get_content_and_tool_calls(
|
|
356
367
|
self, response: Optional[Dict[str, Any]] = None, content: Dict[str, Any] = None
|
|
357
368
|
) -> Tuple[
|
|
358
|
-
List[Union[TextBlock, ThinkingBlock
|
|
369
|
+
List[Union[TextBlock, ThinkingBlock, ToolCallBlock]], List[str], List[str]
|
|
359
370
|
]:
|
|
360
371
|
assert response is not None or content is not None, (
|
|
361
372
|
f"Either response or content must be provided. Got response: {response}, content: {content}"
|
|
@@ -363,10 +374,9 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
363
374
|
assert response is None or content is None, (
|
|
364
375
|
f"Only one of response or content should be provided. Got response: {response}, content: {content}"
|
|
365
376
|
)
|
|
366
|
-
tool_calls = []
|
|
367
377
|
tool_call_ids = []
|
|
368
378
|
status = []
|
|
369
|
-
blocks = []
|
|
379
|
+
blocks: List[TextBlock | ThinkingBlock | ToolCallBlock] = []
|
|
370
380
|
if content is not None:
|
|
371
381
|
content_list = [content]
|
|
372
382
|
else:
|
|
@@ -391,7 +401,13 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
391
401
|
tool_usage["toolUseId"] = content_block["toolUseId"]
|
|
392
402
|
if "name" not in tool_usage:
|
|
393
403
|
tool_usage["name"] = content_block["name"]
|
|
394
|
-
|
|
404
|
+
blocks.append(
|
|
405
|
+
ToolCallBlock(
|
|
406
|
+
tool_name=tool_usage.get("name", ""),
|
|
407
|
+
tool_call_id=tool_usage.get("toolUseId"),
|
|
408
|
+
tool_kwargs=tool_usage.get("input", {}),
|
|
409
|
+
)
|
|
410
|
+
)
|
|
395
411
|
if tool_result := content_block.get("toolResult", None):
|
|
396
412
|
for tool_result_content in tool_result["content"]:
|
|
397
413
|
if text := tool_result_content.get("text", None):
|
|
@@ -399,7 +415,7 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
399
415
|
tool_call_ids.append(tool_result_content.get("toolUseId", ""))
|
|
400
416
|
status.append(tool_result.get("status", ""))
|
|
401
417
|
|
|
402
|
-
return blocks,
|
|
418
|
+
return blocks, tool_call_ids, status
|
|
403
419
|
|
|
404
420
|
@llm_chat_callback()
|
|
405
421
|
def chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse:
|
|
@@ -426,16 +442,13 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
426
442
|
**all_kwargs,
|
|
427
443
|
)
|
|
428
444
|
|
|
429
|
-
blocks,
|
|
430
|
-
response
|
|
431
|
-
)
|
|
445
|
+
blocks, tool_call_ids, status = self._get_content_and_tool_calls(response)
|
|
432
446
|
|
|
433
447
|
return ChatResponse(
|
|
434
448
|
message=ChatMessage(
|
|
435
449
|
role=MessageRole.ASSISTANT,
|
|
436
450
|
blocks=blocks,
|
|
437
451
|
additional_kwargs={
|
|
438
|
-
"tool_calls": tool_calls,
|
|
439
452
|
"tool_call_id": tool_call_ids,
|
|
440
453
|
"status": status,
|
|
441
454
|
},
|
|
@@ -474,6 +487,7 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
474
487
|
stream=True,
|
|
475
488
|
guardrail_identifier=self.guardrail_identifier,
|
|
476
489
|
guardrail_version=self.guardrail_version,
|
|
490
|
+
guardrail_stream_processing_mode=self.guardrail_stream_processing_mode,
|
|
477
491
|
trace=self.trace,
|
|
478
492
|
**all_kwargs,
|
|
479
493
|
)
|
|
@@ -529,7 +543,7 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
529
543
|
current_tool_call, tool_use_delta
|
|
530
544
|
)
|
|
531
545
|
|
|
532
|
-
blocks: List[Union[TextBlock, ThinkingBlock]] = [
|
|
546
|
+
blocks: List[Union[TextBlock, ThinkingBlock, ToolCallBlock]] = [
|
|
533
547
|
TextBlock(text=content.get("text", ""))
|
|
534
548
|
]
|
|
535
549
|
if thinking != "":
|
|
@@ -542,13 +556,21 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
542
556
|
},
|
|
543
557
|
),
|
|
544
558
|
)
|
|
559
|
+
if tool_calls:
|
|
560
|
+
for tool_call in tool_calls:
|
|
561
|
+
blocks.append(
|
|
562
|
+
ToolCallBlock(
|
|
563
|
+
tool_kwargs=tool_call.get("input", {}),
|
|
564
|
+
tool_name=tool_call.get("name", ""),
|
|
565
|
+
tool_call_id=tool_call.get("toolUseId"),
|
|
566
|
+
)
|
|
567
|
+
)
|
|
545
568
|
|
|
546
569
|
yield ChatResponse(
|
|
547
570
|
message=ChatMessage(
|
|
548
571
|
role=role,
|
|
549
572
|
blocks=blocks,
|
|
550
573
|
additional_kwargs={
|
|
551
|
-
"tool_calls": tool_calls,
|
|
552
574
|
"tool_call_id": [
|
|
553
575
|
tc.get("toolUseId", "") for tc in tool_calls
|
|
554
576
|
],
|
|
@@ -568,7 +590,7 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
568
590
|
# Add to our list of tool calls
|
|
569
591
|
tool_calls.append(current_tool_call)
|
|
570
592
|
|
|
571
|
-
blocks: List[Union[TextBlock, ThinkingBlock]] = [
|
|
593
|
+
blocks: List[Union[TextBlock, ThinkingBlock, ToolCallBlock]] = [
|
|
572
594
|
TextBlock(text=content.get("text", ""))
|
|
573
595
|
]
|
|
574
596
|
if thinking != "":
|
|
@@ -582,12 +604,21 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
582
604
|
),
|
|
583
605
|
)
|
|
584
606
|
|
|
607
|
+
if tool_calls:
|
|
608
|
+
for tool_call in tool_calls:
|
|
609
|
+
blocks.append(
|
|
610
|
+
ToolCallBlock(
|
|
611
|
+
tool_kwargs=tool_call.get("input", {}),
|
|
612
|
+
tool_name=tool_call.get("name", ""),
|
|
613
|
+
tool_call_id=tool_call.get("toolUseId"),
|
|
614
|
+
)
|
|
615
|
+
)
|
|
616
|
+
|
|
585
617
|
yield ChatResponse(
|
|
586
618
|
message=ChatMessage(
|
|
587
619
|
role=role,
|
|
588
620
|
blocks=blocks,
|
|
589
621
|
additional_kwargs={
|
|
590
|
-
"tool_calls": tool_calls,
|
|
591
622
|
"tool_call_id": [
|
|
592
623
|
tc.get("toolUseId", "") for tc in tool_calls
|
|
593
624
|
],
|
|
@@ -604,7 +635,7 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
604
635
|
# Handle metadata event - this contains the final token usage
|
|
605
636
|
if usage := metadata.get("usage"):
|
|
606
637
|
# Yield a final response with correct token usage
|
|
607
|
-
blocks: List[Union[TextBlock, ThinkingBlock]] = [
|
|
638
|
+
blocks: List[Union[TextBlock, ThinkingBlock, ToolCallBlock]] = [
|
|
608
639
|
TextBlock(text=content.get("text", ""))
|
|
609
640
|
]
|
|
610
641
|
if thinking != "":
|
|
@@ -617,13 +648,21 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
617
648
|
},
|
|
618
649
|
),
|
|
619
650
|
)
|
|
651
|
+
if tool_calls:
|
|
652
|
+
for tool_call in tool_calls:
|
|
653
|
+
blocks.append(
|
|
654
|
+
ToolCallBlock(
|
|
655
|
+
tool_kwargs=tool_call.get("input", {}),
|
|
656
|
+
tool_name=tool_call.get("name", ""),
|
|
657
|
+
tool_call_id=tool_call.get("toolUseId"),
|
|
658
|
+
)
|
|
659
|
+
)
|
|
620
660
|
|
|
621
661
|
yield ChatResponse(
|
|
622
662
|
message=ChatMessage(
|
|
623
663
|
role=role,
|
|
624
664
|
blocks=blocks,
|
|
625
665
|
additional_kwargs={
|
|
626
|
-
"tool_calls": tool_calls,
|
|
627
666
|
"tool_call_id": [
|
|
628
667
|
tc.get("toolUseId", "") for tc in tool_calls
|
|
629
668
|
],
|
|
@@ -668,21 +707,19 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
668
707
|
stream=False,
|
|
669
708
|
guardrail_identifier=self.guardrail_identifier,
|
|
670
709
|
guardrail_version=self.guardrail_version,
|
|
710
|
+
guardrail_stream_processing_mode=self.guardrail_stream_processing_mode,
|
|
671
711
|
trace=self.trace,
|
|
672
712
|
boto_client_kwargs=self._boto_client_kwargs,
|
|
673
713
|
**all_kwargs,
|
|
674
714
|
)
|
|
675
715
|
|
|
676
|
-
blocks,
|
|
677
|
-
response
|
|
678
|
-
)
|
|
716
|
+
blocks, tool_call_ids, status = self._get_content_and_tool_calls(response)
|
|
679
717
|
|
|
680
718
|
return ChatResponse(
|
|
681
719
|
message=ChatMessage(
|
|
682
720
|
role=MessageRole.ASSISTANT,
|
|
683
721
|
blocks=blocks,
|
|
684
722
|
additional_kwargs={
|
|
685
|
-
"tool_calls": tool_calls,
|
|
686
723
|
"tool_call_id": tool_call_ids,
|
|
687
724
|
"status": status,
|
|
688
725
|
},
|
|
@@ -777,7 +814,7 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
777
814
|
current_tool_call = join_two_dicts(
|
|
778
815
|
current_tool_call, tool_use_delta
|
|
779
816
|
)
|
|
780
|
-
blocks: List[Union[TextBlock, ThinkingBlock]] = [
|
|
817
|
+
blocks: List[Union[TextBlock, ThinkingBlock, ToolCallBlock]] = [
|
|
781
818
|
TextBlock(text=content.get("text", ""))
|
|
782
819
|
]
|
|
783
820
|
if thinking != "":
|
|
@@ -791,12 +828,21 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
791
828
|
),
|
|
792
829
|
)
|
|
793
830
|
|
|
831
|
+
if tool_calls:
|
|
832
|
+
for tool_call in tool_calls:
|
|
833
|
+
blocks.append(
|
|
834
|
+
ToolCallBlock(
|
|
835
|
+
tool_kwargs=tool_call.get("input", {}),
|
|
836
|
+
tool_name=tool_call.get("name", ""),
|
|
837
|
+
tool_call_id=tool_call.get("toolUseId"),
|
|
838
|
+
)
|
|
839
|
+
)
|
|
840
|
+
|
|
794
841
|
yield ChatResponse(
|
|
795
842
|
message=ChatMessage(
|
|
796
843
|
role=role,
|
|
797
844
|
blocks=blocks,
|
|
798
845
|
additional_kwargs={
|
|
799
|
-
"tool_calls": tool_calls,
|
|
800
846
|
"tool_call_id": [
|
|
801
847
|
tc.get("toolUseId", "") for tc in tool_calls
|
|
802
848
|
],
|
|
@@ -816,7 +862,7 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
816
862
|
# Add to our list of tool calls
|
|
817
863
|
tool_calls.append(current_tool_call)
|
|
818
864
|
|
|
819
|
-
blocks: List[Union[TextBlock, ThinkingBlock]] = [
|
|
865
|
+
blocks: List[Union[TextBlock, ThinkingBlock, ToolCallBlock]] = [
|
|
820
866
|
TextBlock(text=content.get("text", ""))
|
|
821
867
|
]
|
|
822
868
|
if thinking != "":
|
|
@@ -830,12 +876,21 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
830
876
|
),
|
|
831
877
|
)
|
|
832
878
|
|
|
879
|
+
if tool_calls:
|
|
880
|
+
for tool_call in tool_calls:
|
|
881
|
+
blocks.append(
|
|
882
|
+
ToolCallBlock(
|
|
883
|
+
tool_kwargs=tool_call.get("input", {}),
|
|
884
|
+
tool_name=tool_call.get("name", ""),
|
|
885
|
+
tool_call_id=tool_call.get("toolUseId"),
|
|
886
|
+
)
|
|
887
|
+
)
|
|
888
|
+
|
|
833
889
|
yield ChatResponse(
|
|
834
890
|
message=ChatMessage(
|
|
835
891
|
role=role,
|
|
836
892
|
blocks=blocks,
|
|
837
893
|
additional_kwargs={
|
|
838
|
-
"tool_calls": tool_calls,
|
|
839
894
|
"tool_call_id": [
|
|
840
895
|
tc.get("toolUseId", "") for tc in tool_calls
|
|
841
896
|
],
|
|
@@ -852,7 +907,7 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
852
907
|
# Handle metadata event - this contains the final token usage
|
|
853
908
|
if usage := metadata.get("usage"):
|
|
854
909
|
# Yield a final response with correct token usage
|
|
855
|
-
blocks: List[Union[TextBlock, ThinkingBlock]] = [
|
|
910
|
+
blocks: List[Union[TextBlock, ThinkingBlock, ToolCallBlock]] = [
|
|
856
911
|
TextBlock(text=content.get("text", ""))
|
|
857
912
|
]
|
|
858
913
|
if thinking != "":
|
|
@@ -866,12 +921,21 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
866
921
|
),
|
|
867
922
|
)
|
|
868
923
|
|
|
924
|
+
if tool_calls:
|
|
925
|
+
for tool_call in tool_calls:
|
|
926
|
+
blocks.append(
|
|
927
|
+
ToolCallBlock(
|
|
928
|
+
tool_kwargs=tool_call.get("input", {}),
|
|
929
|
+
tool_name=tool_call.get("name", ""),
|
|
930
|
+
tool_call_id=tool_call.get("toolUseId"),
|
|
931
|
+
)
|
|
932
|
+
)
|
|
933
|
+
|
|
869
934
|
yield ChatResponse(
|
|
870
935
|
message=ChatMessage(
|
|
871
936
|
role=role,
|
|
872
937
|
blocks=blocks,
|
|
873
938
|
additional_kwargs={
|
|
874
|
-
"tool_calls": tool_calls,
|
|
875
939
|
"tool_call_id": [
|
|
876
940
|
tc.get("toolUseId", "") for tc in tool_calls
|
|
877
941
|
],
|
|
@@ -948,7 +1012,11 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
948
1012
|
**kwargs: Any,
|
|
949
1013
|
) -> List[ToolSelection]:
|
|
950
1014
|
"""Predict and call the tool."""
|
|
951
|
-
tool_calls =
|
|
1015
|
+
tool_calls = [
|
|
1016
|
+
block
|
|
1017
|
+
for block in response.message.blocks
|
|
1018
|
+
if isinstance(block, ToolCallBlock)
|
|
1019
|
+
]
|
|
952
1020
|
|
|
953
1021
|
if len(tool_calls) < 1:
|
|
954
1022
|
if error_on_no_tool_call:
|
|
@@ -960,26 +1028,23 @@ class BedrockConverse(FunctionCallingLLM):
|
|
|
960
1028
|
|
|
961
1029
|
tool_selections = []
|
|
962
1030
|
for tool_call in tool_calls:
|
|
963
|
-
if "toolUseId" not in tool_call or "name" not in tool_call:
|
|
964
|
-
raise ValueError("Invalid tool call.")
|
|
965
|
-
|
|
966
1031
|
# handle empty inputs
|
|
967
1032
|
argument_dict = {}
|
|
968
|
-
if
|
|
1033
|
+
if isinstance(tool_call.tool_kwargs, str):
|
|
969
1034
|
# TODO parse_partial_json is not perfect
|
|
970
1035
|
try:
|
|
971
|
-
argument_dict = parse_partial_json(tool_call
|
|
1036
|
+
argument_dict = parse_partial_json(tool_call.tool_kwargs)
|
|
972
1037
|
except ValueError:
|
|
973
1038
|
argument_dict = {}
|
|
974
|
-
elif
|
|
975
|
-
argument_dict = tool_call
|
|
1039
|
+
elif isinstance(tool_call.tool_kwargs, dict):
|
|
1040
|
+
argument_dict = tool_call.tool_kwargs
|
|
976
1041
|
else:
|
|
977
1042
|
continue
|
|
978
1043
|
|
|
979
1044
|
tool_selections.append(
|
|
980
1045
|
ToolSelection(
|
|
981
|
-
tool_id=tool_call
|
|
982
|
-
tool_name=tool_call
|
|
1046
|
+
tool_id=tool_call.tool_call_id or "",
|
|
1047
|
+
tool_name=tool_call.tool_name,
|
|
983
1048
|
tool_kwargs=argument_dict,
|
|
984
1049
|
)
|
|
985
1050
|
)
|
|
@@ -32,6 +32,7 @@ from llama_index.core.base.llms.types import (
|
|
|
32
32
|
DocumentBlock,
|
|
33
33
|
CachePoint,
|
|
34
34
|
ThinkingBlock,
|
|
35
|
+
ToolCallBlock,
|
|
35
36
|
)
|
|
36
37
|
|
|
37
38
|
|
|
@@ -303,6 +304,23 @@ def _content_block_to_bedrock_format(
|
|
|
303
304
|
elif isinstance(block, AudioBlock):
|
|
304
305
|
logger.warning("Audio blocks are not supported in Bedrock Converse API.")
|
|
305
306
|
return None
|
|
307
|
+
elif isinstance(block, ToolCallBlock):
|
|
308
|
+
if isinstance(block.tool_kwargs, str):
|
|
309
|
+
try:
|
|
310
|
+
tool_input = json.loads(block.tool_kwargs or "{}")
|
|
311
|
+
except json.JSONDecodeError:
|
|
312
|
+
tool_input = {}
|
|
313
|
+
else:
|
|
314
|
+
tool_input = block.tool_kwargs
|
|
315
|
+
|
|
316
|
+
return {
|
|
317
|
+
"toolUse": {
|
|
318
|
+
"input": tool_input,
|
|
319
|
+
"toolUseId": block.tool_call_id or "",
|
|
320
|
+
"name": block.tool_name,
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
306
324
|
else:
|
|
307
325
|
logger.warning(f"Unsupported block type: {type(block)}")
|
|
308
326
|
return None
|
|
@@ -345,7 +363,9 @@ def messages_to_converse_messages(
|
|
|
345
363
|
converse_messages = []
|
|
346
364
|
system_prompt = []
|
|
347
365
|
current_system_prompt = ""
|
|
366
|
+
|
|
348
367
|
for message in messages:
|
|
368
|
+
unique_tool_calls = []
|
|
349
369
|
if message.role == MessageRole.SYSTEM:
|
|
350
370
|
# we iterate over blocks, if content was used, the blocks are added anyway
|
|
351
371
|
for block in message.blocks:
|
|
@@ -402,6 +422,13 @@ def messages_to_converse_messages(
|
|
|
402
422
|
)
|
|
403
423
|
if bedrock_format_block:
|
|
404
424
|
content.append(bedrock_format_block)
|
|
425
|
+
if "toolUse" in bedrock_format_block:
|
|
426
|
+
unique_tool_calls.append(
|
|
427
|
+
(
|
|
428
|
+
bedrock_format_block["toolUse"]["toolUseId"],
|
|
429
|
+
bedrock_format_block["toolUse"]["name"],
|
|
430
|
+
)
|
|
431
|
+
)
|
|
405
432
|
|
|
406
433
|
if content:
|
|
407
434
|
converse_messages.append(
|
|
@@ -411,6 +438,7 @@ def messages_to_converse_messages(
|
|
|
411
438
|
}
|
|
412
439
|
)
|
|
413
440
|
|
|
441
|
+
# keep this code here for compatibility with older chat histories
|
|
414
442
|
# convert tool calls to the AWS Bedrock Converse format
|
|
415
443
|
# NOTE tool calls might show up within any message,
|
|
416
444
|
# e.g. within assistant message or in consecutive tool calls,
|
|
@@ -418,25 +446,28 @@ def messages_to_converse_messages(
|
|
|
418
446
|
tool_calls = message.additional_kwargs.get("tool_calls", [])
|
|
419
447
|
content = []
|
|
420
448
|
for tool_call in tool_calls:
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
449
|
+
try:
|
|
450
|
+
assert "toolUseId" in tool_call
|
|
451
|
+
assert "input" in tool_call
|
|
452
|
+
assert "name" in tool_call
|
|
453
|
+
if (tool_call["toolUseId"], tool_call["name"]) not in unique_tool_calls:
|
|
454
|
+
tool_input = tool_call["input"] if tool_call["input"] else {}
|
|
455
|
+
if isinstance(tool_input, str):
|
|
456
|
+
try:
|
|
457
|
+
tool_input = json.loads(tool_input or "{}")
|
|
458
|
+
except json.JSONDecodeError:
|
|
459
|
+
tool_input = {}
|
|
460
|
+
content.append(
|
|
461
|
+
{
|
|
462
|
+
"toolUse": {
|
|
463
|
+
"input": tool_input,
|
|
464
|
+
"toolUseId": tool_call["toolUseId"],
|
|
465
|
+
"name": tool_call["name"],
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
)
|
|
469
|
+
except AssertionError:
|
|
470
|
+
continue
|
|
440
471
|
if len(content) > 0:
|
|
441
472
|
converse_messages.append(
|
|
442
473
|
{
|
|
@@ -499,9 +530,15 @@ def tools_to_converse_tools(
|
|
|
499
530
|
|
|
500
531
|
|
|
501
532
|
def force_single_tool_call(response: ChatResponse) -> None:
|
|
502
|
-
tool_calls =
|
|
533
|
+
tool_calls = [
|
|
534
|
+
block for block in response.message.blocks if isinstance(block, ToolCallBlock)
|
|
535
|
+
]
|
|
503
536
|
if len(tool_calls) > 1:
|
|
504
|
-
response.message.
|
|
537
|
+
response.message.blocks = [
|
|
538
|
+
block
|
|
539
|
+
for block in response.message.blocks
|
|
540
|
+
if not isinstance(block, ToolCallBlock)
|
|
541
|
+
] + [tool_calls[0]]
|
|
505
542
|
|
|
506
543
|
|
|
507
544
|
def _create_retry_decorator(client: Any, max_retries: int) -> Callable[[Any], Any]:
|
|
@@ -563,6 +600,7 @@ def converse_with_retry(
|
|
|
563
600
|
stream: bool = False,
|
|
564
601
|
guardrail_identifier: Optional[str] = None,
|
|
565
602
|
guardrail_version: Optional[str] = None,
|
|
603
|
+
guardrail_stream_processing_mode: Optional[Literal["sync", "async"]] = None,
|
|
566
604
|
trace: Optional[str] = None,
|
|
567
605
|
**kwargs: Any,
|
|
568
606
|
) -> Any:
|
|
@@ -603,6 +641,10 @@ def converse_with_retry(
|
|
|
603
641
|
converse_kwargs["guardrailConfig"]["guardrailVersion"] = guardrail_version
|
|
604
642
|
if trace:
|
|
605
643
|
converse_kwargs["guardrailConfig"]["trace"] = trace
|
|
644
|
+
if guardrail_stream_processing_mode and stream:
|
|
645
|
+
converse_kwargs["guardrailConfig"]["streamProcessingMode"] = (
|
|
646
|
+
guardrail_stream_processing_mode
|
|
647
|
+
)
|
|
606
648
|
|
|
607
649
|
converse_kwargs = join_two_dicts(
|
|
608
650
|
converse_kwargs,
|
|
@@ -644,6 +686,7 @@ async def converse_with_retry_async(
|
|
|
644
686
|
stream: bool = False,
|
|
645
687
|
guardrail_identifier: Optional[str] = None,
|
|
646
688
|
guardrail_version: Optional[str] = None,
|
|
689
|
+
guardrail_stream_processing_mode: Optional[Literal["sync", "async"]] = None,
|
|
647
690
|
trace: Optional[str] = None,
|
|
648
691
|
boto_client_kwargs: Optional[Dict[str, Any]] = None,
|
|
649
692
|
**kwargs: Any,
|
|
@@ -690,6 +733,10 @@ async def converse_with_retry_async(
|
|
|
690
733
|
converse_kwargs["guardrailConfig"]["guardrailVersion"] = guardrail_version
|
|
691
734
|
if trace:
|
|
692
735
|
converse_kwargs["guardrailConfig"]["trace"] = trace
|
|
736
|
+
if guardrail_stream_processing_mode and stream:
|
|
737
|
+
converse_kwargs["guardrailConfig"]["streamProcessingMode"] = (
|
|
738
|
+
guardrail_stream_processing_mode
|
|
739
|
+
)
|
|
693
740
|
converse_kwargs = join_two_dicts(
|
|
694
741
|
converse_kwargs,
|
|
695
742
|
{
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: llama-index-llms-bedrock-converse
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.11.0
|
|
4
4
|
Summary: llama-index llms bedrock converse integration
|
|
5
5
|
Author-email: Your Name <you@example.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -8,7 +8,7 @@ License-File: LICENSE
|
|
|
8
8
|
Requires-Python: <4.0,>=3.9
|
|
9
9
|
Requires-Dist: aioboto3<16,>=15.0.0
|
|
10
10
|
Requires-Dist: boto3<2,>=1.38.27
|
|
11
|
-
Requires-Dist: llama-index-core<0.15,>=0.14.
|
|
11
|
+
Requires-Dist: llama-index-core<0.15,>=0.14.5
|
|
12
12
|
Description-Content-Type: text/markdown
|
|
13
13
|
|
|
14
14
|
# LlamaIndex Llms Integration: Bedrock Converse
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
llama_index/llms/bedrock_converse/__init__.py,sha256=xE3ZHLXqFr7TTTgQlYH9bLLPRZAV3dJyiz_iUFXBfak,98
|
|
2
|
+
llama_index/llms/bedrock_converse/base.py,sha256=tMex6AHod6YofxaLX9RsucVHBjNU-a_KP9OAgFvRHXo,45059
|
|
3
|
+
llama_index/llms/bedrock_converse/utils.py,sha256=XgAWPSGrEft8bh9NMzhmSIjrKveXXjzIkBOJmeHf9xc,29747
|
|
4
|
+
llama_index_llms_bedrock_converse-0.11.0.dist-info/METADATA,sha256=YgCAv1cu4yxHtvQZm6M2w27-tK_8RWy8wGFWRz5ZqB0,7834
|
|
5
|
+
llama_index_llms_bedrock_converse-0.11.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
6
|
+
llama_index_llms_bedrock_converse-0.11.0.dist-info/licenses/LICENSE,sha256=JPQLUZD9rKvCTdu192Nk0V5PAwklIg6jANii3UmTyMs,1065
|
|
7
|
+
llama_index_llms_bedrock_converse-0.11.0.dist-info/RECORD,,
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
llama_index/llms/bedrock_converse/__init__.py,sha256=xE3ZHLXqFr7TTTgQlYH9bLLPRZAV3dJyiz_iUFXBfak,98
|
|
2
|
-
llama_index/llms/bedrock_converse/base.py,sha256=MISQTYCwaUIinyOGZhOdPYQYQAFrRSLAkiTEE-8f21A,41478
|
|
3
|
-
llama_index/llms/bedrock_converse/utils.py,sha256=-SDwcbgcX_W7p6SEt6-3HH_cAsKxG_4e2wHhi5kFKJU,27893
|
|
4
|
-
llama_index_llms_bedrock_converse-0.10.6.dist-info/METADATA,sha256=0y3xka1zYDazsks_i17_00wj89QwvAu2tBB-s4k2sXE,7834
|
|
5
|
-
llama_index_llms_bedrock_converse-0.10.6.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
6
|
-
llama_index_llms_bedrock_converse-0.10.6.dist-info/licenses/LICENSE,sha256=JPQLUZD9rKvCTdu192Nk0V5PAwklIg6jANii3UmTyMs,1065
|
|
7
|
-
llama_index_llms_bedrock_converse-0.10.6.dist-info/RECORD,,
|
|
File without changes
|