orca-sdk 0.1.3__py3-none-any.whl → 0.1.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- orca_sdk/_shared/metrics.py +179 -40
- orca_sdk/_shared/metrics_test.py +99 -6
- orca_sdk/_utils/data_parsing_test.py +1 -1
- orca_sdk/async_client.py +462 -301
- orca_sdk/classification_model.py +156 -41
- orca_sdk/classification_model_test.py +327 -8
- orca_sdk/client.py +462 -301
- orca_sdk/conftest.py +140 -21
- orca_sdk/datasource.py +45 -2
- orca_sdk/datasource_test.py +120 -0
- orca_sdk/embedding_model.py +32 -24
- orca_sdk/job.py +17 -17
- orca_sdk/memoryset.py +459 -56
- orca_sdk/memoryset_test.py +435 -2
- orca_sdk/regression_model.py +110 -19
- orca_sdk/regression_model_test.py +213 -0
- orca_sdk/telemetry.py +52 -13
- {orca_sdk-0.1.3.dist-info → orca_sdk-0.1.5.dist-info}/METADATA +1 -1
- {orca_sdk-0.1.3.dist-info → orca_sdk-0.1.5.dist-info}/RECORD +20 -20
- {orca_sdk-0.1.3.dist-info → orca_sdk-0.1.5.dist-info}/WHEEL +0 -0
|
@@ -187,18 +187,24 @@ def test_evaluate(classification_model, eval_datasource: Datasource, eval_datase
|
|
|
187
187
|
assert -1.0 <= result.anomaly_score_variance <= 1.0
|
|
188
188
|
|
|
189
189
|
assert result.pr_auc is not None
|
|
190
|
-
assert np.allclose(result.pr_auc, 0.
|
|
190
|
+
assert np.allclose(result.pr_auc, 0.83333)
|
|
191
191
|
assert result.pr_curve is not None
|
|
192
|
-
assert np.allclose(
|
|
193
|
-
|
|
194
|
-
|
|
192
|
+
assert np.allclose(
|
|
193
|
+
result.pr_curve["thresholds"],
|
|
194
|
+
[0.0, 0.3021204173564911, 0.30852025747299194, 0.6932827234268188, 0.6972201466560364],
|
|
195
|
+
)
|
|
196
|
+
assert np.allclose(result.pr_curve["precisions"], [0.5, 0.666666, 0.5, 1.0, 1.0])
|
|
197
|
+
assert np.allclose(result.pr_curve["recalls"], [1.0, 1.0, 0.5, 0.5, 0.0])
|
|
195
198
|
|
|
196
199
|
assert result.roc_auc is not None
|
|
197
|
-
assert np.allclose(result.roc_auc, 0.
|
|
200
|
+
assert np.allclose(result.roc_auc, 0.75)
|
|
198
201
|
assert result.roc_curve is not None
|
|
199
|
-
assert np.allclose(
|
|
200
|
-
|
|
201
|
-
|
|
202
|
+
assert np.allclose(
|
|
203
|
+
result.roc_curve["thresholds"],
|
|
204
|
+
[0.3021204173564911, 0.30852025747299194, 0.6932827234268188, 0.6972201466560364, 1.0],
|
|
205
|
+
)
|
|
206
|
+
assert np.allclose(result.roc_curve["false_positive_rates"], [1.0, 0.5, 0.5, 0.0, 0.0])
|
|
207
|
+
assert np.allclose(result.roc_curve["true_positive_rates"], [1.0, 1.0, 0.5, 0.5, 0.0])
|
|
202
208
|
|
|
203
209
|
|
|
204
210
|
def test_evaluate_datasource_with_nones_raises_error(classification_model: ClassificationModel, datasource: Datasource):
|
|
@@ -221,6 +227,139 @@ def test_evaluate_with_telemetry(classification_model: ClassificationModel, eval
|
|
|
221
227
|
assert all(p.expected_label == l for p, l in zip(predictions, eval_dataset["label"]))
|
|
222
228
|
|
|
223
229
|
|
|
230
|
+
def test_evaluate_with_partition_column_dataset(partitioned_classification_model: ClassificationModel):
|
|
231
|
+
"""Test evaluate with partition_column on a Dataset"""
|
|
232
|
+
# Create a test dataset with partition_id column
|
|
233
|
+
eval_dataset_with_partition = Dataset.from_list(
|
|
234
|
+
[
|
|
235
|
+
{"value": "soup is good", "label": 0, "partition_id": "p1"},
|
|
236
|
+
{"value": "cats are cute", "label": 1, "partition_id": "p1"},
|
|
237
|
+
{"value": "homemade soup recipes", "label": 0, "partition_id": "p2"},
|
|
238
|
+
{"value": "cats purr when happy", "label": 1, "partition_id": "p2"},
|
|
239
|
+
]
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
# Evaluate with partition_column
|
|
243
|
+
result = partitioned_classification_model.evaluate(
|
|
244
|
+
eval_dataset_with_partition,
|
|
245
|
+
partition_column="partition_id",
|
|
246
|
+
partition_filter_mode="exclude_global",
|
|
247
|
+
)
|
|
248
|
+
assert result is not None
|
|
249
|
+
assert isinstance(result, ClassificationMetrics)
|
|
250
|
+
assert isinstance(result.accuracy, float)
|
|
251
|
+
assert isinstance(result.f1_score, float)
|
|
252
|
+
assert isinstance(result.loss, float)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def test_evaluate_with_partition_column_include_global(partitioned_classification_model: ClassificationModel):
|
|
256
|
+
"""Test evaluate with partition_column and include_global mode"""
|
|
257
|
+
eval_dataset_with_partition = Dataset.from_list(
|
|
258
|
+
[
|
|
259
|
+
{"value": "soup is good", "label": 0, "partition_id": "p1"},
|
|
260
|
+
{"value": "cats are cute", "label": 1, "partition_id": "p1"},
|
|
261
|
+
]
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
# Evaluate with partition_column and include_global (default)
|
|
265
|
+
result = partitioned_classification_model.evaluate(
|
|
266
|
+
eval_dataset_with_partition,
|
|
267
|
+
partition_column="partition_id",
|
|
268
|
+
partition_filter_mode="include_global",
|
|
269
|
+
)
|
|
270
|
+
assert result is not None
|
|
271
|
+
assert isinstance(result, ClassificationMetrics)
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def test_evaluate_with_partition_column_exclude_global(partitioned_classification_model: ClassificationModel):
|
|
275
|
+
"""Test evaluate with partition_column and exclude_global mode"""
|
|
276
|
+
eval_dataset_with_partition = Dataset.from_list(
|
|
277
|
+
[
|
|
278
|
+
{"value": "soup is good", "label": 0, "partition_id": "p1"},
|
|
279
|
+
{"value": "cats are cute", "label": 1, "partition_id": "p1"},
|
|
280
|
+
]
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
# Evaluate with partition_column and exclude_global
|
|
284
|
+
result = partitioned_classification_model.evaluate(
|
|
285
|
+
eval_dataset_with_partition,
|
|
286
|
+
partition_column="partition_id",
|
|
287
|
+
partition_filter_mode="exclude_global",
|
|
288
|
+
)
|
|
289
|
+
assert result is not None
|
|
290
|
+
assert isinstance(result, ClassificationMetrics)
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def test_evaluate_with_partition_column_only_global(partitioned_classification_model: ClassificationModel):
|
|
294
|
+
"""Test evaluate with partition_filter_mode only_global"""
|
|
295
|
+
eval_dataset_with_partition = Dataset.from_list(
|
|
296
|
+
[
|
|
297
|
+
{"value": "cats are independent animals", "label": 1, "partition_id": None},
|
|
298
|
+
{"value": "i love the beach", "label": 1, "partition_id": None},
|
|
299
|
+
]
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
# Evaluate with only_global mode
|
|
303
|
+
result = partitioned_classification_model.evaluate(
|
|
304
|
+
eval_dataset_with_partition,
|
|
305
|
+
partition_column="partition_id",
|
|
306
|
+
partition_filter_mode="only_global",
|
|
307
|
+
)
|
|
308
|
+
assert result is not None
|
|
309
|
+
assert isinstance(result, ClassificationMetrics)
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
def test_evaluate_with_partition_column_ignore_partitions(partitioned_classification_model: ClassificationModel):
|
|
313
|
+
"""Test evaluate with partition_filter_mode ignore_partitions"""
|
|
314
|
+
eval_dataset_with_partition = Dataset.from_list(
|
|
315
|
+
[
|
|
316
|
+
{"value": "soup is good", "label": 0, "partition_id": "p1"},
|
|
317
|
+
{"value": "cats are cute", "label": 1, "partition_id": "p2"},
|
|
318
|
+
]
|
|
319
|
+
)
|
|
320
|
+
|
|
321
|
+
# Evaluate with ignore_partitions mode
|
|
322
|
+
result = partitioned_classification_model.evaluate(
|
|
323
|
+
eval_dataset_with_partition,
|
|
324
|
+
partition_column="partition_id",
|
|
325
|
+
partition_filter_mode="ignore_partitions",
|
|
326
|
+
)
|
|
327
|
+
assert result is not None
|
|
328
|
+
assert isinstance(result, ClassificationMetrics)
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
@pytest.mark.parametrize("data_type", ["dataset", "datasource"])
|
|
332
|
+
def test_evaluate_with_partition_column_datasource(partitioned_classification_model: ClassificationModel, data_type):
|
|
333
|
+
"""Test evaluate with partition_column on a Datasource"""
|
|
334
|
+
# Create a test datasource with partition_id column
|
|
335
|
+
eval_data_with_partition = [
|
|
336
|
+
{"value": "soup is good", "label": 0, "partition_id": "p1"},
|
|
337
|
+
{"value": "cats are cute", "label": 1, "partition_id": "p1"},
|
|
338
|
+
{"value": "homemade soup recipes", "label": 0, "partition_id": "p2"},
|
|
339
|
+
{"value": "cats purr when happy", "label": 1, "partition_id": "p2"},
|
|
340
|
+
]
|
|
341
|
+
|
|
342
|
+
if data_type == "dataset":
|
|
343
|
+
eval_data = Dataset.from_list(eval_data_with_partition)
|
|
344
|
+
result = partitioned_classification_model.evaluate(
|
|
345
|
+
eval_data,
|
|
346
|
+
partition_column="partition_id",
|
|
347
|
+
partition_filter_mode="exclude_global",
|
|
348
|
+
)
|
|
349
|
+
else:
|
|
350
|
+
eval_datasource = Datasource.from_list("eval_datasource_with_partition", eval_data_with_partition)
|
|
351
|
+
result = partitioned_classification_model.evaluate(
|
|
352
|
+
eval_datasource,
|
|
353
|
+
partition_column="partition_id",
|
|
354
|
+
partition_filter_mode="exclude_global",
|
|
355
|
+
)
|
|
356
|
+
|
|
357
|
+
assert result is not None
|
|
358
|
+
assert isinstance(result, ClassificationMetrics)
|
|
359
|
+
assert isinstance(result.accuracy, float)
|
|
360
|
+
assert isinstance(result.f1_score, float)
|
|
361
|
+
|
|
362
|
+
|
|
224
363
|
def test_predict(classification_model: ClassificationModel, label_names: list[str]):
|
|
225
364
|
predictions = classification_model.predict(["Do you love soup?", "Are cats cute?"])
|
|
226
365
|
assert len(predictions) == 2
|
|
@@ -284,6 +423,186 @@ def test_predict_constraint_violation(readonly_memoryset: LabeledMemoryset):
|
|
|
284
423
|
model.predict("test")
|
|
285
424
|
|
|
286
425
|
|
|
426
|
+
def test_predict_with_partition_id(partitioned_classification_model: ClassificationModel, label_names: list[str]):
|
|
427
|
+
"""Test predict with a specific partition_id"""
|
|
428
|
+
# Predict with partition_id p1 - should use memories from p1
|
|
429
|
+
prediction = partitioned_classification_model.predict(
|
|
430
|
+
"soup", partition_id="p1", partition_filter_mode="exclude_global"
|
|
431
|
+
)
|
|
432
|
+
assert prediction.label is not None
|
|
433
|
+
assert prediction.label_name in label_names
|
|
434
|
+
assert 0 <= prediction.confidence <= 1
|
|
435
|
+
assert prediction.logits is not None
|
|
436
|
+
assert len(prediction.logits) == 2
|
|
437
|
+
|
|
438
|
+
# Predict with partition_id p2 - should use memories from p2
|
|
439
|
+
prediction_p2 = partitioned_classification_model.predict(
|
|
440
|
+
"cats", partition_id="p2", partition_filter_mode="exclude_global"
|
|
441
|
+
)
|
|
442
|
+
assert prediction_p2.label is not None
|
|
443
|
+
assert prediction_p2.label_name in label_names
|
|
444
|
+
assert 0 <= prediction_p2.confidence <= 1
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
def test_predict_with_partition_id_include_global(
|
|
448
|
+
partitioned_classification_model: ClassificationModel, label_names: list[str]
|
|
449
|
+
):
|
|
450
|
+
"""Test predict with partition_id and include_global mode (default)"""
|
|
451
|
+
# Predict with partition_id p1 and include_global (default) - should include both p1 and global memories
|
|
452
|
+
prediction = partitioned_classification_model.predict(
|
|
453
|
+
"soup", partition_id="p1", partition_filter_mode="include_global"
|
|
454
|
+
)
|
|
455
|
+
assert prediction.label is not None
|
|
456
|
+
assert prediction.label_name in label_names
|
|
457
|
+
assert 0 <= prediction.confidence <= 1
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
def test_predict_with_partition_id_exclude_global(
|
|
461
|
+
partitioned_classification_model: ClassificationModel, label_names: list[str]
|
|
462
|
+
):
|
|
463
|
+
"""Test predict with partition_id and exclude_global mode"""
|
|
464
|
+
# Predict with partition_id p1 and exclude_global - should only use p1 memories
|
|
465
|
+
prediction = partitioned_classification_model.predict(
|
|
466
|
+
"soup", partition_id="p1", partition_filter_mode="exclude_global"
|
|
467
|
+
)
|
|
468
|
+
assert prediction.label is not None
|
|
469
|
+
assert prediction.label_name in label_names
|
|
470
|
+
assert 0 <= prediction.confidence <= 1
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
def test_predict_with_partition_id_only_global(
|
|
474
|
+
partitioned_classification_model: ClassificationModel, label_names: list[str]
|
|
475
|
+
):
|
|
476
|
+
"""Test predict with partition_filter_mode only_global"""
|
|
477
|
+
# Predict with only_global mode - should only use global memories
|
|
478
|
+
prediction = partitioned_classification_model.predict("cats", partition_filter_mode="only_global")
|
|
479
|
+
assert prediction.label is not None
|
|
480
|
+
assert prediction.label_name in label_names
|
|
481
|
+
assert 0 <= prediction.confidence <= 1
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
def test_predict_with_partition_id_ignore_partitions(
|
|
485
|
+
partitioned_classification_model: ClassificationModel, label_names: list[str]
|
|
486
|
+
):
|
|
487
|
+
"""Test predict with partition_filter_mode ignore_partitions"""
|
|
488
|
+
# Predict with ignore_partitions mode - should ignore partition filtering
|
|
489
|
+
prediction = partitioned_classification_model.predict("soup", partition_filter_mode="ignore_partitions")
|
|
490
|
+
assert prediction.label is not None
|
|
491
|
+
assert prediction.label_name in label_names
|
|
492
|
+
assert 0 <= prediction.confidence <= 1
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
def test_predict_batch_with_partition_id(partitioned_classification_model: ClassificationModel, label_names: list[str]):
|
|
496
|
+
"""Test batch predict with partition_id"""
|
|
497
|
+
# Batch predict with partition_id p1
|
|
498
|
+
predictions = partitioned_classification_model.predict(
|
|
499
|
+
["soup is good", "cats are cute"],
|
|
500
|
+
partition_id="p1",
|
|
501
|
+
partition_filter_mode="exclude_global",
|
|
502
|
+
)
|
|
503
|
+
assert len(predictions) == 2
|
|
504
|
+
assert all(p.label is not None for p in predictions)
|
|
505
|
+
assert all(p.label_name in label_names for p in predictions)
|
|
506
|
+
assert all(0 <= p.confidence <= 1 for p in predictions)
|
|
507
|
+
assert all(p.logits is not None and len(p.logits) == 2 for p in predictions)
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
def test_predict_with_partition_id_and_filters(
|
|
511
|
+
partitioned_classification_model: ClassificationModel, label_names: list[str]
|
|
512
|
+
):
|
|
513
|
+
"""Test predict with partition_id and filters"""
|
|
514
|
+
# Predict with partition_id and filters
|
|
515
|
+
prediction = partitioned_classification_model.predict(
|
|
516
|
+
"soup",
|
|
517
|
+
partition_id="p1",
|
|
518
|
+
partition_filter_mode="exclude_global",
|
|
519
|
+
filters=[("key", "==", "g1")],
|
|
520
|
+
)
|
|
521
|
+
assert prediction.label is not None
|
|
522
|
+
assert prediction.label_name in label_names
|
|
523
|
+
assert 0 <= prediction.confidence <= 1
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
def test_predict_batch_with_list_of_partition_ids(
|
|
527
|
+
partitioned_classification_model: ClassificationModel, label_names: list[str]
|
|
528
|
+
):
|
|
529
|
+
"""Test batch predict with a list of partition_ids (one for each query input)"""
|
|
530
|
+
# Batch predict with a list of partition_ids - one for each input
|
|
531
|
+
# First input uses p1, second input uses p2
|
|
532
|
+
predictions = partitioned_classification_model.predict(
|
|
533
|
+
["soup is good", "cats are cute"],
|
|
534
|
+
partition_id=["p1", "p2"],
|
|
535
|
+
partition_filter_mode="exclude_global",
|
|
536
|
+
)
|
|
537
|
+
assert len(predictions) == 2
|
|
538
|
+
assert all(p.label is not None for p in predictions)
|
|
539
|
+
assert all(p.label_name in label_names for p in predictions)
|
|
540
|
+
assert all(0 <= p.confidence <= 1 for p in predictions)
|
|
541
|
+
assert all(p.logits is not None and len(p.logits) == 2 for p in predictions)
|
|
542
|
+
|
|
543
|
+
# Verify that predictions were made using the correct partitions
|
|
544
|
+
# Each prediction should use memories from its respective partition
|
|
545
|
+
assert predictions[0].input_value == "soup is good"
|
|
546
|
+
assert predictions[1].input_value == "cats are cute"
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
@pytest.mark.asyncio
|
|
550
|
+
async def test_predict_async_with_partition_id(
|
|
551
|
+
partitioned_classification_model: ClassificationModel, label_names: list[str]
|
|
552
|
+
):
|
|
553
|
+
"""Test async predict with partition_id"""
|
|
554
|
+
# Async predict with partition_id p1
|
|
555
|
+
prediction = await partitioned_classification_model.apredict(
|
|
556
|
+
"soup", partition_id="p1", partition_filter_mode="exclude_global"
|
|
557
|
+
)
|
|
558
|
+
assert prediction.label is not None
|
|
559
|
+
assert prediction.label_name in label_names
|
|
560
|
+
assert 0 <= prediction.confidence <= 1
|
|
561
|
+
assert prediction.logits is not None
|
|
562
|
+
assert len(prediction.logits) == 2
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
@pytest.mark.asyncio
|
|
566
|
+
async def test_predict_async_batch_with_partition_id(
|
|
567
|
+
partitioned_classification_model: ClassificationModel, label_names: list[str]
|
|
568
|
+
):
|
|
569
|
+
"""Test async batch predict with partition_id"""
|
|
570
|
+
# Async batch predict with partition_id p1
|
|
571
|
+
predictions = await partitioned_classification_model.apredict(
|
|
572
|
+
["soup is good", "cats are cute"],
|
|
573
|
+
partition_id="p1",
|
|
574
|
+
partition_filter_mode="exclude_global",
|
|
575
|
+
)
|
|
576
|
+
assert len(predictions) == 2
|
|
577
|
+
assert all(p.label is not None for p in predictions)
|
|
578
|
+
assert all(p.label_name in label_names for p in predictions)
|
|
579
|
+
assert all(0 <= p.confidence <= 1 for p in predictions)
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
@pytest.mark.asyncio
|
|
583
|
+
async def test_predict_async_batch_with_list_of_partition_ids(
|
|
584
|
+
partitioned_classification_model: ClassificationModel, label_names: list[str]
|
|
585
|
+
):
|
|
586
|
+
"""Test async batch predict with a list of partition_ids (one for each query input)"""
|
|
587
|
+
# Async batch predict with a list of partition_ids - one for each input
|
|
588
|
+
# First input uses p1, second input uses p2
|
|
589
|
+
predictions = await partitioned_classification_model.apredict(
|
|
590
|
+
["soup is good", "cats are cute"],
|
|
591
|
+
partition_id=["p1", "p2"],
|
|
592
|
+
partition_filter_mode="exclude_global",
|
|
593
|
+
)
|
|
594
|
+
assert len(predictions) == 2
|
|
595
|
+
assert all(p.label is not None for p in predictions)
|
|
596
|
+
assert all(p.label_name in label_names for p in predictions)
|
|
597
|
+
assert all(0 <= p.confidence <= 1 for p in predictions)
|
|
598
|
+
assert all(p.logits is not None and len(p.logits) == 2 for p in predictions)
|
|
599
|
+
|
|
600
|
+
# Verify that predictions were made using the correct partitions
|
|
601
|
+
# Each prediction should use memories from its respective partition
|
|
602
|
+
assert predictions[0].input_value == "soup is good"
|
|
603
|
+
assert predictions[1].input_value == "cats are cute"
|
|
604
|
+
|
|
605
|
+
|
|
287
606
|
def test_record_prediction_feedback(classification_model: ClassificationModel):
|
|
288
607
|
predictions = classification_model.predict(["Do you love soup?", "Are cats cute?"])
|
|
289
608
|
expected_labels = [0, 1]
|