INNER CODE UNIT · Python
count_tokens
CassiopeiaCode/q2api · app.py:46
def count_tokens(text: str, apply_multiplier: bool = False) -> int:
"""Counts tokens with tiktoken."""
if not text or not ENCODING:
return 0
token_count = len(ENCODING.encode(text))
if apply_multiplier:
token_count = int(token_count * TOKEN_COUNT_MULTIPLIER)
return token_count
# ------------------------------------------------------------------------------
# Bootstrap
# ------------------------------------------------------------------------------
BASE_DIR = Path(__file__).resolve().parent
load_dotenv(BASE_DIR / ".env")
app = FastAPI(title="v2 OpenAI-compatible Server (Amazon Q Backend)")