Skip to content

Commit

Permalink
community: tablestore vector store check the dimension of the embeddi…
Browse files Browse the repository at this point in the history
…ng when writing it to store. (#28812)

Added some restrictions to a vectorstore I released in the community
before.
  • Loading branch information
xjtushilei authored Dec 19, 2024
1 parent 024f020 commit 97f1e1d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
7 changes: 4 additions & 3 deletions docs/docs/integrations/vectorstores/tablestore.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
}
},
"source": [
"# TablestoreVectorStore\n",
"# Tablestore\n",
"\n",
"> [Tablestore](https://www.aliyun.com/product/ots) is a fully managed NoSQL cloud database service that enables storage of a massive amount of structured\n",
"and semi-structured data.\n",
"[Tablestore](https://www.aliyun.com/product/ots) is a fully managed NoSQL cloud database service.\n",
"\n",
"Tablestore enables storage of a massive amount of structured and semi-structured data.\n",
"\n",
"This notebook shows how to use functionality related to the `Tablestore` vector database.\n",
"\n",
Expand Down
5 changes: 5 additions & 0 deletions libs/community/langchain_community/vectorstores/tablestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,11 @@ def add_texts(
row_id = ids[i]
text = text_list[i]
embedding_vector = embeddings[i]
if len(embedding_vector) != self.__vector_dimension:
raise RuntimeError(
"embedding vector size:%d is not the same as vector store dim:%d"
% (len(embedding_vector), self.__vector_dimension)
)
metadata = dict()
if metadatas and metadatas[i]:
metadata = metadatas[i]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,26 @@ def test_tablestore() -> None:
"""
search_result = store.similarity_search_with_score(query="hello world", k=2)
assert len(search_result) == 2


def test_tablestore_add_documents() -> None:
embeddings = FakeEmbeddings(size=128)
store = TablestoreVectorStore(
embedding=embeddings,
endpoint="http://test.a.com",
instance_name="test",
access_key_id="test",
access_key_secret="test",
vector_dimension=512,
)
doc = Document(
id="1",
page_content="1 hello world",
metadata={"type": "pc", "time": 2000},
)

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

0 comments on commit 97f1e1d

Please sign in to comment.