Skip to content

Commit

Permalink
Merge pull request #2 from turbopuffer/fix_query_bug
Browse files Browse the repository at this point in the history
Fix assert being hit when querying without vector values
  • Loading branch information
xthexder committed Dec 21, 2023
2 parents 37db05c + 9ec01a9 commit 16d5ef8
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
9 changes: 9 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Release Build Instructions

To build and publish the PyPI package:

1. `python -m pip install --upgrade build`
1. `python -m pip install --upgrade twine`
1. `rm -rf dist/`
2. `python -m build`
3. `python -m twine upload dist/*`
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "turbopuffer"
version = "0.1.1"
version = "0.1.2"
description = "Python Client for accessing the turbopuffer API"
authors = ["turbopuffer Inc. <info@turbopuffer.com>"]
maintainers = ["Jacob Wirth"]
Expand Down
26 changes: 23 additions & 3 deletions tests/test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_delete_vectors():
ns.upsert([tpuf.VectorRow(id=2)])
assert False, "Upserting to delete should not be allowed"
except ValueError as err:
assert err.args == ('VectorRow.vector cannot be None, use Namespace.delete([ids...]) instead.',)
assert err.args == ('upsert() call would result in a vector deletion, use Namespace.delete([ids...]) instead.',)

try:
ns.upsert([tpuf.VectorRow(id=2, dist=5)])
Expand All @@ -77,7 +77,7 @@ def test_delete_vectors():
ns.upsert([{'id': 6}])
assert False, "Upserting to delete should not be allowed"
except ValueError as err:
assert err.args == ('VectorRow.vector cannot be None, use Namespace.delete([ids...]) instead.',)
assert err.args == ('upsert() call would result in a vector deletion, use Namespace.delete([ids...]) instead.',)

# Test delete single row
ns.delete(2)
Expand Down Expand Up @@ -155,11 +155,15 @@ def check_result(row, expected):
assert row.id == expected.id
assert row.attributes == expected.attributes
if isinstance(expected.vector, list):
assert isinstance(row.vector, list)
assert abs(row.vector[0] - expected.vector[0]) < 0.000001
assert abs(row.vector[1] - expected.vector[1]) < 0.000001
else:
assert row.vector == expected.vector
assert abs(row.dist - expected.dist) < 0.000001
if isinstance(expected.dist, float):
assert abs(row.dist - expected.dist) < 0.000001
else:
assert row.dist == expected.dist

expected = [
tpuf.VectorRow(id=7, vector=[0.7, 0.7], attributes={'hello': 'world'}, dist=0.01),
Expand Down Expand Up @@ -209,6 +213,22 @@ def check_result(row, expected):
check_result(row, expected[i])
i += 1

# Test query with no vectors
expected = [
tpuf.VectorRow(id=10),
tpuf.VectorRow(id=11),
tpuf.VectorRow(id=12),
]
vector_set = ns.query(
top_k=3,
include_vectors=False,
filters={
'id': [['In', [10, 11, 12]]]
},
)
for i in range(len(vector_set)): # Use VectorResult in index mode
check_result(vector_set[i], expected[i])

def test_list_vectors():
ns = tpuf.Namespace('client_test')

Expand Down
7 changes: 7 additions & 0 deletions turbopuffer/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class Namespace:
backend: Backend

def __init__(self, name: str, api_key: Optional[str] = None):
"""
Creates a new turbopuffer.Namespace object for querying the turbopuffer API.
This function does not make any API calls on its own.
Specifying an api_key here will override the global configuration for API calls to this namespace.
"""
self.name = name
self.backend = Backend(api_key)

Expand Down
4 changes: 1 addition & 3 deletions turbopuffer/vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ class _(JSONSerializable.Meta):
def __post_init__(self):
if not isinstance(self.id, int):
raise ValueError('VectorRow.id must be an int, got:', type(self.id))
if self.vector is None and self.dist is None:
raise ValueError('VectorRow.vector cannot be None, use Namespace.delete([ids...]) instead.')
if not isinstance(self.vector, list) and self.dist is None:
if self.vector is not None and not isinstance(self.vector, list):
raise ValueError('VectorRow.vector must be a list, got:', type(self.vector))
if self.attributes is not None and not isinstance(self.attributes, dict):
raise ValueError('VectorRow.attributes must be a dict, got:', type(self.attributes))
Expand Down
2 changes: 1 addition & 1 deletion turbopuffer/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '0.1.1'
VERSION = '0.1.2'

0 comments on commit 16d5ef8

Please sign in to comment.