Skip to content

Commit 6096bdf

Browse files
author
Val Brodsky
committed
Improve usability of create_project for chat evaluation and add basic validations
1 parent a4970e7 commit 6096bdf

File tree

3 files changed

+93
-21
lines changed

3 files changed

+93
-21
lines changed

libs/labelbox/src/labelbox/client.py

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import urllib.parse
1010
from collections import defaultdict
1111
from datetime import datetime, timezone
12-
from typing import Any, List, Dict, Union, Optional
12+
from typing import Any, List, Dict, Union, Optional, overload
1313

1414
import requests
1515
import requests.exceptions
@@ -596,7 +596,8 @@ def _create(self, db_object_type, data, extra_params={}):
596596
res = res["create%s" % db_object_type.type_name()]
597597
return db_object_type(self, res)
598598

599-
def create_model_config(self, name: str, model_id: str, inference_params: dict) -> ModelConfig:
599+
def create_model_config(self, name: str, model_id: str,
600+
inference_params: dict) -> ModelConfig:
600601
""" Creates a new model config with the given params.
601602
Model configs are scoped to organizations, and can be reused between projects.
602603
@@ -608,6 +609,8 @@ def create_model_config(self, name: str, model_id: str, inference_params: dict)
608609
Returns:
609610
str, id of the created model config
610611
"""
612+
if not name:
613+
raise ValueError("Model config name must not be an empty string.")
611614

612615
query = """mutation CreateModelConfigPyApi($modelId: ID!, $inferenceParams: Json!, $name: String!) {
613616
createModelConfig(input: {modelId: $modelId, inferenceParams: $inferenceParams, name: $name}) {
@@ -640,9 +643,7 @@ def delete_model_config(self, id: str) -> bool:
640643
success
641644
}
642645
}"""
643-
params = {
644-
"id": id
645-
}
646+
params = {"id": id}
646647
result = self.execute(query, params)
647648
return result['deleteModelConfig']['success']
648649

@@ -811,22 +812,66 @@ def create_project(self, **kwargs) -> Project:
811812

812813
return self._create(Entity.Project, params, extra_params)
813814

814-
def create_model_evaluation_project(
815-
self,
816-
dataset_name_or_id: str,
817-
append_to_existing_dataset: bool = False,
818-
data_row_count: int = 100,
819-
**kwargs) -> Project:
815+
@overload
816+
def create_model_evaluation_project(self,
817+
dataset_name: str,
818+
dataset_id: str = None,
819+
data_row_count: int = 100,
820+
**kwargs) -> Project:
821+
pass
822+
823+
@overload
824+
def create_model_evaluation_project(self,
825+
dataset_id: str,
826+
dataset_name: str = None,
827+
data_row_count: int = 100,
828+
**kwargs) -> Project:
829+
pass
830+
831+
def create_model_evaluation_project(self,
832+
dataset_id: Optional[str] = None,
833+
dataset_name: Optional[str] = None,
834+
data_row_count: int = 100,
835+
**kwargs) -> Project:
820836
"""
821837
Use this method exclusively to create a chat model evaluation project.
822838
Args:
823-
dataset_name_or_id: The name or id of the dataset to use for the project
824-
append_to_existing_dataset: If True, the project will append assets (data rows) to the existing dataset
839+
dataset_name: When creating a new dataset, pass the name
840+
dataset_id: When using an existing dataset, pass the id
825841
data_row_count: The number of data row assets to use for the project
826842
**kwargs: Additional parameters to pass to the the create_project method
827843
Returns:
828844
Project: The created project
845+
846+
Examples:
847+
>>> client.create_model_evaluation_project(name=project_name, dataset_name="new data set")
848+
>>> This creates a new dataset with a default number of rows (100), creates new project and assigns a batch of the newly created datarows to the project.
849+
850+
>>> client.create_model_evaluation_project(name=project_name, dataset_name="new data set", data_row_count=10)
851+
>>> This creates a new dataset with 10 data rows, creates new project and assigns a batch of the newly created datarows to the project.
852+
853+
>>> client.create_model_evaluation_project(name=project_name, dataset_id="clr00u8j0j0j0")
854+
>>> This creates a new project, and adds 100 datarows to the dataset with id "clr00u8j0j0j0" and assigns a batch of the newly created data rows to the project.
855+
856+
>>> client.create_model_evaluation_project(name=project_name, dataset_id="clr00u8j0j0j0", data_row_count=0)
857+
>>> This creates a new project, and adds 100 datarows to the dataset with id "clr00u8j0j0j0" and assigns a batch of the newly created data rows to the project.
858+
859+
829860
"""
861+
if not dataset_id and not dataset_name:
862+
raise ValueError(
863+
"dataset_name or data_set_id must be present and not be an empty string."
864+
)
865+
if data_row_count <= 0:
866+
raise ValueError("data_row_count must be a positive integer.")
867+
868+
if dataset_id:
869+
append_to_existing_dataset = True
870+
dataset_name_or_id = dataset_id
871+
else:
872+
append_to_existing_dataset = False
873+
dataset_name_or_id = dataset_name
874+
830875
kwargs["media_type"] = MediaType.Conversational
831876
kwargs["ontology_kind"] = OntologyKind.ModelEvaluation
832877
kwargs["dataset_name_or_id"] = dataset_name_or_id

