Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TablestoreVectorStore check the Dimension of the embedding when writing it to store. #17321

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class TablestoreVectorStore(BasePydanticVectorStore):

is_embedding_query: bool = True
stores_text: bool = True
_vector_dimension: int = PrivateAttr(default=512)
_logger: Any = PrivateAttr(default=None)
_tablestore_client: tablestore.OTSClient = PrivateAttr(default=None)
_table_name: str = PrivateAttr(default="llama_index_vector_store_ots_v1")
Expand Down Expand Up @@ -91,6 +92,7 @@ def __init__(
)
else:
self._tablestore_client = tablestore_client
self._vector_dimension = vector_dimension
self._table_name = table_name
self._index_name = index_name
self._text_field = text_field
Expand Down Expand Up @@ -632,6 +634,11 @@ def add(self, nodes: List[BaseNode], **kwargs: Any) -> List[str]:
return []
ids = []
for node in nodes:
if len(node.get_embedding()) != self._vector_dimension:
raise RuntimeError(
"node embedding size:%d is not the same as vector store dim:%d"
% (len(node.get_embedding()), self._vector_dimension)
)
self._write_row(
row_id=node.node_id,
content=node.text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,27 @@ def test_tablestore() -> None:
assert query_result.ids is not None
assert query_result.similarities is not None
assert query_result.similarities is not None


def test_tablestore_add_dim() -> None:
store = TablestoreVectorStore(
endpoint="http://test.a.com",
instance_name="test",
access_key_id="test",
access_key_secret="test",
vector_dimension=512,
vector_metric_type=tablestore.VectorMetricType.VM_COSINE,
)
embedder = MockEmbedding(128)
node = TextNode(
id_="1",
text="hello world",
metadata={"type": "a", "time": 1995},
)
node.embedding = embedder.get_text_embedding(node.get_text())

try:
store.add([node])
raise RuntimeError("should failed")
except Exception as e:
assert "not the same as" in e.args[0]
Loading