amazon-bedrock-haystack 4.0.0__py3-none-any.whl → 4.1.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: 4.0.0
3
+ Version: 4.1.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
@@ -12,12 +12,12 @@ haystack_integrations/components/generators/amazon_bedrock/__init__.py,sha256=lv
12
12
  haystack_integrations/components/generators/amazon_bedrock/adapters.py,sha256=yBC-3YwV6qAwSXMtdZiLSYh2lUpPQIDy7Efl7w-Cu-k,19640
13
13
  haystack_integrations/components/generators/amazon_bedrock/generator.py,sha256=Brzw0XvtPJhz2kR2I3liAqWHRmDR6p5HzJerEAPhoJU,14743
14
14
  haystack_integrations/components/generators/amazon_bedrock/chat/__init__.py,sha256=6GZ8Y3Lw0rLOsOAqi6Tu5mZC977UzQvgDxKpOWr8IQw,110
15
- haystack_integrations/components/generators/amazon_bedrock/chat/chat_generator.py,sha256=_0dpBoZGY9kgK9zQOTskcjElcTifwhyBAixXDliK-vY,24918
16
- haystack_integrations/components/generators/amazon_bedrock/chat/utils.py,sha256=eF2wldu1IppL64f01N3PIa9_-BZEolQzEz9NjXvEFTQ,25810
15
+ haystack_integrations/components/generators/amazon_bedrock/chat/chat_generator.py,sha256=qArwfXcforWnPzLXrAW-1hkPFpMy3NSdDyJ5GOta25w,26068
16
+ haystack_integrations/components/generators/amazon_bedrock/chat/utils.py,sha256=1M_k8CG2WH23Yz-sB7a1kiIqVh2QB8Pqi0zbWXyMUL8,27255
17
17
  haystack_integrations/components/rankers/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
18
18
  haystack_integrations/components/rankers/amazon_bedrock/__init__.py,sha256=Zrc3BSVkEaXYpliEi6hKG9bqW4J7DNk93p50SuoyT1Q,107
19
19
  haystack_integrations/components/rankers/amazon_bedrock/ranker.py,sha256=enAjf2QyDwfpidKkFCdLz954cx-Tjh9emrOS3vINJDg,12344
20
- amazon_bedrock_haystack-4.0.0.dist-info/METADATA,sha256=8iHYVwqO_nLLbLOh59yoqxwnskmfBiDupxxSTWiCvcc,2222
21
- amazon_bedrock_haystack-4.0.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
22
- amazon_bedrock_haystack-4.0.0.dist-info/licenses/LICENSE.txt,sha256=B05uMshqTA74s-0ltyHKI6yoPfJ3zYgQbvcXfDVGFf8,10280
23
- amazon_bedrock_haystack-4.0.0.dist-info/RECORD,,
20
+ amazon_bedrock_haystack-4.1.0.dist-info/METADATA,sha256=P6e8VfoRQ0hZrA6gkaTwjCCxZez2NqKjHNAJuFdwK6c,2222
21
+ amazon_bedrock_haystack-4.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
22
+ amazon_bedrock_haystack-4.1.0.dist-info/licenses/LICENSE.txt,sha256=B05uMshqTA74s-0ltyHKI6yoPfJ3zYgQbvcXfDVGFf8,10280
23
+ amazon_bedrock_haystack-4.1.0.dist-info/RECORD,,
@@ -27,6 +27,7 @@ from haystack_integrations.components.generators.amazon_bedrock.chat.utils impor
27
27
  _parse_completion_response,
28
28
  _parse_streaming_response,
29
29
  _parse_streaming_response_async,
30
+ _validate_guardrail_config,
30
31
  )
31
32
 
32
33
  logger = logging.getLogger(__name__)
@@ -154,10 +155,11 @@ class AmazonBedrockChatGenerator:
154
155
  aws_region_name: Optional[Secret] = Secret.from_env_var(["AWS_DEFAULT_REGION"], strict=False), # noqa: B008
155
156
  aws_profile_name: Optional[Secret] = Secret.from_env_var(["AWS_PROFILE"], strict=False), # noqa: B008
