Send a batch of vectors to the server via POST /bulk_insert. Args: server_url: Base URL of the server (e.g. http://127.0.0.1:8080). batch: List of vector dicts ({"id": int, "vector": [float, ...]}). Returns: Number of vectors inserted as reported by the server.
(server_url, batch)
| 77 | |
| 78 | |
| 79 | def send_batch(server_url, batch): |
| 80 | """Send a batch of vectors to the server via POST /bulk_insert. |
| 81 | |
| 82 | Args: |
| 83 | server_url: Base URL of the server (e.g. http://127.0.0.1:8080). |
| 84 | batch: List of vector dicts ({"id": int, "vector": [float, ...]}). |
| 85 | |
| 86 | Returns: |
| 87 | Number of vectors inserted as reported by the server. |
| 88 | |
| 89 | Raises: |
| 90 | RuntimeError: If the server returns an error or unexpected response. |
| 91 | """ |
| 92 | url = f"{server_url.rstrip('/')}/bulk_insert" |
| 93 | payload = json.dumps({"vectors": batch}).encode("utf-8") |
| 94 | |
| 95 | req = urllib.request.Request( |
| 96 | url, |
| 97 | data=payload, |
| 98 | headers={"Content-Type": "application/json"}, |
| 99 | method="POST", |
| 100 | ) |
| 101 | |
| 102 | try: |
| 103 | with urllib.request.urlopen(req) as resp: |
| 104 | body = json.loads(resp.read().decode("utf-8")) |
| 105 | except urllib.error.HTTPError as e: |
| 106 | error_body = e.read().decode("utf-8", errors="replace") |
| 107 | raise RuntimeError( |
| 108 | f"Server returned HTTP {e.code}: {error_body}" |
| 109 | ) from e |
| 110 | except urllib.error.URLError as e: |
| 111 | raise RuntimeError( |
| 112 | f"Cannot connect to {url}: {e.reason}" |
| 113 | ) from e |
| 114 | |
| 115 | if body.get("status") != "ok": |
| 116 | raise RuntimeError(f"Unexpected response from server: {body}") |
| 117 | |
| 118 | return body.get("inserted", len(batch)) |
| 119 | |
| 120 | |
| 121 | def load_data(server_url, data_dir, batch_size): |