Python scraper based on AI
Python ai-crawlerai-scrapingai-searchcrawlerdata-extractionfirecrawl-alternativelarge-language-modelllmmarkdownragscrapingscraping-pythonweb-crawlerweb-crawlersweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
fix(ParseNode): leave room for LLM reply in context window
Federico Aguzzi683bf57d895d
diff --git a/pyproject.toml b/pyproject.tomlindex cb3e6be2..53972f17 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -14,12 +14,12 @@ authors = []dependencies = [- "langchain==0.2.14",+ "langchain>=0.2.14","langchain-fireworks>=0.1.3","langchain_community>=0.2.9","langchain-google-genai>=1.0.7","langchain-google-vertexai>=1.0.7",- "langchain-openai==0.1.22",+ "langchain-openai>=0.1.22","langchain-groq>=0.1.3","langchain-aws>=0.1.3","langchain-anthropic>=0.1.11",diff --git a/requirements-dev.lock b/requirements-dev.lockindex 44cc64de..64af8ee8 100644--- a/requirements-dev.lock+++ b/requirements-dev.lock@@ -179,6 +179,7 @@ graphviz==0.20.3# via scrapegraphaigreenlet==3.0.3# via playwright+ # via sqlalchemygroq==0.9.0# via langchain-groqgrpc-google-iam-v1==0.13.1diff --git a/requirements.lock b/requirements.lockindex 1812ab21..1d80e1bf 100644--- a/requirements.lock+++ b/requirements.lock@@ -133,6 +133,7 @@ graphviz==0.20.3# via scrapegraphaigreenlet==3.0.3# via playwright+ # via sqlalchemygroq==0.9.0# via langchain-groqgrpc-google-iam-v1==0.13.1diff --git a/requirements.txt b/requirements.txtindex 754eab61..21c2fd3b 100644--- a/requirements.txt+++ b/requirements.txt@@ -1,9 +1,9 @@-langchain>=0.2.10+langchain>=0.2.14langchain-fireworks>=0.1.3langchain_community>=0.2.9langchain-google-genai>=1.0.7langchain-google-vertexai>=1.0.7-langchain-openai>=0.1.17+langchain-openai>=0.1.22langchain-groq>=0.1.3langchain-aws>=0.1.3langchain-anthropic>=0.1.11diff --git a/scrapegraphai/nodes/parse_node.py b/scrapegraphai/nodes/parse_node.pyindex db7f8518..8c536bad 100644--- a/scrapegraphai/nodes/parse_node.py+++ b/scrapegraphai/nodes/parse_node.py@@ -6,7 +6,6 @@ from typing import List, Optionalfrom semchunk import chunkfrom langchain_community.document_transformers import Html2TextTransformerfrom langchain_core.documents import Document-from ..utils.logging import get_loggerfrom .base_node import BaseNodeclass ParseNode(BaseNode):@@ -79,16 +78,18 @@ class ParseNode(BaseNode):else:docs_transformed = docs_transformed[0]+ # Adapt the chunk size, leaving room for the reply, the prompt and the schema+ chunk_size = self.node_config.get("chunk_size", 4096)+ chunk_size = min(chunk_size - 500, int(chunk_size * 0.9))+if isinstance(docs_transformed, Document):-chunks = chunk(text=docs_transformed.page_content,- chunk_size=self.node_config.get("chunk_size", 4096)-250,+ chunk_size=chunk_size,token_counter=lambda text: len(text.split()),memoize=False)else:-chunks = chunk(text=docs_transformed,- chunk_size=self.node_config.get("chunk_size", 4096)-250,+ chunk_size=chunk_size,token_counter=lambda text: len(text.split()),memoize=False)diff --git a/scrapegraphai/utils/token_calculator.py b/scrapegraphai/utils/token_calculator.pyindex c5263efe..c5e5fbbb 100644--- a/scrapegraphai/utils/token_calculator.py+++ b/scrapegraphai/utils/token_calculator.py@@ -1,5 +1,5 @@"""-Module for truncatinh in chunks the messages+Module for truncating in chunks the messages"""from typing import Listimport tiktoken@@ -27,7 +27,7 @@ def truncate_text_tokens(text: str, model: str, encoding_name: str) -> List[str]"""encoding = tiktoken.get_encoding(encoding_name)- max_tokens = models_tokens[model] - 500+ max_tokens = min(models_tokens[model] - 500, int(models_tokens[model] * 0.9))encoded_text = encoding.encode(text)chunks = [encoded_text[i:i + max_tokens]