Skip to content

Example of using CreateDataSource API for a web crawler in Amazon Bedrock?

0

Hello, I'm trying to crawl a website using the CreateDataSource API but I can't find the proper methods to call for creating the data source itself (it is not create_data_source). Can anyone provide me with a working example or where can I find the proper documentation? I've manually crawled so far using BeautifulSoup but the results are subpar.

1 Answer
0

Certainly! I can provide you with an example of using the CreateDataSource API for a web crawler in Amazon Bedrock. The method you're looking for is indeed "create_data_source" when using the Boto3 client for Bedrock Agent.

Here's a Python code example using Boto3 to create a web crawler data source for an Amazon Bedrock knowledge base:

import boto3

client = boto3.client('bedrock-agent', region_name='us-east-1')

knowledge_base_id = 'your-knowledge-base-id'

response = client.create_data_source(
    knowledgeBaseId=knowledge_base_id,
    name='example',
    description='test description',
    dataSourceConfiguration={
        'type': 'WEB',
        'webConfiguration': {
            'sourceConfiguration': {
                'urlConfiguration': {
                    'seedUrls': [
                        {'url': 'https://example.com/'}
                    ]
                }
            },
            'crawlerConfiguration': {
                'crawlerLimits': {
                    'rateLimit': 300
                },
                'inclusionFilters': [
                    '.*products.*'
                ],
                'exclusionFilters': [
                    '.*\.pdfCertainly! I can provide you with an example of using the CreateDataSource API for a web crawler in Amazon Bedrock. The method you're looking for is indeed "create_data_source" when using the Boto3 client for Bedrock Agent.

Here's a Python code example using Boto3 to create a web crawler data source for an Amazon Bedrock knowledge base:

```python
import boto3

client = boto3.client('bedrock-agent', region_name='us-east-1')

knowledge_base_id = 'your-knowledge-base-id'

response = client.create_data_source(
    knowledgeBaseId=knowledge_base_id,
    name='example',
    description='test description',
    dataSourceConfiguration={
        'type': 'WEB',
        'webConfiguration': {
            'sourceConfiguration': {
                'urlConfiguration': {
                    'seedUrls': [
                        {'url': 'https://example.com/'}
                    ]
                }
            },
            'crawlerConfiguration': {
                'crawlerLimits': {
                    'rateLimit': 300
                },
                'inclusionFilters': [
                    '.*products.*'
                ],
                'exclusionFilters': [
                    '.*\.pdf$'
                ],
                'scope': 'HOST_ONLY'
            }
        }
    }
)

In this example, you'll need to replace 'your-knowledge-base-id' with your actual knowledge base ID. The code sets up a web crawler with a seed URL, rate limit, and inclusion/exclusion filters. You can customize these parameters based on your specific requirements.

Make sure you have the necessary permissions set up in your IAM role to use the Bedrock Agent API. Also, ensure that you're authorized to crawl the websites you're targeting and that you're respecting their robots.txt files.

This approach using Amazon Bedrock's built-in web crawler should provide better results than manually crawling with BeautifulSoup, as it's designed specifically for creating knowledge bases and integrates well with other Amazon Bedrock features.#39; ], 'scope': 'HOST_ONLY' } } } )


In this example, you'll need to replace 'your-knowledge-base-id' with your actual knowledge base ID. The code sets up a web crawler with a seed URL, rate limit, and inclusion/exclusion filters. You can customize these parameters based on your specific requirements.

Make sure you have the necessary permissions set up in your IAM role to use the Bedrock Agent API. Also, ensure that you're authorized to crawl the websites you're targeting and that you're respecting their robots.txt files.

This approach using Amazon Bedrock's built-in web crawler should provide better results than manually crawling with BeautifulSoup, as it's designed specifically for creating knowledge bases and integrates well with other Amazon Bedrock features.  
**Sources**  
[Implement web crawling in Amazon Bedrock Knowledge Bases | AWS Machine Learning Blog](https://aws.amazon.com/blogs/machine-learning/implement-web-crawling-in-knowledge-bases-for-amazon-bedrock/)

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.