feat: Sanitize input and handle encoding issues in LLMExtractionStrategy
This commit is contained in:
@@ -96,7 +96,7 @@ def sanitize_html(html):
|
|||||||
|
|
||||||
return sanitized_html
|
return sanitized_html
|
||||||
|
|
||||||
def sanitize_input_encode(self, text: str) -> str:
|
def sanitize_input_encode(text: str) -> str:
|
||||||
"""Sanitize input to handle potential encoding issues."""
|
"""Sanitize input to handle potential encoding issues."""
|
||||||
try:
|
try:
|
||||||
# Attempt to encode and decode as UTF-8 to handle potential encoding issues
|
# Attempt to encode and decode as UTF-8 to handle potential encoding issues
|
||||||
|
|||||||
@@ -155,8 +155,8 @@ class WebCrawler:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
if cached:
|
if cached:
|
||||||
html = cached[1]
|
html = sanitize_input_encode(cached[1])
|
||||||
extracted_content = cached[4]
|
extracted_content = sanitize_input_encode(cached[4])
|
||||||
if screenshot:
|
if screenshot:
|
||||||
screenshot_data = cached[9]
|
screenshot_data = cached[9]
|
||||||
if not screenshot_data:
|
if not screenshot_data:
|
||||||
@@ -166,7 +166,7 @@ class WebCrawler:
|
|||||||
if user_agent:
|
if user_agent:
|
||||||
self.crawler_strategy.update_user_agent(user_agent)
|
self.crawler_strategy.update_user_agent(user_agent)
|
||||||
t1 = time.time()
|
t1 = time.time()
|
||||||
html = self.crawler_strategy.crawl(url, **kwargs)
|
html = sanitize_input_encode(self.crawler_strategy.crawl(url, **kwargs))
|
||||||
t2 = time.time()
|
t2 = time.time()
|
||||||
if verbose:
|
if verbose:
|
||||||
print(f"[LOG] 🚀 Crawling done for {url}, success: {bool(html)}, time taken: {t2 - t1} seconds")
|
print(f"[LOG] 🚀 Crawling done for {url}, success: {bool(html)}, time taken: {t2 - t1} seconds")
|
||||||
@@ -213,8 +213,8 @@ class WebCrawler:
|
|||||||
except InvalidCSSSelectorError as e:
|
except InvalidCSSSelectorError as e:
|
||||||
raise ValueError(str(e))
|
raise ValueError(str(e))
|
||||||
|
|
||||||
cleaned_html = result.get("cleaned_html", "")
|
cleaned_html = sanitize_input_encode(result.get("cleaned_html", ""))
|
||||||
markdown = result.get("markdown", "")
|
markdown = sanitize_input_encode(result.get("markdown", ""))
|
||||||
media = result.get("media", [])
|
media = result.get("media", [])
|
||||||
links = result.get("links", [])
|
links = result.get("links", [])
|
||||||
metadata = result.get("metadata", {})
|
metadata = result.get("metadata", {})
|
||||||
|
|||||||
Reference in New Issue
Block a user