(input_file, size="small", model="text-embedding-3-small", num_to_process=10000000)
| 40 | return text.encode('utf-8', errors='ignore').decode('utf-8') |
| 41 | |
| 42 | def process_files(input_file, size="small", model="text-embedding-3-small", num_to_process=10000000): |
| 43 | num_done = 0 |
| 44 | if (size == "small"): |
| 45 | output_path = EMBEDDINGS_PATH_SMALL + "/" + input_file + ".txt" |
| 46 | model = EMBEDDING_MODEL_SMALL |
| 47 | elif (size == "compact"): |
| 48 | output_path = EMBEDDINGS_PATH_COMPACT + "/" + input_file + ".txt" |
| 49 | model = EMBEDDING_MODEL_SMALL |
| 50 | else: |
| 51 | output_path = EMBEDDINGS_PATH_LARGE + "/" + input_file + ".txt" |
| 52 | model = EMBEDDING_MODEL_LARGE |
| 53 | |
| 54 | input_path = JSONL_PATH + input_file + "_schemas.txt" |
| 55 | |
| 56 | if (size == "compact"): |
| 57 | input_path = JSONL_PATH_COMPACT + input_file + "_schemas.txt" |
| 58 | |
| 59 | try: |
| 60 | with open(input_path) as input_file, \ |
| 61 | open(output_path, 'w', encoding='utf-8') as output_file: |
| 62 | |
| 63 | batch = [] |
| 64 | batch_urls = [] |
| 65 | batch_jsons = [] |
| 66 | |
| 67 | for line in input_file: |
| 68 | # Skip empty lines |
| 69 | if not line.strip(): |
| 70 | continue |
| 71 | |
| 72 | line = clean_utf8(line) |
| 73 | try: |
| 74 | # Split line by tab |
| 75 | url, json_str = line.strip().split('\t') |
| 76 | |
| 77 | batch_urls.append(url) |
| 78 | batch_jsons.append(json_str) |
| 79 | batch.append(json_str[0:6000]) |
| 80 | num_done += 1 |
| 81 | # Process batch when it reaches size 100 |
| 82 | if len(batch) == 100 or (num_done > num_to_process): |
| 83 | # Get embeddings for the batch |
| 84 | embeddings = client.embeddings.create(input=batch, model=model).data |
| 85 | |
| 86 | # Write results for the batch |
| 87 | for i in range(len(batch)): |
| 88 | output_file.write(f"{batch_urls[i]}\t{batch_jsons[i]}\t{embeddings[i].embedding}\n") |
| 89 | print(f"Processed {num_done} lines") |
| 90 | # Clear the batches |
| 91 | batch = [] |
| 92 | batch_urls = [] |
| 93 | batch_jsons = [] |
| 94 | time.sleep(5) |
| 95 | except Exception as e: |
| 96 | print(f"Error processing line: {e!s}") |
| 97 | continue |
| 98 | if num_done > num_to_process: |
| 99 | break |
no test coverage detected