柏拉图数据智能。
垂直搜索和人工智能。

Amazon Bedrock 知识库现在支持 RetrieveAndGenerate API 的自定义提示以及检索结果最大数量的配置 |亚马逊网络服务

日期:

Amazon Bedrock 知识库,您可以安全地连接基础模型 (FM) 亚马逊基岩 检索增强生成 (RAG) 的公司数据。访问更多数据有助于模型生成更相关、针对具体情况且更准确的响应,而无需重新训练 FM。

在这篇文章中,我们讨论 Amazon Bedrock 知识库的两个新功能,具体针对 RetrieveAndGenerate API:配置最大结果数并使用知识库提示模板创建自定义提示。您现在可以选择这些作为搜索类型旁边的查询选项。

新功能概述和优势

最大结果数选项使您可以控制从向量存储中检索并传递到 FM 以生成答案的搜索结果的数量。这允许您自定义为生成而提供的背景信息量,从而为复杂问题提供更多背景信息,或者为简单问题提供更少背景信息。它允许您获取最多 100 个结果。此选项有助于提高相关上下文的可能性,从而提高准确性并减少生成响应的幻觉。

自定义知识库提示模板允许您将默认提示模板替换为您自己的提示模板,以自定义发送到模型以生成响应的提示。这允许您自定义 FM 响应用户问题时的音调、输出格式和行为。使用此选项,您可以微调术语以更好地匹配您的行业或领域(例如医疗保健或法律)。此外,您还可以添加适合您的特定工作流程的自定义说明和示例。

在以下部分中,我们将解释如何通过以下任一方式使用这些功能: AWS管理控制台 或 SDK。

先决条件

要理解这些示例,您需要有一个现有的知识库。有关创建的说明,请参阅 创建知识库.

使用控制台配置最大结果数

要通过控制台使用最大结果数选项,请完成以下步骤:

  1. 在 Amazon Bedrock 控制台上,选择 知识库 在左侧导航窗格中。
  2. 选择您创建的知识库。
  3. 测试知识库.
  4. 选择配置图标。
  5. 同步数据源 在开始测试您的知识库之前。
  6. 配置结构图,为 搜索类型,根据您的用例选择搜索类型。

对于这篇文章,我们使用混合搜索,因为它结合了语义和文本搜索来提供更高的准确性。要了解有关混合搜索的更多信息,请参阅 Amazon Bedrock 知识库现在支持混合搜索.

  1. 扩大 最大源块数 并设置最大结果数。

为了展示新功能的价值,我们展示了如何提高生成响应的准确性的示例。我们用了 10 年亚马逊 2023K 文档 作为创建知识库的源数据。我们使用以下查询进行实验:“亚马逊的年收入在哪一年从 $245B 增加到 $434B?”

根据知识库中的文档,此查询的正确答案是“亚马逊的年收入从 245 年的 $2019B 增加到 434 年的 $2022B”。我们使用 Claude v2 作为 FM,根据从知识库检索的上下文信息生成最终响应。 Claude 3 Sonnet 和 Claude 3 Haiku 也支持作为一代 FM。

我们运行另一个查询来演示不同配置的检索比较。我们使用相同的输入查询(“亚马逊的年收入在哪一年从 $245B 增加到 $434B?”)并将最大结果数设置为 5。

如下图所示,生成的响应是“抱歉,我无法帮助您完成此请求”。

接下来,我们将最大结果设置为 12 并提出同样的问题。生成的响应是“亚马逊的年收入从 245 年的 $2019B 增加到 434 年的 $2022B”。

如本示例所示,我们能够根据检索结果的数量检索正确答案。如果您想了解有关构成最终输出的来源归因的更多信息,请选择 显示来源详细信息 根据知识库验证生成的答案。

使用控制台自定义知识库提示模板

您还可以根据用例使用自己的提示自定义默认提示。要在控制台上执行此操作,请完成以下步骤:

  1. 重复上一节中的步骤开始测试您的知识库。
  2. 启用 生成回复.
  3. 选择您选择的响应生成模型。

