Bedrock Knowledge Bases のパース戦略で Amazon Bedrock Data Automation を試す

記事タイトルとURLをコピーする

はじめに

Amazon Bedrock Knowledge base を利用することで、特定の情報を会話のコンテキストに含めて精度を向上させたチャット AI を効率的に作成できます。この手法は RAG と呼ばれ、その基本と作成方法について以下のブログで紹介していますので、基本から学びたい・振り返りたいという方はぜひご参照ください。

blog.serverworks.co.jp

blog.serverworks.co.jp

前回はプレーンテキスト中心でしたが、本記事では画像を含む PDF のような資料でも RAG を適用できるように Amazon Bedrock Data Automation (BDA) をパーサーとして設定して、実際に回答をテストするところまでを紹介します。BDA については以下のブログで紹介していますので、よければご参照ください。

[]

Knowledge Base の作成

データソース用と BDA のファイル出力用 S3 バケットの作成

以下の2つの S3 バケットを作成します。

Bucket 名 用途
kb-bda-aws-arichitectures-ds-{ACCOUNT_NUMBER} RAG に利用する元ソースを格納する
kb-bda-aws-arichitectures-output-{ACCOUNT_NUMBER} BDA による解析過程で出力されるファイルを格納する

Upload data source

ここでは以下の2つのアーキテクチャの PDF を kb-bda-aws-arichitectures-ds-{ACCOUNT_NUMBER} にアップロードしました。

aws.amazon.com

aws.amazon.com

上記の構成図の PDF にはテキストデータと画像データが含まれます。今回はこの中の画像データに関して回答ができるかどうかが重要な観点になります。

Knowledge base の設定

Bedrock のコンソール画面を開き「Knowledge Bases」>「Create」>「Knowledge Base with vector store」を押下し、フォームに以下の情報を設定します。記載しない項目はデフォルトの値として省略します。

項目
Knowledge Base name knowledge-base-aws-architectures
Data source name knowledge-base-aws-architectures-data-source
S3 URI kb-bda-aws-arichitectures-ds-{ACCOUNT_NUMBER}
Parsing strategy Amazon Bedrock Data Automation as parser
Embedding model Titan Text Embeddings V2
Multimodal storage destination kb-bda-aws-arichitectures-output-{ACCOUNT_NUMBER}

「Create Knowledge Base」を押下します。

作成が完了したら「Knowledge Bases」>「knowledge-base-aws-architectures」>「knowledge-base-aws-architectures-data-source」>「Sync」を押下し、ベクターストアを更新(データソースを反映)します。

私は Knowledge base を作成したら OpenSearch Serverless の Collection を確認しています。Bedrock の Knowledge base の機能のブラックボックス性が強いので、ちゃんとデータベースにデータが存在しているのか確認したくなってしまうのです..

「OpenSearch Service」>「Serverless: Collections」から対象の情報を確認できます。

OpenSearch Serverless Collections for Knowledge Base

正常にベクターストア(インデックス)が作成されていることが確認できました。

テスト

RAG ベースの回答を実施する(Retrieve and Generate)

Knowledge base test

Bedrock のコンソールに戻り「Knowledge Bases」>「knowledge-base-aws-architectures」>「Test」>「Select model」>「Anthropic」>「Claude 3.5 Sonnet」>「Apply」を押下します。

テキストエリアに以下の内容を入力し「Run」を押下します。

I want to create an application that uses generative AI. I want to deploy the functionality via a serverless API, but what configuration should I use?

AI からの回答は以下です。

For a serverless generative AI application, you can implement the following core configuration: 1. Use Amazon CloudFront and S3 for hosting your frontend assets and configuration files 2. Implement user authentication through Amazon Cognito 3. Set up Amazon API Gateway to handle user requests, which will route to AWS Lambda functions 4. Use Agents for Amazon Bedrock to handle the AI functionality, which provides automated prompt engineering and LLM access 5. Implement Knowledge Bases for Amazon Bedrock using Amazon OpenSearch Service and S3 for data retrieval and augmented generation (RAG workflow)[1] For cost management and monitoring of your serverless AI application: 1. Use CloudWatch to monitor runtime metrics and set up alarms 2. Implement CloudTrail for tracking successful invocations and usage information 3. Set up DynamoDB if you need to store streaming responses 4. Configure EventBridge rules to track daily usage and costs This architecture allows you to build a scalable, serverless generative AI application while maintaining control over costs and usage.[2]

引用された2つのチャンクにはそれぞれ次のダイアグラムが含まれていました。

Chunk 1-1

Chunk 1-2

これだけだと画像の内容を読み取って回答しているかが分かりにくいので、以下のプロンプトを渡してみました。

How are the AWS icons arranged? We would like to know how each icon is positioned relative to the other.

すると、以下のように返答されました。

