AWS Bedrock Knowledge Bases enables you to give foundation models access to your company’s private data sources, delivering more relevant, accurate, and customized responses through Retrieval Augmented Generation (RAG).
With Portkey’s integration, you can seamlessly create and connect to AWS Bedrock Knowledge Bases while gaining enterprise features like observability, caching, and reliability - all through a unified API that simplifies authentication.
What is AWS Bedrock Knowledge Bases?
AWS Bedrock Knowledge Bases is a fully managed service that implements the entire RAG workflow - from data ingestion to retrieval and prompt augmentation. It allows you to:
- Connect to Multiple Data Sources: Automatically fetch data from Amazon S3, Confluence, Salesforce, SharePoint, and more.
- Managed Vector Storage: Store embeddings in supported vector databases like Amazon Aurora, OpenSearch, Neptune, MongoDB, Pinecone, or Redis.
- Advanced Retrieval: Use semantic search and filtering for accurate information retrieval.
- Source Attribution: All retrieved information includes citations to improve transparency and minimize hallucinations.
Prerequisites
Before integrating AWS Bedrock Knowledge Bases with Portkey, ensure you have:
- AWS Account with Bedrock access enabled.
- AWS Credentials with permissions to create and access Bedrock Knowledge Bases, configured in Portkey.
- Portkey Account with an API key.
- An IAM Role with the necessary permissions for Bedrock to access your data sources.
- A Vector Store (like Amazon OpenSearch Serverless) to store the indexed data.
Setup Guide
Step 1: Create an AWS Integration in Portkey
You need to connect your AWS account to Portkey. This allows Portkey to make authenticated requests to AWS on your behalf.
Navigate to Integrations
Navigate to the Integrations section on Portkey’s Sidebar. This is where you’ll connect your LLM providers.
- Find Bedrock and click Connect.
- In the “Create New Integration” window:
- Enter a Name for reference (e.g.,
aws-bedrock-prod
).
- Enter a Slug for the integration (e.g.,
aws-bedrock-prod
).
- Enter your AWS Access Key ID, Secret Access Key, and Default Region.
- Click Next Step.
The Slug you create here will be used as the virtual_key
in your Portkey client initialization.
Configure Models
On the model provisioning page:
- Leave all models selected (or customize).
- Toggle “Automatically enable new models” if desired.
Click Create Integration to complete the integration.
Step 2: Create a Knowledge Base
To create an AWS Bedrock Knowledge Base, you use Portkey’s put
method. This acts as a proxy, sending a signed request directly to the AWS PUT /knowledgebases/
API endpoint. Portkey handles the complex AWS Signature Version 4 authentication process for you.
When initializing the Portkey client, you must provide a custom_host
that points to the AWS Bedrock Agent API endpoint for your region.
AWS Bedrock Docs to create Knowledgebase
from portkey_ai import Portkey
# The virtual_key is the slug of your Bedrock provider in Portkey
# The custom_host is the Bedrock Agent API endpoint for your region
portkey = Portkey(
api_key="YOUR_PORTKEY_API_KEY",
virtual_key="your-bedrock-provider-slug",
custom_host="https://bedrock-agent.us-east-1.amazonaws.com"
)
# The body for the request to create a new Knowledge Base
# Replace placeholders with your actual resource ARNs and names
kb_body = {
"name": "MyKB",
"description": "My knowledge base",
"roleArn": "arn:aws:iam::111122223333:role/service-role/AmazonBedrockExecutionRoleForKnowledgeBase_123",
"knowledgeBaseConfiguration": {
"type": "VECTOR",
"vectorKnowledgeBaseConfiguration": {
"embeddingModelArn": "arn:aws:bedrock:us-east-1::foundation-model/amazon.titan-embed-text-v2:0",
"embeddingModelConfiguration": {
"bedrockEmbeddingModelConfiguration": {
"dimensions": 1024,
"embeddingDataType": "BINARY"
}
},
"supplementalDataStorageConfiguration": {
"storageLocations": [
{
"s3Location": {
"uri": "arn:aws:s3:::MyBucket"
},
"type": "S3"
}
]
}
}
},
"storageConfiguration": {
"opensearchServerlessConfiguration": {
"collectionArn": "arn:aws:aoss:us-east-1:111122223333:collection/abcdefghij1234567890",
"fieldMapping": {
"metadataField": "metadata",
"textField": "text",
"vectorField": "vector"
},
"vectorIndexName": "MyVectorIndex"
}
}
}
# Make the PUT request to create the knowledge base
response = portkey.put(
path='/knowledgebases/',
body=kb_body
)
print(response.json())
from portkey_ai import Portkey
# The virtual_key is the slug of your Bedrock provider in Portkey
# The custom_host is the Bedrock Agent API endpoint for your region
portkey = Portkey(
api_key="YOUR_PORTKEY_API_KEY",
virtual_key="your-bedrock-provider-slug",
custom_host="https://bedrock-agent.us-east-1.amazonaws.com"
)
# The body for the request to create a new Knowledge Base
# Replace placeholders with your actual resource ARNs and names
kb_body = {
"name": "MyKB",
"description": "My knowledge base",
"roleArn": "arn:aws:iam::111122223333:role/service-role/AmazonBedrockExecutionRoleForKnowledgeBase_123",
"knowledgeBaseConfiguration": {
"type": "VECTOR",
"vectorKnowledgeBaseConfiguration": {
"embeddingModelArn": "arn:aws:bedrock:us-east-1::foundation-model/amazon.titan-embed-text-v2:0",
"embeddingModelConfiguration": {
"bedrockEmbeddingModelConfiguration": {
"dimensions": 1024,
"embeddingDataType": "BINARY"
}
},
"supplementalDataStorageConfiguration": {
"storageLocations": [
{
"s3Location": {
"uri": "arn:aws:s3:::MyBucket"
},
"type": "S3"
}
]
}
}
},
"storageConfiguration": {
"opensearchServerlessConfiguration": {
"collectionArn": "arn:aws:aoss:us-east-1:111122223333:collection/abcdefghij1234567890",
"fieldMapping": {
"metadataField": "metadata",
"textField": "text",
"vectorField": "vector"
},
"vectorIndexName": "MyVectorIndex"
}
}
}
# Make the PUT request to create the knowledge base
response = portkey.put(
path='/knowledgebases/',
body=kb_body
)
print(response.json())
import { Portkey } from 'portkey-ai';
// The virtualKey is the slug of your Bedrock provider in Portkey
// The customHost is the Bedrock Agent API endpoint for your region
const portkey = new Portkey({
apiKey: "YOUR_PORTKEY_API_KEY",
virtualKey: "your-bedrock-provider-slug",
customHost: "https://bedrock-agent.us-east-1.amazonaws.com"
});
async function createKnowledgeBase() {
// The body for the request to create a new Knowledge Base
// Replace placeholders with your actual resource ARNs and names
const kbBody = {
"name": "MyKB",
"description": "My knowledge base",
"roleArn": "arn:aws:iam::111122223333:role/service-role/AmazonBedrockExecutionRoleForKnowledgeBase_123",
"knowledgeBaseConfiguration": {
"type": "VECTOR",
"vectorKnowledgeBaseConfiguration": {
"embeddingModelArn": "arn:aws:bedrock:us-east-1::foundation-model/amazon.titan-embed-text-v2:0",
"embeddingModelConfiguration": {
"bedrockEmbeddingModelConfiguration": {
"dimensions": 1024,
"embeddingDataType": "BINARY"
}
},
"supplementalDataStorageConfiguration": {
"storageLocations": [
{
"s3Location": {
"uri": "arn:aws:s3:::MyBucket"
},
"type": "S3"
}
]
}
}
},
"storageConfiguration": {
"opensearchServerlessConfiguration": {
"collectionArn": "arn:aws:aoss:us-east-1:111122223333:collection/abcdefghij1234567890",
"fieldMapping": {
"metadataField": "metadata",
"textField": "text",
"vectorField": "vector"
},
"vectorIndexName": "MyVectorIndex"
}
}
};
try {
const response = await portkey.put('/knowledgebases/', kbBody);
console.log(response);
} catch (error) {
console.error('Error creating knowledge base:', error);
}
}
createKnowledgeBase();
curl --request PUT \
--url https://api.portkey.ai/v1/knowledgebases/ \
--header 'Content-Type: application/json' \
--header 'x-portkey-api-key: YOUR_PORTKEY_API_KEY' \
--header 'x-portkey-virtual-key: your-bedrock-provider-slug' \
--header 'x-portkey-custom-host: https://bedrock-agent.us-east-1.amazonaws.com' \
--data '{
"name": "MyKB",
"description": "My knowledge base",
"roleArn": "arn:aws:iam::111122223333:role/service-role/AmazonBedrockExecutionRoleForKnowledgeBase_123",
"knowledgeBaseConfiguration": {
"type": "VECTOR",
"vectorKnowledgeBaseConfiguration": {
"embeddingModelArn": "arn:aws:bedrock:us-east-1::foundation-model/amazon.titan-embed-text-v2:0",
"embeddingModelConfiguration": {
"bedrockEmbeddingModelConfiguration": {
"dimensions": 1024,
"embeddingDataType": "BINARY"
}
},
"supplementalDataStorageConfiguration": {
"storageLocations": [
{
"s3Location": {
"uri": "arn:aws:s3:::MyBucket"
},
"type": "S3"
}
]
}
}
},
"storageConfiguration": {
"opensearchServerlessConfiguration": {
"collectionArn": "arn:aws:aoss:us-east-1:111122223333:collection/abcdefghij1234567890",
"fieldMapping": {
"metadataField": "metadata",
"textField": "text",
"vectorField": "vector"
},
"vectorIndexName": "MyVectorIndex"
}
}
}'
Step 3: Retrieve from a Knowledge Base
Once your knowledge base is created and has finished syncing, you can query it using Portkey’s post
method. This request is sent to the AWS retrieve
API endpoint: POST /knowledgebases/{knowledgeBaseId}/retrieve
.
AWS Bedrock Docs to create Knowledgebase
Important: For retrieval, the custom_host
URL is different. You must use the Bedrock Agent Runtime endpoint for your region (e.g., bedrock-agent-runtime.us-east-1.amazonaws.com
).
from portkey_ai import Portkey
# For retrieval, use the Bedrock Agent Runtime URL
portkey = Portkey(
api_key="YOUR_PORTKEY_API_KEY",
virtual_key="your-bedrock-provider-slug",
custom_host="bedrock-agent-runtime.us-east-1.amazonaws.com"
)
# Your Knowledge Base ID from the AWS console
knowledge_base_id = "YOUR_KNOWLEDGE_BASE_ID"
query = "What is our company's remote work policy?"
response = portkey.post(
url=f"/knowledgebases/{knowledge_base_id}/retrieve",
retrievalQuery={
"text": query
}
)
# The response contains the retrieved chunks
print(response.json())
from portkey_ai import Portkey
# For retrieval, use the Bedrock Agent Runtime URL
portkey = Portkey(
api_key="YOUR_PORTKEY_API_KEY",
virtual_key="your-bedrock-provider-slug",
custom_host="bedrock-agent-runtime.us-east-1.amazonaws.com"
)
# Your Knowledge Base ID from the AWS console
knowledge_base_id = "YOUR_KNOWLEDGE_BASE_ID"
query = "What is our company's remote work policy?"
response = portkey.post(
url=f"/knowledgebases/{knowledge_base_id}/retrieve",
retrievalQuery={
"text": query
}
)
# The response contains the retrieved chunks
print(response.json())
import { Portkey } from 'portkey-ai';
// For retrieval, use the Bedrock Agent Runtime URL
const portkey = new Portkey({
apiKey: "YOUR_PORTKEY_API_KEY",
virtualKey: "your-bedrock-provider-slug",
customHost: "bedrock-agent-runtime.us-east-1.amazonaws.com"
});
async function queryKnowledgeBase() {
// Your Knowledge Base ID from the AWS console
const knowledgeBaseId = "YOUR_KNOWLEDGE_BASE_ID";
const query = "What is our company's remote work policy?";
try {
// Construct the API path and request body
const response = await portkey.post(
`/knowledgebases/${knowledgeBaseId}/retrieve`,
{
retrievalQuery: {
text: query
}
}
);
// The response contains the retrieved chunks
console.log(response);
} catch (error) {
console.error('Error querying knowledge base:', error);
throw error;
}
}
queryKnowledgeBase();
Next Steps
Now that you can create and retrieve data from your knowledge base, you can build a full RAG application.
For enterprise support with your AWS Bedrock integration, contact our enterprise team.