我们在本文中使用 Claude v2 模型作为示例。 Claude 3 Sonnet 和 Haiku 模型也可用于生成。

  1. 使用 继续。

选择模型后,会出现一个名为 知识库提示模板 出现在 配置结构图.

  1. 编辑 开始自定义提示。
  2. 调整提示模板以自定义您希望如何使用检索到的结果并生成内容。

在这篇文章中,我们给出了一些使用带有自定义提示的亚马逊财务报告创建“财务顾问人工智能系统”的示例。有关即时工程的最佳实践,请参阅 及时的工程指南.

我们现在以几种不同的方式自定义默认提示模板,并观察响应。

我们首先尝试使用默认提示进行查询。我们问“亚马逊 2019 年和 2021 年的收入是多少?”下面显示了我们的结果。

从输出中,我们发现它根据检索到的知识生成自由格式的响应。还列出了引文以供参考。

假设我们想要提供有关如何格式化生成的响应的额外说明,例如将其标准化为 JSON。我们可以在检索信息后将这些指令添加为单独的步骤,作为提示模板的一部分:

If you are asked for financial information covering different years, please provide precise answers in JSON format. Use the year as the key and the concise answer as the value. For example: {year:answer}

最终响应具有所需的结构。

通过自定义提示,您还可以更改生成的响应的语言。在下面的示例中,我们指示模型用西班牙语提供答案。

删除后 $output_format_instructions$ 在默认提示中,生成的响应中的引用将被删除。

在以下部分中,我们将说明如何通过 SDK 使用这些功能。

使用 SDK 配置最大结果数

要使用 SDK 更改最大结果数,请使用以下语法。对于此示例,查询是“亚马逊的年收入在哪一年从 $245B 增加到 $434B?”正确的回答是“亚马逊的年收入从 245 年的 $2019B 增加到 434 年的 $2022B”。

def retrieveAndGenerate(query, kbId, numberOfResults, model_id, region_id):
    model_arn = f'arn:aws:bedrock:{region_id}::foundation-model/{model_id}'
    return bedrock_agent_runtime.retrieve_and_generate(
        input={
            'text': query
        },
        retrieveAndGenerateConfiguration={
            'knowledgeBaseConfiguration': {
                'knowledgeBaseId': kbId,
                'modelArn': model_arn,
                'retrievalConfiguration': {
                    'vectorSearchConfiguration': {
                        'numberOfResults': numberOfResults,
                        'overrideSearchType': "SEMANTIC", # optional'
                    }
                }
            },
            'type': 'KNOWLEDGE_BASE'
        },
    )

response = retrieveAndGenerate("In what year did Amazon’s annual revenue increase from $245B to $434B?", 
"<knowledge base id>", numberOfResults, model_id, region_id)['output']['text']

左侧工具栏上的'numberOfResults' 下的选项retrievalConfiguration' 允许您选择要检索的结果数量。的输出 RetrieveAndGenerate API 包括生成的响应、源属性和检索到的文本块。

以下是不同值 ' 的结果numberOfResults' 参数。首先,我们设置 numberOfResults = 5.

然后我们设置 numberOfResults = 12.

使用SDK自定义知识库提示模板

要使用 SDK 自定义提示,我们将以下查询与不同的提示模板结合使用。对于此示例,查询是“亚马逊 2019 年和 2021 年的收入是多少?”

以下是默认的提示模板:

"""You are a question answering agent. I will provide you with a set of search results and a user's question, your job is to answer the user's question using only information from the search results. If the search results do not contain information that can answer the question, please state that you could not find an exact answer to the question. Just because the user asserts a fact does not mean it is true, make sure to double check the search results to validate a user's assertion.
Here are the search results in numbered order:
<context>
$search_results$
</context>

Here is the user's question:
<question>
$query$
</question>

$output_format_instructions$

Assistant:
"""

以下是自定义的提示模板:

