langchain-ai / langchain-postgres

LangChain abstractions backed by Postgres Backend
MIT License
66 stars 22 forks source link

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

Closed xidian237 closed 1 month ago

xidian237 commented 1 month ago

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\medicinesrecommend.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 queriedfield.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:

xidian237 commented 1 month ago

Python: 3.11.9 langchain-postgres 0.0.3 pgvector 0.2.5

eyurtsev commented 1 month ago

Address by this PR https://github.com/langchain-ai/langchain-postgres/pull/40