Python scraper based on AI
Python ai-crawlerai-scrapingai-searchcrawlerdata-extractionfirecrawl-alternativelarge-language-modelllmmarkdownragscrapingscraping-pythonweb-crawlerweb-crawlersweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
add chunking functionn
Marco Vinciguerra380174d49033
diff --git a/scrapegraphai/nodes/parse_node.py b/scrapegraphai/nodes/parse_node.pyindex a28b2e86..4a8638e7 100644--- a/scrapegraphai/nodes/parse_node.py+++ b/scrapegraphai/nodes/parse_node.py@@ -6,6 +6,7 @@ from semchunk import chunkfrom langchain_community.document_transformers import Html2TextTransformerfrom langchain_core.documents import Documentfrom .base_node import BaseNode+from tokenizer import num_tokens_calculusclass ParseNode(BaseNode):"""diff --git a/scrapegraphai/utils/__init__.py b/scrapegraphai/utils/__init__.pyindex 86fa0840..aa367a9c 100644--- a/scrapegraphai/utils/__init__.py+++ b/scrapegraphai/utils/__init__.py@@ -11,6 +11,9 @@ from .sys_dynamic_import import dynamic_import, srcfile_importfrom .cleanup_html import cleanup_htmlfrom .logging import *from .convert_to_md import convert_to_md-from .screenshot_scraping.screenshot_preparation import take_screenshot, select_area_with_opencv, select_area_with_ipywidget, crop_image+from .screenshot_scraping.screenshot_preparation import (take_screenshot,+ select_area_with_opencv,+ select_area_with_ipywidget,+ crop_image)from .screenshot_scraping.text_detection import detect_text-from .token_calculator import *+from .tokenizer import num_tokens_calculusdiff --git a/scrapegraphai/utils/token_calculator.py b/scrapegraphai/utils/token_calculator.pydeleted file mode 100644index 2545c865..00000000--- a/scrapegraphai/utils/token_calculator.py+++ /dev/null@@ -1,62 +0,0 @@-"""-Module for truncating in chunks the messages-"""-from typing import List-import tiktoken-from ..helpers.models_tokens import models_tokens---def truncate_text_tokens(text: str, model: str) -> List[str]:- """- Truncates text into chunks that are small enough to be processed by specified llm models.-- Args:- text (str): The input text to be truncated.- model (str): The name of the llm model to determine the maximum token limit.-- Returns:- List[str]: A list of text chunks, each within the token limit of the specified model.-- Example:- >>> truncate_text_tokens("This is a sample text for truncation.", "gpt-4o-mini")- ["This is a sample text", "for truncation."]-- This function ensures that each chunk of text can be tokenized- by the specified model without exceeding the model's token limit.- """-- encoding = tiktoken.encoding_for_model(model)- 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]- for i in range(0, len(encoded_text), max_tokens)]-- result = [encoding.decode(chunk) for chunk in chunks]-- return result---def token_count(text: str, model: str) -> List[str]:- """- Return the number of tokens within the text, based on the encoding of the specified model.-- Args:- text (str): The input text to be counted.- model (str): The name of the llm model to determine the encoding.-- Returns:- int: Number of tokens.-- Example:- >>> token_count("This is a sample text for counting.", "gpt-4o-mini")- 9-- This function ensures that each chunk of text can be tokenized- by the specified model without exceeding the model's token limit.- """-- encoding = tiktoken.encoding_for_model(model)- num_tokens = len(encoding.encode(text))-- return num_tokensdiff --git a/scrapegraphai/utils/tokenizer.py b/scrapegraphai/utils/tokenizer.pynew file mode 100644index 00000000..c946405f--- /dev/null+++ b/scrapegraphai/utils/tokenizer.py@@ -0,0 +1,10 @@+"""+Module for calculting the token_for_openai+"""+import tiktoken++def num_tokens_calculus(string: str) -> int:+ """Returns the number of tokens in a text string."""+ encoding = tiktoken.get_encoding("cl100k_base")+ num_tokens = len(encoding.encode(string))+ return num_tokens