156
157
  generation_kwargs: Optional[Dict[str, Any]] = None,
157
- stop_words: Optional[List[str]] = None,
158
158
  streaming_callback: Optional[StreamingCallbackT] = None,
159
159
  boto3_config: Optional[Dict[str, Any]] = None,
160
160
  tools: Optional[Union[List[Tool], Toolset]] = None,
161
+ *,
162
+ guardrail_config: Optional[Dict[str, str]] = None,
161
163
  ) -> None:
162
164
  """
163
165
  Initializes the `AmazonBedrockChatGenerator` with the provided parameters. The parameters are passed to the
@@ -179,10 +181,6 @@ class AmazonBedrockChatGenerator:
179
181
  :param generation_kwargs: Keyword arguments sent to the model. These parameters are specific to a model.
180
182
  You can find the model specific arguments in the AWS Bedrock API
181
183
  [documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html).
182
- :param stop_words: A list of stop words that stop the model from generating more text
183
- when encountered. You can provide them using this parameter or using the model's `generation_kwargs`
184
- under a model's specific key for stop words.
185
- For example, you can provide stop words for Anthropic Claude in the `stop_sequences` key.
186
184
  :param streaming_callback: A callback function called when a new token is received from the stream.
187
185
  By default, the model is not set up for streaming. To enable streaming, set this parameter to a callback
188
186
  function that handles the streaming chunks. The callback function receives a
@@ -190,6 +188,19 @@ class AmazonBedrockChatGenerator:
190
188
  the streaming mode on.
191
189
  :param boto3_config: The configuration for the boto3 client.
192
190
  :param tools: A list of Tool objects or a Toolset that the model can use. Each tool should have a unique name.
191
+ :param guardrail_config: Optional configuration for a guardrail that has been created in Amazon Bedrock.
192
+ This must be provided as a dictionary matching either
193
+ [GuardrailConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_GuardrailConfiguration.html).
194
+ or, in streaming mode (when `streaming_callback` is set),
195
+ [GuardrailStreamConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_GuardrailStreamConfiguration.html).
196
+ If `trace` is set to `enabled`, the guardrail trace will be included under the `trace` key in the `meta`
197
+ attribute of the resulting `ChatMessage`.
198
+ Note: Enabling guardrails in streaming mode may introduce additional latency.
199
+ To manage this, you can adjust the `streamProcessingMode` parameter.
200
+ See the
201
+ [Guardrails Streaming documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails-streaming.html)
202
+ for more information.
203
+
193
204
 
194
205
  :raises ValueError: If the model name is empty or None.
195
206
  :raises AmazonBedrockConfigurationError: If the AWS environment is not configured correctly or the model is
@@ -204,12 +215,15 @@ class AmazonBedrockChatGenerator:
204
215
  self.aws_session_token = aws_session_token
205
216
  self.aws_region_name = aws_region_name
206
217
  self.aws_profile_name = aws_profile_name
207
- self.stop_words = stop_words or []
208
218
  self.streaming_callback = streaming_callback
209
219
  self.boto3_config = boto3_config
220
+
210
221
  _check_duplicate_tool_names(list(tools or [])) # handles Toolset as well
211
222
  self.tools = tools
212
223
 
224
+ _validate_guardrail_config(guardrail_config=guardrail_config, streaming=streaming_callback is not None)
225
+ self.guardrail_config = guardrail_config
226
+
213
227
  def resolve_secret(secret: Optional[Secret]) -> Optional[str]:
214
228
  return secret.resolve_value() if secret else None
215
229
 
@@ -237,7 +251,6 @@ class AmazonBedrockChatGenerator:
237
251
  raise AmazonBedrockConfigurationError(msg) from exception
238
252
 
239
253
  self.generation_kwargs = generation_kwargs or {}
240
- self.stop_words = stop_words or []
241
254
  self.async_session: Optional[aioboto3.Session] = None
242
255
 
243
256
  def _get_async_session(self) -> aioboto3.Session:
@@ -291,11 +304,11 @@ class AmazonBedrockChatGenerator:
291
304
  aws_region_name=self.aws_region_name.to_dict() if self.aws_region_name else None,
292
305
  aws_profile_name=self.aws_profile_name.to_dict() if self.aws_profile_name else None,
293
306
  model=self.model,
294
- stop_words=self.stop_words,
295
307
  generation_kwargs=self.generation_kwargs,
296
308
  streaming_callback=callback_name,
297
309
  boto3_config=self.boto3_config,
298
310
  tools=serialize_tools_or_toolset(self.tools),
311
+ guardrail_config=self.guardrail_config,
299
312
  )
300
313
 
301
314
  @classmethod
@@ -308,6 +321,12 @@ class AmazonBedrockChatGenerator:
308
321
  Instance of `AmazonBedrockChatGenerator`.
309
322
  """
