amazon-bedrock-haystack 3.8.0__py3-none-any.whl → 3.9.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: amazon-bedrock-haystack
3
- Version: 3.8.0
3
+ Version: 3.9.0
4
4
  Summary: An integration of Amazon Bedrock as an AmazonBedrockGenerator component.
5
5
  Project-URL: Documentation, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/amazon_bedrock#readme
6
6
  Project-URL: Issues, https://github.com/deepset-ai/haystack-core-integrations/issues
@@ -21,7 +21,7 @@ Classifier: Programming Language :: Python :: Implementation :: PyPy
21
21
  Requires-Python: >=3.9
22
22
  Requires-Dist: aioboto3>=14.0.0
23
23
  Requires-Dist: boto3>=1.28.57
24
- Requires-Dist: haystack-ai>=2.15.1
24
+ Requires-Dist: haystack-ai>=2.16.0
25
25
  Description-Content-Type: text/markdown
26
26
 
27
27
  # amazon-bedrock-haystack
@@ -11,12 +11,12 @@ haystack_integrations/components/generators/amazon_bedrock/__init__.py,sha256=lv
11
11
  haystack_integrations/components/generators/amazon_bedrock/adapters.py,sha256=yBC-3YwV6qAwSXMtdZiLSYh2lUpPQIDy7Efl7w-Cu-k,19640
12
12
  haystack_integrations/components/generators/amazon_bedrock/generator.py,sha256=c_saV5zxFYQVJT0Hzo80lKty46itL0Dp31VuDueYa3M,14716
13
13
  haystack_integrations/components/generators/amazon_bedrock/chat/__init__.py,sha256=6GZ8Y3Lw0rLOsOAqi6Tu5mZC977UzQvgDxKpOWr8IQw,110
14
- haystack_integrations/components/generators/amazon_bedrock/chat/chat_generator.py,sha256=6sCjyIzZBpfQRoXuu3PzziLVasb82xWG17sMy_zBf2Y,24130
15
- haystack_integrations/components/generators/amazon_bedrock/chat/utils.py,sha256=RY7NbwdAx1uvCazasqBeJP3RjXgTXdEwgU3EFfESvkg,21555
14
+ haystack_integrations/components/generators/amazon_bedrock/chat/chat_generator.py,sha256=iIaMsOOX9eYvR1GNgpxNKxaOli91ShrCv3MuBBK1NSs,24743
15
+ haystack_integrations/components/generators/amazon_bedrock/chat/utils.py,sha256=GLGbUSpnmGLUQyBNrlHMR_GaS_zpSQbVIzXVTL8F3IU,23188
16
16
  haystack_integrations/components/rankers/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
17
17
  haystack_integrations/components/rankers/amazon_bedrock/__init__.py,sha256=Zrc3BSVkEaXYpliEi6hKG9bqW4J7DNk93p50SuoyT1Q,107
18
18
  haystack_integrations/components/rankers/amazon_bedrock/ranker.py,sha256=enAjf2QyDwfpidKkFCdLz954cx-Tjh9emrOS3vINJDg,12344
19
- amazon_bedrock_haystack-3.8.0.dist-info/METADATA,sha256=5XRkUVgYK3BZxIYmlJ2I9xIjhXXKCSIO7bTnI8rP3Xw,2287
20
- amazon_bedrock_haystack-3.8.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
21
- amazon_bedrock_haystack-3.8.0.dist-info/licenses/LICENSE.txt,sha256=B05uMshqTA74s-0ltyHKI6yoPfJ3zYgQbvcXfDVGFf8,10280
22
- amazon_bedrock_haystack-3.8.0.dist-info/RECORD,,
19
+ amazon_bedrock_haystack-3.9.0.dist-info/METADATA,sha256=GgpwClHZ8LJLpikG4KNZDUdSjsaWzpamMRe3zfWf-aw,2287
20
+ amazon_bedrock_haystack-3.9.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
21
+ amazon_bedrock_haystack-3.9.0.dist-info/licenses/LICENSE.txt,sha256=B05uMshqTA74s-0ltyHKI6yoPfJ3zYgQbvcXfDVGFf8,10280
22
+ amazon_bedrock_haystack-3.9.0.dist-info/RECORD,,
@@ -56,6 +56,22 @@ class AmazonBedrockChatGenerator:
56
56
  client.run(messages, generation_kwargs={"max_tokens": 512})