In the architecture diagrams, the AWS icons are arranged in a left-to-right flow that represents the service interactions: Starting from the left side: - Users/Tenants connect to front-end assets served by Amazon CloudFront and S3 - Authentication is handled by Amazon Cognito - Requests flow through Amazon API Gateway to AWS Lambda - The core processing happens in the center with Amazon Bedrock and its associated services - On the right side are the internal systems and databases like DynamoDB The icons are organized in logical groupings, often shown with dotted line boxes to indicate related services or functional areas. The flow lines and numbered steps between icons show the sequence of operations and how the services interact with each other.

Chunk 2-1

対応するチャンクは上図です。説明からもわかるように、およそアイコンの配置について正しく説明しており、画像右側の点線でグルーピングされたリソースについても言及されています。

チャンクの詳細

チャンクには x-amz-bedrock-kb-description というメタデータが付されており、Chunk 2-1 には以下の内容が記載されていました。

※ 視認性のために改行を追加しています。

## Guidance for Building Custom Chatbots for Order Recommendations Using Agents for Amazon Bedrock This architecture diagram shows how to build a serverless, scalable generative AI chatbot using both Agents for Amazon Bedrock and Knowledge Bases for Amazon Bedrock. The chatbot can integrate with internal systems to provide personalized recommendations, order placement, and order status. 

<figure> <figure_type>DIAGRAM</figure_type> Title: AWS Serverless Generative AI Chatbot Architecture with Amazon Bedrock This architecture diagram illustrates a serverless chatbot solution using AWS services, centered around Amazon Bedrock. The workflow begins with users accessing front-end assets through Amazon CloudFront and S3. User authentication is handled by Amazon Cognito, with requests flowing through API Gateway to AWS Lambda. The core functionality leverages Agents for Amazon Bedrock, which interfaces with Knowledge Bases for Amazon Bedrock, incorporating OpenSearch Service and S3 for data retrieval. The system integrates with customer's internal systems including product catalog, location services, order services, and user profile services through Lambda functions and DynamoDB. The architecture demonstrates a complete flow from user interaction to data retrieval and response generation using LLM capabilities, implementing a Retrieval Augmented Generation (RAG) workflow for enhanced AI responses. </figure>

- Users access the webpage, which is served **1** by **Amazon CloudFront** and backed by **Amazon Simple Storage Service** (Amazon S3) for website and configuration file storage. Customers can request recommendations or place orders directly from the webpage. - **2** The user authenticates with **Amazon Cognito.** - 3 **AWS Lambda** uses **Amazon API Gateway** to handle user requests for recommendations and order placement. - **Lambda** uses the InvokeAgent API to initiate 4 calls to Agents for **Amazon Bedrock.** Agents automate prompt engineering, invoke large language models (LLMs), and orchestrate the user-requested task by dynamically invoking APIs. **Amazon Bedrock** offers access to foundation models to build generative AI applications. - Agents for **Amazon Bedrock** query the 5 Knowledge Bases for **Amazon Bedrock** Retrieve APIs to retrieve relevant text, such as the product catalog from **Amazon OpenSearch Service,** and augment prompts during inference. The Knowledge Bases for **Amazon Bedrock** are set up with **Amazon S3** and **OpenSearch Service** to automate the end-to-end Retrieval Augmented Generation (RAG) workflow. - 6 Agents for **Amazon Bedrock** invoke the **Lambda** function from their action group, along with function detail parameters or Open API schema configuration, to execute actions such as placing orders, searching for nearest locations, and retrieving customer order history. - **Lambda** integrates with the user's internal **7** systems and **Amazon DynamoDB** to provide the Agents with the appropriate data.

<figure> <figure_type>LOGO</figure_type> Title: AWS Logo The image shows the logo for Amazon Web Services (AWS), a cloud computing platform and service provided by Amazon. </figure>

*Reviewed for technical accuracy August 16, 2024* © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. **AWS Reference Architecture**

上に示したように <figure> 及び <figure_type> というタグが使用され、対象の画像の生成された説明が含まれています。ただしこれだけでは often shown with dotted line boxes to indicate related services or functional areas のような説明には至ることができないため、該当のチャンクに紐づく画像データを Claude 3.5 Sonnet のマルチモーダル機能(Vision)を使用して説明可能にしていると考えられます。

まとめ

本記事ではパース戦略として BDA を指定した Knowledge base の設定と、その挙動の確認結果について説明しました。BDA を指定することで追加の機能を用意することなく、簡潔に画像情報をもとにした回答を得ることができました。

BDA では動画の解析もできるため、大量にまたは長時間で作成される動画などの扱いも容易になりそうです。re:Invent の動画でユーザーが気になるコンテンツについて話されている再生時間なども、セマンティックにクエリできそうです。

2021年4月新卒入社。目に見えて、動かせるものが好き。