310
323
  init_params = data.get("init_parameters", {})
324
+
325
+ stop_words = init_params.pop("stop_words", None)
326
+ msg = "stop_words parameter will be ignored. Use the `stopSequences` key in `generation_kwargs` instead."
327
+ if stop_words:
328
+ logger.warning(msg)
329
+
311
330
  serialized_callback_handler = init_params.get("streaming_callback")
312
331
  if serialized_callback_handler:
313
332
  data["init_parameters"]["streaming_callback"] = deserialize_callable(serialized_callback_handler)
@@ -387,6 +406,8 @@ class AmazonBedrockChatGenerator:
387
406
  params["toolConfig"] = tool_config
388
407
  if additional_fields:
389
408
  params["additionalModelRequestFields"] = additional_fields
409
+ if self.guardrail_config:
410
+ params["guardrailConfig"] = self.guardrail_config
390
411
 
391
412
  # overloads that exhaust finite Literals(bool) not treated as exhaustive
392
413
  # see https://github.com/python/mypy/issues/14764
@@ -273,6 +273,7 @@ def _parse_completion_response(response_body: Dict[str, Any], model: str) -> Lis
273
273
  :param model: The model ID used for generation, included in message metadata.
274
274
  :returns: List of ChatMessage objects containing the assistant's response(s) with appropriate metadata.
275
275
  """
276
+
276
277
  replies = []
277
278
  if "output" in response_body and "message" in response_body["output"]:
278
279
  message = response_body["output"]["message"]
@@ -280,7 +281,7 @@ def _parse_completion_response(response_body: Dict[str, Any], model: str) -> Lis
280
281
  content_blocks = message["content"]
281
282
 
282
283
  # Common meta information
283
- base_meta = {
284
+ meta = {
284
285
  "model": model,
285
286
  "index": 0,
286
287
  "finish_reason": FINISH_REASON_MAPPING.get(response_body.get("stopReason", "")),
@@ -291,6 +292,9 @@ def _parse_completion_response(response_body: Dict[str, Any], model: str) -> Lis
291
292
  "total_tokens": response_body.get("usage", {}).get("totalTokens", 0),
292
293
  },
293
294
  }
295
+ # guardrail trace
296
+ if "trace" in response_body:
297
+ meta["trace"] = response_body["trace"]
294
298
 
295
299
  # Process all content blocks and combine them into a single message
296
300
  text_content = []
@@ -329,7 +333,7 @@ def _parse_completion_response(response_body: Dict[str, Any], model: str) -> Lis
329
333
  ChatMessage.from_assistant(
330
334
  " ".join(text_content),
331
335
  tool_calls=tool_calls,
332
- meta=base_meta,
336
+ meta=meta,
333
337
  reasoning=ReasoningContent(
334
338
  reasoning_text=reasoning_text, extra={"reasoning_contents": reasoning_contents}
335
339
  )
@@ -355,6 +359,7 @@ def _convert_event_to_streaming_chunk(
355
359
  :param component_info: ComponentInfo object
356
360
  :returns: StreamingChunk object containing the content and metadata extracted from the event.
357
361
  """
362
+
358
363
  # Initialize an empty StreamingChunk to return if no relevant event is found
