feat: initial commit for Blog Expert multi-agent system

This commit is contained in:
2026-10-16 11:59:15 +05:30
commit 1f952ded52
79 changed files with 9156 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
"""
chunker.py
"""
from pathlib import Path
import sys
from langchain_text_splitters import RecursiveCharacterTextSplitter
if __package__ is None or __package__ == "":
sys.path.append(str(Path(__file__).resolve().parents[1]))
from config import settings
splitter = RecursiveCharacterTextSplitter(
chunk_size=settings.CHUNK_SIZE,
chunk_overlap=settings.CHUNK_OVERLAP,
separators=[
"\n\n",
"\n",
". ",
" ",
""
]
)
def create_chunks(text):
return splitter.split_text(text)
+46
View File
@@ -0,0 +1,46 @@
"""
html_cleaner.py
Removes unnecessary HTML.
"""
from bs4 import BeautifulSoup
REMOVE_TAGS = [
"script",
"style",
"svg",
"iframe",
"noscript",
"footer",
"header",
"nav",
"aside"
]
def clean_html(html: str):
soup = BeautifulSoup(
html,
"lxml"
)
for tag in REMOVE_TAGS:
for item in soup.find_all(tag):
item.decompose()
return str(soup)
+62
View File
@@ -0,0 +1,62 @@
"""
ingestion.py
"""
from pathlib import Path
import sys
if __package__ is None or __package__ == "":
sys.path.append(str(Path(__file__).resolve().parents[1]))
from config import settings
from langchain_core.documents import Document
from langchain_pinecone import PineconeVectorStore
from config.embeddings import embeddings
from config.pinecone_manager import get_or_create_index
index = get_or_create_index()
vectorstore = PineconeVectorStore(
index=index,
embedding=embeddings
)
def ingest_blog(
chunks,
url
):
if not chunks:
return
docs = []
for i, chunk in enumerate(chunks):
docs.append(
Document(
page_content=chunk,
metadata={
"url": url,
"chunk": i
}
)
)
vectorstore.add_documents(docs)
+18
View File
@@ -0,0 +1,18 @@
"""
markdown_converter.py
"""
from markdownify import markdownify
def html_to_markdown(html: str):
markdown = markdownify(
html,
heading_style="ATX"
)
return markdown
+45
View File
@@ -0,0 +1,45 @@
"""
retriever.py
"""
from pathlib import Path
import sys
if __package__ is None or __package__ == "":
sys.path.append(str(Path(__file__).resolve().parents[1]))
from langchain_pinecone import PineconeVectorStore
from config.embeddings import embeddings
from config.pinecone_manager import get_or_create_index
from config import settings
index = get_or_create_index()
vectorstore = PineconeVectorStore(
index=index,
embedding=embeddings
)
retriever = vectorstore.as_retriever(
search_kwargs={
"k": settings.TOP_K
}
)
def retrieve(query):
return retriever.invoke(query)
+137
View File
@@ -0,0 +1,137 @@
"""
scraper.py
Scrapes a webpage using Crawl4AI.
Returns:
raw_html
cleaned_markdown
title
metadata
"""
import asyncio
import requests
from bs4 import BeautifulSoup
from crawl4ai import AsyncWebCrawler
class BlogScraper:
def _metadata_from_html(self, html: str):
soup = BeautifulSoup(html, "lxml")
title = ""
if soup.title and soup.title.string:
title = soup.title.string.strip()
metadata = {"title": title}
for tag in soup.find_all("meta"):
name = tag.get("name") or tag.get("property")
content = tag.get("content")
if name and content:
metadata[name] = content
return metadata
def _fast_http_scrape(self, url: str):
headers = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/126.0 Safari/537.36"
),
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.9",
}
try:
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()
html = response.text
if len(html) > 1200:
metadata = self._metadata_from_html(html)
return {
"url": url,
"title": metadata.get("title", ""),
"html": html,
"markdown": "",
"metadata": metadata,
}
except Exception:
pass
return None
def _fallback_scrape(self, url: str, crawl_error: str = ""):
headers = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/126.0 Safari/537.36"
),
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.9",
}
try:
response = requests.get(url, headers=headers, timeout=20)
response.raise_for_status()
html = response.text
metadata = self._metadata_from_html(html)
return {
"url": url,
"title": metadata.get("title", ""),
"html": html,
"markdown": "",
"metadata": metadata,
}
except Exception as exc:
detail = f"Crawl4AI error: {crawl_error}. " if crawl_error else ""
raise Exception(f"Unable to scrape {url}. {detail}HTTP fallback error: {exc}") from exc
async def scrape(self, url: str):
# 1. Try ultra-fast HTTP request first (< 0.5s)
fast_result = self._fast_http_scrape(url)
if fast_result is not None:
return fast_result
# 2. Fallback to Crawl4AI headless browser for JS-rendered apps
try:
async with AsyncWebCrawler() as crawler:
try:
result = await asyncio.wait_for(
crawler.arun(
url=url,
bypass_cache=True,
),
timeout=40.0,
)
except Exception as exc:
return self._fallback_scrape(url, f"Crawl4AI timeout/error: {exc}")
if not result.success:
crawl_error = (
getattr(result, "error_message", "")
or getattr(result, "status_message", "")
or "unknown Crawl4AI failure"
)
return self._fallback_scrape(url, crawl_error)
return {
"url": url,
"title": result.metadata.get("title", ""),
"html": result.html,
"markdown": result.markdown,
"metadata": result.metadata,
}
except Exception as exc:
return self._fallback_scrape(url, f"Playwright / Crawl4AI startup error: {exc}")
def scrape_blog(url: str):
scraper = BlogScraper()
return asyncio.run(scraper.scrape(url))