[Vector Store] Fix function add_texts in TencentVectorDB (#24469)

Regardless of whether `embedding_func` is set or not, the 'text'
attribute of document should be assigned, otherwise the `page_content`
in the document of the final search result will be lost
This commit is contained in:
ZhangShenao 2024-07-22 21:50:22 +08:00 committed by GitHub
parent 7ab82eb8cc
commit 0f6737cbfe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 2 deletions

View File

@ -375,8 +375,7 @@ class TencentVectorDB(VectorStore):
}
if embeddings:
doc_attrs["vector"] = embeddings[id]
else:
doc_attrs["text"] = texts[id]
doc_attrs["text"] = texts[id]
doc_attrs.update(metadata)
doc = self.document.Document(**doc_attrs)
docs.append(doc)

View File

@ -85,3 +85,27 @@ def test_tencent_vector_db_no_drop() -> None:
time.sleep(3)
output = docsearch.similarity_search("foo", k=10)
assert len(output) == 6
def test_tencent_vector_db_add_texts_and_search_with_score() -> None:
"""Test add texts to a new-created db and search with score."""
texts = ["foo", "bar", "baz"]
metadatas = [{"page": i} for i in range(len(texts))]
conn_params = ConnectionParams(
url="http://10.0.X.X",
key="eC4bLRy2va******************************",
username="root",
timeout=20,
)
docsearch = TencentVectorDB(
embedding=FakeEmbeddings(),
connection_params=conn_params,
)
docsearch.add_texts(texts, metadatas)
output = docsearch.similarity_search_with_score("foo", k=3)
docs = [o[0] for o in output]
assert docs == [
Document(page_content="foo", metadata={"page": 0}),
Document(page_content="bar", metadata={"page": 1}),
Document(page_content="baz", metadata={"page": 2}),
]