Fixes scope of query Session in PGVector (#5194)

`vectorstore.PGVector`: The transactional boundary should be increased
to cover the query itself

Currently, within the `similarity_search_with_score_by_vector` the
transactional boundary (created via the `Session` call) does not include
the select query being made.

This can result in un-intended consequences when interacting with the
PGVector instance methods directly


---------

Co-authored-by: Dev 2049 <dev.dev2049@gmail.com>
This commit is contained in:
Matt Wells 2023-05-24 18:37:45 +01:00 committed by GitHub
parent 52714cedd4
commit c173bf1c62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -298,15 +298,17 @@ class PGVector(VectorStore):
for key, value in filter.items():
IN = "in"
if isinstance(value, dict) and IN in map(str.lower, value):
value_case_insensitive = {k.lower(): v for k, v in value.items()}
value_case_insensitive = {
k.lower(): v for k, v in value.items()
}
filter_by_metadata = EmbeddingStore.cmetadata[key].astext.in_(
value_case_insensitive[IN]
)
filter_clauses.append(filter_by_metadata)
else:
filter_by_metadata = EmbeddingStore.cmetadata[key].astext == str(
value
)
filter_by_metadata = EmbeddingStore.cmetadata[
key
].astext == str(value)
filter_clauses.append(filter_by_metadata)
filter_by = sqlalchemy.and_(filter_by, *filter_clauses)
@ -325,6 +327,7 @@ class PGVector(VectorStore):
.limit(k)
.all()
)
docs = [
(
Document(