I have this script to count for tokens in a llm dataset. I want to remove any examples…
- User
I have this script to count for tokens in a llm dataset. I want to remove any examples that are >2048 tokens whenever it finds them in the dataset, and make a new dataset without them. How can I adapt this script to do so: import json, tiktoken
encoding = tiktoken.get_encoding("cl100k_base")
Add your custom function here
def custom_function(combined_string): return len(encoding.encode(combined_string))
def process_json_file(file_name): with open(file_name, 'r', encoding='utf-8') as f: data = json.load(f)
codetotal = 0 max_int = None count = 0 count_2048 = 0 count_4000 = 0 count_1000 = 0 for entry in data: combined_string = entry['instruction'] + entry['input'] + entry['output'] token_count = len(encoding.encode(combined_string)) result = custom_function(combined_string) total += result count += 1 if max_int is None or result > max_int: max_int = result if token_count > 2048: count_2048 += 1 if token_count > 4000: count_4000 += 1 if token_count > 1000: count_1000 += 1 average = total / count return { "average": average, "total": total, "max_int": max_int, "count_1000": count_1000, "count_2048": count_2048, "count_4000": count_4000 }if name == "main": file_name = "hermes-old.json" result = process_json_file(file_name) print(result)
- Assistant
In order to create a new dataset without examples that have more than 2048 tokens, you can modify the
process_json_filefunction to generate a new list of entries, where each entry has a token count of less than or equal to 2048. You can then write this list back to a new JSON file. Here is an adaptation of your script that implements this:python