From 565c02173045e09d6afc42ec12643bdea3c811c2 Mon Sep 17 00:00:00 2001 From: Eddie Cohen <31940792+edcohen08@users.noreply.github.com> Date: Sun, 3 Sep 2023 17:45:11 -0400 Subject: [PATCH] Add ne comparator (#10006) Description: Adds the not comparator and operator to pinecone, chroma and deeplake. Issue: Not a registered issue but when using a selfqueryretriever with pinecone I got this error + stacktrace when I entered a query that asked to not include specific data: > raised following `error:` > Received unrecognized function ne. Valid functions are [, , , , , , , ] I noticed that chroma and deeplake also support not equals/not filtering so I added it there as well [pinecone](https://docs.pinecone.io/docs/metadata-filtering#metadata-query-language) [chroma](https://docs.trychroma.com/usage-guide#filtering-by-metadata) [deeplake](https://docs.activeloop.ai/enterprise-features/compute-engine/querying-datasets/query-syntax#and-or-not) --- libs/langchain/langchain/chains/query_constructor/ir.py | 1 + libs/langchain/langchain/retrievers/self_query/chroma.py | 1 + libs/langchain/langchain/retrievers/self_query/deeplake.py | 3 ++- libs/langchain/langchain/retrievers/self_query/pinecone.py | 1 + 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/libs/langchain/langchain/chains/query_constructor/ir.py b/libs/langchain/langchain/chains/query_constructor/ir.py index 04eac8be91..c8d6fc976b 100644 --- a/libs/langchain/langchain/chains/query_constructor/ir.py +++ b/libs/langchain/langchain/chains/query_constructor/ir.py @@ -81,6 +81,7 @@ class Comparator(str, Enum): """Enumerator of the comparison operators.""" EQ = "eq" + NE = "ne" GT = "gt" GTE = "gte" LT = "lt" diff --git a/libs/langchain/langchain/retrievers/self_query/chroma.py b/libs/langchain/langchain/retrievers/self_query/chroma.py index 9f9900358a..8c9a79b12d 100644 --- a/libs/langchain/langchain/retrievers/self_query/chroma.py +++ b/libs/langchain/langchain/retrievers/self_query/chroma.py @@ -17,6 +17,7 @@ class ChromaTranslator(Visitor): """Subset of allowed logical operators.""" allowed_comparators = [ Comparator.EQ, + Comparator.NE, Comparator.GT, Comparator.GTE, Comparator.LT, diff --git a/libs/langchain/langchain/retrievers/self_query/deeplake.py b/libs/langchain/langchain/retrievers/self_query/deeplake.py index 1e65eb39a1..030933b32e 100644 --- a/libs/langchain/langchain/retrievers/self_query/deeplake.py +++ b/libs/langchain/langchain/retrievers/self_query/deeplake.py @@ -22,6 +22,7 @@ COMPARATOR_TO_TQL = { OPERATOR_TO_TQL = { Operator.AND: "and", Operator.OR: "or", + Operator.NOT: "NOT", } @@ -37,7 +38,7 @@ def can_cast_to_float(string: str) -> bool: class DeepLakeTranslator(Visitor): """Translate `DeepLake` internal query language elements to valid filters.""" - allowed_operators = [Operator.AND, Operator.OR] + allowed_operators = [Operator.AND, Operator.OR, Operator.NOT] """Subset of allowed logical operators.""" allowed_comparators = [ Comparator.EQ, diff --git a/libs/langchain/langchain/retrievers/self_query/pinecone.py b/libs/langchain/langchain/retrievers/self_query/pinecone.py index 003514953c..a39fc310bf 100644 --- a/libs/langchain/langchain/retrievers/self_query/pinecone.py +++ b/libs/langchain/langchain/retrievers/self_query/pinecone.py @@ -15,6 +15,7 @@ class PineconeTranslator(Visitor): allowed_comparators = ( Comparator.EQ, + Comparator.NE, Comparator.LT, Comparator.LTE, Comparator.GT,