(parquet_path: str, bf_csv_path: str)
| 164 | |
| 165 | |
| 166 | def calculate_server_similarities(parquet_path: str, bf_csv_path: str): |
| 167 | df = pl.read_parquet(parquet_path, low_memory=True) |
| 168 | constants = { |
| 169 | "COLLECTION_NAME": os.path.splitext(os.path.basename(parquet_path))[0], |
| 170 | "DIMENSIONS": max(len(emb) for emb in df["emb"]), |
| 171 | "MAX_VAL": 1.0, |
| 172 | "MIN_VAL": -1.0, |
| 173 | "BATCH_SIZE": 500, |
| 174 | "BATCH_COUNT": None, |
| 175 | } |
| 176 | |
| 177 | vectors = [] |
| 178 | df = df[["id", "emb"]] |
| 179 | for v in df.iter_rows(): |
| 180 | vector = {"id": v[0], "values": v[1]} |
| 181 | vectors.append(vector) |
| 182 | constants["TOKEN"] = login() |
| 183 | create_db( |
| 184 | constants["COLLECTION_NAME"], "Embeddings from dataset", constants["DIMENSIONS"] |
| 185 | ) |
| 186 | |
| 187 | def upsert_with_retry(start_idx, retries=20): |
| 188 | for attempt in range(retries): |
| 189 | try: |
| 190 | upsert_in_transaction( |
| 191 | constants["COLLECTION_NAME"], |
| 192 | txn_id, |
| 193 | vectors[start_idx : start_idx + batch_size], |
| 194 | ) |
| 195 | return |
| 196 | except Exception as e: |
| 197 | print( |
| 198 | f"Upsert attempt {attempt + 1} failed for batch starting at index {start_idx}: {e}" |
| 199 | ) |
| 200 | time.sleep(random.uniform(1, 3)) |
| 201 | print( |
| 202 | f"Failed to upsert batch starting at index {start_idx} after {retries} attempts" |
| 203 | ) |
| 204 | |
| 205 | transaction = create_transaction(constants["COLLECTION_NAME"]) |
| 206 | txn_id = transaction["transaction_id"] |
| 207 | |
| 208 | batch_size = constants["BATCH_SIZE"] |
| 209 | with ThreadPoolExecutor() as executor: |
| 210 | futures = [ |
| 211 | executor.submit(upsert_with_retry, i) |
| 212 | for i in range(0, len(vectors), batch_size) |
| 213 | ] |
| 214 | for future in futures: |
| 215 | future.result() |
| 216 | |
| 217 | commit_transaction(constants["COLLECTION_NAME"], txn_id) |
| 218 | print(colored("Vectors upserted successfully!", "green")) |
| 219 | |
| 220 | def search_dataset_vectors(query_id, query_emb): |
| 221 | result = ann_vector(query_id, constants["COLLECTION_NAME"], query_emb) |
| 222 | return result |
| 223 |
no test coverage detected