AmazonBedrockChatGenerator
Use chat completion models hosted on Amazon Bedrock. This component supports models from AI21 Labs, Anthropic, Cohere, Meta, Stability AI, and Amazon.
Amazon Bedrock is a fully managed service that makes state-of-the-art language models available for use through a unified API. To learn more, see Amazon Bedrock documentation.
With AmazonBedrockChatGenerator, you can use chat completion models from AI21 Labs, Anthropic, Cohere, Meta, Stability AI, and Amazon.
Key Features
- Supports chat completion models from multiple providers: AI21 Labs, Anthropic, Cohere, Meta, Stability AI, and Amazon.
- Accepts a list of
ChatMessageobjects as input, making it compatible withChatPromptBuilder. - Supports tool calling for agentic workflows.
- Supports streaming responses token by token.
- Uses AWS credentials for authentication.
Configuration
- Drag the
AmazonBedrockChatGeneratorcomponent onto the canvas from the Component Library. - Click on the component to open the configuration panel.
- On the General tab:
-
Enter the model name in the Model field (for example,
amazon.nova-pro-v1:0). -
Configure your AWS credentials. You'll need:
- AWS Access Key ID
- AWS Secret Access Key
- AWS Region Name (make sure the region supports Amazon Bedrock)
- Optionally, AWS Session Token and AWS Profile Name.
For detailed explanation, see Use Amazon Bedrock and SageMaker Models.
-
- Go to the Advanced tab to configure additional settings such as
generation_kwargs,streaming_callback,boto3_config,stop_words, andtools.
Connections
AmazonBedrockChatGenerator receives a list of ChatMessage objects from ChatPromptBuilder through its messages input. It outputs a list of ChatMessage objects through its replies output. You typically connect its replies output to an OutputAdapter that converts the replies into a format that AnswerBuilder or DeepsetAnswerBuilder can accept.
Source Code
To check this component's source code, open chat_generator.py in the Haystack Core Integrations repository.
Usage Examples
Basic Configuration
AmazonBedrockChatGenerator:
type: haystack_integrations.components.generators.amazon_bedrock.chat.chat_generator.AmazonBedrockChatGenerator
init_parameters:
model: amazon.nova-pro-v1:0
aws_access_key_id:
type: env_var
env_vars:
- AWS_ACCESS_KEY_ID
strict: false
aws_secret_access_key:
type: env_var
env_vars:
- AWS_SECRET_ACCESS_KEY
strict: false
aws_session_token:
type: env_var
env_vars:
- AWS_SESSION_TOKEN
strict: false
aws_region_name:
type: env_var
env_vars:
- AWS_DEFAULT_REGION
strict: false
aws_profile_name:
type: env_var
env_vars:
- AWS_PROFILE
strict: false
Using the Component in a Pipeline
This is an example of a RAG chat pipeline with AmazonBedrockChatGenerator. Note that it receives instructions from ChatPromptBuilder, and it needs an OutputAdapter to send the generated replies to DeepsetAnswerBuilder:
components:
bm25_retriever: # Selects the most similar documents from the document store
type: haystack_integrations.components.retrievers.opensearch.bm25_retriever.OpenSearchBM25Retriever
init_parameters:
document_store:
type: haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore
init_parameters:
hosts:
index: 'Standard-Index-English'
max_chunk_bytes: 104857600
embedding_dim: 768
return_embedding: false
method:
mappings:
settings:
create_index: true
http_auth:
use_ssl:
verify_certs:
timeout:
top_k: 20 # The number of results to return
fuzziness: 0
query_embedder:
type: deepset_cloud_custom_nodes.embedders.nvidia.text_embedder.DeepsetNvidiaTextEmbedder
init_parameters:
normalize_embeddings: true
model: intfloat/e5-base-v2
embedding_retriever: # Selects the most similar documents from the document store
type: haystack_integrations.components.retrievers.opensearch.embedding_retriever.OpenSearchEmbeddingRetriever
init_parameters:
document_store:
type: haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore
init_parameters:
hosts:
index: 'Standard-Index-English'
max_chunk_bytes: 104857600
embedding_dim: 768
return_embedding: false
method:
mappings:
settings:
create_index: true
http_auth:
use_ssl:
verify_certs:
timeout:
top_k: 20 # The number of results to return
document_joiner:
type: haystack.components.joiners.document_joiner.DocumentJoiner
init_parameters:
join_mode: concatenate
ranker:
type: deepset_cloud_custom_nodes.rankers.nvidia.ranker.DeepsetNvidiaRanker
init_parameters:
model: intfloat/simlm-msmarco-reranker
top_k: 8
meta_field_grouping_ranker:
type: haystack.components.rankers.meta_field_grouping_ranker.MetaFieldGroupingRanker
init_parameters:
group_by: file_id
subgroup_by:
sort_docs_by: split_id
answer_builder:
type: deepset_cloud_custom_nodes.augmenters.deepset_answer_builder.DeepsetAnswerBuilder
init_parameters:
reference_pattern: acm
AmazonBedrockChatGenerator:
type: haystack_integrations.components.generators.amazon_bedrock.chat.chat_generator.AmazonBedrockChatGenerator
init_parameters:
model: amazon.nova-pro-v1:0
aws_access_key_id:
type: env_var
env_vars:
- AWS_ACCESS_KEY_ID
strict: false
aws_secret_access_key:
type: env_var
env_vars:
- AWS_SECRET_ACCESS_KEY
strict: false
aws_session_token:
type: env_var
env_vars:
- AWS_SESSION_TOKEN
strict: false
aws_region_name:
type: env_var
env_vars:
- AWS_DEFAULT_REGION
strict: false
aws_profile_name:
type: env_var
env_vars:
- AWS_PROFILE
strict: false
generation_kwargs:
stop_words:
streaming_callback:
boto3_config:
tools:
ChatPromptBuilder:
type: haystack.components.builders.chat_prompt_builder.ChatPromptBuilder
init_parameters:
template:
- _content:
- text: "You are a helpful assistant answering the user's questions based on the provided documents.\nIf the answer is not in the documents, rely on the web_search tool to find information.\nDo not use your own knowledge.\n"
_role: system
- _content:
- text: "Provided documents:\n{% for document in documents %}\nDocument [{{ loop.index }}] :\n{{ document.content }}\n{% endfor %}\n\nQuestion: {{ query }}\n"
_role: user
required_variables:
variables:
OutputAdapter:
type: haystack.components.converters.output_adapter.OutputAdapter
init_parameters:
template: '{{ replies[0] }}'
output_type: List[str]
custom_filters:
unsafe: false
connections: # Defines how the components are connected
- sender: bm25_retriever.documents
receiver: document_joiner.documents
- sender: query_embedder.embedding
receiver: embedding_retriever.query_embedding
- sender: embedding_retriever.documents
receiver: document_joiner.documents
- sender: document_joiner.documents
receiver: ranker.documents
- sender: ranker.documents
receiver: meta_field_grouping_ranker.documents
- sender: meta_field_grouping_ranker.documents
receiver: answer_builder.documents
- sender: meta_field_grouping_ranker.documents
receiver: ChatPromptBuilder.documents
- sender: ChatPromptBuilder.prompt
receiver: AmazonBedrockChatGenerator.messages
- sender: AmazonBedrockChatGenerator.replies
receiver: OutputAdapter.replies
- sender: OutputAdapter.output
receiver: answer_builder.replies
inputs: # Define the inputs for your pipeline
query: # These components will receive the query as input
- "bm25_retriever.query"
- "query_embedder.text"
- "ranker.query"
- "answer_builder.query"
- "ChatPromptBuilder.query"
filters: # These components will receive a potential query filter as input
- "bm25_retriever.filters"
- "embedding_retriever.filters"
outputs: # Defines the output of your pipeline
documents: "meta_field_grouping_ranker.documents" # The output of the pipeline is the retrieved documents
answers: "answer_builder.answers" # The output of the pipeline is the generated answers
max_runs_per_component: 100
metadata: {}
Parameters
Inputs
| Parameter | Type | Description |
|---|---|---|
messages | List[ChatMessage] | A list of ChatMessage objects that form the chat history. |
streaming_callback | Optional[Callable[[StreamingChunk], None]] | A callback function to invoke when the model starts streaming responses. |
generation_kwargs | Optional[Dict[str, Any]] | Additional keyword arguments passed to the model. |
tools | Optional[Union[List[Tool], Toolset]] | A list of tools for the model to call. |
Outputs
| Parameter | Type | Description |
|---|---|---|
replies | List[ChatMessage] | Responses generated by the model. |
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
model | str | The name of the model to use. | |
aws_access_key_id | Optional[Secret] | Secret.from_env_var('AWS_ACCESS_KEY_ID', strict=False) | The AWS access key ID. |
aws_secret_access_key | Optional[Secret] | Secret.from_env_var('AWS_SECRET_ACCESS_KEY', strict=False) | The AWS secret access key. |
aws_session_token | Optional[Secret] | Secret.from_env_var('AWS_SESSION_TOKEN', strict=False) | The AWS session token. |
aws_region_name | Optional[Secret] | Secret.from_env_var('AWS_DEFAULT_REGION', strict=False) | The AWS region name. Make sure the region you set supports Amazon Bedrock. |
aws_profile_name | Optional[Secret] | Secret.from_env_var('AWS_PROFILE', strict=False) | The AWS profile name. |
max_length | Optional[int] | None | The maximum length of the generated text. This can also be set in the kwargs parameter by using the model specific parameter name. |
truncate | Optional[bool] | None | Deprecated. This parameter no longer has any effect. |
streaming_callback | Optional[Callable[[StreamingChunk], None]] | None | A callback function that is called when a new token is received from the stream. The callback function accepts StreamingChunk as an argument. |
boto3_config | Optional[Dict[str, Any]] | None | The configuration for the boto3 client. |
model_family | Optional[MODEL_FAMILIES] | None | The model family to use. If not provided, the model adapter is selected based on the model name. |
kwargs | Any | Additional keyword arguments to be passed to the model. You can find the model specific arguments in AWS Bedrock's documentation. |
Run Method Parameters
These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.
| Parameter | Type | Default | Description |
|---|---|---|---|
prompt | str | The prompt to generate a response for. | |
streaming_callback | Optional[Callable[[StreamingChunk], None]] | None | A callback function that is called when a new token is received from the stream. |
generation_kwargs | Optional[Dict[str, Any]] | None | Additional keyword arguments passed to the generator. |
Related Information
Was this page helpful?