mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-03 18:46:54 +00:00
The backend import package is now docsgpt, the name it will carry on PyPI; application was far too generic to install into anyone's site-packages. git mv plus a mechanical rewrite of every import, dotted string and path reference: 734 Python files, the compose files, Dockerfile, workflows, docs, setup scripts, devcontainer, k8s manifests, vscode config, pytest and coverage config, .gitignore. Behaviour is unchanged. Kept for one release: - A top-level application package whose meta-path finder resolves application.x.y to the already-imported docsgpt.x.y object, so old imports and entry points (celery -A application.app.celery, uvicorn application.asgi:asgi_app) keep working with a FutureWarning. - Celery registers every application.* task name as an alias of its docsgpt.* task on start-up, so messages queued by the previous release still run. The redbeat key prefix moves to redbeat:docsgpt:v2: so schedule entries the previous release wrote are left unread instead of firing twice. The backend image builds from the repository root (docker build -f docsgpt/Dockerfile .) so it can ship the alias package; a root .dockerignore allow-lists docsgpt/ and application/ and keeps caches, local data, .env files, the sample index files and the Dockerfile out. Compose and the image workflows point at the new context.
103 lines
3.5 KiB
Python
103 lines
3.5 KiB
Python
import logging
|
|
import os
|
|
from bs4 import BeautifulSoup
|
|
from urllib.parse import urljoin, urlparse
|
|
|
|
from docsgpt.parser.remote.base import BaseRemote
|
|
from docsgpt.parser.schema.base import Document
|
|
from docsgpt.core.url_validation import validate_url, SSRFError
|
|
from docsgpt.security.safe_url import pinned_request
|
|
|
|
|
|
class CrawlerLoader(BaseRemote):
|
|
def __init__(self, limit=10):
|
|
self.limit = limit # Set the limit for the number of pages to scrape
|
|
|
|
def load_data(self, inputs):
|
|
url = inputs
|
|
if isinstance(url, list) and url:
|
|
url = url[0]
|
|
|
|
# Validate URL to prevent SSRF attacks
|
|
try:
|
|
url = validate_url(url)
|
|
except SSRFError as e:
|
|
logging.error(f"URL validation failed: {e}")
|
|
return []
|
|
|
|
visited_urls = set()
|
|
base_url = urlparse(url).scheme + "://" + urlparse(url).hostname
|
|
urls_to_visit = [url]
|
|
loaded_content = []
|
|
|
|
while urls_to_visit:
|
|
current_url = urls_to_visit.pop(0)
|
|
visited_urls.add(current_url)
|
|
|
|
try:
|
|
response = pinned_request("GET", current_url, timeout=30)
|
|
response.raise_for_status()
|
|
soup = BeautifulSoup(response.text, "html.parser")
|
|
extra_info = {
|
|
"source": current_url,
|
|
"file_path": self._url_to_virtual_path(current_url),
|
|
}
|
|
# Mirror WebLoader: without a title, citations fall back to the
|
|
# chunk body and render as a wall of text.
|
|
if soup.title:
|
|
title = soup.title.get_text(strip=True)
|
|
if title:
|
|
extra_info["title"] = title
|
|
loaded_content.append(
|
|
Document(
|
|
soup.get_text(separator="\n", strip=True),
|
|
extra_info=extra_info,
|
|
)
|
|
)
|
|
except Exception as e:
|
|
logging.error(f"Error processing URL {current_url}: {e}", exc_info=True)
|
|
continue
|
|
|
|
# Parse the HTML content to extract all links
|
|
all_links = [
|
|
urljoin(current_url, a['href'])
|
|
for a in soup.find_all('a', href=True)
|
|
if base_url in urljoin(current_url, a['href'])
|
|
]
|
|
|
|
# Add new links to the list of URLs to visit if they haven't been visited yet
|
|
urls_to_visit.extend([link for link in all_links if link not in visited_urls])
|
|
urls_to_visit = list(set(urls_to_visit))
|
|
|
|
# Stop crawling if the limit of pages to scrape is reached
|
|
if self.limit is not None and len(visited_urls) >= self.limit:
|
|
break
|
|
|
|
return loaded_content
|
|
|
|
def _url_to_virtual_path(self, url):
|
|
"""
|
|
Convert a URL to a virtual file path ending with .md.
|
|
|
|
Examples:
|
|
https://docs.docsgpt.cloud/ -> index.md
|
|
https://docs.docsgpt.cloud/guides/setup -> guides/setup.md
|
|
https://docs.docsgpt.cloud/guides/setup/ -> guides/setup.md
|
|
https://example.com/page.html -> page.md
|
|
"""
|
|
parsed = urlparse(url)
|
|
path = parsed.path.strip("/")
|
|
|
|
if not path:
|
|
return "index.md"
|
|
|
|
# Remove common file extensions and add .md
|
|
base, ext = os.path.splitext(path)
|
|
if ext.lower() in [".html", ".htm", ".php", ".asp", ".aspx", ".jsp"]:
|
|
path = base
|
|
|
|
if not path.endswith(".md"):
|
|
path = f"{path}.md"
|
|
|
|
return path
|