Protect for empty description

This commit is contained in:
Ori Kotek
2023-08-10 02:08:36 +03:00
parent e00500b90c
commit b206b1c5ff

View File

@ -298,12 +298,16 @@ def clip_tokens(text: str, max_tokens: int) -> str:
str: The clipped string. str: The clipped string.
""" """
# We'll estimate the number of tokens by hueristically assuming 2.5 tokens per word # We'll estimate the number of tokens by hueristically assuming 2.5 tokens per word
encoder = get_token_encoder() try:
num_input_tokens = len(encoder.encode(text)) encoder = get_token_encoder()
if num_input_tokens <= max_tokens: num_input_tokens = len(encoder.encode(text))
return text if num_input_tokens <= max_tokens:
num_chars = len(text) return text
chars_per_token = num_chars / num_input_tokens num_chars = len(text)
num_output_chars = int(chars_per_token * max_tokens) chars_per_token = num_chars / num_input_tokens
clipped_text = text[:num_output_chars] num_output_chars = int(chars_per_token * max_tokens)
return clipped_text clipped_text = text[:num_output_chars]
return clipped_text
except Exception as e:
logging.warning(f"Failed to clip tokens: {e}")
return text