57
57
  ```
58
58
 
59
+ ### Multimodal example
60
+ ```python
61
+ from haystack.dataclasses import ChatMessage, ImageContent
62
+ from haystack_integrations.components.generators.amazon_bedrock import AmazonBedrockChatGenerator
63
+
64
+ generator = AmazonBedrockChatGenerator(model="anthropic.claude-3-5-sonnet-20240620-v1:0")
65
+
66
+ image_content = ImageContent.from_file_path(file_path="apple.jpg")
67
+
68
+ message = ChatMessage.from_user(content_parts=["Describe the image using 10 words at most.", image_content])
69
+
70
+ response = generator.run(messages=[message])["replies"][0].text
71
+
72
+ print(response)
73
+ > The image shows a red apple.
74
+
59
75
  ### Tool usage example
60
76
  # AmazonBedrockChatGenerator supports Haystack's unified tool architecture, allowing tools to be used
61
77
  # across different chat generators. The same tool definitions and usage patterns work consistently
@@ -1,3 +1,4 @@
1
+ import base64
1
2
  import json
2
3
  from datetime import datetime, timezone
3
4
  from typing import Any, Dict, List, Optional, Tuple
@@ -9,8 +10,10 @@ from haystack.dataclasses import (
9
10
  ChatMessage,
10
11
  ChatRole,
11
12
  ComponentInfo,
13
+ ImageContent,
12
14
  StreamingChunk,
13
15
  SyncStreamingCallbackT,
16
+ TextContent,
14
17
  ToolCall,
15
18
  )
16
19
  from haystack.tools import Tool
@@ -18,6 +21,10 @@ from haystack.tools import Tool
18
21
  logger = logging.getLogger(__name__)
19
22
 
20
23
 
24
+ # see https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ImageBlock.html for supported formats
25
+ IMAGE_SUPPORTED_FORMATS = ["png", "jpeg", "gif", "webp"]
26
+
27
+
21
28
  # Haystack to Bedrock util methods
22
29
  def _format_tools(tools: Optional[List[Tool]] = None) -> Optional[Dict[str, Any]]:
23
30
  """
@@ -150,6 +157,39 @@ def _repair_tool_result_messages(bedrock_formatted_messages: List[Dict[str, Any]
150
157
  return [msg for _, msg in repaired_bedrock_formatted_messages]
151
158
 
152
159
 
160
+ def _format_text_image_message(message: ChatMessage) -> Dict[str, Any]:
161
+ """
162
+ Format a Haystack ChatMessage containing text and optional image content into Bedrock format.
163
+
164
+ :param message: Haystack ChatMessage.
165
+ :returns: Dictionary representing the message in Bedrock's expected format.
166
+ :raises ValueError: If image content is found in an assistant message or an unsupported image format is used.
167
+ """
168
+ content_parts = message._content
169
+
170
+ bedrock_content_blocks: List[Dict[str, Any]] = []
171
+ for part in content_parts:
172
+ if isinstance(part, TextContent):
173
+ bedrock_content_blocks.append({"text": part.text})
174
+
175
+ elif isinstance(part, ImageContent):
176
+ if message.is_from(ChatRole.ASSISTANT):
177
+ err_msg = "Image content is not supported for assistant messages"
178
+ raise ValueError(err_msg)
179
+
180
+ image_format = part.mime_type.split("/")[-1] if part.mime_type else None
181
+ if image_format not in IMAGE_SUPPORTED_FORMATS:
182
+ err_msg = (
183
+ f"Unsupported image format: {image_format}. "
184
+ f"Bedrock supports the following image formats: {IMAGE_SUPPORTED_FORMATS}"
185
+ )
186
+ raise ValueError(err_msg)
187
+ source = {"bytes": base64.b64decode(part.base64_image)}
188
+ bedrock_content_blocks.append({"image": {"format": image_format, "source": source}})
189
+
190
+ return {"role": message.role.value, "content": bedrock_content_blocks}
191
+
192
+
153
193
  def _format_messages(messages: List[ChatMessage]) -> Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]:
154
194
  """
155
195
  Format a list of Haystack ChatMessages to the format expected by Bedrock API.
@@ -175,8 +215,7 @@ def _format_messages(messages: List[ChatMessage]) -> Tuple[List[Dict[str, Any]],
175
215
  elif msg.tool_call_results:
176
216
  bedrock_formatted_messages.append(_format_tool_result_message(msg))
177
217
  else:
178
- # regular user or assistant messages with only text content
179
- bedrock_formatted_messages.append({"role": msg.role.value, "content": [{"text": msg.text}]})
218
+ bedrock_formatted_messages.append(_format_text_image_message(msg))
180
219
 
181
220
  repaired_bedrock_formatted_messages = _repair_tool_result_messages(bedrock_formatted_messages)
182
221
  return system_prompts, repaired_bedrock_formatted_messages