# Embeddings API का base64 जवाब: 8192 अक्षरों को Float32Array में सही बदलें (और Node.js Buffer pool वाला जाल)

OpenAI के embeddings endpoint पर `encoding_format: "base64"` भेजेंगे तो `embedding` फ़ील्ड में floats की लंबी array नहीं आती, एक अकेली string आती है। पहली बार देखने पर वह कुछ ऐसी लगती है: `8IVJPAAAAL8AAIA/...`। उसे `JSON.parse` करने या text की तरह decode करने से काम नहीं चलेगा। यह string असल में **float32 संख्याओं के कच्चे bytes** हैं, जिन्हें Base64 में लपेटा गया है। इस लेख में हम bytes के स्तर पर देखेंगे कि अंदर क्या है, और Python और JavaScript में इसे बिना गलती के कैसे खोलें।

## यह फ़ॉर्मैट कहाँ से आता है

- OpenAI गाइड के अनुसार `text-embedding-3-small` vector की डिफ़ॉल्ट लंबाई **1536** है और `text-embedding-3-large` की **3072**। `dimensions` पैरामीटर से इसे छोटा किया जा सकता है। ([Vector embeddings guide](https://platform.openai.com/docs/guides/embeddings), 1 दिसंबर 2026 को देखा गया)
- आधिकारिक `openai-python` SDK में `encoding_format` न दें, तो SDK ख़ुद request में `"base64"` भेजता है। जवाब आने पर वह string को `base64.b64decode` करता है और numpy हो तो `np.frombuffer(..., dtype="float32")` से, वरना `array.array("f", ...)` से वापस list बना देता है। ([`src/openai/lib/_parsing/_embeddings.py`](https://github.com/openai/openai-python/blob/main/src/openai/lib/_parsing/_embeddings.py), main branch, 1 दिसंबर 2026 को पढ़ा गया)

यानी SDK यह decoding आपकी नज़र से छिपाकर कर देता है। दिक्कत तब शुरू होती है जब आप raw HTTP (`fetch`, `curl`, किसी दूसरी भाषा का client) से वही base64 माँगते हैं।

## Size का हिसाब: 1536 → 6144 bytes → 8192 अक्षर

हर float32 = 4 bytes, और Base64 हर 3 bytes को 4 अक्षरों में लिखता है:

| dimensions | bytes (×4) | Base64 लंबाई = 4 × ⌈bytes/3⌉ |
|---|---|---|
| 256 | 1024 | 1368 |
| 1536 | 6144 | **8192** |
| 3072 | 12288 | 16384 |

6144 को 3 से पूरा भाग जाता है, इसलिए 1536-dim वाली string के अंत में `=` padding नहीं होगी। 256-dim में 1024 mod 3 = 1 बचता है, इसलिए अंत में `==` होगा। इससे एक आसान जाँच मिलती है: **decode के बाद `bytes / 4 == dimensions` होना ही चाहिए।**

तुलना के लिए हमने 1536 *उदाहरण* (random, illustrative) float32 मानों को JSON में लिखा तो 32,659 अक्षर बने, जबकि Base64 में ठीक 8192। असली जवाब की लंबाई संख्याओं पर निर्भर करती है, पर फ़र्क़ का अंदाज़ा इससे लग जाता है।

## Worked example: 4 संख्याएँ, 16 bytes (illustrative)

हमने चार *उदाहरण* मान `[0.0123, -0.5, 1.0, -0.0069]` लिए, उन्हें little-endian float32 में pack किया (Python `struct.pack('<4f', ...)`) और फिर Base64 बनाया:

```
Base64: 8IVJPAAAAL8AAIA/ZRniuw==      (24 अक्षर, 16 bytes)
Hex:    F0 85 49 3C | 00 00 00 BF | 00 00 80 3F | 65 19 E2 BB
```

हर 4 bytes को उल्टे क्रम में पढ़ें (little-endian):

- `3C 49 85 F0` → 0.012299999594688416। float32 में 0.0123 का सबसे नज़दीकी मान यही है।
- `BF 00 00 00` → −0.5
- `3F 80 00 00` → 1.0। इसे याद रख लें, debugging में बहुत काम आता है।
- `BB E2 19 65` → −0.006899999920278788

ये सारे मान हमने लोकल मशीन पर Python 3 और Node.js v24.22.1 दोनों में decode करके मिलाए हैं।

## सही decoding

**Python**

```python
import base64, struct
raw = base64.b64decode(s)
assert len(raw) % 4 == 0
vec = struct.unpack(f"<{len(raw)//4}f", raw)
# या: numpy.frombuffer(raw, dtype="<f4")
```

**Browser / Deno**

```js
const u8 = Uint8Array.from(atob(s), c => c.charCodeAt(0));
const vec = new Float32Array(u8.buffer); // u8 का अपना नया buffer है, offset 0
```

**Node.js: यहाँ जाल है**

```js
const buf = Buffer.from(s, "base64");
// ❌ गलत: new Float32Array(buf.buffer)
// ✅ सही:
const vec = new Float32Array(buf.buffer, buf.byteOffset, buf.length / 4);
```

## चार आम गलतियाँ (हमारे टेस्ट के नतीजों के साथ)

1. **`new Float32Array(buf.buffer)`।** छोटे Buffer अक्सर Node के साझा memory pool से कटते हैं। हमारे टेस्ट (Node v24.22.1) में 16 bytes वाले Buffer का `byteOffset` 8 था और `buf.buffer.byteLength` 65536। नतीजा: 4 की जगह **16384** floats मिले, और पहले दो मान `-562949953421312` और `4.59e-41` जैसे कचरे थे। 1024 bytes (256-dim) वाला Buffer भी इसी pool में से कटा था। हमेशा `byteOffset` और length साथ दें, या पहले `Uint8Array.from(buf)` से कॉपी कर लें।
2. **Offset 4 का गुणज नहीं।** अगर आपने किसी बड़े Buffer का `subarray` लिया है और offset 4 से नहीं बँटता, तो `RangeError: start offset of Float32Array should be a multiple of 4` आता है। ऐसे में `DataView` लें और `getFloat32(i * 4, true)` पढ़ें।
3. **Endianness भूल जाना।** `DataView.getFloat32(i)` का डिफ़ॉल्ट big-endian है। ऊपर के 16 bytes को big-endian में पढ़ने पर `-3.3e29, 2.68e-43, ...` जैसे बेतुके मान निकलते हैं। दूसरा parameter `true` देना न भूलें। Python में `"<f"` लिखकर क्रम साफ़ बताएँ।
4. **float64 मान लेना।** `Float64Array` या `struct "<d"` से पढ़ेंगे तो संख्याएँ आधी रह जाती हैं (16 bytes → 2 मान) और दोनों गलत होते हैं (`-3.05e-05`, `-3.07e-20`)। `len == dimensions` वाली जाँच यह गलती तुरंत पकड़ लेती है।

## टूल से 10 सेकंड में जाँच

1. [Hex ↔ Base64 पेज](https://base64decodetool.com/hi/hex/) खोलें और **Base64 to Hex** मोड चुनें। मोड और इनपुट/आउटपुट के कुछ लेबल अंग्रेज़ी में हैं।
2. **Base64 Input** में embedding string या उसका शुरुआती हिस्सा paste करें। `-`/`_` वाला URL-safe रूप और बिना `=` वाली string भी चलती है।
3. **Hexadecimal Output** में बड़े अक्षरों में, space से अलग bytes दिखेंगे। हर 4 bytes उल्टे क्रम में पढ़ें। `00 00 80 3F` दिखे तो वह 1.0 है।

हमने पेज का JavaScript पढ़कर और वही logic लोकल चलाकर पुष्टि की है कि ऊपर वाली string, उसका URL-safe रूप और बिना padding वाला रूप, तीनों से वही 16 bytes निकलते हैं।

ध्यान दें: embedding में आपके अपने दस्तावेज़ों का अर्थ छिपा होता है। production डेटा कहीं भी paste करने से पहले अपनी कंपनी की नीति देख लें। API key कभी किसी टूल में न डालें। इस लेख की सारी संख्याएँ उदाहरण हैं।

Disclosure: this tool is from the Mind vs Drive team. *(खुलासा: यह टूल Mind vs Drive टीम का है।)*

— The Mind vs Drive team

