Skip to content

Commit

Permalink
[REFACTOR]: argilla: Rename status to response.status for filte…
Browse files Browse the repository at this point in the history
…ring using the SDK (#5145)

<!-- Please include a summary of the changes and the related issue.
Please also include relevant motivation and context. List any
dependencies that are required for this change. -->

This PR renames the `status` key to `response.status` for filtering with
the SDK.


**Type of change**
<!-- Please delete options that are not relevant. Remember to title the
PR according to the type of change -->

- Refactor (change restructuring the codebase without changing
functionality)

**How Has This Been Tested**
<!-- Please add some reference about how your feature has been tested.
-->

**Checklist**
<!-- Please go over the list and make sure you've taken everything into
account -->

- I added relevant documentation
- follows the style guidelines of this project
- I did a self-review of my code
- I made corresponding changes to the documentation
- I confirm My changes generate no new warnings
- I have added tests that prove my fix is effective or that my feature
works
- I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)
  • Loading branch information
frascuchon authored Jul 4, 2024
1 parent 54d2e60 commit 239d6b1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
2 changes: 1 addition & 1 deletion argilla/docs/how_to_guides/query_export.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ workspace = client.workspaces("my_workspace")
dataset = client.datasets(name="my_dataset", workspace=workspace)

status_filter = rg.Query(
filter=rg.Filter(("status", "==", "submitted"))
filter=rg.Filter(("response.status", "==", "submitted"))
)

filtered_records = list(dataset.records(status_filter))
Expand Down
2 changes: 1 addition & 1 deletion argilla/docs/how_to_guides/record.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ dataset.records.delete(records=records_to_delete)

```python
status_filter = rg.Query(
filter = rg.Filter(("status", "==", "pending"))
filter = rg.Filter(("response.status", "==", "pending"))
)
records_to_delete = list(dataset.records(status_filter))

Expand Down
2 changes: 1 addition & 1 deletion argilla/src/argilla/records/_dataset_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _fetch_from_server_with_list(self) -> List[RecordModel]:
def _fetch_from_server_with_search(self) -> List[RecordModel]:
search_items, total = self.__client.api.records.search(
dataset_id=self.__dataset.id,
query=self.__query.model,
query=self.__query.api_model(),
limit=self.__batch_size,
offset=self.__offset,
with_responses=self.__with_responses,
Expand Down
17 changes: 8 additions & 9 deletions argilla/src/argilla/records/_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
class Condition(Tuple[str, str, Any]):
"""This class is used to map user conditions to the internal filter models"""

@property
def model(self) -> FilterModel:
def api_model(self) -> FilterModel:
field, operator, value = self

field = field.strip()
Expand All @@ -55,7 +54,7 @@ def model(self) -> FilterModel:
def _extract_filter_scope(field: str) -> ScopeModel:
field = field.strip()

if field == "status":
if field == "response.status":
return ResponseFilterScopeModel(property="status")
elif "metadata" in field:
_, md_property = field.split(".")
Expand All @@ -70,6 +69,8 @@ def _extract_filter_scope(field: str) -> ScopeModel:
question, _ = field.split(".")
return ResponseFilterScopeModel(question=question)
else: # Question field -> Suggestion
# TODO: The default path would be raise an error instead of consider suggestions by default
# (can be confusing)
return SuggestionFilterScopeModel(question=field)


Expand All @@ -91,9 +92,8 @@ def __init__(self, conditions: Union[List[Tuple[str, str, Any]], Tuple[str, str,
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]})
def api_model(self) -> AndFilterModel:
return AndFilterModel.model_validate({"and": [condition.api_model() for condition in self.conditions]})


class Query:
Expand All @@ -112,16 +112,15 @@ def __init__(self, *, query: Union[str, None] = None, filter: Union[Filter, None
self.query = query
self.filter = filter

@property
def model(self) -> SearchQueryModel:
def api_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
model.filters = self.filter.api_model()

return model

Expand Down
30 changes: 30 additions & 0 deletions argilla/tests/unit/test_search/test_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2024-present, Argilla, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from argilla.records import Filter


class TestFilters:
def test_filter_by_responses_status(self):
test_filter = Filter(("response.status", "in", ["submitted", "discard"]))
assert test_filter.api_model().model_dump(by_alias=True) == {
"type": "and",
"and": [
{
"scope": {"entity": "response", "property": "status", "question": None},
"type": "terms",
"values": ["submitted", "discard"],
}
],
}

0 comments on commit 239d6b1

Please sign in to comment.