Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions langchain_postgres/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from importlib import metadata

from langchain_postgres.chat_message_histories import PostgresChatMessageHistory
from langchain_postgres.translator import PGVectorTranslator
from langchain_postgres.vectorstores import PGVector

try:
Expand All @@ -13,4 +14,5 @@
"__version__",
"PostgresChatMessageHistory",
"PGVector",
"PGVectorTranslator",
]
52 changes: 52 additions & 0 deletions langchain_postgres/translator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from typing import Dict, Tuple, Union

from langchain_core.structured_query 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
1 change: 1 addition & 0 deletions tests/unit_tests/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
EXPECTED_ALL = [
"__version__",
"PGVector",
"PGVectorTranslator",
"PostgresChatMessageHistory",
]

Expand Down