3 ways to ingest data from websites with Firecrawl
You can also check this cookbook in colab hereIn this notebook, we’ll introduce Firecrawl, a versatile web scraping and crawling tool designed to extract data efficiently from websites, which has been integrated with CAMEL. Today we’ll walk through three key features—Scrape, Crawl, and Map—each tailored with a CAMEL agent use case.In this notebook, you’ll explore:
CAMEL: A powerful multi-agent framework that enables Retrieval-Augmented Generation and multi-agent role-playing scenarios, allowing for sophisticated AI-driven tasks.
Firecrawl: A data ingestion tool that simplifies web data extraction through web scraping, API integration, and automated browser actions.
Firecrawl developed by the Mendable.ai team, is a data ingestion tool that streamlines web data extraction using web scraping, API access, and automated browser interactions. It’s ideal for collecting structured and unstructured data from websites for analytics.It effectively manages complex tasks such as handling reverse proxies, implementing caching strategies, adhering to rate limits, and accessing content blocked by JavaScript.
Crawl: Collects content from all URLs within a web page, converting it into an LLM-ready format for seamless analysis.Scrape: Extracts content from a single URL, delivering it in formats ready for LLMs, including markdown, structured data (via LLM Extract), screenshots, and HTML.Map: Inputs a website and retrieves all URLs associated with it at high speed, enabling a comprehensive and efficient site overview.All the above features make it ideal for collecting structured and unstructured data from websites for agentic workflows.
CAMEL-AI has integrated Firecrawl to enhance its web data extraction capabilities.
import osfrom getpass import getpass# Prompt for the API key securelyopenai_api_key = getpass('Enter your API key: ')os.environ["OPENAI_API_KEY"] = openai_api_key
Let’s get started with the exploration of the first feature of Firecrawl - Crawl: Extracts content from all subpages in an LLM-ready format (markdown, structured data, screenshot, HTML, links, metadata) for easy analysis.Step 1: Set up your firecrawl API keyYou just need to go to this link and sign in to get your API Key: https://www.firecrawl.dev/app/api-keys
Copy
import osfrom getpass import getpass# Prompt for the Firecrawl API key securelyfirecrawl_api_key = getpass('Enter your API key: ')os.environ["FIRECRAWL_API_KEY"] = firecrawl_api_key
Alternatively, if running on Colab, you could save your API keys and tokens as Colab Secrets, and use them across notebooks.To do so, comment out the above manual API key prompt code block(s), and uncomment the following codeblock.⚠️ Don’t forget granting access to the API key you would be using to the current notebook.
Step 3: Crawl the websiteIt will crawl the CAMEL-AI website and generate the LLM-friendly output as shown in markdown below.
Copy
# Initialize the Firecrawl instancefirecrawl = Firecrawl()# Use the `crawl` method to retrieve content from the specified URLfirecrawl_response = firecrawl.crawl( url="https://www.camel-ai.org/about" # Target URL to crawl for content)print(firecrawl_response["status"])# Print the markdown content from the first page in the crawled dataprint(firecrawl_response["data"][0]["markdown"])
Step 4: Interact with CAMEL agent
Copy
from camel.agents import ChatAgent# Initialize a ChatAgentagent = ChatAgent( system_message="You're a helpful assistant", # Define the agent's role or purpose)# Use the ChatAgent to generate a response based on the Firecrawl crawl dataresponse = agent.step(f"Based on {firecrawl_response}, explain what CAMEL is.")# Print the content of the first message in the response, which contains the assistant's answerprint(response.msgs[0].content)
Scrape: This feature allows you to extract content from a single URL and convert it into various formats optimized for LLMs. The data is delivered in markdown, structured data (via LLM Extract), screenshots, or raw HTML, making it versatile for analysis and integration with other AI applications.
Copy
# Define the schemaclass ExtractSchema(BaseModel): company_mission: str is_open_source: bool# Perform the structured scraperesponse = firecrawl.structured_scrape( url='https://www.camel-ai.org/about', # URL to scrape data from response_format=ExtractSchema)print(response)
Let’s have a look how the assistant CAMEL agent can answer our questions with the response from Firecrawl.
Copy
# Use the ChatAgent to generate a response based on the Firecrawl crawl dataresponse = agent.step(f"Based on {response}, explain what the company mission of CAMEL is.")# Print the content of the first message in the response, which contains the assistant's answerprint(response.msgs[0].content)
Map: This feature takes a website as input and rapidly retrieves all associated URLs, providing a quick and comprehensive overview of the site’s structure. This high-speed mapping is ideal for efficient content discovery and organization.
Copy
# Call the `map_site` function from Firecrawl to retrieve all URLs from the specified websitemap_result = firecrawl.map_site( url="https://www.camel-ai.org" # Target URL to map)# Print the resulting map, which should contain all associated URLs from the websiteprint(map_result)
Copy
# Use the ChatAgent to generate a response based on the Firecrawl crawl dataresponse = agent.step(f"Based on {map_result}, which one is the main website for CAMEL-AI.")# Print the content of the first message in the response, which contains the assistant's answerprint(response.msgs[0].content)
This notebook has guided you through streamlining the process of web data extraction and enhances your agents capabilities using Firecrawl within the CAMEL framework. With Firecrawl’s powerful features like Scrape, Crawl, and Map, you can efficiently gather content in formats ready for LLMs to use, directly feeding into CAMEL-AI’s multi-agent workflows. This setup not only simplifies data collection but also enables more intelligent and insightful agents.Key tools utilized in this notebook include:
CAMEL: A powerful multi-agent framework that enables Retrieval-Augmented Generation and multi-agent role-playing scenarios, allowing for sophisticated AI-driven tasks.
Firecrawl: A data ingestion tool that streamlines web data extraction using web scraping, API access, and automated browser interactions.
That’s everything: Got questions about 🐫 CAMEL-AI? Join us on Discord! Whether you want to share feedback, explore the latest in multi-agent systems, get support, or connect with others on exciting projects, we’d love to have you in the community! 🤝Check out some of our other work: