mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-21 18:39:57 +00:00
community[minor]: Add SelfQueryRetriever support to PGVector (#16991)
- **Description:** Add SelfQueryRetriever support to PGVector - **Issue:** - - **Dependencies:** - - **Twitter handle:** - --------- Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
@@ -12,6 +12,7 @@ from langchain_community.vectorstores import (
|
||||
MongoDBAtlasVectorSearch,
|
||||
MyScale,
|
||||
OpenSearchVectorSearch,
|
||||
PGVector,
|
||||
Pinecone,
|
||||
Qdrant,
|
||||
Redis,
|
||||
@@ -43,6 +44,7 @@ from langchain.retrievers.self_query.milvus import MilvusTranslator
|
||||
from langchain.retrievers.self_query.mongodb_atlas import MongoDBAtlasTranslator
|
||||
from langchain.retrievers.self_query.myscale import MyScaleTranslator
|
||||
from langchain.retrievers.self_query.opensearch import OpenSearchTranslator
|
||||
from langchain.retrievers.self_query.pgvector import PGVectorTranslator
|
||||
from langchain.retrievers.self_query.pinecone import PineconeTranslator
|
||||
from langchain.retrievers.self_query.qdrant import QdrantTranslator
|
||||
from langchain.retrievers.self_query.redis import RedisTranslator
|
||||
@@ -58,6 +60,7 @@ def _get_builtin_translator(vectorstore: VectorStore) -> Visitor:
|
||||
"""Get the translator class corresponding to the vector store class."""
|
||||
BUILTIN_TRANSLATORS: Dict[Type[VectorStore], Type[Visitor]] = {
|
||||
AstraDB: AstraDBTranslator,
|
||||
PGVector: PGVectorTranslator,
|
||||
Pinecone: PineconeTranslator,
|
||||
Chroma: ChromaTranslator,
|
||||
DashVector: DashvectorTranslator,
|
||||
|
52
libs/langchain/langchain/retrievers/self_query/pgvector.py
Normal file
52
libs/langchain/langchain/retrievers/self_query/pgvector.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from typing import Dict, Tuple, Union
|
||||
|
||||
from langchain.chains.query_constructor.ir import (
|
||||
Comparator,
|
||||
Comparison,
|
||||
Operation,
|
||||
Operator,
|
||||
StructuredQuery,
|
||||
Visitor,
|
||||
)
|
||||
|
||||
|
||||
class PGVectorTranslator(Visitor):
|
||||
"""Translate `PGVector` internal query language elements to valid filters."""
|
||||
|
||||
allowed_operators = [Operator.AND, Operator.OR]
|
||||
"""Subset of allowed logical operators."""
|
||||
allowed_comparators = [
|
||||
Comparator.EQ,
|
||||
Comparator.NE,
|
||||
Comparator.GT,
|
||||
Comparator.LT,
|
||||
Comparator.IN,
|
||||
Comparator.NIN,
|
||||
Comparator.CONTAIN,
|
||||
Comparator.LIKE,
|
||||
]
|
||||
"""Subset of allowed logical comparators."""
|
||||
|
||||
def _format_func(self, func: Union[Operator, Comparator]) -> str:
|
||||
self._validate_func(func)
|
||||
return f"{func.value}"
|
||||
|
||||
def visit_operation(self, operation: Operation) -> Dict:
|
||||
args = [arg.accept(self) for arg in operation.arguments]
|
||||
return {self._format_func(operation.operator): args}
|
||||
|
||||
def visit_comparison(self, comparison: Comparison) -> Dict:
|
||||
return {
|
||||
comparison.attribute: {
|
||||
self._format_func(comparison.comparator): comparison.value
|
||||
}
|
||||
}
|
||||
|
||||
def visit_structured_query(
|
||||
self, structured_query: StructuredQuery
|
||||
) -> Tuple[str, dict]:
|
||||
if structured_query.filter is None:
|
||||
kwargs = {}
|
||||
else:
|
||||
kwargs = {"filter": structured_query.filter.accept(self)}
|
||||
return structured_query.query, kwargs
|
@@ -0,0 +1,87 @@
|
||||
from typing import Dict, Tuple
|
||||
|
||||
import pytest as pytest
|
||||
|
||||
from langchain.chains.query_constructor.ir import (
|
||||
Comparator,
|
||||
Comparison,
|
||||
Operation,
|
||||
Operator,
|
||||
StructuredQuery,
|
||||
)
|
||||
from langchain.retrievers.self_query.pgvector import PGVectorTranslator
|
||||
|
||||
DEFAULT_TRANSLATOR = PGVectorTranslator()
|
||||
|
||||
|
||||
def test_visit_comparison() -> None:
|
||||
comp = Comparison(comparator=Comparator.LT, attribute="foo", value=1)
|
||||
expected = {"foo": {"lt": 1}}
|
||||
actual = DEFAULT_TRANSLATOR.visit_comparison(comp)
|
||||
assert expected == actual
|
||||
|
||||
|
||||
@pytest.mark.skip("Not implemented")
|
||||
def test_visit_operation() -> None:
|
||||
op = Operation(
|
||||
operator=Operator.AND,
|
||||
arguments=[
|
||||
Comparison(comparator=Comparator.LT, attribute="foo", value=2),
|
||||
Comparison(comparator=Comparator.EQ, attribute="bar", value="baz"),
|
||||
Comparison(comparator=Comparator.GT, attribute="abc", value=2.0),
|
||||
],
|
||||
)
|
||||
expected = {
|
||||
"foo": {"lt": 2},
|
||||
"bar": {"eq": "baz"},
|
||||
"abc": {"gt": 2.0},
|
||||
}
|
||||
actual = DEFAULT_TRANSLATOR.visit_operation(op)
|
||||
assert expected == actual
|
||||
|
||||
|
||||
def test_visit_structured_query() -> None:
|
||||
query = "What is the capital of France?"
|
||||
structured_query = StructuredQuery(
|
||||
query=query,
|
||||
filter=None,
|
||||
)
|
||||
expected: Tuple[str, Dict] = (query, {})
|
||||
actual = DEFAULT_TRANSLATOR.visit_structured_query(structured_query)
|
||||
assert expected == actual
|
||||
|
||||
comp = Comparison(comparator=Comparator.LT, attribute="foo", value=1)
|
||||
structured_query = StructuredQuery(
|
||||
query=query,
|
||||
filter=comp,
|
||||
)
|
||||
expected = (query, {"filter": {"foo": {"lt": 1}}})
|
||||
actual = DEFAULT_TRANSLATOR.visit_structured_query(structured_query)
|
||||
assert expected == actual
|
||||
|
||||
op = Operation(
|
||||
operator=Operator.AND,
|
||||
arguments=[
|
||||
Comparison(comparator=Comparator.LT, attribute="foo", value=2),
|
||||
Comparison(comparator=Comparator.EQ, attribute="bar", value="baz"),
|
||||
Comparison(comparator=Comparator.GT, attribute="abc", value=2.0),
|
||||
],
|
||||
)
|
||||
structured_query = StructuredQuery(
|
||||
query=query,
|
||||
filter=op,
|
||||
)
|
||||
expected = (
|
||||
query,
|
||||
{
|
||||
"filter": {
|
||||
"and": [
|
||||
{"foo": {"lt": 2}},
|
||||
{"bar": {"eq": "baz"}},
|
||||
{"abc": {"gt": 2.0}},
|
||||
]
|
||||
}
|
||||
},
|
||||
)
|
||||
actual = DEFAULT_TRANSLATOR.visit_structured_query(structured_query)
|
||||
assert expected == actual
|
Reference in New Issue
Block a user