文档API 参考📓 教程🧑‍🍳 食谱🤝 集成💜 Discord🎨 Studio
文档

AmazonBedrockGenerator

此组件允许使用 Amazon Bedrock 服务通过模型生成文本。

pipeline 中的最常见位置PromptBuilder 之后
必需的初始化变量"model": 要使用的模型

"aws_access_key_id": AWS 访问密钥 ID。可以通过以下方式设置:AWS_ACCESS_KEY_ID 环境变量。

"aws_secret_access_key": AWS 密钥访问密钥。可以通过以下方式设置:AWS_SECRET_ACCESS_KEY 环境变量。

"aws_region_name": AWS 区域名称。可以通过以下方式设置:AWS_DEFAULT_REGION 环境变量。
强制运行变量“prompt”: Generator 的指令
输出变量“replies”: 包含模型生成的所有回复的字符串列表
API 参考Amazon Bedrock
GitHub 链接https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/amazon_bedrock

Amazon Bedrock 是一项全托管服务,通过统一的 API 提供领先的 AI 初创公司和 Amazon 的高性能基础模型。您可以选择各种基础模型,找到最适合您用例的模型。

AmazonBedrockGenerator 可使用单个组件通过 AI21 Labs、Anthropic、Cohere、Meta、Stability AI 和 Amazon 的模型进行文本生成。

我们目前支持的模型包括 Anthropic 的 Claude、AI21 Labs 的 Jurassic-2、Stability AI 的 Stable Diffusion、Cohere 的 Command 和 Embed、Meta 的 Llama 2,以及 Amazon Titan 的语言和嵌入模型。

概述

此组件使用 AWS 进行身份验证。您可以使用 AWS CLI 通过您的 IAM 进行身份验证。有关设置 IAM 基于身份的策略的更多信息,请参阅官方文档

📘

使用 AWS CLI

考虑使用 AWS CLI 作为更简单的工具来管理您的 AWS 服务。借助 AWS CLI,您可以快速配置您的boto3 凭证。这样,您在初始化 Haystack 中的 Amazon Bedrock Generator 时就不需要提供详细的身份验证参数。

要使用此组件进行文本生成,请使用模型名称初始化 AmazonBedrockGenerator,AWS 凭证(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_DEFAULT_REGION)应设置为环境变量,或如上所述进行配置,或作为Secret参数传递。请注意,确保您设置的区域支持 Amazon Bedrock。

要开始使用 Haystack 中的 Amazon Bedrock,请安装amazon-bedrock-haystack

pip install amazon-bedrock-haystack

流式传输

此 Generator 支持将 LLM 的 token直接流式传输到输出中。要做到这一点,请将一个函数传递给streaming_callback 初始化参数。

用法

单独使用

基本用法

from haystack_integrations.components.generators.amazon_bedrock import AmazonBedrockGenerator

aws_access_key_id="..."
aws_secret_access_key="..."
aws_region_name="eu-central-1"

generator = AmazonBedrockGenerator(model="anthropic.claude-v2")
result = generator.run("Who is the best American actor?")
for reply in result["replies"]:
    print(reply)

# >>> 'There is no definitive "best" American actor, as acting skill and talent a# re subjective. However, some of the most acclaimed and influential American act# ors include Tom Hanks, Daniel Day-Lewis, Denzel Washington, Meryl Streep, Rober# t De Niro, Al Pacino, Marlon Brando, Jack Nicholson, Leonardo DiCaprio and John# ny Depp. Choosing a single "best" actor comes down to personal preference.'

在 pipeline 中

在 RAG 管道中

from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
from haystack.components.builders import PromptBuilder
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack import Pipeline

from haystack_integrations.components.generators.amazon_bedrock import AmazonBedrockGenerator

template = """
Given the following information, answer the question.

Context: 
{% for document in documents %}
    {{ document.content }}
{% endfor %}

Question: What's the official language of {{ country }}?
"""

aws_access_key_id="..."
aws_secret_access_key="..."
aws_region_name="eu-central-1"
generator = AmazonBedrockGenerator(model="anthropic.claude-v2")
docstore = InMemoryDocumentStore()

pipe = Pipeline()
pipe.add_component("retriever", InMemoryBM25Retriever(document_store=docstore))
pipe.add_component("prompt_builder", PromptBuilder(template=template))
pipe.add_component("generator", generator)
pipe.connect("retriever", "prompt_builder.documents")
pipe.connect("prompt_builder", "generator")

pipe.run({
    "retriever":{ "query": "France"},
    "prompt_builder": {
        "country": "France"
    }
})

# {'generator': {'replies': ['Based on the context provided, the official language of France is French.']}}

其他参考资料

🧑‍🍳 食谱:使用 Amazon Bedrock 和 Haystack 进行基于 PDF 的问答