code-chunker / utils.py
CintraAI's picture
Add code chunking functionality
b7b387e
raw
history blame contribute delete
356 Bytes
import tiktoken
import json
def count_tokens(string: str, encoding_name: str) -> int:
"""Returns the number of tokens in a text string."""
encoding = tiktoken.encoding_for_model(encoding_name)
num_tokens = len(encoding.encode(string))
return num_tokens
def load_json(json_file):
with open(json_file) as f:
return json.load(f)