2023-09-12 18:15:17 +00:00
|
|
|
(embeddings-storage)=
|
2023-09-12 18:04:45 +00:00
|
|
|
# Embedding storage format
|
2023-08-28 05:24:10 +00:00
|
|
|
|
|
|
|
|
The default output format of the `llm embed` command is a JSON array of floating point numbers.
|
|
|
|
|
|
2023-09-12 18:04:45 +00:00
|
|
|
LLM stores embeddings in space-efficient format: a little-endian binary sequences of 32-bit floating point numbers, each represented using 4 bytes.
|
2023-08-28 05:24:10 +00:00
|
|
|
|
2023-09-12 18:04:45 +00:00
|
|
|
These are stored in a `BLOB` column in a SQLite database.
|
|
|
|
|
|
|
|
|
|
The following Python functions can be used to convert between this format and an array of floating point numbers:
|
2023-08-28 05:24:10 +00:00
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
import struct
|
|
|
|
|
|
|
|
|
|
def encode(values):
|
|
|
|
|
return struct.pack("<" + "f" * len(values), *values)
|
|
|
|
|
|
|
|
|
|
def decode(binary):
|
|
|
|
|
return struct.unpack("<" + "f" * (len(binary) // 4), binary)
|
|
|
|
|
```
|
2023-09-12 18:32:12 +00:00
|
|
|
|
|
|
|
|
These functions are available as `llm.encode()` and `llm.decode()`.
|
2023-09-14 21:01:27 +00:00
|
|
|
|
|
|
|
|
If you are using [NumPy](https://numpy.org/) you can decode one of these binary values like this:
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
numpy_array = np.frombuffer(value, "<f4")
|
|
|
|
|
```
|
|
|
|
|
The `<f4` format string here ensures NumPy will treat the data as a little-endian sequence of 32-bit floats.
|