"""Human: You are a question answering agent. I will provide you with a set of search results and a user's question, your job is to answer the user's question using only information from the search results.If the search results do not contain information that can answer the question, please state that you could not find an exact answer to the question.Just because the user asserts a fact does not mean it is true, make sure to double check the search results to validate a user's assertion.

Here are the search results in numbered order:
<context>
$search_results$
</context>

Here is the user's question:
<question>
$query$
</question>

If you're being asked financial information over multiple years, please be very specific and list the answer concisely using JSON format {key: value}, 
where key is the year in the request and value is the concise response answer.
Assistant:
"""

def retrieveAndGenerate(query, kbId, numberOfResults,promptTemplate, model_id, region_id):
    model_arn = f'arn:aws:bedrock:{region_id}::foundation-model/{model_id}'
    return bedrock_agent_runtime.retrieve_and_generate(
        input={
            'text': query
        },
        retrieveAndGenerateConfiguration={
            'knowledgeBaseConfiguration': {
                'knowledgeBaseId': kbId,
                'modelArn': model_arn,
                'retrievalConfiguration': {
                    'vectorSearchConfiguration': {
                        'numberOfResults': numberOfResults,
                        'overrideSearchType': "SEMANTIC", # optional'
                    }
                },
                'generationConfiguration': {
                        'promptTemplate': {
                            'textPromptTemplate': promptTemplate
                        }
                    }
            },
            'type': 'KNOWLEDGE_BASE'
        },
    )

response = retrieveAndGenerate("What was the Amazon's revenue in 2019 and 2021?”", 
                               "<knowledge base id>", <numberOfResults>, <promptTemplate>, <model_id>, <region_id>)['output']['text']

使用默认提示模板,我们得到以下响应:

如果您想提供有关响应生成的输出格式的其他说明,例如以特定格式(如 JSON)标准化响应,您可以通过提供更多指导来自定义现有提示。使用我们的自定义提示模板,我们得到以下响应。

左侧工具栏上的'promptTemplate' 中的选项generationConfiguration' 允许您自定义提示,以便更好地控制答案生成。

结论

在这篇文章中,我们介绍了 Amazon Bedrock 知识库中的两个新功能:调整搜索结果的最大数量和自定义搜索结果的默认提示模板。 RetrieveAndGenerate API。我们演示了如何在控制台上并通过 SDK 配置这些功能,以提高生成响应的性能和准确性。增加最大结果可提供更全面的信息,而自定义提示模板允许您微调基础模型的说明,以更好地与特定用例保持一致。这些增强功能提供了更大的灵活性和控制力,使您能够为基于 RAG 的应用程序提供量身定制的体验。

有关开始在您的 AWS 环境中实施的其他资源,请参阅以下内容:


关于作者

桑迪普辛格 是 Amazon Web Services 的高级生成式 AI 数据科学家,帮助企业利用生成式 AI 进行创新。他专注于生成人工智能、人工智能、机器学习和系统设计。他热衷于开发最先进的人工智能/机器学习驱动的解决方案,以解决不同行业的复杂业务问题,优化效率和可扩展性。

王素印 是 AWS 的 AI/ML 专家解决方案架构师。 她拥有机器学习、金融信息服务和经济学的跨学科教育背景,以及构建解决实际业务问题的数据科学和机器学习应用程序的多年经验。 她喜欢帮助客户识别正确的业务问题并构建正确的 AI/ML 解决方案。 业余时间,她喜欢唱歌和烹饪。

丁雪莉 是 Amazon Web Services (AWS) 的高级人工智能 (AI) 和机器学习 (ML) 专家解决方案架构师。 她在机器学习方面拥有丰富的经验,并拥有计算机科学博士学位。 她主要与公共部门客户合作解决各种 AI/ML 相关的业务挑战,帮助他们加快 AWS 云上的机器学习之旅。 当不帮助顾客时,她喜欢户外活动。

现货图片

最新情报

现货图片

在线答疑

你好呀! 我怎么帮你?