I have web base Chatbot application, backend is in Python. Backend is connected do vector database - Redis. I have implemented RAG algorithym that extracts most relevant document chunks from Redis, passes to final LLM call to generate answer to users questions. Im using Openai LLMs and Openai embeddings. I have created my own document loader pipeline, like PDF, DOCX, etc. and save them into Redis.
Know i would like to add Websites as a source, my chatbot needs to answer based on websites. I want to set as parameter two things: main website and blacklist. This new feature should start from main website and visit every website beneath (besides blacklist), scrap every information with proper well-fromated way + download every document listed in those websites. Those websites and documents on websites should be new data source for chatbot to answer, it can be saved in Redis aswell, so as my RAG algorithm would work as well. How to do it best way?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
A fantastic project! You're building a robust chatbot with a solid foundation, and now you want to expand its knowledge base by crawling websites and ingesting their content. I'll guide you through the best approach to achieve this.
Overview of the Website Crawler & Ingestion Pipeline
To accomplish this, you'll need to design a website crawler that:
Starts from the main website (seed URL) and explores all linked web pages (recursively) while respecting the blacklist (URLs or domains to exclude).
Extracts relevant text content from each webpage, handling various formats (HTML, PDF, DOCX, etc.).
Downloads and processes documents (e.g., PDF, DOCX, etc.) linked from these webpages.
Stores the extracted data in Redis, alongside your existing document chunks, to be used by your RAG algorithm.
Here's a step-by-step plan to implement this pipeline:
Step 1: Choose a Web Crawling Library
Select a reliable, Python-based web crawling library. I recommend:
Scrapy (scrapy.org): Fast, flexible, and widely-used. Handles anti-scraping measures, rate limiting, and more.
Beautiful Soup (beautiful-soup-4.readthedocs.io) with Requests (requests.readthedocs.io): Simpler, more intuitive, but less performant than Scrapy.
For this task, I'll suggest using Scrapy, as it:
Handles concurrency and rate limiting out-of-the-box.
Offers built-in support for filtering out unwanted URLs (blacklist).
In website_crawler/website_crawler/spiders, create a new spider (website_spider.py):
python
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
class WebsiteSpider(CrawlSpider):
name = "website_spider"
start_urls = ["https://main-website.com"] # Your main website (seed URL)
blacklist = ["https://unwanted-subdomain.main-website.com", "https://external-site.com"] # Blacklist
rules = (
Rule(LinkExtractor(deny=blacklist), callback="parse_item", follow=True), # Follow links, except blacklisted
)
def parse_item(self, response):
# Extract text content from HTML pages
text = response.css("body ::text").get()
# ... (clean, tokenize, and format text as needed)
# Extract linked documents (e.g., PDF, DOCX)
doc_links = response.css("a::attr(href)").re(r".*\.(pdf|docx)$")
for doc_link in doc_links:
yield response.follow(doc_link, self.parse_document)
# Yield the webpage content as an item
yield {
"url": response.url,
"text": text,
"type": "webpage",
}
def parse_document(self, response):
# Handle document downloads (e.g., PDF, DOCX)
doc_content = response.body
# ... (process doc_content using your existing document loader pipeline)
# Yield the document content as an item
yield {
"url": response.url,
"content": doc_content,
"type": "document",
}
This spider:
Starts at the main-website.com
Follows all links, except those in the blacklist
Extracts text from HTML pages and linked documents (PDF, DOCX)
Yields items containing webpage text or document content
Step 3: Integrate with your Document Loader Pipeline
Modify the parse_document method to leverage your existing document loader pipeline (e.g., PDF, DOCX processing). This will ensure consistent processing and formatting for documents from websites.
Rate limiting: Configure Scrapy's DOWNLOAD_DELAY and CONCURRENT_REQUESTS to avoid overwhelming websites and prevent IP blocking.
Respect robots.txt: Scrapy honors robots.txt by default. Ensure you comply with website terms of service.
Monitoring and logging: Set up Scrapy's built-in logging and monitoring tools to track crawler performance and errors.
Data freshness: Schedule regular crawls (e.g., via scrapy-scheduler) to keep your chatbot's knowledge up-to-date.
By following these steps, you'll successfully integrate website crawling and ingestion into your chatbot's RAG pipeline, expanding its knowledge base and improving its ability to answer user questions based on the latest web content.