Skip to content

Commit

Permalink
Merge pull request #95 from supabase/or/add-l1-distance
Browse files Browse the repository at this point in the history
Adds l1 distance, added on 0.7.0
  • Loading branch information
olirice authored Oct 9, 2024
2 parents 668249d + 192177d commit c893d56
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11']
postgres-version: ['15.1.0.118']
postgres-version: ['15.1.1.78']

services:

Expand Down
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Available options for distance `measure` are:

- `vecs.IndexMeasure.cosine_distance`
- `vecs.IndexMeasure.l2_distance`
- `vecs.IndexMeasure.l1_distance`
- `vecs.IndexMeasure.max_inner_product`

which correspond to different methods for comparing query vectors to the vectors in the database.
Expand Down
1 change: 1 addition & 0 deletions docs/concepts_indexes.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Available options for distance `measure` are:

- `vecs.IndexMeasure.cosine_distance`
- `vecs.IndexMeasure.l2_distance`
- `vecs.IndexMeasure.l1_distance`
- `vecs.IndexMeasure.max_inner_product`

which correspond to different methods for comparing query vectors to the vectors in the database.
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Vecs is a Python client library for managing and querying vector stores in Postg
## Overview

- Vector Management: create collections to persist and update vectors in a PostgreSQL database.
- Querying: Query vectors efficiently using measures such as cosine distance, l2 distance, or max inner product.
- Querying: Query vectors efficiently using measures such as cosine distance, l2 distance, l1 distance, or max inner product.
- Metadata: Each vector can have associated metadata, which can also be used as filters during queries.
- Hybrid Data: vecs creates its own schema and can coexist with your existing relational data

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def read_package_variable(key, filename="__init__.py"):
long_description = (Path(__file__).parent / "README.md").read_text()

REQUIRES = [
"pgvector==0.1.*",
"pgvector==0.3.*",
"sqlalchemy==2.*",
"psycopg2-binary==2.9.*",
"flupy==1.*",
Expand Down
2 changes: 1 addition & 1 deletion src/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def maybe_start_pg() -> Generator[None, None, None]:
to using the PYTEST_DB connection string"""

container_name = "vecs_pg"
image = "supabase/postgres:15.1.0.118"
image = "supabase/postgres:15.1.1.78"

connection_template = "postgresql://{user}:{pw}@{host}:{port:d}/{db}"
conn_args = parse(connection_template, PYTEST_DB)
Expand Down
13 changes: 13 additions & 0 deletions src/tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,19 @@ def test_l2_index_query(client: vecs.Client) -> None:
assert len(results) == 1


def test_l1_index_query(client: vecs.Client) -> None:
dim = 4
bar = client.get_or_create_collection(name="bar", dimension=dim)
bar.upsert([("a", [1, 2, 3, 4], {})])
bar.create_index(measure=vecs.IndexMeasure.l1_distance)
results = bar.query(
data=[1, 2, 3, 4],
limit=1,
measure="l1_distance",
)
assert len(results) == 1


def test_max_inner_product_index_query(client: vecs.Client) -> None:
dim = 4
bar = client.get_or_create_collection(name="bar", dimension=dim)
Expand Down
2 changes: 1 addition & 1 deletion src/vecs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
)

__project__ = "vecs"
__version__ = "0.4.4"
__version__ = "0.4.5"


__all__ = [
Expand Down
3 changes: 3 additions & 0 deletions src/vecs/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class IndexMeasure(str, Enum):
cosine_distance = "cosine_distance"
l2_distance = "l2_distance"
max_inner_product = "max_inner_product"
l1_distance = "l1_distance"


@dataclass
Expand Down Expand Up @@ -124,12 +125,14 @@ class IndexArgsHNSW:
IndexMeasure.cosine_distance: "vector_cosine_ops",
IndexMeasure.l2_distance: "vector_l2_ops",
IndexMeasure.max_inner_product: "vector_ip_ops",
IndexMeasure.l1_distance: "vector_l1_ops",
}

INDEX_MEASURE_TO_SQLA_ACC = {
IndexMeasure.cosine_distance: lambda x: x.cosine_distance,
IndexMeasure.l2_distance: lambda x: x.l2_distance,
IndexMeasure.max_inner_product: lambda x: x.max_inner_product,
IndexMeasure.l1_distance: lambda x: x.l1_distance,
}


Expand Down

0 comments on commit c893d56

Please sign in to comment.