📘 **TELUS Agriculture & Consumer Goods** 如何通过 **Haystack Agents** 转变促销交易
由 deepset 维护

集成:Amazon Bedrock

通过 Amazon Bedrock 的统一 API,使用 AI21 Labs、Anthropic、Cohere、Meta 和 Amazon 的模型

作者
deepset

目录

概述

Amazon Bedrock 是一项全托管服务,可通过统一的 API 为您提供来自领先 AI 初创公司和亚马逊的高性能基础模型。您可以从各种基础模型中进行选择,找到最适合您用例的模型。您可以在 Amazon Bedrock 文档页面 上找到更多信息。

安装

安装 Amazon Bedrock 集成

pip install amazon-bedrock-haystack

使用

安装后,您将可以访问 AmazonBedrockChatGeneratorAmazonBedrockGenerator 组件,它们支持 Amazon Bedrock 上的生成语言模型。您还可以使用 AmazonBedrockTextEmbedderAmazonBedrockDocumentEmbedder 来计算嵌入。

要使用此集成,请将 AWS 凭证 (AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_DEFAULT_REGION) 设置为环境变量,或作为 Secret 参数传递。

注意:确保您设置的区域支持 Amazon Bedrock。

AmazonBedrockChatGenerator

要为此集成用于聊天模型,请使用模型名称初始化 AmazonBedrockChatGenerator

一些支持的模型是

  • AI21 Labs 的 Jamba 1.5 Large、Jamba 1.5 Mini
  • Amazon 的 Nova Canvas、Nova Lite、Nova Pro
  • Amazon 的 Titan Text G1 - Express、Titan Text G1 - Lite
  • Anthropic 的 Claude 聊天模型
  • Cohere 的 Command、Command Light 和其他 Command 模型
  • DeepSeek 的 DeepSeek-R1
  • Meta 的 Llama 模型
  • Mistral AI 的

Amazon Bedrock 支持的基础模型 中获取完整模型列表

from haystack_integrations.components.generators.amazon_bedrock import AmazonBedrockChatGenerator
from haystack.dataclasses import ChatMessage
    
generator = AmazonBedrockChatGenerator(model="amazon.nova-pro-v1:0")
messages = [ChatMessage.from_system("You are a helpful assistant that answers question in Spanish only"), 
            ChatMessage.from_user("What's Natural Language Processing? Be brief.")]
    
response = generator.run(messages)
print(response)

输出

{'replies': [ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>, _content=[TextContent(text='El procesamiento del lenguaje natural (PLN) es una rama de la inteligencia artificial que permite a las computadoras comprender, interpretar y generar lenguaje humano.')], _name=None, _meta={'model': 'amazon.nova-pro-v1:0', 'index': 0, 'finish_reason': 'end_turn', 'usage': {'prompt_tokens': 21, 'completion_tokens': 31, 'total_tokens': 52}})]}

AmazonBedrockGenerator

大多数 支持的模型 都可以与 AmazonBedrockGenerator 一起使用,但我们强烈建议改用 AmazonBedrockChatGenerator

from haystack_integrations.components.generators.amazon_bedrock import AmazonBedrockGenerator

generator = AmazonBedrockGenerator(model="mistral.mixtral-8x7b-instruct-v0:1")
result = generator.run("Who is the best American actor?")
print(result)

输出

