#Count tokens

1 messages · Page 1 of 1 (latest)

sweet geyser
#

In a TypeScript environment, what is the most correct and performant way to calculate the number of tokens in a string?

frozen inletBOT
#

Hi @sweet geyser. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

I'm sorry, but the knowledge sources provided do not contain information on how to count tokens in a TypeScript environment. The sources mainly discuss token counting in Python using OpenAI's tiktoken library.

For Python, you can use the tiktoken library as follows:

import tiktoken

def num_tokens_from_string(string: str, encoding_name: str) -> int:
 """Returns the number of tokens in a text string."""
 encoding = tiktoken.get_encoding(encoding_name)
 num_tokens = len(encoding.encode(string))
 return num_tokens

num_tokens_from_string("tiktoken is great!", "cl100k_base")

(Source: OpenAI Cookbook (https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken#3-turn-text-into-tokens-with-encodingencode))

For TypeScript or JavaScript, you might need to use a different library or method, but I don't have that information based on the provided sources. I recommend looking into NLP libraries for JavaScript/TypeScript that can perform tokenization.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: