Home / Class/ QueryTransformer Class — langchain Architecture

QueryTransformer Class — langchain Architecture

Architecture documentation for the QueryTransformer class in parser.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  d2aabb64_66b3_2786_7add_413f9d85974a["QueryTransformer"]
  92600185_016b_97ba_a688_bf4a65bf4556["Comparator"]
  d2aabb64_66b3_2786_7add_413f9d85974a -->|extends| 92600185_016b_97ba_a688_bf4a65bf4556
  abbee30c_e57e_5369_8bf6_b446aa4d1528["Operator"]
  d2aabb64_66b3_2786_7add_413f9d85974a -->|extends| abbee30c_e57e_5369_8bf6_b446aa4d1528
  d4b02378_467e_cfa4_ebea_eada03733716["parser.py"]
  d2aabb64_66b3_2786_7add_413f9d85974a -->|defined in| d4b02378_467e_cfa4_ebea_eada03733716
  362b6905_2ad1_b7e1_e15b_3785484f99cd["__init__()"]
  d2aabb64_66b3_2786_7add_413f9d85974a -->|method| 362b6905_2ad1_b7e1_e15b_3785484f99cd
  94d881c4_09fd_5a6c_f3cc_e266b0086a01["program()"]
  d2aabb64_66b3_2786_7add_413f9d85974a -->|method| 94d881c4_09fd_5a6c_f3cc_e266b0086a01
  79b2c87b_f5a2_8b42_c1fb_83fee547ba4b["func_call()"]
  d2aabb64_66b3_2786_7add_413f9d85974a -->|method| 79b2c87b_f5a2_8b42_c1fb_83fee547ba4b
  0aa54eb0_c724_bcbe_40fb_7f17a020a6a7["_match_func_name()"]
  d2aabb64_66b3_2786_7add_413f9d85974a -->|method| 0aa54eb0_c724_bcbe_40fb_7f17a020a6a7
  cba16820_b7c5_812a_1a87_5bc90c0185ad["args()"]
  d2aabb64_66b3_2786_7add_413f9d85974a -->|method| cba16820_b7c5_812a_1a87_5bc90c0185ad
  d5fb243f_2ebe_0acb_c8a8_7c92ea0aa6e8["false()"]
  d2aabb64_66b3_2786_7add_413f9d85974a -->|method| d5fb243f_2ebe_0acb_c8a8_7c92ea0aa6e8
  c3e0bf71_7b44_5d09_f15a_dc11a0505707["true()"]
  d2aabb64_66b3_2786_7add_413f9d85974a -->|method| c3e0bf71_7b44_5d09_f15a_dc11a0505707
  db56747d_57d1_d35c_e9a0_934c8dec2b7b["list()"]
  d2aabb64_66b3_2786_7add_413f9d85974a -->|method| db56747d_57d1_d35c_e9a0_934c8dec2b7b
  ac155745_b827_a0ba_1ae1_68ed499f03ea["int()"]
  d2aabb64_66b3_2786_7add_413f9d85974a -->|method| ac155745_b827_a0ba_1ae1_68ed499f03ea
  f9878373_6444_9257_1944_5a79a4fa3085["float()"]
  d2aabb64_66b3_2786_7add_413f9d85974a -->|method| f9878373_6444_9257_1944_5a79a4fa3085
  491e2e9d_3cbd_ae5e_125e_d511a665396b["date()"]
  d2aabb64_66b3_2786_7add_413f9d85974a -->|method| 491e2e9d_3cbd_ae5e_125e_d511a665396b

Relationship Graph

Source Code

libs/langchain/langchain_classic/chains/query_constructor/parser.py lines 78–254

class QueryTransformer(Transformer):
    """Transform a query string into an intermediate representation."""

    def __init__(
        self,
        *args: Any,
        allowed_comparators: Sequence[Comparator] | None = None,
        allowed_operators: Sequence[Operator] | None = None,
        allowed_attributes: Sequence[str] | None = None,
        **kwargs: Any,
    ):
        """Initialize the QueryTransformer.

        Args:
            *args: Positional arguments.
            allowed_comparators: Optional sequence of allowed comparators.
            allowed_operators: Optional sequence of allowed operators.
            allowed_attributes: Optional sequence of allowed attributes for comparators.
            **kwargs: Additional keyword arguments.
        """
        super().__init__(*args, **kwargs)
        self.allowed_comparators = allowed_comparators
        self.allowed_operators = allowed_operators
        self.allowed_attributes = allowed_attributes

    def program(self, *items: Any) -> tuple:
        """Transform the items into a tuple."""
        return items

    def func_call(self, func_name: Any, args: list) -> FilterDirective:
        """Transform a function name and args into a FilterDirective.

        Args:
            func_name: The name of the function.
            args: The arguments passed to the function.

        Returns:
            The filter directive.

        Raises:
            ValueError: If the function is a comparator and the first arg is not in the
            allowed attributes.
        """
        func = self._match_func_name(str(func_name))
        if isinstance(func, Comparator):
            if self.allowed_attributes and args[0] not in self.allowed_attributes:
                msg = (
                    f"Received invalid attributes {args[0]}. Allowed attributes are "
                    f"{self.allowed_attributes}"
                )
                raise ValueError(msg)
            return Comparison(comparator=func, attribute=args[0], value=args[1])
        if len(args) == 1 and func in (Operator.AND, Operator.OR):
            return args[0]
        return Operation(operator=func, arguments=args)

    def _match_func_name(self, func_name: str) -> Operator | Comparator:
        if func_name in set(Comparator):
            if (
                self.allowed_comparators is not None
                and func_name not in self.allowed_comparators
            ):
                msg = (
                    f"Received disallowed comparator {func_name}. Allowed "
                    f"comparators are {self.allowed_comparators}"
                )
                raise ValueError(msg)
            return Comparator(func_name)
        if func_name in set(Operator):
            if (
                self.allowed_operators is not None
                and func_name not in self.allowed_operators
            ):
                msg = (
                    f"Received disallowed operator {func_name}. Allowed operators"
                    f" are {self.allowed_operators}"
                )
                raise ValueError(msg)
            return Operator(func_name)
        msg = (
            f"Received unrecognized function {func_name}. Valid functions are "

Frequently Asked Questions

What is the QueryTransformer class?
QueryTransformer is a class in the langchain codebase, defined in libs/langchain/langchain_classic/chains/query_constructor/parser.py.
Where is QueryTransformer defined?
QueryTransformer is defined in libs/langchain/langchain_classic/chains/query_constructor/parser.py at line 78.
What does QueryTransformer extend?
QueryTransformer extends Comparator, Operator.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free