diff --git a/llama-index-integrations/vector_stores/llama-index-vector-stores-tablestore/llama_index/vector_stores/tablestore/base.py b/llama-index-integrations/vector_stores/llama-index-vector-stores-tablestore/llama_index/vector_stores/tablestore/base.py index e8eac6f35d030..e058a82235bc3 100644 --- a/llama-index-integrations/vector_stores/llama-index-vector-stores-tablestore/llama_index/vector_stores/tablestore/base.py +++ b/llama-index-integrations/vector_stores/llama-index-vector-stores-tablestore/llama_index/vector_stores/tablestore/base.py @@ -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") @@ -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 @@ -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, diff --git a/llama-index-integrations/vector_stores/llama-index-vector-stores-tablestore/tests/test_vector_stores_tablestore.py b/llama-index-integrations/vector_stores/llama-index-vector-stores-tablestore/tests/test_vector_stores_tablestore.py index 7e3abe2d70365..c01d401607e78 100644 --- a/llama-index-integrations/vector_stores/llama-index-vector-stores-tablestore/tests/test_vector_stores_tablestore.py +++ b/llama-index-integrations/vector_stores/llama-index-vector-stores-tablestore/tests/test_vector_stores_tablestore.py @@ -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]