Skip to content

Fix tokenizer loading for GPT2 #757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions scrapegraphai/utils/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from langchain_ollama import ChatOllama
from langchain_mistralai import ChatMistralAI
from langchain_core.language_models.chat_models import BaseChatModel
from transformers import GPT2TokenizerFast

def num_tokens_calculus(string: str, llm_model: BaseChatModel) -> int:
"""
Expand All @@ -23,6 +24,13 @@ def num_tokens_calculus(string: str, llm_model: BaseChatModel) -> int:
from .tokenizers.tokenizer_ollama import num_tokens_ollama
num_tokens_fn = num_tokens_ollama

elif isinstance(llm_model, GPT2TokenizerFast):
def num_tokens_gpt2(text: str, model: BaseChatModel) -> int:
tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
tokens = tokenizer.encode(text)
return len(tokens)
num_tokens_fn = num_tokens_gpt2

else:
from .tokenizers.tokenizer_openai import num_tokens_openai
num_tokens_fn = num_tokens_openai
Expand Down
7 changes: 6 additions & 1 deletion scrapegraphai/utils/tokenizers/tokenizer_ollama.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never seen before thank you

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
from langchain_core.language_models.chat_models import BaseChatModel
from ..logging import get_logger
from transformers import GPT2TokenizerFast

def num_tokens_ollama(text: str, llm_model:BaseChatModel) -> int:
"""
Expand All @@ -21,8 +22,12 @@ def num_tokens_ollama(text: str, llm_model:BaseChatModel) -> int:

logger.debug(f"Counting tokens for text of {len(text)} characters")

if isinstance(llm_model, GPT2TokenizerFast):
tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
tokens = tokenizer.encode(text)
return len(tokens)

# Use langchain token count implementation
# NB: https://github.com/ollama/ollama/issues/1716#issuecomment-2074265507
tokens = llm_model.get_num_tokens(text)
return tokens

9 changes: 9 additions & 0 deletions tests/graphs/smart_scraper_ollama_test.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for the test

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import pytest
from scrapegraphai.graphs import SmartScraperGraph
from transformers import GPT2TokenizerFast


@pytest.fixture
Expand Down Expand Up @@ -50,3 +51,11 @@ def test_get_execution_info(graph_config: dict):
graph_exec_info = smart_scraper_graph.get_execution_info()

assert graph_exec_info is not None


def test_gpt2_tokenizer_loading():
"""
Test loading of GPT2TokenizerFast
"""
tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
assert tokenizer is not None
Loading