libs/labelbox/tests/integration/conftest.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,25 @@ def chat_evaluation_ontology(client, rand_gen):
363363

364364

365365
@pytest.fixture
366-
def chat_evaluation_project(client, rand_gen):
366+
def chat_evaluation_project_create_dataset(client, rand_gen):
367367
project_name = f"test-model-evaluation-project-{rand_gen(str)}"
368-
dataset_name_or_id = f"test-model-evaluation-dataset-{rand_gen(str)}"
369-
project = client.create_model_evaluation_project(
370-
name=project_name, dataset_name_or_id=dataset_name_or_id)
368+
dataset_name = f"test-model-evaluation-dataset-{rand_gen(str)}"
369+
project = client.create_model_evaluation_project(name=project_name,
370+
dataset_name=dataset_name,
371+
data_row_count=1)
372+
373+
yield project
374+
375+
project.delete()
376+
377+
378+
@pytest.fixture
379+
def chat_evaluation_project_append_to_dataset(client, dataset, rand_gen):
380+
project_name = f"test-model-evaluation-project-{rand_gen(str)}"
381+
dataset_id = dataset.uid
382+
project = client.create_model_evaluation_project(name=project_name,
383+
dataset_id=dataset_id,
384+
data_row_count=1)
371385

372386
yield project
373387

libs/labelbox/tests/integration/test_chat_evaluation_ontology_project.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from labelbox.schema.labeling_frontend import LabelingFrontend
66

77

8-
def test_create_chat_evaluation_ontology_project(client,
9-
chat_evaluation_ontology,
10-
chat_evaluation_project):
8+
def test_create_chat_evaluation_ontology_project(
9+
client, chat_evaluation_ontology,
10+
chat_evaluation_project_create_dataset):
1111
ontology = chat_evaluation_ontology
1212

1313
# here we are essentially testing the ontology creation which is a fixture
@@ -18,7 +18,20 @@ def test_create_chat_evaluation_ontology_project(client,
1818
assert tool.schema_id
1919
assert tool.feature_schema_id
2020

21-
project = chat_evaluation_project
21+
project = chat_evaluation_project_create_dataset
22+
project.setup_editor(ontology)
23+
24+
assert project.labeling_frontend().name == "Editor"
25+
assert project.ontology().name == ontology.name
26+
27+
28+
def test_create_chat_evaluation_ontology_project_existing_dataset(
29+
client, chat_evaluation_ontology,
30+
chat_evaluation_project_append_to_dataset):
31+
ontology = chat_evaluation_ontology
32+
33+
project = chat_evaluation_project_append_to_dataset
34+
assert project
2235
project.setup_editor(ontology)
2336

2437
assert project.labeling_frontend().name == "Editor"

0 commit comments

Comments
 (0)