Fix: float transfer exception. (#6197)

### What problem does this PR solve?

#6177

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
Kevin Hu
2025-03-18 11:13:44 +08:00
committed by GitHub
parent 222a2c8fa5
commit 1333d3c02a
5 changed files with 27 additions and 15 deletions

View File

@ -19,6 +19,7 @@ import re
import tiktoken
from api.utils.file_utils import get_project_base_directory
def singleton(cls, *args, **kw):
instances = {}
@ -89,3 +90,12 @@ def num_tokens_from_string(string: str) -> int:
def truncate(string: str, max_len: int) -> str:
"""Returns truncated text if the length of text exceed max_len."""
return encoder.decode(encoder.encode(string)[:max_len])
def get_float(v: str | None):
if v is None:
return float('-inf')
try:
return float(v)
except Exception:
return float('-inf')