Skip to content

Smart Rules

The Smart Rules module provides functionality for creating and working with smart rules (UniqueQL queries) to filter and search content in the knowledge base.

Overview

Smart rules allow you to create complex queries using UniqueQL to filter and search content. They act as conditionals to reduce the amount of retrieved information by evaluating metadata of documents.

Components

Core Classes

unique_toolkit.content.smart_rules.Statement

Bases: BaseStatement

Source code in unique_toolkit/unique_toolkit/content/smart_rules.py
class Statement(BaseStatement):
    operator: Operator
    value: Union[str, int, bool, list[str], "AndStatement", "OrStatement"]
    path: List[str] = Field(default_factory=list)

    def _fill_in_variables(
        self,
        user_metadata: Mapping[str, Union[str, int, bool]],
        tool_parameters: Mapping[str, Union[str, int, bool]],
    ) -> Self:
        new_stmt = self.model_copy()
        new_stmt.value = eval_operator(self, user_metadata, tool_parameters)
        return new_stmt

unique_toolkit.content.smart_rules.Operator

Bases: str, Enum

Source code in unique_toolkit/unique_toolkit/content/smart_rules.py
class Operator(str, Enum):
    EQUALS = "equals"
    NOT_EQUALS = "notEquals"
    GREATER_THAN = "greaterThan"
    GREATER_THAN_OR_EQUAL = "greaterThanOrEqual"
    LESS_THAN = "lessThan"
    LESS_THAN_OR_EQUAL = "lessThanOrEqual"
    IN = "in"
    NOT_IN = "notIn"
    CONTAINS = "contains"
    NOT_CONTAINS = "notContains"
    IS_NULL = "isNull"
    IS_NOT_NULL = "isNotNull"
    IS_EMPTY = "isEmpty"
    IS_NOT_EMPTY = "isNotEmpty"
    NESTED = "nested"

unique_toolkit.content.smart_rules.AndStatement

Bases: BaseStatement

Source code in unique_toolkit/unique_toolkit/content/smart_rules.py
class AndStatement(BaseStatement):
    and_list: List[Union["Statement", "AndStatement", "OrStatement"]] = Field(
        validation_alias=AliasChoices("and", "and_list"), serialization_alias="and"
    )

    def _fill_in_variables(
        self,
        user_metadata: Mapping[str, Union[str, int, bool]],
        tool_parameters: Mapping[str, Union[str, int, bool]],
    ) -> Self:
        new_stmt = self.model_copy()
        new_stmt.and_list = [
            sub_query._fill_in_variables(user_metadata, tool_parameters)
            for sub_query in self.and_list
        ]
        return new_stmt

unique_toolkit.content.smart_rules.OrStatement

Bases: BaseStatement

Source code in unique_toolkit/unique_toolkit/content/smart_rules.py
class OrStatement(BaseStatement):
    or_list: List[Union["Statement", "AndStatement", "OrStatement"]] = Field(
        validation_alias=AliasChoices("or", "or_list"), serialization_alias="or"
    )

    def _fill_in_variables(
        self,
        user_metadata: Mapping[str, Union[str, int, bool]],
        tool_parameters: Mapping[str, Union[str, int, bool]],
    ) -> Self:
        new_stmt = self.model_copy()
        new_stmt.or_list = [
            sub_query._fill_in_variables(user_metadata, tool_parameters)
            for sub_query in self.or_list
        ]
        return new_stmt

unique_toolkit.content.smart_rules.BaseStatement

Bases: BaseModel

Source code in unique_toolkit/unique_toolkit/content/smart_rules.py
class BaseStatement(BaseModel):
    model_config = ConfigDict(serialize_by_alias=True)

    def with_variables(
        self,
        user_metadata: Mapping[str, Union[str, int, bool]],
        tool_parameters: Mapping[str, Union[str, int, bool]],
    ) -> Self:
        return self._fill_in_variables(user_metadata, tool_parameters)

    def is_compiled(self) -> bool:
        # Serialize the object to json string
        json_str = self.model_dump_json()
        # Check if the json string has <T> or <T+> or <T-> or <toolParameters or <userMetadata
        return (
            "<T>" in json_str
            or "<T+" in json_str
            or "<T-" in json_str
            or "<toolParameters" in json_str
            or "<userMetadata" in json_str
        )

    def _fill_in_variables(
        self,
        user_metadata: Mapping[str, Union[str, int, bool]],
        tool_parameters: Mapping[str, Union[str, int, bool]],
    ) -> Self:
        return self.model_copy()