359
364
  # (e.g. for messageStart and contentBlockStop)
360
365
  base_meta = {"model": model, "received_at": datetime.now(timezone.utc).isoformat()}
@@ -426,19 +431,23 @@ def _convert_event_to_streaming_chunk(
426
431
  meta=base_meta,
427
432
  )
428
433
 
429
- elif "metadata" in event and "usage" in event["metadata"]:
430
- metadata = event["metadata"]
431
- streaming_chunk = StreamingChunk(
432
- content="",
433
- meta={
434
- **base_meta,
435
- "usage": {
436
- "prompt_tokens": metadata["usage"].get("inputTokens", 0),
437
- "completion_tokens": metadata["usage"].get("outputTokens", 0),
438
- "total_tokens": metadata["usage"].get("totalTokens", 0),
439
- },
440
- },
441
- )
434
+ elif "metadata" in event:
435
+ event_meta = event["metadata"]
436
+ chunk_meta: Dict[str, Any] = {**base_meta}
437
+
438
+ if "usage" in event_meta:
439
+ usage = event_meta["usage"]
440
+ chunk_meta["usage"] = {
441
+ "prompt_tokens": usage.get("inputTokens", 0),
442
+ "completion_tokens": usage.get("outputTokens", 0),
443
+ "total_tokens": usage.get("totalTokens", 0),
444
+ }
445
+ if "trace" in event_meta:
446
+ chunk_meta["trace"] = event_meta["trace"]
447
+
448
+ # Only create chunk if we added usage or trace data
449
+ if len(chunk_meta) > len(base_meta):
450
+ streaming_chunk = StreamingChunk(content="", meta=chunk_meta)
442
451
 
443
452
  streaming_chunk.component_info = component_info
444
453
 
@@ -547,8 +556,15 @@ def _parse_streaming_response(
547
556
  content_block_idxs.add(content_block_idx)
548
557
  streaming_callback(streaming_chunk)
549
558
  chunks.append(streaming_chunk)
559
+
550
560
  reply = _convert_streaming_chunks_to_chat_message(chunks=chunks)
561
+
562
+ # both the reasoning content and the trace are ignored in _convert_streaming_chunks_to_chat_message
563
+ # so we need to process them separately
551
564
  reasoning_content = _process_reasoning_contents(chunks=chunks)
565
+ if chunks[-1].meta and "trace" in chunks[-1].meta:
566
+ reply.meta["trace"] = chunks[-1].meta["trace"]
567
+
552
568
  reply = ChatMessage.from_assistant(
553
569
  text=reply.text,
554
570
  meta=reply.meta,
@@ -556,6 +572,7 @@ def _parse_streaming_response(
556
572
  tool_calls=reply.tool_calls,
557
573
  reasoning=reasoning_content,
558
574
  )
575
+
559
576
  return [reply]
560
577
 
561
578
 
@@ -594,3 +611,24 @@ async def _parse_streaming_response_async(
594
611
  reasoning=reasoning_content,
595
612
  )
596
613
  return [reply]
614
+
615
+
616
+ def _validate_guardrail_config(guardrail_config: Optional[Dict[str, str]] = None, streaming: bool = False) -> None:
617
+ """
618
+ Validate the guardrail configuration.
619
+
620
+ :param guardrail_config: The guardrail configuration.
621
+ :param streaming: Whether the streaming is enabled.
622
+
623
+ :raises ValueError: If the guardrail configuration is invalid.
624
+ """
625
+ if guardrail_config is None:
626
+ return
627
+
628
+ required_fields = {"guardrailIdentifier", "guardrailVersion"}
629
+ if not required_fields.issubset(guardrail_config):
630
+ msg = "`guardrailIdentifier` and `guardrailVersion` fields are required in guardrail configuration."
631
+ raise ValueError(msg)
632
+ if not streaming and "streamProcessingMode" in guardrail_config:
633
+ msg = "`streamProcessingMode` field is only supported for streaming (when `streaming_callback` is not None)."
634
+ raise ValueError(msg)