Files
wehub-resource-sync fbfefa28d3
CodeQL / Analyze (python) (push) Failing after 0s
Release / Build (push) Failing after 1s
Test Suite / Unit Tests (push) Failing after 0s
Release / Release (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:18:10 +08:00

31 lines
870 B
Python

"""
Tokenization utilities for Ollama models
"""
from langchain_core.language_models.chat_models import BaseChatModel
from ..logging import get_logger
def num_tokens_ollama(text: str, llm_model: BaseChatModel) -> int:
"""
Estimate the number of tokens in a given text using Ollama's tokenization method,
adjusted for different Ollama models.
Args:
text (str): The text to be tokenized and counted.
llm_model (BaseChatModel): The specific Ollama model to adjust tokenization.
Returns:
int: The number of tokens in the text.
"""
logger = get_logger()
logger.debug(f"Counting tokens for text of {len(text)} characters")
# Use langchain token count implementation
# NB: https://github.com/ollama/ollama/issues/1716#issuecomment-2074265507
tokens = llm_model.get_num_tokens(text)
return tokens