Welcome to this guide on using Amazon Bedrock Agents to simplify library management! This step-by-step tutorial will show you how to set up and use these agents to automate tasks, enhance library operations, and build a smart knowledge base. Discover how AI can make your library more efficient and effective. Ready to transform your library? Let’s get started!
You can find all the magic PDFs and code goodies in my GitHub treasure chest π©β¨:
GitHub - RISING-CODER777/awsberock_agents
π Synopsis
In this guide, we’ll cover.
- π Prerequisites
- ποΈIntroduction to Amazon Bedrock Agents
- π οΈ Setting Up Your Environment
- π Building a Knowledge Base
- π§ Creating Action Groups
- π§ͺ Testing and Troubleshooting
- β Unsubscribing to Avoid Costs
- π Conclusion
π Prerequisites
Before we dive into the fun, make sure you have the following:
- π An AWS Subscription. If you don’t have an AWS subscription, you can create a free account.
- π Access to Claude v2. and Titan Embeddings G1 — Text v1.2. from Amazon Bedrock.
- π Familiarity with AWS Lambda, S3, and IAM.
ποΈIntroduction
Amazon Bedrock Agents provides a framework for automating tasks and responding to queries based on library data. Key components include.
- Foundation Model: The AI model that processes and interprets user inputs, making sense of queries related to library inventory.
- Action Groups: Defined actions performed by AWS Lambda functions and described using OpenAPI schemas. They handle tasks like retrieving book details or updating inventory.
- Knowledge Bases: Store library data in vector format for efficient search and retrieval. They help the agent provide accurate and contextual responses.
Agent Workflow
- Contextualize User Input: Validate and process user requests to understand their needs.
- Generate Orchestration Prompt: Create a detailed prompt using user input and metadata.
- Execute Actions: Based on the prompt, perform tasks such as,
- Retrieving information
- Updating records
- Calling APIs or searching databases
- Generate Output: Produce the final response, which may include further actions or queries.
π οΈ Setting Up Your Environment
Follow these steps to create and configure a Bedrock Agent tailored for library inventory.
Create a Bedrock Agent
- In the Amazon Bedrock dashboard, look for Builder tools in the left navigation pane.
- Create Agent: Click on “Create Agent,” give it a name (for example, “Agent-LibraryManagement”), and choose the foundation model as Anthropic — Claude v2.
- Provide Instructions: Add instructions like “You are a helpful agent that manages book catalog inquiries, including book details, stock levels, prices, and availability.”
- Click Create Agent.
- Let’s set aside action groups and knowledge bases for the time being.
Initial Testing
- Your agent may initially respond with “Sorry, I don’t have enough information” as action groups and knowledge bases are not yet configured.
π Building a Knowledge Base
Setting up a knowledge base is crucial for providing accurate responses. Here’s how to configure it.
Upload Data
Create an S3 Bucket
- Go to the S3 section from the Services menu.
- Click “Create bucket”.
- Enter a unique bucket name (e.g., “librarycatalog-kb”).
- Choose your preferred AWS Region and configure other settings as needed.
- Click “Create bucket”.
Upload Files
- Go to your newly created bucket in the S3 dashboard.
- Click “Upload”.
- Drag and drop your CSV and PDF files, or click “Add files” to select them.
- Click “Upload” to transfer your files to the S3 bucket.
File Details
- CSV File: Should contain fields such as Book ID, Title, Author, Genre, ISBN, and Availability.
- PDF File: This can include detailed descriptions or additional information about the books.
Create a Knowledge Base
Access Knowledge Base Menu.
- In the AWS Console, go to the Amazon Bedrock section.
- Navigate to Builder tools and click on “Knowledge Base”.
Configure Your Knowledge Base
- Name Your Knowledge Base: Enter a name like “knowledge-base-library-catalog”.
- Data Source Name: Enter a name for your data source.
- Browse for S3 URI: Click “Browse” to locate and select the S3 URI for your bucket (e.g., s3://librarycatlog-kb).
- Choose Embedding Model: Select Titan Embeddings G1 — Text v1.2.
- Vector Database: Choose Quick Create a vector store — Recommended.
- Click “Create” to set up the knowledge base.
Sync Knowledge Base
AWS will convert the text from your files into vector embeddings for efficient retrieval. This process ensures that your knowledge base is ready for quick and accurate responses.
In the data source, click “Sync” to start the process.
Verify Vector Store
Check OpenSearch Dashboard
- In the AWS Console, navigate to OpenSearch Service.
- Go to the Dashboard: Verify that the collections and indexes related to your knowledge base have been created and are functioning correctly.
Set Up and Test an Agent
Set Up
- Navigate to Agent Builder.
- Go to Knowledge Bases and click “Add”.
- Select the knowledge base you created (e.g., “knowledge-base-library-catlog”).
- Enter the following instruction: “This knowledge base contains a list of books available in the library. Each book has attributes such as BookID, Title, Author, Genre, Publication Year, ISBN, Available Copies, and Price.”
- Click “Prepare”.
Note. Amazon Bedrock does not allow knowledge-based creation with the root account. Create an IAM user with the right permissions and enable AWS Management Console access for managing knowledge bases and related resources.
Test
- After preparing the agent, click “Test”.
- Enter queries to ensure the agent retrieves accurate information based on the knowledge base.
π§ Creating Action Groups
Action groups allow your agent to perform specific actions. Here’s how to set them up,
Create a Lambda Function
- Go to the AWS Lambda section in the AWS Management Console.
- Click “Create function”.
- Set the Function Name to “LibraryInventoryFunction”.
- Choose Python 3.12 as the Runtime.
- Select x86_64 for the Architecture.
- Click “Create function”.
- Navigate to the Code section of your Lambda function.
- Copy and paste the following code into the editor:
This Lambda function handles two API paths: /GetBooksInventory and /RestockBook. For /GetBooksInventory, it returns a hardcoded list of book details. For /RestockBook, it provides a success message. The function formats responses into JSON and handles different API paths to return appropriate data.
Save and deploy your Lambda function code.
Configure Lambda Function Permissions
- Navigate to the Lambda function’s Configurations section.
- Select Permissions.
- Under Resource-based policy statements, click Add permissions.
- In the Edit Policy Statement section
- Choose AWS Service: Select Other AWS services.
- Statement ID: Enter an identifier, e.g., st-001.
- Principal: Enter bedrock.amazonaws.com.
- Source ARN: Go to the Bedrock Agent dashboard, copy the Agent ARN, and paste it here.
- Action: Select lambda: InvokeFunction.
- Click Save to apply the changes.
Add Action Group to Agent
- Navigate to the Action Group in the Agent Details section.
- Click on Add.
- Choose the action group type as Defined with API Schemas.
- For Action Group Invocation, select Select an existing Lambda function.
- In Select Lambda Function, choose LibraryCatlogFunction from the list.
- For Select API Schema, choose Define with inline OpenAPI Schema Editor.
- Paste the following schema.
{
"openapi": "3.0.0",
"info": {
"title": "Library Management System",
"version": "1.0.0",
"description": "APIs for managing book inventory in a library"
},
"paths": {
"/GetBooksInventory": {
"get": {
"summary": "Gets books inventory",
"description": "Retrieves the list of books and their available copies",
"operationId": "getBooksInventory",
"parameters": [],
"responses": {
"200": {
"description": "Returns the inventory of all books",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"bookId": {
"type": "string",
"description": "Book ID"
},
"title": {
"type": "string",
"description": "Book Title"
},
"availableCopies": {
"type": "number",
"description": "Number of available copies"
}
}
}
}
}
}
}
}
}
},
"/RestockBook": {
"post": {
"summary": "Creates a Book Restock Order",
"description": "Creates an order to restock books in the library",
"operationId": "restockBook",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"required": ["bookId", "quantity"],
"properties": {
"bookId": {
"type": "string",
"description": "Book ID"
},
"quantity": {
"type": "number",
"description": "Quantity to restock"
}
}
}
}
}
},
"responses": {
"200": {
"description": "Returns the status of the book restock order",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"status": {
"type": "string",
"description": "Status of the book restock order - Success or Failure"
},
"message": {
"type": "string",
"description": "Additional information about the restock operation"
}
}
}
}
}
}
}
}
}
}
}
This schema defines the API paths for managing book inventory. The /GetBooksInventory endpoint retrieves book details, while the /RestockBook endpoint allows placing restock orders.
Click on Add.
π§ͺ Testing and Troubleshooting
Ensure your agent is functioning correctly and troubleshoot any issues.
Test Your Agent
- Run Prompts: Test the agent with various prompts to verify its responses.
- For example, ask to Get all book inventory.
- Restock the book with ID B003.
- Restock 3 copies of the book with ID B001.
Troubleshoot Errors
- Check for accurate information: If errors like “unknown API path” occur, verify the details provided by the agent.
- Inspect CloudWatch Logs: Go to CloudWatch, then Log Groups and Log Streams.
- Verify API Endpoint Configuration: Find the API path in the relevant log stream and ensure it matches the Lambda function code.
- In this case, verify that the /GetBooksInventory endpoint is correctly configured.
Some additional troubleshooting tips
- Check Permissions and Authentication: Verify that proper permissions and authentication are in place.
- Validate Request Format: Ensure the request format matches the expected format for the API.
- Confirm Knowledge Base Setup: Verify that the knowledge base and API integration are correctly set up.
Create an Alias
- Alias Creation: In the agent details page, create an alias for versioning and testing specific versions of your agent.
- Test Alias: Test the alias just like you would with a working draft to ensure it behaves as expected.
β Unsubscribing to Avoid Costs
Keep your costs in check by deleting unused resources and regularly reviewing your AWS account. To avoid extra charges, cancel any unneeded subscriptions in Claude v2 via AWS Marketplace.
Conclusion
You did it! π With Amazon Bedrock Agents, you’ve taken library management to the next level. By setting up knowledge bases and action groups, you’re now ready to automate tasks and impress users with smart responses.
Keep experimenting and tweaking to perfect your setup. Dive into the world of Amazon Bedrock Agents and see how much more efficient and fun managing your library can be. Happy building! π
If you enjoyed this blog, don’t forget to share it with your friends and colleagues.
Follow me on LinkedIn! Did you enjoy this guide? Feel free to clap and comment below!
Also any comment on anything missing or not right is very welcome. Long live the AWS Learning community π€!
Attributes and References
- Amazon Bedrock Agents: Create and Configure AWS Documentation
- Amazon Bedrock Agents: How They Work AWS Documentation
- Amazon Bedrock Knowledge Base: Create AWS Documentation
- Amazon Bedrock Knowledge Base: How It Works AWS Documentation