langchain-ai / langchain-postgres

LangChain abstractions backed by Postgres Backend

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Attribute Error raised when using "$nin" operator in filter

xidian237 opened this issue · comments

Hi Expert,
I notice that we support the $ini operator:
image

But currently an Attribute Error raised in my case:
docs = db.similarity_search(query="this is a doc", k=2, filter={'info': {'$nin': [1, 2, 3]}})
`Traceback (most recent call last):
File "F:\WorkProject\medicines_recommend.venv\Lib\site-packages\sqlalchemy\sql\elements.py", line 1496, in getattr
return getattr(self.comparator, key)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Comparator' object has no attribute 'nin_'. Did you mean: 'in_'?

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "F:\WorkProject\medicines_recommend.venv\Lib\site-packages\langchain_postgres\vectorstores.py", line 909, in __query_collection
filter_clauses = self._create_filter_clause(filter)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "F:\WorkProject\medicines_recommend.venv\Lib\site-packages\langchain_postgres\vectorstores.py", line 835, in create_filter_clause
return self.handle_field_filter(key, filters[key])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "F:\WorkProject\medicines_recommend.venv\Lib\site-packages\langchain_postgres\vectorstores.py", line 705, in handle_field_filter
return queried_field.nin
([str(val) for val in filter_value])
^^^^^^^^^^^^^^^^^^
File "F:\WorkProject\medicines_recommend.venv\Lib\site-packages\sqlalchemy\sql\elements.py", line 1498, in getattr
raise AttributeError(
AttributeError: Neither 'BinaryExpression' object nor 'Comparator' object has an attribute 'nin
'. Did you mean: 'in
'?
python-BaseException`

Actually, I find here is the entry interface:
elif operator in {"$nin"}: return queried_field.nin_([str(val) for val in filter_value])
but there is no "nin_" in .venv/Lib/site-packages/sqlalchemy/sql/operators.py, however, they provide the "not_in"
def not_in(self, other: Any) -> ColumnOperators:

Python: 3.11.9
langchain-postgres 0.0.3
pgvector 0.2.5

Address by this PR #40