amazon_bedrock.md
1 --- 2 title: "Amazon Bedrock" 3 id: integrations-amazon-bedrock 4 description: "Amazon Bedrock integration for Haystack" 5 slug: "/integrations-amazon-bedrock" 6 --- 7 8 9 ## haystack_integrations.common.amazon_bedrock.errors 10 11 ### AmazonBedrockError 12 13 Bases: <code>Exception</code> 14 15 Any error generated by the Amazon Bedrock integration. 16 17 This error wraps its source transparently in such a way that its attributes 18 can be accessed directly: for example, if the original error has a `message` attribute, 19 `AmazonBedrockError.message` will exist and have the expected content. 20 21 ### AWSConfigurationError 22 23 Bases: <code>AmazonBedrockError</code> 24 25 Exception raised when AWS is not configured correctly 26 27 ### AmazonBedrockConfigurationError 28 29 Bases: <code>AmazonBedrockError</code> 30 31 Exception raised when AmazonBedrock node is not configured correctly 32 33 ### AmazonBedrockInferenceError 34 35 Bases: <code>AmazonBedrockError</code> 36 37 Exception for issues that occur in the Bedrock inference node 38 39 ## haystack_integrations.common.amazon_bedrock.errors 40 41 ### AmazonBedrockError 42 43 Bases: <code>Exception</code> 44 45 Any error generated by the Amazon Bedrock integration. 46 47 This error wraps its source transparently in such a way that its attributes 48 can be accessed directly: for example, if the original error has a `message` attribute, 49 `AmazonBedrockError.message` will exist and have the expected content. 50 51 ### AWSConfigurationError 52 53 Bases: <code>AmazonBedrockError</code> 54 55 Exception raised when AWS is not configured correctly 56 57 ### AmazonBedrockConfigurationError 58 59 Bases: <code>AmazonBedrockError</code> 60 61 Exception raised when AmazonBedrock node is not configured correctly 62 63 ### AmazonBedrockInferenceError 64 65 Bases: <code>AmazonBedrockError</code> 66 67 Exception for issues that occur in the Bedrock inference node 68 69 ## haystack_integrations.common.s3.errors 70 71 ### S3Error 72 73 Bases: <code>Exception</code> 74 75 Exception for issues that occur in the S3 based components 76 77 ### S3ConfigurationError 78 79 Bases: <code>S3Error</code> 80 81 Exception raised when AmazonS3 node is not configured correctly 82 83 ### S3StorageError 84 85 Bases: <code>S3Error</code> 86 87 This exception is raised when an error occurs while interacting with a S3Storage object. 88 89 ## haystack_integrations.common.s3.utils 90 91 ### S3Storage 92 93 This class provides a storage class for downloading files from an AWS S3 bucket. 94 95 #### __init__ 96 97 ```python 98 __init__( 99 s3_bucket: str, 100 session: Session, 101 s3_prefix: str | None = None, 102 endpoint_url: str | None = None, 103 config: Config | None = None, 104 ) -> None 105 ``` 106 107 Initializes the S3Storage object with the provided parameters. 108 109 **Parameters:** 110 111 - **s3_bucket** (<code>str</code>) – The name of the S3 bucket to download files from. 112 - **session** (<code>Session</code>) – The session to use for the S3 client. 113 - **s3_prefix** (<code>str | None</code>) – The optional prefix of the files in the S3 bucket. 114 Can be used to specify folder or naming structure. 115 For example, if the file is in the folder "folder/subfolder/file.txt", 116 the s3_prefix should be "folder/subfolder/". If the file is in the root of the S3 bucket, 117 the s3_prefix should be None. 118 - **endpoint_url** (<code>str | None</code>) – The endpoint URL of the S3 bucket to download files from. 119 - **config** (<code>Config | None</code>) – The configuration to use for the S3 client. 120 121 #### download 122 123 ```python 124 download(key: str, local_file_path: Path) -> None 125 ``` 126 127 Download a file from S3. 128 129 **Parameters:** 130 131 - **key** (<code>str</code>) – The key of the file to download. 132 - **local_file_path** (<code>Path</code>) – The folder path to download the file to. 133 It will be created if it does not exist. The file will be downloaded to 134 the folder with the same name as the key. 135 136 **Raises:** 137 138 - <code>S3ConfigurationError</code> – If the S3 session client cannot be created. 139 - <code>S3StorageError</code> – If the file does not exist in the S3 bucket 140 or the file cannot be downloaded. 141 142 #### from_env 143 144 ```python 145 from_env( 146 *, 147 session: Session, 148 config: Config, 149 s3_bucket_name_env: str = "S3_DOWNLOADER_BUCKET" 150 ) -> S3Storage 151 ``` 152 153 Create a S3Storage object from environment variables. 154 155 The following environment variables are read: 156 157 - `S3_DOWNLOADER_BUCKET` (or the value of `s3_bucket_name_env`): The name of the S3 bucket 158 to download files from. Required — raises `ValueError` if not set. 159 - `S3_DOWNLOADER_PREFIX`: Optional prefix to apply to all S3 keys (e.g. `"folder/subfolder/"`). 160 - `AWS_ENDPOINT_URL`: Optional custom endpoint URL, useful for S3-compatible services 161 such as MinIO or LocalStack. 162 163 **Parameters:** 164 165 - **session** (<code>Session</code>) – The boto3 `Session` to use when creating the S3 client. 166 - **config** (<code>Config</code>) – The botocore `Config` to apply to the S3 client. 167 - **s3_bucket_name_env** (<code>str</code>) – The name of the environment variable of the S3 bucket to download files from. 168 By default, the value is `"S3_DOWNLOADER_BUCKET"`. 169 170 **Returns:** 171 172 - <code>S3Storage</code> – A fully initialized `S3Storage` instance. 173 174 **Raises:** 175 176 - <code>ValueError</code> – If the environment variable specified by `s3_bucket_name_env` is not set 177 or is empty. 178 179 ## haystack_integrations.components.downloaders.s3.s3_downloader 180 181 ### S3Downloader 182 183 A component for downloading files from AWS S3 Buckets to local filesystem. 184 185 Supports filtering by file extensions. 186 187 #### __init__ 188 189 ```python 190 __init__( 191 *, 192 aws_access_key_id: Secret | None = Secret.from_env_var( 193 "AWS_ACCESS_KEY_ID", strict=False 194 ), 195 aws_secret_access_key: Secret | None = Secret.from_env_var( 196 "AWS_SECRET_ACCESS_KEY", strict=False 197 ), 198 aws_session_token: Secret | None = Secret.from_env_var( 199 "AWS_SESSION_TOKEN", strict=False 200 ), 201 aws_region_name: Secret | None = Secret.from_env_var( 202 "AWS_DEFAULT_REGION", strict=False 203 ), 204 aws_profile_name: Secret | None = Secret.from_env_var( 205 "AWS_PROFILE", strict=False 206 ), 207 boto3_config: dict[str, Any] | None = None, 208 file_root_path: str | None = None, 209 file_extensions: list[str] | None = None, 210 file_name_meta_key: str = "file_name", 211 max_workers: int = 32, 212 max_cache_size: int = 100, 213 s3_key_generation_function: Callable[[Document], str] | None = None, 214 s3_bucket_name_env: str = "S3_DOWNLOADER_BUCKET" 215 ) -> None 216 ``` 217 218 Initializes the `S3Downloader` with the provided parameters. 219 220 Note that the AWS credentials are not required if the AWS environment is configured correctly. These are loaded 221 automatically from the environment or the AWS configuration file and do not need to be provided explicitly via 222 the constructor. If the AWS environment is not configured users need to provide the AWS credentials via the 223 constructor. Three required parameters are `aws_access_key_id`, `aws_secret_access_key`, 224 and `aws_region_name`. 225 226 **Parameters:** 227 228 - **aws_access_key_id** (<code>Secret | None</code>) – AWS access key ID. 229 - **aws_secret_access_key** (<code>Secret | None</code>) – AWS secret access key. 230 - **aws_session_token** (<code>Secret | None</code>) – AWS session token. 231 - **aws_region_name** (<code>Secret | None</code>) – AWS region name. 232 - **aws_profile_name** (<code>Secret | None</code>) – AWS profile name. 233 - **boto3_config** (<code>dict\[str, Any\] | None</code>) – Dictionary of configuration options for the underlying Boto3 client. 234 Can be used to tune [retry behavior](https://docs.aws.amazon.com/boto3/latest/guide/retries.html) 235 and other low-level settings like timeouts and connection management. 236 - **file_root_path** (<code>str | None</code>) – The path where the file will be downloaded. 237 Can be set through this parameter or the `FILE_ROOT_PATH` environment variable. 238 If none of them is set, a `ValueError` is raised. 239 - **file_extensions** (<code>list\[str\] | None</code>) – The file extensions that are permitted to be downloaded. 240 By default, all file extensions are allowed. 241 - **max_workers** (<code>int</code>) – The maximum number of workers to use for concurrent downloads. 242 - **max_cache_size** (<code>int</code>) – The maximum number of files to cache. 243 - **file_name_meta_key** (<code>str</code>) – The name of the meta key that contains the file name to download. The file name 244 will also be used to create local file path for download. 245 By default, the `Document.meta["file_name"]` is used. If you want to use a 246 different key in `Document.meta`, you can set it here. 247 - **s3_key_generation_function** (<code>Callable\\[[Document\], str\] | None</code>) – An optional function that generates the S3 key for the file to download. 248 If not provided, the default behavior is to use `Document.meta[file_name_meta_key]`. 249 The function must accept a `Document` object and return a string. 250 If the environment variable `S3_DOWNLOADER_PREFIX` is set, its value will be automatically 251 prefixed to the generated S3 key. 252 - **s3_bucket_name_env** (<code>str</code>) – The name of the environment variable of the S3 bucket to download files from. 253 By default, the value is `"S3_DOWNLOADER_BUCKET"`. 254 255 **Raises:** 256 257 - <code>ValueError</code> – If the `file_root_path` is not set through 258 the constructor or the `FILE_ROOT_PATH` environment variable. 259 260 #### warm_up 261 262 ```python 263 warm_up() -> None 264 ``` 265 266 Warm up the component by initializing the settings and storage. 267 268 #### run 269 270 ```python 271 run(documents: list[Document]) -> dict[str, list[Document]] 272 ``` 273 274 Download files from AWS S3 Buckets to local filesystem. 275 276 Return enriched `Document`s with the path of the downloaded file. 277 278 **Parameters:** 279 280 - **documents** (<code>list\[Document\]</code>) – Document containing the name of the file to download in the meta field. 281 282 **Returns:** 283 284 - <code>dict\[str, list\[Document\]\]</code> – A dictionary with: 285 - `documents`: The downloaded `Document`s; each has `meta['file_path']`. 286 287 **Raises:** 288 289 - <code>S3Error</code> – If a download attempt fails or the file does not exist in the S3 bucket. 290 - <code>ValueError</code> – If the path where files will be downloaded is not set. 291 292 #### to_dict 293 294 ```python 295 to_dict() -> dict[str, Any] 296 ``` 297 298 Serialize the component to a dictionary. 299 300 #### from_dict 301 302 ```python 303 from_dict(data: dict[str, Any]) -> S3Downloader 304 ``` 305 306 Deserializes the component from a dictionary. 307 308 **Parameters:** 309 310 - **data** (<code>dict\[str, Any\]</code>) – Dictionary to deserialize from. 311 312 **Returns:** 313 314 - <code>S3Downloader</code> – Deserialized component. 315 316 ## haystack_integrations.components.embedders.amazon_bedrock.document_embedder 317 318 ### AmazonBedrockDocumentEmbedder 319 320 A component for computing Document embeddings using Amazon Bedrock. 321 322 The embedding of each Document is stored in the `embedding` field of the Document. 323 324 Usage example: 325 326 ```python 327 import os 328 from haystack.dataclasses import Document 329 from haystack_integrations.components.embedders.amazon_bedrock import AmazonBedrockDocumentEmbedder 330 331 os.environ["AWS_ACCESS_KEY_ID"] = "..." 332 os.environ["AWS_SECRET_ACCESS_KEY_ID"] = "..." 333 os.environ["AWS_DEFAULT_REGION"] = "..." 334 335 embedder = AmazonBedrockDocumentEmbedder( 336 model="cohere.embed-english-v3", 337 input_type="search_document", 338 ) 339 340 doc = Document(content="I love Paris in the winter.", meta={"name": "doc1"}) 341 342 result = embedder.run([doc]) 343 print(result['documents'][0].embedding) 344 345 # [0.002, 0.032, 0.504, ...] 346 ``` 347 348 #### __init__ 349 350 ```python 351 __init__( 352 model: str, 353 aws_access_key_id: Secret | None = Secret.from_env_var( 354 "AWS_ACCESS_KEY_ID", strict=False 355 ), 356 aws_secret_access_key: Secret | None = Secret.from_env_var( 357 "AWS_SECRET_ACCESS_KEY", strict=False 358 ), 359 aws_session_token: Secret | None = Secret.from_env_var( 360 "AWS_SESSION_TOKEN", strict=False 361 ), 362 aws_region_name: Secret | None = Secret.from_env_var( 363 "AWS_DEFAULT_REGION", strict=False 364 ), 365 aws_profile_name: Secret | None = Secret.from_env_var( 366 "AWS_PROFILE", strict=False 367 ), 368 batch_size: int = 32, 369 progress_bar: bool = True, 370 meta_fields_to_embed: list[str] | None = None, 371 embedding_separator: str = "\n", 372 boto3_config: dict[str, Any] | None = None, 373 **kwargs: Any 374 ) -> None 375 ``` 376 377 Initializes the AmazonBedrockDocumentEmbedder with the provided parameters. 378 379 The parameters are passed to the Amazon Bedrock client. 380 381 Note that the AWS credentials are not required if the AWS environment is configured correctly. These are loaded 382 automatically from the environment or the AWS configuration file and do not need to be provided explicitly via 383 the constructor. If the AWS environment is not configured users need to provide the AWS credentials via the 384 constructor. Aside from model, three required parameters are `aws_access_key_id`, `aws_secret_access_key`, 385 and `aws_region_name`. 386 387 **Parameters:** 388 389 - **model** (<code>str</code>) – The embedding model to use. 390 Amazon Titan and Cohere embedding models are supported, for example: 391 "amazon.titan-embed-text-v1", "amazon.titan-embed-text-v2:0", "amazon.titan-embed-image-v1", 392 "cohere.embed-english-v3", "cohere.embed-multilingual-v3", "cohere.embed-v4:0". 393 To find all supported models, refer to the Amazon Bedrock 394 [documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html) and 395 filter for "embedding", then select models from the Amazon Titan and Cohere series. 396 - **aws_access_key_id** (<code>Secret | None</code>) – AWS access key ID. 397 - **aws_secret_access_key** (<code>Secret | None</code>) – AWS secret access key. 398 - **aws_session_token** (<code>Secret | None</code>) – AWS session token. 399 - **aws_region_name** (<code>Secret | None</code>) – AWS region name. 400 - **aws_profile_name** (<code>Secret | None</code>) – AWS profile name. 401 - **batch_size** (<code>int</code>) – Number of Documents to encode at once. 402 Only Cohere models support batch inference. This parameter is ignored for Amazon Titan models. 403 - **progress_bar** (<code>bool</code>) – Whether to show a progress bar or not. Can be helpful to disable in production deployments 404 to keep the logs clean. 405 - **meta_fields_to_embed** (<code>list\[str\] | None</code>) – List of meta fields that should be embedded along with the Document text. 406 - **embedding_separator** (<code>str</code>) – Separator used to concatenate the meta fields to the Document text. 407 - **boto3_config** (<code>dict\[str, Any\] | None</code>) – Dictionary of configuration options for the underlying Boto3 client. 408 Can be used to tune [retry behavior](https://docs.aws.amazon.com/boto3/latest/guide/retries.html) 409 and other low-level settings like timeouts and connection management. 410 - **kwargs** (<code>Any</code>) – Additional parameters to pass for model inference. For example, `input_type` and `truncate` for 411 Cohere models. 412 413 **Raises:** 414 415 - <code>ValueError</code> – If the model is not supported. 416 - <code>AmazonBedrockConfigurationError</code> – If the AWS environment is not configured correctly. 417 418 #### run 419 420 ```python 421 run(documents: list[Document]) -> dict[str, list[Document]] 422 ``` 423 424 Embed the provided `Document`s using the specified model. 425 426 **Parameters:** 427 428 - **documents** (<code>list\[Document\]</code>) – The `Document`s to embed. 429 430 **Returns:** 431 432 - <code>dict\[str, list\[Document\]\]</code> – A dictionary with the following keys: 433 - `documents`: The `Document`s with the `embedding` field populated. 434 435 **Raises:** 436 437 - <code>AmazonBedrockInferenceError</code> – If the inference fails. 438 439 #### to_dict 440 441 ```python 442 to_dict() -> dict[str, Any] 443 ``` 444 445 Serializes the component to a dictionary. 446 447 **Returns:** 448 449 - <code>dict\[str, Any\]</code> – Dictionary with serialized data. 450 451 #### from_dict 452 453 ```python 454 from_dict(data: dict[str, Any]) -> AmazonBedrockDocumentEmbedder 455 ``` 456 457 Deserializes the component from a dictionary. 458 459 **Parameters:** 460 461 - **data** (<code>dict\[str, Any\]</code>) – Dictionary to deserialize from. 462 463 **Returns:** 464 465 - <code>AmazonBedrockDocumentEmbedder</code> – Deserialized component. 466 467 ## haystack_integrations.components.embedders.amazon_bedrock.document_image_embedder 468 469 ### AmazonBedrockDocumentImageEmbedder 470 471 A component for computing Document embeddings based on images using Amazon Bedrock models. 472 473 The embedding of each Document is stored in the `embedding` field of the Document. 474 475 ### Usage example 476 477 ```python 478 from haystack import Document 479 rom haystack_integrations.components.embedders.amazon_bedrock import AmazonBedrockDocumentImageEmbedder 480 481 os.environ["AWS_ACCESS_KEY_ID"] = "..." 482 os.environ["AWS_SECRET_ACCESS_KEY_ID"] = "..." 483 os.environ["AWS_DEFAULT_REGION"] = "..." 484 485 embedder = AmazonBedrockDocumentImageEmbedder(model="amazon.titan-embed-image-v1") 486 487 documents = [ 488 Document(content="A photo of a cat", meta={"file_path": "cat.jpg"}), 489 Document(content="A photo of a dog", meta={"file_path": "dog.jpg"}), 490 ] 491 492 result = embedder.run(documents=documents) 493 documents_with_embeddings = result["documents"] 494 print(documents_with_embeddings) 495 496 # [Document(id=..., 497 # content='A photo of a cat', 498 # meta={'file_path': 'cat.jpg', 499 # 'embedding_source': {'type': 'image', 'file_path_meta_field': 'file_path'}}, 500 # embedding=vector of size 512), 501 # ...] 502 ``` 503 504 #### __init__ 505 506 ```python 507 __init__( 508 *, 509 model: str, 510 aws_access_key_id: Secret | None = Secret.from_env_var( 511 "AWS_ACCESS_KEY_ID", strict=False 512 ), 513 aws_secret_access_key: Secret | None = Secret.from_env_var( 514 "AWS_SECRET_ACCESS_KEY", strict=False 515 ), 516 aws_session_token: Secret | None = Secret.from_env_var( 517 "AWS_SESSION_TOKEN", strict=False 518 ), 519 aws_region_name: Secret | None = Secret.from_env_var( 520 "AWS_DEFAULT_REGION", strict=False 521 ), 522 aws_profile_name: Secret | None = Secret.from_env_var( 523 "AWS_PROFILE", strict=False 524 ), 525 file_path_meta_field: str = "file_path", 526 root_path: str | None = None, 527 image_size: tuple[int, int] | None = None, 528 progress_bar: bool = True, 529 boto3_config: dict[str, Any] | None = None, 530 **kwargs: Any 531 ) -> None 532 ``` 533 534 Creates a AmazonBedrockDocumentImageEmbedder component. 535 536 **Parameters:** 537 538 - **model** (<code>str</code>) – The embedding model to use. 539 Amazon Titan and Cohere multimodal embedding models are supported, for example: 540 "amazon.titan-embed-image-v1", "cohere.embed-english-v3", "cohere.embed-multilingual-v3", 541 "cohere.embed-v4:0". 542 To find all supported models, refer to the Amazon Bedrock 543 [documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html) and 544 filter for "embedding", then select multimodal models from the Amazon Titan and Cohere series. 545 - **aws_access_key_id** (<code>Secret | None</code>) – AWS access key ID. 546 - **aws_secret_access_key** (<code>Secret | None</code>) – AWS secret access key. 547 - **aws_session_token** (<code>Secret | None</code>) – AWS session token. 548 - **aws_region_name** (<code>Secret | None</code>) – AWS region name. 549 - **aws_profile_name** (<code>Secret | None</code>) – AWS profile name. 550 - **file_path_meta_field** (<code>str</code>) – The metadata field in the Document that contains the file path to the image or PDF. 551 - **root_path** (<code>str | None</code>) – The root directory path where document files are located. If provided, file paths in 552 document metadata will be resolved relative to this path. If None, file paths are treated as absolute paths. 553 - **image_size** (<code>tuple\[int, int\] | None</code>) – If provided, resizes the image to fit within the specified dimensions (width, height) while 554 maintaining aspect ratio. This reduces file size, memory usage, and processing time, which is beneficial 555 when working with models that have resolution constraints or when transmitting images to remote services. 556 - **progress_bar** (<code>bool</code>) – If `True`, shows a progress bar when embedding documents. 557 - **boto3_config** (<code>dict\[str, Any\] | None</code>) – Dictionary of configuration options for the underlying Boto3 client. 558 Can be used to tune [retry behavior](https://docs.aws.amazon.com/boto3/latest/guide/retries.html) 559 and other low-level settings like timeouts and connection management. 560 - **kwargs** (<code>Any</code>) – Additional parameters to pass for model inference. 561 For example, `embeddingConfig` for Amazon Titan models and 562 `embedding_types` for Cohere models. 563 564 **Raises:** 565 566 - <code>ValueError</code> – If the model is not supported. 567 - <code>AmazonBedrockConfigurationError</code> – If the AWS environment is not configured correctly. 568 569 #### to_dict 570 571 ```python 572 to_dict() -> dict[str, Any] 573 ``` 574 575 Serializes the component to a dictionary. 576 577 **Returns:** 578 579 - <code>dict\[str, Any\]</code> – Dictionary with serialized data. 580 581 #### from_dict 582 583 ```python 584 from_dict(data: dict[str, Any]) -> AmazonBedrockDocumentImageEmbedder 585 ``` 586 587 Deserializes the component from a dictionary. 588 589 **Parameters:** 590 591 - **data** (<code>dict\[str, Any\]</code>) – Dictionary to deserialize from. 592 593 **Returns:** 594 595 - <code>AmazonBedrockDocumentImageEmbedder</code> – Deserialized component. 596 597 #### run 598 599 ```python 600 run(documents: list[Document]) -> dict[str, list[Document]] 601 ``` 602 603 Embed a list of images. 604 605 **Parameters:** 606 607 - **documents** (<code>list\[Document\]</code>) – Documents to embed. 608 609 **Returns:** 610 611 - <code>dict\[str, list\[Document\]\]</code> – A dictionary with the following keys: 612 - `documents`: Documents with embeddings. 613 614 ## haystack_integrations.components.embedders.amazon_bedrock.text_embedder 615 616 ### AmazonBedrockTextEmbedder 617 618 A component for embedding strings using Amazon Bedrock. 619 620 Usage example: 621 622 ```python 623 import os 624 from haystack_integrations.components.embedders.amazon_bedrock import AmazonBedrockTextEmbedder 625 626 os.environ["AWS_ACCESS_KEY_ID"] = "..." 627 os.environ["AWS_SECRET_ACCESS_KEY_ID"] = "..." 628 os.environ["AWS_DEFAULT_REGION"] = "..." 629 630 embedder = AmazonBedrockTextEmbedder( 631 model="cohere.embed-english-v3", 632 input_type="search_query", 633 ) 634 635 print(text_embedder.run("I love Paris in the summer.")) 636 637 # {'embedding': [0.002, 0.032, 0.504, ...]} 638 ``` 639 640 #### __init__ 641 642 ```python 643 __init__( 644 model: str, 645 aws_access_key_id: Secret | None = Secret.from_env_var( 646 "AWS_ACCESS_KEY_ID", strict=False 647 ), 648 aws_secret_access_key: Secret | None = Secret.from_env_var( 649 "AWS_SECRET_ACCESS_KEY", strict=False 650 ), 651 aws_session_token: Secret | None = Secret.from_env_var( 652 "AWS_SESSION_TOKEN", strict=False 653 ), 654 aws_region_name: Secret | None = Secret.from_env_var( 655 "AWS_DEFAULT_REGION", strict=False 656 ), 657 aws_profile_name: Secret | None = Secret.from_env_var( 658 "AWS_PROFILE", strict=False 659 ), 660 boto3_config: dict[str, Any] | None = None, 661 **kwargs: Any 662 ) -> None 663 ``` 664 665 Initializes the AmazonBedrockTextEmbedder with the provided parameters. 666 667 The parameters are passed to the Amazon Bedrock client. 668 669 Note that the AWS credentials are not required if the AWS environment is configured correctly. These are loaded 670 automatically from the environment or the AWS configuration file and do not need to be provided explicitly via 671 the constructor. If the AWS environment is not configured users need to provide the AWS credentials via the 672 constructor. Aside from model, three required parameters are `aws_access_key_id`, `aws_secret_access_key`, 673 and `aws_region_name`. 674 675 **Parameters:** 676 677 - **model** (<code>str</code>) – The embedding model to use. 678 Amazon Titan and Cohere embedding models are supported, for example: 679 "amazon.titan-embed-text-v1", "amazon.titan-embed-text-v2:0", "amazon.titan-embed-image-v1", 680 "cohere.embed-english-v3", "cohere.embed-multilingual-v3", "cohere.embed-v4:0". 681 To find all supported models, refer to the Amazon Bedrock 682 [documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html) and 683 filter for "embedding", then select models from the Amazon Titan and Cohere series. 684 - **aws_access_key_id** (<code>Secret | None</code>) – AWS access key ID. 685 - **aws_secret_access_key** (<code>Secret | None</code>) – AWS secret access key. 686 - **aws_session_token** (<code>Secret | None</code>) – AWS session token. 687 - **aws_region_name** (<code>Secret | None</code>) – AWS region name. 688 - **aws_profile_name** (<code>Secret | None</code>) – AWS profile name. 689 - **boto3_config** (<code>dict\[str, Any\] | None</code>) – Dictionary of configuration options for the underlying Boto3 client. 690 Can be used to tune [retry behavior](https://docs.aws.amazon.com/boto3/latest/guide/retries.html) 691 and other low-level settings like timeouts and connection management. 692 - **kwargs** (<code>Any</code>) – Additional parameters to pass for model inference. For example, `input_type` and `truncate` for 693 Cohere models. 694 695 **Raises:** 696 697 - <code>ValueError</code> – If the model is not supported. 698 - <code>AmazonBedrockConfigurationError</code> – If the AWS environment is not configured correctly. 699 700 #### run 701 702 ```python 703 run(text: str) -> dict[str, list[float]] 704 ``` 705 706 Embeds the input text using the Amazon Bedrock model. 707 708 **Parameters:** 709 710 - **text** (<code>str</code>) – The input text to embed. 711 712 **Returns:** 713 714 - <code>dict\[str, list\[float\]\]</code> – A dictionary with the following keys: 715 - `embedding`: The embedding of the input text. 716 717 **Raises:** 718 719 - <code>TypeError</code> – If the input text is not a string. 720 - <code>AmazonBedrockInferenceError</code> – If the model inference fails. 721 722 #### to_dict 723 724 ```python 725 to_dict() -> dict[str, Any] 726 ``` 727 728 Serializes the component to a dictionary. 729 730 **Returns:** 731 732 - <code>dict\[str, Any\]</code> – Dictionary with serialized data. 733 734 #### from_dict 735 736 ```python 737 from_dict(data: dict[str, Any]) -> AmazonBedrockTextEmbedder 738 ``` 739 740 Deserializes the component from a dictionary. 741 742 **Parameters:** 743 744 - **data** (<code>dict\[str, Any\]</code>) – Dictionary to deserialize from. 745 746 **Returns:** 747 748 - <code>AmazonBedrockTextEmbedder</code> – Deserialized component. 749 750 ## haystack_integrations.components.generators.amazon_bedrock.adapters 751 752 ### BedrockModelAdapter 753 754 Bases: <code>ABC</code> 755 756 Base class for Amazon Bedrock model adapters. 757 758 Each subclass of this class is designed to address the unique specificities of a particular LLM it adapts, 759 focusing on preparing the requests and extracting the responses from the Amazon Bedrock hosted LLMs. 760 761 **Parameters:** 762 763 - **model_kwargs** (<code>dict\[str, Any\]</code>) – Keyword arguments for the model. You can find the full list of parameters in the 764 Amazon Bedrock API [documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html). 765 - **max_length** (<code>int | None</code>) – Maximum length of generated text. This is mapped to the correct parameter for each model. 766 It will be overridden by the corresponding parameter in the `model_kwargs` if it is present. 767 768 #### prepare_body 769 770 ```python 771 prepare_body(prompt: str, **inference_kwargs: Any) -> dict[str, Any] 772 ``` 773 774 Prepares the body for the Amazon Bedrock request. 775 776 Each subclass should implement this method to prepare the request body for the specific model. 777 778 **Parameters:** 779 780 - **prompt** (<code>str</code>) – The prompt to be sent to the model. 781 - **inference_kwargs** (<code>Any</code>) – Additional keyword arguments passed to the handler. 782 783 **Returns:** 784 785 - <code>dict\[str, Any\]</code> – A dictionary containing the body for the request. 786 787 #### get_responses 788 789 ```python 790 get_responses(response_body: dict[str, Any]) -> list[str] 791 ``` 792 793 Extracts the responses from the Amazon Bedrock response. 794 795 **Parameters:** 796 797 - **response_body** (<code>dict\[str, Any\]</code>) – The response body from the Amazon Bedrock request. 798 799 **Returns:** 800 801 - <code>list\[str\]</code> – A list of responses. 802 803 #### get_stream_responses 804 805 ```python 806 get_stream_responses( 807 stream: EventStream, streaming_callback: SyncStreamingCallbackT 808 ) -> list[str] 809 ``` 810 811 Extracts the responses from the Amazon Bedrock streaming response. 812 813 **Parameters:** 814 815 - **stream** (<code>EventStream</code>) – The streaming response from the Amazon Bedrock request. 816 - **streaming_callback** (<code>SyncStreamingCallbackT</code>) – The handler for the streaming response. 817 818 **Returns:** 819 820 - <code>list\[str\]</code> – A list of string responses. 821 822 ### AnthropicClaudeAdapter 823 824 Bases: <code>BedrockModelAdapter</code> 825 826 Adapter for the Anthropic Claude models. 827 828 **Parameters:** 829 830 - **model_kwargs** (<code>dict\[str, Any\]</code>) – Keyword arguments for the model. You can find the full list of parameters in the 831 Amazon Bedrock API documentation for the Claude model 832 [here](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html). 833 Some example parameters are: 834 - use_messages_api: Whether to use the messages API, default: True 835 - include_thinking: Whether to include thinking output, default: True 836 - thinking_tag: XML tag for thinking content, default: "thinking" 837 - **max_length** (<code>int | None</code>) – Maximum length of generated text 838 839 #### prepare_body 840 841 ```python 842 prepare_body(prompt: str, **inference_kwargs: Any) -> dict[str, Any] 843 ``` 844 845 Prepares the body for the Claude model 846 847 **Parameters:** 848 849 - **prompt** (<code>str</code>) – The prompt to be sent to the model. 850 - **inference_kwargs** (<code>Any</code>) – Additional keyword arguments passed to the handler. 851 852 **Returns:** 853 854 - <code>dict\[str, Any\]</code> – A dictionary with the following keys: 855 - `prompt`: The prompt to be sent to the model. 856 - specified inference parameters. 857 858 ### MistralAdapter 859 860 Bases: <code>BedrockModelAdapter</code> 861 862 Adapter for the Mistral models. 863 864 #### prepare_body 865 866 ```python 867 prepare_body(prompt: str, **inference_kwargs: Any) -> dict[str, Any] 868 ``` 869 870 Prepares the body for the Mistral model 871 872 **Parameters:** 873 874 - **prompt** (<code>str</code>) – The prompt to be sent to the model. 875 - **inference_kwargs** (<code>Any</code>) – Additional keyword arguments passed to the handler. 876 877 **Returns:** 878 879 - <code>dict\[str, Any\]</code> – A dictionary with the following keys: 880 - `prompt`: The prompt to be sent to the model. 881 - specified inference parameters. 882 883 ### CohereCommandAdapter 884 885 Bases: <code>BedrockModelAdapter</code> 886 887 Adapter for the Cohere Command model. 888 889 #### prepare_body 890 891 ```python 892 prepare_body(prompt: str, **inference_kwargs: Any) -> dict[str, Any] 893 ``` 894 895 Prepares the body for the Command model 896 897 **Parameters:** 898 899 - **prompt** (<code>str</code>) – The prompt to be sent to the model. 900 - **inference_kwargs** (<code>Any</code>) – Additional keyword arguments passed to the handler. 901 902 **Returns:** 903 904 - <code>dict\[str, Any\]</code> – A dictionary with the following keys: 905 - `prompt`: The prompt to be sent to the model. 906 - specified inference parameters. 907 908 ### CohereCommandRAdapter 909 910 Bases: <code>BedrockModelAdapter</code> 911 912 Adapter for the Cohere Command R models. 913 914 #### prepare_body 915 916 ```python 917 prepare_body(prompt: str, **inference_kwargs: Any) -> dict[str, Any] 918 ``` 919 920 Prepares the body for the Command model 921 922 **Parameters:** 923 924 - **prompt** (<code>str</code>) – The prompt to be sent to the model. 925 - **inference_kwargs** (<code>Any</code>) – Additional keyword arguments passed to the handler. 926 927 **Returns:** 928 929 - <code>dict\[str, Any\]</code> – A dictionary with the following keys: 930 - `prompt`: The prompt to be sent to the model. 931 - specified inference parameters. 932 933 ### AI21LabsJurassic2Adapter 934 935 Bases: <code>BedrockModelAdapter</code> 936 937 Model adapter for AI21 Labs' Jurassic 2 models. 938 939 #### prepare_body 940 941 ```python 942 prepare_body(prompt: str, **inference_kwargs: Any) -> dict[str, Any] 943 ``` 944 945 Prepares the body for the Jurassic 2 model. 946 947 **Parameters:** 948 949 - **prompt** (<code>str</code>) – The prompt to be sent to the model. 950 - **inference_kwargs** (<code>Any</code>) – Additional keyword arguments passed to the handler. 951 952 **Returns:** 953 954 - <code>dict\[str, Any\]</code> – A dictionary with the following keys: 955 - `prompt`: The prompt to be sent to the model. 956 - specified inference parameters. 957 958 ### AmazonTitanAdapter 959 960 Bases: <code>BedrockModelAdapter</code> 961 962 Adapter for Amazon's Titan models. 963 964 #### prepare_body 965 966 ```python 967 prepare_body(prompt: str, **inference_kwargs: Any) -> dict[str, Any] 968 ``` 969 970 Prepares the body for the Titan model 971 972 **Parameters:** 973 974 - **prompt** (<code>str</code>) – The prompt to be sent to the model. 975 - **inference_kwargs** (<code>Any</code>) – Additional keyword arguments passed to the handler. 976 977 **Returns:** 978 979 - <code>dict\[str, Any\]</code> – A dictionary with the following keys 980 - `inputText`: The prompt to be sent to the model. 981 - specified inference parameters. 982 983 ### MetaLlamaAdapter 984 985 Bases: <code>BedrockModelAdapter</code> 986 987 Adapter for Meta's Llama2 models. 988 989 #### prepare_body 990 991 ```python 992 prepare_body(prompt: str, **inference_kwargs: Any) -> dict[str, Any] 993 ``` 994 995 Prepares the body for the Llama2 model 996 997 **Parameters:** 998 999 - **prompt** (<code>str</code>) – The prompt to be sent to the model. 1000 - **inference_kwargs** (<code>Any</code>) – Additional keyword arguments passed to the handler. 1001 1002 **Returns:** 1003 1004 - <code>dict\[str, Any\]</code> – A dictionary with the following keys: 1005 - `prompt`: The prompt to be sent to the model. 1006 - specified inference parameters. 1007 1008 ## haystack_integrations.components.generators.amazon_bedrock.chat.chat_generator 1009 1010 ### AmazonBedrockChatGenerator 1011 1012 Completes chats using LLMs hosted on Amazon Bedrock available via the Bedrock Converse API. 1013 1014 For example, to use the Anthropic Claude 4.6 Sonnet model, initialize this component with the 1015 'global.anthropic.claude-sonnet-4-6' model name. 1016 1017 **Usage example** 1018 1019 ```python 1020 from haystack_integrations.components.generators.amazon_bedrock import AmazonBedrockChatGenerator 1021 from haystack.dataclasses import ChatMessage 1022 from haystack.components.generators.utils import print_streaming_chunk 1023 1024 messages = [ChatMessage.from_system("\nYou are a helpful, respectful and honest assistant, answer in German only"), 1025 ChatMessage.from_user("What's Natural Language Processing?")] 1026 1027 1028 client = AmazonBedrockChatGenerator(model="global.anthropic.claude-sonnet-4-6", 1029 streaming_callback=print_streaming_chunk) 1030 client.run(messages, generation_kwargs={"max_tokens": 512}) 1031 ``` 1032 1033 **Multimodal example** 1034 1035 ```python 1036 from haystack.dataclasses import ChatMessage, ImageContent 1037 from haystack_integrations.components.generators.amazon_bedrock import AmazonBedrockChatGenerator 1038 1039 generator = AmazonBedrockChatGenerator(model="global.anthropic.claude-sonnet-4-6") 1040 1041 image_content = ImageContent.from_file_path(file_path="apple.jpg") 1042 1043 message = ChatMessage.from_user(content_parts=["Describe the image using 10 words at most.", image_content]) 1044 1045 response = generator.run(messages=[message])["replies"][0].text 1046 1047 print(response) 1048 > The image shows a red apple. 1049 ``` 1050 1051 **Tool usage example** 1052 1053 AmazonBedrockChatGenerator supports Haystack's unified tool architecture, allowing tools to be used 1054 across different chat generators. The same tool definitions and usage patterns work consistently 1055 whether using Amazon Bedrock, OpenAI, Ollama, or any other supported LLM providers. 1056 1057 ```python 1058 from haystack.dataclasses import ChatMessage 1059 from haystack.tools import Tool 1060 from haystack_integrations.components.generators.amazon_bedrock import AmazonBedrockChatGenerator 1061 1062 def weather(city: str): 1063 return f'The weather in {city} is sunny and 32°C' 1064 1065 # Define tool parameters 1066 tool_parameters = { 1067 "type": "object", 1068 "properties": {"city": {"type": "string"}}, 1069 "required": ["city"] 1070 } 1071 1072 # Create weather tool 1073 weather_tool = Tool( 1074 name="weather", 1075 description="useful to determine the weather in a given location", 1076 parameters=tool_parameters, 1077 function=weather 1078 ) 1079 1080 # Initialize generator with tool 1081 client = AmazonBedrockChatGenerator( 1082 model="global.anthropic.claude-sonnet-4-6", 1083 tools=[weather_tool] 1084 ) 1085 1086 # Run initial query 1087 messages = [ChatMessage.from_user("What's the weather like in Paris?")] 1088 results = client.run(messages=messages) 1089 1090 # Get tool call from response 1091 tool_message = next(msg for msg in results["replies"] if msg.tool_call) 1092 tool_call = tool_message.tool_call 1093 1094 # Execute tool and send result back 1095 weather_result = weather(**tool_call.arguments) 1096 new_messages = [ 1097 messages[0], 1098 tool_message, 1099 ChatMessage.from_tool(tool_result=weather_result, origin=tool_call) 1100 ] 1101 1102 # Get final response 1103 final_result = client.run(new_messages) 1104 print(final_result["replies"][0].text) 1105 1106 > Based on the information I've received, I can tell you that the weather in Paris is 1107 > currently sunny with a temperature of 32°C (which is about 90°F). 1108 ``` 1109 1110 **Prompt caching** 1111 1112 This component supports prompt caching. You can use the `tools_cachepoint_config` parameter to configure the cache 1113 point for tools. 1114 To cache messages, you can use the `cachePoint` key in `ChatMessage.meta` attribute. 1115 1116 ```python 1117 ChatMessage.from_user("Long message...", meta={"cachePoint": {"type": "default"}}) 1118 ``` 1119 1120 For more information, see the [Amazon Bedrock documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html). 1121 1122 **Authentication** 1123 1124 AmazonBedrockChatGenerator uses AWS for authentication. You can use the AWS CLI to authenticate through your IAM. 1125 For more information on setting up an IAM identity-based policy, see [Amazon Bedrock documentation] 1126 (https://docs.aws.amazon.com/bedrock/latest/userguide/security_iam_id-based-policy-examples.html). 1127 1128 If the AWS environment is configured correctly, the AWS credentials are not required as they're loaded 1129 automatically from the environment or the AWS configuration file. 1130 If the AWS environment is not configured, set `aws_access_key_id`, `aws_secret_access_key`, 1131 and `aws_region_name` as environment variables or pass them as 1132 [Secret](https://docs.haystack.deepset.ai/docs/secret-management) arguments. Make sure the region you set 1133 supports Amazon Bedrock. 1134 1135 #### __init__ 1136 1137 ```python 1138 __init__( 1139 model: str, 1140 aws_access_key_id: Secret | None = Secret.from_env_var( 1141 ["AWS_ACCESS_KEY_ID"], strict=False 1142 ), 1143 aws_secret_access_key: Secret | None = Secret.from_env_var( 1144 ["AWS_SECRET_ACCESS_KEY"], strict=False 1145 ), 1146 aws_session_token: Secret | None = Secret.from_env_var( 1147 ["AWS_SESSION_TOKEN"], strict=False 1148 ), 1149 aws_region_name: Secret | None = Secret.from_env_var( 1150 ["AWS_DEFAULT_REGION"], strict=False 1151 ), 1152 aws_profile_name: Secret | None = Secret.from_env_var( 1153 ["AWS_PROFILE"], strict=False 1154 ), 1155 generation_kwargs: dict[str, Any] | None = None, 1156 streaming_callback: StreamingCallbackT | None = None, 1157 boto3_config: dict[str, Any] | None = None, 1158 tools: ToolsType | None = None, 1159 *, 1160 guardrail_config: dict[str, str] | None = None, 1161 tools_cachepoint_config: dict[str, str] | None = None 1162 ) -> None 1163 ``` 1164 1165 Initializes the `AmazonBedrockChatGenerator` with the provided parameters. 1166 1167 The parameters are passed to the Amazon Bedrock client. 1168 1169 Note that the AWS credentials are not required if the AWS environment is configured correctly. These are loaded 1170 automatically from the environment or the AWS configuration file and do not need to be provided explicitly via 1171 the constructor. If the AWS environment is not configured users need to provide the AWS credentials via the 1172 constructor. Aside from model, three required parameters are `aws_access_key_id`, `aws_secret_access_key`, 1173 and `aws_region_name`. 1174 1175 **Parameters:** 1176 1177 - **model** (<code>str</code>) – The model to use for text generation. The model must be available in Amazon Bedrock and must 1178 be specified in the format outlined in the [Amazon Bedrock documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids-arns.html). 1179 1180 - **aws_access_key_id** (<code>Secret | None</code>) – AWS access key ID. 1181 1182 - **aws_secret_access_key** (<code>Secret | None</code>) – AWS secret access key. 1183 1184 - **aws_session_token** (<code>Secret | None</code>) – AWS session token. 1185 1186 - **aws_region_name** (<code>Secret | None</code>) – AWS region name. Make sure the region you set supports Amazon Bedrock. 1187 1188 - **aws_profile_name** (<code>Secret | None</code>) – AWS profile name. 1189 1190 - **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Optional dictionary of generation parameters. Some common parameters are: 1191 1192 - `maxTokens`: Maximum number of tokens to generate. 1193 1194 - `stopSequences`: List of stop sequences to stop generation. 1195 1196 - `temperature`: Sampling temperature. 1197 1198 - `topP`: Nucleus sampling parameter. 1199 1200 - `response_format`: Request structured JSON output validated against a schema. Provide a dict with: 1201 1202 - `schema` (required): a JSON Schema dict describing the expected output structure. 1203 - `name` (optional): a name for the schema, defaults to `"response_schema"`. 1204 - `description` (optional): a description of the schema. 1205 1206 Example:: 1207 1208 ``` 1209 generation_kwargs={ 1210 "response_format": { 1211 "name": "person", 1212 "schema": { 1213 "type": "object", 1214 "properties": {"name": {"type": "string"}, "age": {"type": "integer"}}, 1215 "required": ["name", "age"], 1216 "additionalProperties": False, 1217 }, 1218 } 1219 } 1220 ``` 1221 1222 When set, the parsed JSON object is stored in `reply.meta["structured_output"]`. 1223 You can find the model specific arguments in the AWS Bedrock API[documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html). 1224 1225 - **streaming_callback** (<code>StreamingCallbackT | None</code>) – A callback function called when a new token is received from the stream. 1226 By default, the model is not set up for streaming. To enable streaming, set this parameter to a callback 1227 function that handles the streaming chunks. The callback function receives a 1228 [StreamingChunk](https://docs.haystack.deepset.ai/docs/data-classes#streamingchunk) object and switches 1229 the streaming mode on. 1230 1231 - **boto3_config** (<code>dict\[str, Any\] | None</code>) – Dictionary of configuration options for the underlying Boto3 client. 1232 Can be used to tune [retry behavior](https://docs.aws.amazon.com/boto3/latest/guide/retries.html) 1233 and other low-level settings like timeouts and connection management. 1234 1235 - **tools** (<code>ToolsType | None</code>) – A list of Tool and/or Toolset objects, or a single Toolset for which the model can prepare calls. 1236 Each tool should have a unique name. 1237 1238 - **guardrail_config** (<code>dict\[str, str\] | None</code>) – Optional configuration for a guardrail that has been created in Amazon Bedrock. 1239 This must be provided as a dictionary matching either 1240 [GuardrailConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_GuardrailConfiguration.html). 1241 or, in streaming mode (when `streaming_callback` is set), 1242 [GuardrailStreamConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_GuardrailStreamConfiguration.html). 1243 If `trace` is set to `enabled`, the guardrail trace will be included under the `trace` key in the `meta` 1244 attribute of the resulting `ChatMessage`. 1245 Note: Enabling guardrails in streaming mode may introduce additional latency. 1246 To manage this, you can adjust the `streamProcessingMode` parameter. 1247 See the 1248 [Guardrails Streaming documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails-streaming.html) 1249 for more information. 1250 1251 - **tools_cachepoint_config** (<code>dict\[str, str\] | None</code>) – Optional configuration to use prompt caching for tools. 1252 The dictionary must match the 1253 [CachePointBlock schema](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_CachePointBlock.html). 1254 Example: `{"type": "default", "ttl": "5m"}` 1255 1256 **Raises:** 1257 1258 - <code>ValueError</code> – If the model name is empty or None. 1259 - <code>AmazonBedrockConfigurationError</code> – If the AWS environment is not configured correctly or the model is 1260 not supported. 1261 1262 #### to_dict 1263 1264 ```python 1265 to_dict() -> dict[str, Any] 1266 ``` 1267 1268 Serializes the component to a dictionary. 1269 1270 **Returns:** 1271 1272 - <code>dict\[str, Any\]</code> – Dictionary with serialized data. 1273 1274 #### from_dict 1275 1276 ```python 1277 from_dict(data: dict[str, Any]) -> AmazonBedrockChatGenerator 1278 ``` 1279 1280 Deserializes the component from a dictionary. 1281 1282 **Parameters:** 1283 1284 - **data** (<code>dict\[str, Any\]</code>) – Dictionary with serialized data. 1285 1286 **Returns:** 1287 1288 - <code>AmazonBedrockChatGenerator</code> – Instance of `AmazonBedrockChatGenerator`. 1289 1290 #### run 1291 1292 ```python 1293 run( 1294 messages: list[ChatMessage], 1295 streaming_callback: StreamingCallbackT | None = None, 1296 generation_kwargs: dict[str, Any] | None = None, 1297 tools: ToolsType | None = None, 1298 ) -> dict[str, list[ChatMessage]] 1299 ``` 1300 1301 Executes a synchronous inference call to the Amazon Bedrock model using the Converse API. 1302 1303 Supports both standard and streaming responses depending on whether a streaming callback is provided. 1304 1305 **Parameters:** 1306 1307 - **messages** (<code>list\[ChatMessage\]</code>) – A list of `ChatMessage` objects forming the chat history. 1308 - **streaming_callback** (<code>StreamingCallbackT | None</code>) – Optional callback for handling streaming outputs. 1309 - **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Optional dictionary of generation parameters. Some common parameters are: 1310 - `maxTokens`: Maximum number of tokens to generate. 1311 - `stopSequences`: List of stop sequences to stop generation. 1312 - `temperature`: Sampling temperature. 1313 - `topP`: Nucleus sampling parameter. 1314 - `response_format`: Request structured JSON output validated against a schema. 1315 - **tools** (<code>ToolsType | None</code>) – A list of Tool and/or Toolset objects, or a single Toolset for which the model can prepare calls. 1316 Each tool should have a unique name. 1317 1318 **Returns:** 1319 1320 - <code>dict\[str, list\[ChatMessage\]\]</code> – A dictionary containing the model-generated replies under the `"replies"` key. 1321 1322 **Raises:** 1323 1324 - <code>AmazonBedrockInferenceError</code> – If the Bedrock inference API call fails. 1325 1326 #### run_async 1327 1328 ```python 1329 run_async( 1330 messages: list[ChatMessage], 1331 streaming_callback: StreamingCallbackT | None = None, 1332 generation_kwargs: dict[str, Any] | None = None, 1333 tools: ToolsType | None = None, 1334 ) -> dict[str, list[ChatMessage]] 1335 ``` 1336 1337 Executes an asynchronous inference call to the Amazon Bedrock model using the Converse API. 1338 1339 Designed for use cases where non-blocking or concurrent execution is desired. 1340 1341 **Parameters:** 1342 1343 - **messages** (<code>list\[ChatMessage\]</code>) – A list of `ChatMessage` objects forming the chat history. 1344 - **streaming_callback** (<code>StreamingCallbackT | None</code>) – Optional async-compatible callback for handling streaming outputs. 1345 - **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Optional dictionary of generation parameters. Some common parameters are: 1346 - `maxTokens`: Maximum number of tokens to generate. 1347 - `stopSequences`: List of stop sequences to stop generation. 1348 - `temperature`: Sampling temperature. 1349 - `topP`: Nucleus sampling parameter. 1350 - `response_format`: Request structured JSON output validated against a schema. 1351 - **tools** (<code>ToolsType | None</code>) – A list of Tool and/or Toolset objects, or a single Toolset for which the model can prepare calls. 1352 Each tool should have a unique name. 1353 1354 **Returns:** 1355 1356 - <code>dict\[str, list\[ChatMessage\]\]</code> – A dictionary containing the model-generated replies under the `"replies"` key. 1357 1358 **Raises:** 1359 1360 - <code>AmazonBedrockInferenceError</code> – If the Bedrock inference API call fails. 1361 1362 ## haystack_integrations.components.generators.amazon_bedrock.generator 1363 1364 ### AmazonBedrockGenerator 1365 1366 Generates text using models hosted on Amazon Bedrock. 1367 1368 For example, to use the Anthropic Claude model, pass 'anthropic.claude-v2' in the `model` parameter. 1369 Provide AWS credentials either through the local AWS profile or directly through 1370 `aws_access_key_id`, `aws_secret_access_key`, `aws_session_token`, and `aws_region_name` parameters. 1371 1372 ### Usage example 1373 1374 ```python 1375 from haystack_integrations.components.generators.amazon_bedrock import AmazonBedrockGenerator 1376 1377 generator = AmazonBedrockGenerator( 1378 model="anthropic.claude-v2", 1379 max_length=99 1380 ) 1381 1382 print(generator.run("Who is the best American actor?")) 1383 ``` 1384 1385 AmazonBedrockGenerator uses AWS for authentication. You can use the AWS CLI to authenticate through your IAM. 1386 For more information on setting up an IAM identity-based policy, see [Amazon Bedrock documentation] 1387 (https://docs.aws.amazon.com/bedrock/latest/userguide/security_iam_id-based-policy-examples.html). 1388 If the AWS environment is configured correctly, the AWS credentials are not required as they're loaded 1389 automatically from the environment or the AWS configuration file. 1390 If the AWS environment is not configured, set `aws_access_key_id`, `aws_secret_access_key`, 1391 `aws_session_token`, and `aws_region_name` as environment variables or pass them as 1392 [Secret](https://docs.haystack.deepset.ai/docs/secret-management) arguments. Make sure the region you set 1393 supports Amazon Bedrock. 1394 1395 #### __init__ 1396 1397 ```python 1398 __init__( 1399 model: str, 1400 aws_access_key_id: Secret | None = Secret.from_env_var( 1401 "AWS_ACCESS_KEY_ID", strict=False 1402 ), 1403 aws_secret_access_key: Secret | None = Secret.from_env_var( 1404 "AWS_SECRET_ACCESS_KEY", strict=False 1405 ), 1406 aws_session_token: Secret | None = Secret.from_env_var( 1407 "AWS_SESSION_TOKEN", strict=False 1408 ), 1409 aws_region_name: Secret | None = Secret.from_env_var( 1410 "AWS_DEFAULT_REGION", strict=False 1411 ), 1412 aws_profile_name: Secret | None = Secret.from_env_var( 1413 "AWS_PROFILE", strict=False 1414 ), 1415 max_length: int | None = None, 1416 truncate: bool | None = None, 1417 streaming_callback: Callable[[StreamingChunk], None] | None = None, 1418 boto3_config: dict[str, Any] | None = None, 1419 model_family: MODEL_FAMILIES | None = None, 1420 **kwargs: Any 1421 ) -> None 1422 ``` 1423 1424 Create a new `AmazonBedrockGenerator` instance. 1425 1426 **Parameters:** 1427 1428 - **model** (<code>str</code>) – The name of the model to use. 1429 - **aws_access_key_id** (<code>Secret | None</code>) – The AWS access key ID. 1430 - **aws_secret_access_key** (<code>Secret | None</code>) – The AWS secret access key. 1431 - **aws_session_token** (<code>Secret | None</code>) – The AWS session token. 1432 - **aws_region_name** (<code>Secret | None</code>) – The AWS region name. Make sure the region you set supports Amazon Bedrock. 1433 - **aws_profile_name** (<code>Secret | None</code>) – The AWS profile name. 1434 - **max_length** (<code>int | None</code>) – The maximum length of the generated text. This can also be set in the `kwargs` parameter 1435 by using the model specific parameter name. 1436 - **truncate** (<code>bool | None</code>) – Deprecated. This parameter no longer has any effect. 1437 - **streaming_callback** (<code>Callable\\[[StreamingChunk\], None\] | None</code>) – A callback function that is called when a new token is received from the stream. 1438 The callback function accepts StreamingChunk as an argument. 1439 - **boto3_config** (<code>dict\[str, Any\] | None</code>) – Dictionary of configuration options for the underlying Boto3 client. 1440 Can be used to tune [retry behavior](https://docs.aws.amazon.com/boto3/latest/guide/retries.html) 1441 and other low-level settings like timeouts and connection management. 1442 - **model_family** (<code>MODEL_FAMILIES | None</code>) – The model family to use. If not provided, the model adapter is selected based on the model 1443 name. 1444 - **kwargs** (<code>Any</code>) – Additional keyword arguments to be passed to the model. 1445 You can find the model specific arguments in AWS Bedrock's 1446 [documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html). 1447 These arguments are specific to the model. You can find them in the model's documentation. 1448 1449 **Raises:** 1450 1451 - <code>ValueError</code> – If the model name is empty or None. 1452 - <code>AmazonBedrockConfigurationError</code> – If the AWS environment is not configured correctly or the model is 1453 not supported. 1454 1455 #### run 1456 1457 ```python 1458 run( 1459 prompt: str, 1460 streaming_callback: Callable[[StreamingChunk], None] | None = None, 1461 generation_kwargs: dict[str, Any] | None = None, 1462 ) -> dict[str, list[str] | dict[str, Any]] 1463 ``` 1464 1465 Generates a list of string response to the given prompt. 1466 1467 **Parameters:** 1468 1469 - **prompt** (<code>str</code>) – The prompt to generate a response for. 1470 - **streaming_callback** (<code>Callable\\[[StreamingChunk\], None\] | None</code>) – A callback function that is called when a new token is received from the stream. 1471 - **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Additional keyword arguments passed to the generator. 1472 1473 **Returns:** 1474 1475 - <code>dict\[str, list\[str\] | dict\[str, Any\]\]</code> – A dictionary with the following keys: 1476 - `replies`: A list of generated responses. 1477 - `meta`: A dictionary containing response metadata. 1478 1479 **Raises:** 1480 1481 - <code>ValueError</code> – If the prompt is empty or None. 1482 - <code>AmazonBedrockInferenceError</code> – If the model cannot be invoked. 1483 1484 #### get_model_adapter 1485 1486 ```python 1487 get_model_adapter( 1488 model: str, model_family: str | None = None 1489 ) -> type[BedrockModelAdapter] 1490 ``` 1491 1492 Gets the model adapter for the given model. 1493 1494 If `model_family` is provided, the adapter for the model family is returned. 1495 If `model_family` is not provided, the adapter is auto-detected based on the model name. 1496 1497 **Parameters:** 1498 1499 - **model** (<code>str</code>) – The model name. 1500 - **model_family** (<code>str | None</code>) – The model family. 1501 1502 **Returns:** 1503 1504 - <code>type\[BedrockModelAdapter\]</code> – The model adapter class, or None if no adapter is found. 1505 1506 **Raises:** 1507 1508 - <code>AmazonBedrockConfigurationError</code> – If the model family is not supported or the model cannot be 1509 auto-detected. 1510 1511 #### to_dict 1512 1513 ```python 1514 to_dict() -> dict[str, Any] 1515 ``` 1516 1517 Serializes the component to a dictionary. 1518 1519 **Returns:** 1520 1521 - <code>dict\[str, Any\]</code> – Dictionary with serialized data. 1522 1523 #### from_dict 1524 1525 ```python 1526 from_dict(data: dict[str, Any]) -> AmazonBedrockGenerator 1527 ``` 1528 1529 Deserializes the component from a dictionary. 1530 1531 **Parameters:** 1532 1533 - **data** (<code>dict\[str, Any\]</code>) – Dictionary to deserialize from. 1534 1535 **Returns:** 1536 1537 - <code>AmazonBedrockGenerator</code> – Deserialized component. 1538 1539 ## haystack_integrations.components.rankers.amazon_bedrock.ranker 1540 1541 ### AmazonBedrockRanker 1542 1543 Ranks Documents based on their similarity to the query using Amazon Bedrock's Cohere Rerank model. 1544 1545 Documents are indexed from most to least semantically relevant to the query. 1546 1547 Supported Amazon Bedrock models: 1548 1549 - cohere.rerank-v3-5:0 1550 - amazon.rerank-v1:0 1551 1552 Usage example: 1553 1554 ```python 1555 from haystack import Document 1556 from haystack.utils import Secret 1557 from haystack_integrations.components.rankers.amazon_bedrock import AmazonBedrockRanker 1558 1559 ranker = AmazonBedrockRanker( 1560 model="cohere.rerank-v3-5:0", 1561 top_k=2, 1562 aws_region_name=Secret.from_token("eu-central-1") 1563 ) 1564 1565 docs = [Document(content="Paris"), Document(content="Berlin")] 1566 query = "What is the capital of germany?" 1567 output = ranker.run(query=query, documents=docs) 1568 docs = output["documents"] 1569 ``` 1570 1571 AmazonBedrockRanker uses AWS for authentication. You can use the AWS CLI to authenticate through your IAM. 1572 For more information on setting up an IAM identity-based policy, see [Amazon Bedrock documentation] 1573 (https://docs.aws.amazon.com/bedrock/latest/userguide/security_iam_id-based-policy-examples.html). 1574 1575 If the AWS environment is configured correctly, the AWS credentials are not required as they're loaded 1576 automatically from the environment or the AWS configuration file. 1577 If the AWS environment is not configured, set `aws_access_key_id`, `aws_secret_access_key`, 1578 and `aws_region_name` as environment variables or pass them as 1579 [Secret](https://docs.haystack.deepset.ai/docs/secret-management) arguments. Make sure the region you set 1580 supports Amazon Bedrock. 1581 1582 #### __init__ 1583 1584 ```python 1585 __init__( 1586 model: str = "cohere.rerank-v3-5:0", 1587 top_k: int = 10, 1588 aws_access_key_id: Secret | None = Secret.from_env_var( 1589 ["AWS_ACCESS_KEY_ID"], strict=False 1590 ), 1591 aws_secret_access_key: Secret | None = Secret.from_env_var( 1592 ["AWS_SECRET_ACCESS_KEY"], strict=False 1593 ), 1594 aws_session_token: Secret | None = Secret.from_env_var( 1595 ["AWS_SESSION_TOKEN"], strict=False 1596 ), 1597 aws_region_name: Secret | None = Secret.from_env_var( 1598 ["AWS_DEFAULT_REGION"], strict=False 1599 ), 1600 aws_profile_name: Secret | None = Secret.from_env_var( 1601 ["AWS_PROFILE"], strict=False 1602 ), 1603 max_chunks_per_doc: int | None = None, 1604 meta_fields_to_embed: list[str] | None = None, 1605 meta_data_separator: str = "\n", 1606 ) -> None 1607 ``` 1608 1609 Creates an instance of the 'AmazonBedrockRanker'. 1610 1611 **Parameters:** 1612 1613 - **model** (<code>str</code>) – Amazon Bedrock model name for Cohere Rerank. Default is "cohere.rerank-v3-5:0". 1614 - **top_k** (<code>int</code>) – The maximum number of documents to return. 1615 - **aws_access_key_id** (<code>Secret | None</code>) – AWS access key ID. 1616 - **aws_secret_access_key** (<code>Secret | None</code>) – AWS secret access key. 1617 - **aws_session_token** (<code>Secret | None</code>) – AWS session token. 1618 - **aws_region_name** (<code>Secret | None</code>) – AWS region name. 1619 - **aws_profile_name** (<code>Secret | None</code>) – AWS profile name. 1620 - **max_chunks_per_doc** (<code>int | None</code>) – If your document exceeds 512 tokens, this determines the maximum number of 1621 chunks a document can be split into. If `None`, the default of 10 is used. 1622 Note: This parameter is not currently used in the implementation but is included for future compatibility. 1623 - **meta_fields_to_embed** (<code>list\[str\] | None</code>) – List of meta fields that should be concatenated 1624 with the document content for reranking. 1625 - **meta_data_separator** (<code>str</code>) – Separator used to concatenate the meta fields 1626 to the Document content. 1627 1628 #### to_dict 1629 1630 ```python 1631 to_dict() -> dict[str, Any] 1632 ``` 1633 1634 Serializes the component to a dictionary. 1635 1636 **Returns:** 1637 1638 - <code>dict\[str, Any\]</code> – Dictionary with serialized data. 1639 1640 #### from_dict 1641 1642 ```python 1643 from_dict(data: dict[str, Any]) -> AmazonBedrockRanker 1644 ``` 1645 1646 Deserializes the component from a dictionary. 1647 1648 **Parameters:** 1649 1650 - **data** (<code>dict\[str, Any\]</code>) – The dictionary to deserialize from. 1651 1652 **Returns:** 1653 1654 - <code>AmazonBedrockRanker</code> – The deserialized component. 1655 1656 #### run 1657 1658 ```python 1659 run( 1660 query: str, documents: list[Document], top_k: int | None = None 1661 ) -> dict[str, list[Document]] 1662 ``` 1663 1664 Use the Amazon Bedrock Reranker to re-rank the list of documents based on the query. 1665 1666 **Parameters:** 1667 1668 - **query** (<code>str</code>) – Query string. 1669 - **documents** (<code>list\[Document\]</code>) – List of Documents. 1670 - **top_k** (<code>int | None</code>) – The maximum number of Documents you want the Ranker to return. 1671 1672 **Returns:** 1673 1674 - <code>dict\[str, list\[Document\]\]</code> – A dictionary with the following keys: 1675 - `documents`: List of Documents most similar to the given query in descending order of similarity. 1676 1677 **Raises:** 1678 1679 - <code>ValueError</code> – If `top_k` is not > 0.