Welcome to building document assistant using Amazon Bedrock Agents.
- Summarize: given a document, return its summary
- Retrieve info: question-answer based on a document
- Analyze data: simple quantitative data analysis
- Generate plot: based on the document data
- Make changes: adjust content formats
- AWS services: integrate with various services, ex. Textract, Translate, etc.
- Complex pipelines: accomplish multi-step changes and tasks
- Knowledge bases: RAG-based search across documents
Agents for Amazon Bedrock helps you accelerate generative artificial intelligence (AI) application development by orchestrating multistep tasks. They can make different API calls. Agents extend FMs to understand user requests, break down complex tasks into multiple steps, carry on a conversation to collect additional information, and take actions to fulfill the request.
Prerequisites: we will use Amazon Nova Lite v1
as model, make sure that you have access to it in your account.
- Go to Bedrock console, select
Agents
in the left navigation panel, then click on theCreate Agent
button - Provide
agent-for-document-processing
as Agent name, provide a description (optional). Click on theCreate
button.
- You can now open the
Agent builder
, the place where you can access and edit the overall configuration of an agent. We will selectAmazon Nova Lite v1
as model (Pricing) [Note: If you face any issues with the model, try change it to Claude family models];
Paste the following text as Instructions for the Agent:
You are a document processing agent skilled at extracting key information from documents, translating content, summarizing text, and manipulating data formats.
Your tasks include finding key points in documents, locating documents in Amazon S3 and querying them, altering date formats in Excel files, summarizing long documents,
parsing PDFs with Amazon Textract and saving results to Amazon DynamoDB, and translating documents to required languages.
Use your capabilities to assist users with efficiently processing and analyzing document data.
- In Additional settings, select
Enabled
for Code Interpreter
Leave all the rest as default. Then, choose Save and Exit
to update the configuration of the agent.
In the test chat, click Prepare
to update the agent.
π Append sample-company-report.docx
(can be found inside example-documents
) and ask:
what are the next crucial action items?
π Append sales_data.xlsx
(can be found inside example-documents
) TO THE CODE EDITOR and ask:
alter sales dates to american format: instead of using YYYY-MM-DD, use YYYY-DD-MM, output updated file
- Go to S3 console and click on the
Create bucket
button - Give your bucket a unique name, for example
bucket-for-documents-NUMBER
. ClickCreate bucket
. - Select your created bucket, click on the
Upload
button. - You can drag and drop
invoice.pdf
andcontrato-servicios.pdf
files (can be found insideexample-documents
) and then click on theUpload
button
- Navigate to the DynamoDB console and click on
Create table
. - Enter
invoices-parsed
asTable name
anddoc-name
asPartition key
. Click on theCreate table
button. - Once the table is created, we don't need to add values. Agent will add values there.
Let's create one more table:
- Go to
Tables
and click onCreate table
. - Enter
foreign-docs
asTable name
anddoc-name
asPartition key
. Click on theCreate table
button. - Click on the created table, then click on the
Actions
button and selectCreate item
from the drop-down list. - Paste
contrato-servicios
asdoc-name
- Click on the
Add new attribute
button and selectString
. Pastedocument_path
as Attribute name ands3://bucket-for-documents-NUMBER/contrato-servicios.pdf
as value. - Click on the
Add new attribute
button and selectString
. Pastelanguage
as Attribute name andes
as value. - Finally, click on the
Create item
button.
Lambda Function will manage the logic required for complex actions. Code contains set of APIs that Bedrock agent will call. The function will then format the response and send it back to the agent.
- Navigate to the Lambda Console and click on
Create function
button. - Paste
document-agent-action-group-lambda
as a function name and choosePython 3.11
as a runtime - Click on
Create function
button in the bottom of the page
Update permissions:
- Once the function is created, click on the Configuration Tab in the same page and Choose
Permissions
from the left side panel - Click on
Add permissions
button in Resource-based policy statement section to provide the permission to invoke lambda functions from Bedrock - Select
AWS service
,Other
as Service, provide any valid Statement Id. Providebedrock.amazonaws.com
as Principal, your agent ARN as Source ARN, and Action aslambda:InvokeFunction
. ClickSave
.
- Under the Execution role, click on the role link. Under Permission policies, click on
Add permissions
button, selectAttach policies
. - Search for
TranslateReadOnly
, then click onAdd permissions
. Now Lambda can call Translate. - Under Permission policies, click on
Add permissions
button, selectCreate inline policy
. - Click on
JSON
, then paste JSON from thelambda-policy/DocumentLambdaServicePolicy.json
. - Click
Next
, name the policyDocumentLambdaServicePolicy
and click onCreate policy
. Now your Lambda has required access to Textract, S3, DynamoDB.
Adjust the timeout:
- Choose
General configuration
from the left side panel - Click on the
Edit
button - Modify
Timeout
by increasing it to 3 minutes 30 seconds to make sure your Lambda won't fail while waiting for document parsing. - Click on the
Save
button
Add code:
- Now you can go back to the Lambda function, click on the
Code
tab in the same page - Copy the code from
lambda_function.py
and replace the code in code editor. - Click on the
Deploy
button.
Test your Lambda:
Since our Lambda contains a set of APIs, you may want to create several test events to test each API.
- Click on the
Test
tab near the top of the page. - Fill in
Event name
:extract-text
- Paste the code from
lambda-payloads/extract-text.json
inEvent JSON
window. DON'T FORGET TO CHANGE S3 BUCKET NAME! This will be a test event for theextract_text_from_pdf
API that matches how the Agent will send a request. - Click on
Save
and thenTest
to execute the Lambda function. You should see the results of the successful function invocation.
- Click on
Create new event
button and repeat steps 2-4 to add one more test event (you can find JSON payload in thelambda-payloads/translate-test.json
)
An action group is a toolbox that defines actions the agent can help the user perform.
One agent can have up to 20 action groups - see Bedrock quotas.
To create Action group: in the Agent builder
choose Add
in the Action groups section.
Use extract-and-translate-action-group
as Action group name.
Use the following description:
The action group contains tools that parse PDFs with Amazon Textract and save results to Amazon DynamoDB, and translate documents to required languages.
In the Action group type section, select Define with function details
.
In the Action group invocation section, select Select an existing Lambda function
and select document-agent-action-group-lambda
as Lambda function.
We will add 2 action group functions.
Click on the Add action group function
, then select JSON Editor
and paste the following:
{
"name": "extract_text_from_pdf",
"description": "Parse a PDF with Amazon Textract and save results to DynamoDB",
"parameters": {
"s3_path": {
"description": "The path to the PDF file",
"required": "True",
"type": "String"
},
"table_name": {
"description": "The DynamoDB table name to save results to",
"required": "True",
"type": "String"
}
},
"requireConfirmation": "DISABLED"
}
Next, click on the Add action group function
, then select JSON Editor
and paste the following:
{
"name": "translate_document",
"description": "Retrieve a PDF document by its name, check its language, translate if needed, save and update DynamoDB.",
"parameters": {
"document_name": {
"description": "PDF document name",
"required": "True",
"type": "String"
},
"table_name": {
"description": "The DynamoDB table name to save results to",
"required": "True",
"type": "String"
}
},
"requireConfirmation": "DISABLED"
}
Click on Save and exit
to exit from Action group editing.
Next, click on Save and exit
to exit from the Agent builder.
In the test chat, click Prepare
to update the agent.
π Go to the chat with agent and ask:
translate the contrato-servicios document, you can find it in foreign-docs table
π Go to the chat with agent and ask (DON'T FORGET TO CHANGE THE BUCKET NAME!):
extract text from file s3://bucket-for-documents-NUMBER/invoice.pdf and update the invoices-parsed table
- Go to S3 console and click on the
Create bucket
button - Give your bucket a unique name, for example
knowledge-base-NUMBER
. ClickCreate bucket
. - Select your created bucket, click on the
Upload
button. - You can drag and drop
how-many-days-to-pay.docx
andpo-creation.docx
files (can be found insideknowledge-base-docs
) and then click on theUpload
button
- Go to Bedrock console, choose Builder tools -> Knowledge Bases from the navigation pane.
- Click on
Create
and selectKnowledge base with vector store
- Specify
knowledge-base-internal-docs
as name, selectAmazon S3
as data source, clickNext
.
- Add 2 data sources: these should be .docx files that you uploaded to the
knowledge-base-NUMBER
S3 bucket. Specify S3 URI locations for each of them, leave the rest as default. ClickNext
- Specify any embedding model (make sure you have enabled access to it). Select
Quick create a new vector store
andAmazon OpenSearch Serverless
, clickNext
. - After you review and create your knowledge base, make sure to Sync data sources and select a model to test.
- In the chat, write:
days to pay for US
to make sure your Knowledge base works.
- Go back to your agent, open
Agent builder
. Under theKnowledge bases
, clickAdd
- Select
knowledge-base-internal-docs
and provide for Instructions:
Search in knowledge base information that is specific to a company or certain country
- Click
Add
. You can add up to 2 Knowledge bases per agent. - In the test chat, click
Prepare
to update the agent.
π Go to the chat with agent and ask:
purchase order number generation
To resolve: Enable Code Editor
To resolve: add a resource-based policy statement on the Lambda
To resolve: Check Lambda output format