Skip to content

rg.Query

To collect records based on searching criteria, you can use the Query and Filter classes. The Query class is used to define the search criteria, while the Filter class is used to filter the search results. Filter is passed to a Query object so you can combine multiple filters to create complex search queries. A Query object can also be passed to Dataset.records to fetch records based on the search criteria.

Usage Examples

Searching for records with terms

To search for records with terms, you can use the Dataset.records attribute with a query string. The search terms are used to search for records that contain the terms in the text field.

for record in dataset.records(query="paris"):
    print(record)

Filtering records by conditions

Argilla allows you to filter records based on conditions. You can use the Filter class to define the conditions and pass them to the Dataset.records attribute to fetch records based on the conditions. Conditions include "==", ">=", "<=", or "in". Conditions can be combined with dot notation to filter records based on metadata, suggestions, or responses.

# create a range from 10 to 20
range_filter = rg.Filter(
    [
        ("metadata.count", ">=", 10),
        ("metadata.count", "<=", 20)
    ]
)

# query records with metadata count greater than 10 and less than 20
query = rg.Query(filters=range_filter, query="paris")

# iterate over the results
for record in dataset.records(query=query):
    print(record)

Class Reference

rg.Query

This class is used to map user queries to the internal query models

Source code in src/argilla_sdk/records/_search.py
class Query:
    """This class is used to map user queries to the internal query models"""

    query: Optional[str] = None

    def __init__(self, *, query: Union[str, None] = None, filter: Union[Filter, None] = None):
        """Create a query object for use in Argilla search requests.add()

        Parameters:
            query (Union[str, None], optional): The query string that will be used to search.
            filter (Union[Filter, None], optional): The filter object that will be used to filter the search results.
        """

        self.query = query
        self.filter = filter

    @property
    def model(self) -> SearchQueryModel:
        model = SearchQueryModel()

        if self.query is not None:
            text_query = TextQueryModel(q=self.query)
            model.query = QueryModel(text=text_query)

        if self.filter is not None:
            model.filters = self.filter.model

        return model

__init__(*, query=None, filter=None)

Create a query object for use in Argilla search requests.add()

Parameters:

Name Type Description Default
query Union[str, None]

The query string that will be used to search.

None
filter Union[Filter, None]

The filter object that will be used to filter the search results.

None
Source code in src/argilla_sdk/records/_search.py
def __init__(self, *, query: Union[str, None] = None, filter: Union[Filter, None] = None):
    """Create a query object for use in Argilla search requests.add()

    Parameters:
        query (Union[str, None], optional): The query string that will be used to search.
        filter (Union[Filter, None], optional): The filter object that will be used to filter the search results.
    """

    self.query = query
    self.filter = filter

rg.Filter

This class is used to map user filters to the internal filter models

Source code in src/argilla_sdk/records/_search.py
class Filter:
    """This class is used to map user filters to the internal filter models"""

    def __init__(self, conditions: Union[List[Tuple[str, str, Any]], Tuple[str, str, Any], None] = None):
        """ Create a filter object for use in Argilla search requests.

        Parameters:
            conditions (Union[List[Tuple[str, str, Any]], Tuple[str, str, Any], None], optional): \
                The conditions that will be used to filter the search results. \
                The conditions should be a list of tuples where each tuple contains \
                the field, operator, and value. For example `("label", "in", ["positive","happy"])`.\

        """

        if isinstance(conditions, tuple):
            conditions = [conditions]
        self.conditions = [Condition(condition) for condition in conditions]

    @property
    def model(self) -> AndFilterModel:
        return AndFilterModel.model_validate({"and": [condition.model for condition in self.conditions]})

__init__(conditions=None)

Create a filter object for use in Argilla search requests.

Parameters:

Name Type Description Default
conditions Union[List[Tuple[str, str, Any]], Tuple[str, str, Any], None]

The conditions that will be used to filter the search results. The conditions should be a list of tuples where each tuple contains the field, operator, and value. For example ("label", "in", ["positive","happy"]).

None
Source code in src/argilla_sdk/records/_search.py
def __init__(self, conditions: Union[List[Tuple[str, str, Any]], Tuple[str, str, Any], None] = None):
    """ Create a filter object for use in Argilla search requests.

    Parameters:
        conditions (Union[List[Tuple[str, str, Any]], Tuple[str, str, Any], None], optional): \
            The conditions that will be used to filter the search results. \
            The conditions should be a list of tuples where each tuple contains \
            the field, operator, and value. For example `("label", "in", ["positive","happy"])`.\

    """

    if isinstance(conditions, tuple):
        conditions = [conditions]
    self.conditions = [Condition(condition) for condition in conditions]