{'replies': ['It\'s subjective to determine the "best" American actor as it depends on personal preferences, critical acclaim, and the impact of their work. However, some of the most renowned and influential American actors include:\n\n1. Daniel Day-Lewis - Known for his versatility and commitment to his roles, Day-Lewis is a three-time Academy Award winner for Best Actor.\n2. Meryl Streep - With a record 21 Academy Award nominations and three wins, Streep is widely regarded as one of the greatest actresses in American film history.\n3. Jack Nicholson - A three-time Academy Award winner and 12-time nominee, Nicholson is known for his iconic roles in films like "One Flew Over the Cuckoo\'s Nest," "Terms of Endearment," and "As Good as It Gets."\n4. Robert De Niro - A two-time Academy Award winner, De Niro is known for his collaborations with Martin Scorsese and his memorable roles in films like "Taxi Driver," "Raging Bull," and "The Godfather: Part II."\n5. Leonardo DiCaprio - A four-time Academy Award nominee and one-time winner, DiCaprio has had a successful career in both blockbusters and independent films.\n\nThese are just a few examples of highly acclaimed American actors, and there are many other talented actors who could be considered for this title.'], 'meta': {'RequestId': 'ed9c8566-0b13-4c08-ba72-c88be1aecd02', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Mon, 28 Apr 2025 11:00:15 GMT', 'content-type': 'application/json', 'content-length': '1322', 'connection': 'keep-alive', 'x-amzn-requestid': 'ed9c8566-0b13-4c08-ba72-c88be1aecd02', 'x-amzn-bedrock-invocation-latency': '7065', 'x-amzn-bedrock-output-token-count': '323', 'x-amzn-bedrock-input-token-count': '16'}, 'RetryAttempts': 0}}

嵌入器

有三个组件可用于使用 Amazon Bedrock 的嵌入模型:AmazonBedrockTextEmbedderAmazonBedrockDocumentEmbedderAmazonBedrockDocumentImageEmbedder

支持的模型有“amazon.titan-embed-text-v1”、“amazon.titan-embed-text-v2:0”、“cohere.embed-english-v3”和“cohere.embed-multilingual-v3”。

要为文本文档创建嵌入,请在索引管道中使用 AmazonBedrockDocumentEmbedder。要为基于图像的文档创建嵌入,请使用 AmazonBedrockDocumentImageEmbedder。要为查询生成嵌入,请使用 AmazonBedrockTextEmbedder

使用 AmazonBedrockDocumentEmbedderAmazonBedrockTextEmbedder 的示例

from haystack import Pipeline
from haystack.dataclasses import Document
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack_integrations.components.embedders.amazon_bedrock import (
    AmazonBedrockDocumentEmbedder,
    AmazonBedrockTextEmbedder,
)
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever

document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")

documents = [Document(content="My name is Wolfgang and I live in Berlin"),
             Document(content="I saw a black horse running"),
             Document(content="Germany has many big cities")]

indexing_pipeline = Pipeline()
indexing_pipeline.add_component("embedder", AmazonBedrockDocumentEmbedder(model="cohere.embed-english-v3"))
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
indexing_pipeline.connect("embedder", "writer")

indexing_pipeline.run({"embedder": {"documents": documents}})


query_pipeline = Pipeline()
query_pipeline.add_component("text_embedder", AmazonBedrockTextEmbedder(model="cohere.embed-english-v3"))
query_pipeline.add_component("retriever", InMemoryEmbeddingRetriever(document_store=document_store))
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")

query = "Who lives in Berlin?"

result = query_pipeline.run({"text_embedder":{"text": query}})

print(result['retriever']['documents'][0])

# Document(id=..., content: 'My name is Wolfgang and I live in Berlin')

使用 AmazonBedrockDocumentImageEmbedderAmazonBedrockTextEmbedder 的示例

from haystack import Document, Pipeline
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.writers import DocumentWriter
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
from haystack_integrations.components.embedders.amazon_bedrock import (
    AmazonBedrockDocumentImageEmbedder,
    AmazonBedrockTextEmbedder,
)

document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")


documents = [
    Document(content="A hyena", meta={"file_path": "hyena.png"}),
    Document(content="A dog", meta={"file_path": "dog.jpg"}),
]

indexing = Pipeline()
indexing.add_component("image_embedder", AmazonBedrockDocumentImageEmbedder(model="cohere.embed-english-v3"))
indexing.add_component("writer", DocumentWriter(document_store=document_store))
indexing.connect("image_embedder", "writer")
indexing.run({"image_embedder": {"documents": documents}})


query = Pipeline()
query.add_component("text_embedder", AmazonBedrockTextEmbedder(model="cohere.embed-english-v3"))
query.add_component("retriever", InMemoryEmbeddingRetriever(document_store=document_store))
query.connect("text_embedder.embedding", "retriever.query_embedding")

res = query.run({"text_embedder": {"text": "man's best friend"}})