Skip to content

Python scraper based on AI

add chunking functionn

Marco Vinciguerra380174d49033
diff --git a/scrapegraphai/nodes/parse_node.py b/scrapegraphai/nodes/parse_node.py
index a28b2e86..4a8638e7 100644
--- a/scrapegraphai/nodes/parse_node.py
+++ b/scrapegraphai/nodes/parse_node.py
@@ -6,6 +6,7 @@ from semchunk import chunk
from langchain_community.document_transformers import Html2TextTransformer
from langchain_core.documents import Document
from .base_node import BaseNode
+from tokenizer import num_tokens_calculus
class ParseNode(BaseNode):
"""
diff --git a/scrapegraphai/utils/__init__.py b/scrapegraphai/utils/__init__.py
index 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_import
from .cleanup_html import cleanup_html
from .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_calculus
diff --git a/scrapegraphai/utils/token_calculator.py b/scrapegraphai/utils/token_calculator.py
deleted file mode 100644
index 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_tokens
diff --git a/scrapegraphai/utils/tokenizer.py b/scrapegraphai/utils/tokenizer.py
new file mode 100644
index 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