forked from microsoft/promptflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_flow_run.py
2043 lines (1801 loc) · 86.8 KB
/
test_flow_run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import os
import shutil
import sys
import tempfile
import uuid
from pathlib import Path
from typing import Callable
from unittest.mock import patch
import numpy as np
import pandas as pd
import pytest
from _constants import PROMPTFLOW_ROOT
from marshmallow import ValidationError
from pytest_mock import MockerFixture
from promptflow._constants import PROMPTFLOW_CONNECTIONS, FlowType
from promptflow._sdk._constants import (
FLOW_DIRECTORY_MACRO_IN_CONFIG,
PROMPT_FLOW_DIR_NAME,
FlowRunProperties,
LocalStorageFilenames,
RunStatus,
)
from promptflow._sdk._errors import (
ConnectionNotFoundError,
InvalidFlowError,
InvalidRunError,
InvalidRunStatusError,
RunExistsError,
RunNotFoundError,
)
from promptflow._sdk._load_functions import load_flow, load_run
from promptflow._sdk._orchestrator.utils import SubmitterHelper
from promptflow._sdk._run_functions import create_yaml_run
from promptflow._sdk._utilities.general_utils import _get_additional_includes
from promptflow._sdk._utilities.tracing_utils import _parse_otel_span_status_code
from promptflow._sdk.entities import Run
from promptflow._sdk.operations._local_storage_operations import LocalStorageOperations
from promptflow._utils.context_utils import _change_working_dir, inject_sys_path
from promptflow._utils.yaml_utils import load_yaml
from promptflow.client import PFClient
from promptflow.connections import AzureOpenAIConnection
from promptflow.core import AzureOpenAIModelConfiguration, OpenAIModelConfiguration
from promptflow.exceptions import UserErrorException
TEST_ROOT = PROMPTFLOW_ROOT / "tests"
CONNECTION_FILE = (PROMPTFLOW_ROOT / "connections.json").resolve().absolute().as_posix()
FLOWS_DIR = PROMPTFLOW_ROOT / "tests/test_configs/flows"
EAGER_FLOWS_DIR = PROMPTFLOW_ROOT / "tests/test_configs/eager_flows"
RUNS_DIR = PROMPTFLOW_ROOT / "tests/test_configs/runs"
DATAS_DIR = PROMPTFLOW_ROOT / "tests/test_configs/datas"
def my_entry(input1: str):
return input1
async def my_async_entry(input2: str):
return input2
def create_run_against_multi_line_data(client) -> Run:
return client.run(
flow=f"{FLOWS_DIR}/web_classification",
data=f"{DATAS_DIR}/webClassification3.jsonl",
column_mapping={"url": "${data.url}"},
)
def create_run_against_multi_line_data_without_llm(client: PFClient) -> Run:
return client.run(
flow=f"{FLOWS_DIR}/print_env_var",
data=f"{DATAS_DIR}/env_var_names.jsonl",
)
def create_run_against_run(client, run: Run) -> Run:
return client.run(
flow=f"{FLOWS_DIR}/classification_accuracy_evaluation",
data=f"{DATAS_DIR}/webClassification3.jsonl",
run=run.name,
column_mapping={
"groundtruth": "${data.answer}",
"prediction": "${run.outputs.category}",
"variant_id": "${data.variant_id}",
},
)
def assert_run_with_invalid_column_mapping(client: PFClient, run: Run) -> None:
assert run.status == RunStatus.FAILED
with pytest.raises(InvalidRunStatusError):
client.stream(run.name)
local_storage = LocalStorageOperations(run)
assert os.path.exists(local_storage._exception_path)
exception = local_storage.load_exception()
assert "The input for batch run is incorrect. Couldn't find these mapping relations" in exception["message"]
assert exception["code"] == "UserError"
assert exception["innerError"]["innerError"]["code"] == "BulkRunException"
@pytest.mark.usefixtures(
"use_secrets_config_file", "recording_injection", "setup_local_connection", "install_custom_tool_pkg"
)
@pytest.mark.sdk_test
@pytest.mark.e2etest
class TestFlowRun:
def test_basic_flow_bulk_run(self, azure_open_ai_connection: AzureOpenAIConnection, pf) -> None:
data_path = f"{DATAS_DIR}/webClassification3.jsonl"
pf.run(flow=f"{FLOWS_DIR}/web_classification", data=data_path)
# Test repeated execute flow run
pf.run(flow=f"{FLOWS_DIR}/web_classification", data=data_path)
pf.run(flow=f"{FLOWS_DIR}/web_classification_v1", data=data_path)
pf.run(flow=f"{FLOWS_DIR}/web_classification_v2", data=data_path)
# TODO: check details
# df = pf.show_details(baseline, v1, v2)
def test_basic_run_bulk(self, azure_open_ai_connection: AzureOpenAIConnection, local_client, pf):
column_mapping = {"url": "${data.url}"}
result = pf.run(
flow=f"{FLOWS_DIR}/web_classification",
data=f"{DATAS_DIR}/webClassification1.jsonl",
column_mapping=column_mapping,
)
local_storage = LocalStorageOperations(result)
detail = local_storage.load_detail()
tuning_node = next((x for x in detail["node_runs"] if x["node"] == "summarize_text_content"), None)
# used default variant config
assert tuning_node["inputs"]["temperature"] == 0.3
assert "variant_0" in result.name
run = local_client.runs.get(name=result.name)
assert run.status == "Completed"
assert run.column_mapping == column_mapping
# write to user_dir/.promptflow/.runs
assert ".promptflow" in run.properties["output_path"]
def test_local_storage_delete(self, pf):
result = pf.run(flow=f"{FLOWS_DIR}/print_env_var", data=f"{DATAS_DIR}/env_var_names.jsonl")
local_storage = LocalStorageOperations(result)
local_storage.delete()
assert not os.path.exists(local_storage._outputs_path)
def test_flow_run_delete(self, pf):
result = pf.run(flow=f"{FLOWS_DIR}/print_env_var", data=f"{DATAS_DIR}/env_var_names.jsonl")
local_storage = LocalStorageOperations(result)
output_path = local_storage.path
# delete new created run by name
pf.runs.delete(result.name)
# check folders and dbs are deleted
assert not os.path.exists(output_path)
from promptflow._sdk._orm import RunInfo as ORMRun
pytest.raises(RunNotFoundError, lambda: ORMRun.get(result.name))
pytest.raises(RunNotFoundError, lambda: pf.runs.get(result.name))
def test_flow_run_delete_fake_id_raise(self, pf: PFClient):
run = "fake_run_id"
# delete new created run by name
pytest.raises(RunNotFoundError, lambda: pf.runs.delete(name=run))
@pytest.mark.skipif(sys.platform == "win32", reason="Windows doesn't support chmod, just test permission errors")
def test_flow_run_delete_invalid_permission_raise(self, pf: PFClient):
result = pf.run(flow=f"{FLOWS_DIR}/print_env_var", data=f"{DATAS_DIR}/env_var_names.jsonl")
local_storage = LocalStorageOperations(result)
output_path = local_storage.path
os.chmod(output_path, 0o555)
# delete new created run by name
pytest.raises(InvalidRunError, lambda: pf.runs.delete(name=result.name))
# Change folder permission back
os.chmod(output_path, 0o755)
pf.runs.delete(name=result.name)
assert not os.path.exists(output_path)
def test_visualize_run_with_referenced_run_deleted(self, pf: PFClient):
run_id = str(uuid.uuid4())
run = load_run(
source=f"{RUNS_DIR}/sample_bulk_run.yaml",
params_override=[{"name": run_id}],
)
run_a = pf.runs.create_or_update(run=run)
local_storage_a = LocalStorageOperations(run_a)
output_path_a = local_storage_a.path
run = load_run(source=f"{RUNS_DIR}/sample_eval_run.yaml", params_override=[{"run": run_id}])
run_b = pf.runs.create_or_update(run=run)
local_storage_b = LocalStorageOperations(run_b)
output_path_b = local_storage_b.path
pf.runs.delete(run_a.name)
assert not os.path.exists(output_path_a)
assert os.path.exists(output_path_b)
# visualize doesn't raise error
pf.runs.visualize(run_b.name)
def test_basic_flow_with_variant(self, azure_open_ai_connection: AzureOpenAIConnection, local_client, pf) -> None:
result = pf.run(
flow=f"{FLOWS_DIR}/web_classification",
data=f"{DATAS_DIR}/webClassification1.jsonl",
column_mapping={"url": "${data.url}"},
variant="${summarize_text_content.variant_0}",
)
local_storage = LocalStorageOperations(result)
detail = local_storage.load_detail()
tuning_node = next((x for x in detail["node_runs"] if x["node"] == "summarize_text_content"), None)
assert "variant_0" in result.name
# used variant_0 config
assert tuning_node["inputs"]["temperature"] == 0.2
result = pf.run(
flow=f"{FLOWS_DIR}/web_classification",
data=f"{DATAS_DIR}/webClassification1.jsonl",
column_mapping={"url": "${data.url}"},
variant="${summarize_text_content.variant_1}",
)
local_storage = LocalStorageOperations(result)
detail = local_storage.load_detail()
tuning_node = next((x for x in detail["node_runs"] if x["node"] == "summarize_text_content"), None)
assert "variant_1" in result.name
# used variant_1 config
assert tuning_node["inputs"]["temperature"] == 0.3
def test_run_bulk_error(self, pf):
# path not exist
with pytest.raises(UserErrorException) as e:
pf.run(
flow=f"{FLOWS_DIR}/not_exist",
data=f"{DATAS_DIR}/webClassification3.jsonl",
column_mapping={"question": "${data.question}", "context": "${data.context}"},
variant="${summarize_text_content.variant_0}",
)
assert "not exist" in str(e.value)
# tuning_node not exist
with pytest.raises(InvalidFlowError) as e:
pf.run(
flow=f"{FLOWS_DIR}/web_classification",
data=f"{DATAS_DIR}/webClassification3.jsonl",
column_mapping={"question": "${data.question}", "context": "${data.context}"},
variant="${not_exist.variant_0}",
)
assert "Node not_exist not found in flow" in str(e.value)
# invalid variant format
with pytest.raises(UserErrorException) as e:
pf.run(
flow=f"{FLOWS_DIR}/web_classification",
data=f"{DATAS_DIR}/webClassification3.jsonl",
column_mapping={"question": "${data.question}", "context": "${data.context}"},
variant="v",
)
assert "Invalid variant format: v, variant should be in format of ${TUNING_NODE.VARIANT}" in str(e.value)
def test_basic_evaluation(self, azure_open_ai_connection: AzureOpenAIConnection, local_client, pf):
result = pf.run(
flow=f"{FLOWS_DIR}/print_env_var",
data=f"{DATAS_DIR}/env_var_names.jsonl",
)
assert local_client.runs.get(result.name).status == "Completed"
eval_result = pf.run(
flow=f"{FLOWS_DIR}/classification_accuracy_evaluation",
run=result.name,
column_mapping={
"prediction": "${run.outputs.output}",
# evaluation reference run.inputs
# NOTE: we need this value to guard behavior when a run reference another run's inputs
"variant_id": "${run.inputs.key}",
# can reference other columns in data which doesn't exist in base run's inputs
"groundtruth": "${run.inputs.extra_key}",
},
)
assert local_client.runs.get(eval_result.name).status == "Completed"
def test_flow_demo(self, azure_open_ai_connection, pf):
data_path = f"{DATAS_DIR}/webClassification3.jsonl"
column_mapping = {
"groundtruth": "${data.answer}",
"prediction": "${run.outputs.category}",
"variant_id": "${data.variant_id}",
}
metrics = {}
for flow_name, output_key in [
("web_classification", "baseline"),
("web_classification_v1", "v1"),
("web_classification_v2", "v2"),
]:
v = pf.run(flow=f"{FLOWS_DIR}/web_classification", data=data_path)
metrics[output_key] = pf.run(
flow=f"{FLOWS_DIR}/classification_accuracy_evaluation",
data=data_path,
run=v,
column_mapping=column_mapping,
)
def test_submit_run_from_yaml(self, local_client, pf):
run_id = str(uuid.uuid4())
run = create_yaml_run(source=f"{RUNS_DIR}/sample_bulk_run.yaml", params_override=[{"name": run_id}])
assert local_client.runs.get(run.name).status == "Completed"
eval_run = create_yaml_run(
source=f"{RUNS_DIR}/sample_eval_run.yaml",
params_override=[{"run": run_id}],
)
assert local_client.runs.get(eval_run.name).status == "Completed"
@pytest.mark.usefixtures("enable_logger_propagate")
def test_submit_run_with_extra_params(self, pf, caplog):
run_id = str(uuid.uuid4())
run = create_yaml_run(source=f"{RUNS_DIR}/extra_field.yaml", params_override=[{"name": run_id}])
assert pf.runs.get(run.name).status == "Completed"
assert "Run schema validation warnings. Unknown fields found" in caplog.text
def test_run_with_connection(self, local_client, local_aoai_connection, pf):
# remove connection file to test connection resolving
os.environ.pop(PROMPTFLOW_CONNECTIONS)
result = pf.run(
flow=f"{FLOWS_DIR}/web_classification",
data=f"{DATAS_DIR}/webClassification1.jsonl",
column_mapping={"url": "${data.url}"},
)
local_storage = LocalStorageOperations(result)
detail = local_storage.load_detail()
tuning_node = next((x for x in detail["node_runs"] if x["node"] == "summarize_text_content"), None)
# used default variant config
assert tuning_node["inputs"]["temperature"] == 0.3
run = local_client.runs.get(name=result.name)
assert run.status == "Completed"
def test_run_with_connection_overwrite(self, local_client, local_aoai_connection, local_alt_aoai_connection, pf):
result = pf.run(
flow=f"{FLOWS_DIR}/web_classification",
data=f"{DATAS_DIR}/webClassification1.jsonl",
connections={"classify_with_llm": {"connection": "new_ai_connection"}},
)
run = local_client.runs.get(name=result.name)
assert run.status == "Completed"
def test_custom_connection_overwrite(self, local_client, local_custom_connection, pf):
result = pf.run(
flow=f"{FLOWS_DIR}/custom_connection_flow",
data=f"{DATAS_DIR}/env_var_names.jsonl",
connections={"print_env": {"connection": "test_custom_connection"}},
)
run = local_client.runs.get(name=result.name)
assert run.status == "Completed"
# overwrite non-exist connection
with pytest.raises(InvalidFlowError) as e:
pf.run(
flow=f"{FLOWS_DIR}/custom_connection_flow",
data=f"{DATAS_DIR}/env_var_names.jsonl",
connections={"print_env": {"new_connection": "test_custom_connection"}},
)
assert "Unsupported llm connection overwrite keys" in str(e.value)
def test_basic_flow_with_package_tool_with_custom_strong_type_connection(
self, install_custom_tool_pkg, local_client, pf
):
result = pf.run(
flow=f"{FLOWS_DIR}/flow_with_package_tool_with_custom_strong_type_connection",
data=f"{FLOWS_DIR}/flow_with_package_tool_with_custom_strong_type_connection/data.jsonl",
connections={"My_First_Tool_00f8": {"connection": "custom_strong_type_connection"}},
)
run = local_client.runs.get(name=result.name)
assert run.status == "Completed"
def test_basic_flow_with_script_tool_with_custom_strong_type_connection(
self, install_custom_tool_pkg, local_client, pf
):
# Prepare custom connection
from promptflow.connections import CustomConnection
conn = CustomConnection(name="custom_connection_2", secrets={"api_key": "test"}, configs={"api_url": "test"})
local_client.connections.create_or_update(conn)
result = pf.run(
flow=f"{FLOWS_DIR}/flow_with_script_tool_with_custom_strong_type_connection",
data=f"{FLOWS_DIR}/flow_with_script_tool_with_custom_strong_type_connection/data.jsonl",
)
run = local_client.runs.get(name=result.name)
assert run.status == "Completed"
def test_run_with_connection_overwrite_non_exist(self, local_client, local_aoai_connection, pf):
# overwrite non_exist connection
with pytest.raises(ConnectionNotFoundError):
pf.run(
flow=f"{FLOWS_DIR}/web_classification",
data=f"{DATAS_DIR}/webClassification1.jsonl",
connections={"classify_with_llm": {"connection": "Not_exist"}},
)
def test_run_reference_failed_run(self, pf):
failed_run = pf.run(
flow=f"{FLOWS_DIR}/failed_flow",
data=f"{DATAS_DIR}/webClassification1.jsonl",
column_mapping={"text": "${data.url}"},
)
# "update" run status to failed since currently all run will be completed unless there's bug
pf.runs.update(
name=failed_run.name,
status="Failed",
)
run_name = str(uuid.uuid4())
with pytest.raises(UserErrorException) as e:
pf.run(
name=run_name,
flow=f"{FLOWS_DIR}/custom_connection_flow",
run=failed_run,
connections={"print_env": {"connection": "test_custom_connection"}},
)
assert "is not completed, got status" in str(e.value)
# run should not be created
with pytest.raises(RunNotFoundError):
pf.runs.get(name=run_name)
def test_referenced_output_not_exist(self, pf: PFClient) -> None:
# failed run won't generate output
failed_run = pf.run(
flow=f"{FLOWS_DIR}/failed_flow",
data=f"{DATAS_DIR}/webClassification1.jsonl",
column_mapping={"text": "${data.url}"},
)
run_name = str(uuid.uuid4())
run = pf.run(
name=run_name,
run=failed_run,
flow=f"{FLOWS_DIR}/failed_flow",
column_mapping={"text": "${run.outputs.text}"},
)
assert_run_with_invalid_column_mapping(pf, run)
def test_connection_overwrite_file(self, local_client, local_aoai_connection):
run = create_yaml_run(
source=f"{RUNS_DIR}/run_with_connections.yaml",
)
run = local_client.runs.get(name=run.name)
assert run.status == "Completed"
def test_connection_overwrite_model(self, local_client, local_aoai_connection):
run = create_yaml_run(
source=f"{RUNS_DIR}/run_with_connections_model.yaml",
)
run = local_client.runs.get(name=run.name)
assert run.status == "Completed"
def test_resolve_connection(self, local_client, local_aoai_connection):
flow = load_flow(f"{FLOWS_DIR}/web_classification_no_variants")
connections = SubmitterHelper.resolve_connections(flow, local_client)
assert local_aoai_connection.name in connections
def test_run_with_env_overwrite(self, local_client, local_aoai_connection):
run = create_yaml_run(
source=f"{RUNS_DIR}/run_with_env.yaml",
)
outputs = local_client.runs._get_outputs(run=run)
assert outputs["output"][0] == local_aoai_connection.api_base
def test_pf_run_with_env_overwrite(self, local_client, local_aoai_connection, pf):
run = pf.run(
flow=f"{FLOWS_DIR}/print_env_var",
data=f"{DATAS_DIR}/env_var_names.jsonl",
environment_variables={"API_BASE": "${azure_open_ai_connection.api_base}"},
)
outputs = local_client.runs._get_outputs(run=run)
assert outputs["output"][0] == local_aoai_connection.api_base
def test_eval_run_not_exist(self, pf):
name = str(uuid.uuid4())
with pytest.raises(RunNotFoundError) as e:
pf.runs.create_or_update(
run=Run(
name=name,
flow=Path(f"{FLOWS_DIR}/classification_accuracy_evaluation"),
run="not_exist",
column_mapping={
"groundtruth": "${data.answer}",
"prediction": "${run.outputs.category}",
# evaluation reference run.inputs
"url": "${run.inputs.url}",
},
)
)
assert "Run name 'not_exist' cannot be found" in str(e.value)
# run should not be created
with pytest.raises(RunNotFoundError):
pf.runs.get(name=name)
def test_eval_run_data_deleted(self, pf):
with tempfile.TemporaryDirectory() as temp_dir:
shutil.copy(f"{DATAS_DIR}/env_var_names.jsonl", temp_dir)
result = pf.run(
flow=f"{FLOWS_DIR}/print_env_var",
data=f"{temp_dir}/env_var_names.jsonl",
)
assert pf.runs.get(result.name).status == "Completed"
# delete original run's input data
os.remove(f"{temp_dir}/env_var_names.jsonl")
with pytest.raises(UserErrorException) as e:
pf.run(
flow=f"{FLOWS_DIR}/classification_accuracy_evaluation",
run=result.name,
column_mapping={
"prediction": "${run.outputs.output}",
# evaluation reference run.inputs
# NOTE: we need this value to guard behavior when a run reference another run's inputs
"variant_id": "${run.inputs.key}",
# can reference other columns in data which doesn't exist in base run's inputs
"groundtruth": "${run.inputs.extra_key}",
},
)
assert "Please make sure it exists and not deleted." in str(e.value)
def test_eval_run_data_not_exist(self, pf):
base_run = pf.run(
flow=f"{FLOWS_DIR}/print_env_var",
data=f"{DATAS_DIR}/env_var_names.jsonl",
)
assert pf.runs.get(base_run.name).status == "Completed"
eval_run = pf.run(
flow=f"{FLOWS_DIR}/classification_accuracy_evaluation",
run=base_run.name,
column_mapping={
"prediction": "${run.outputs.output}",
# evaluation reference run.inputs
# NOTE: we need this value to guard behavior when a run reference another run's inputs
"variant_id": "${run.inputs.key}",
# can reference other columns in data which doesn't exist in base run's inputs
"groundtruth": "${run.inputs.extra_key}",
},
)
result = pf.run(
flow=f"{FLOWS_DIR}/classification_accuracy_evaluation",
run=eval_run.name,
column_mapping={
"prediction": "${run.outputs.output}",
# evaluation reference run.inputs
# NOTE: we need this value to guard behavior when a run reference another run's inputs
"variant_id": "${run.inputs.key}",
# can reference other columns in data which doesn't exist in base run's inputs
"groundtruth": "${run.inputs.extra_key}",
},
)
# Run failed because run inputs data is None, and error will be in the run output error.json
assert result.status == "Failed"
def test_create_run_with_tags(self, pf):
name = str(uuid.uuid4())
display_name = "test_run_with_tags"
tags = {"key1": "tag1"}
run = pf.run(
name=name,
display_name=display_name,
tags=tags,
flow=f"{FLOWS_DIR}/print_env_var",
data=f"{DATAS_DIR}/env_var_names.jsonl",
environment_variables={"API_BASE": "${azure_open_ai_connection.api_base}"},
)
assert run.name == name
assert "test_run_with_tags" == run.display_name
assert run.tags == tags
def test_run_display_name(self, pf):
# use run name if not specify display_name
run = pf.runs.create_or_update(
run=Run(
flow=Path(f"{FLOWS_DIR}/print_env_var"),
data=f"{DATAS_DIR}/env_var_names.jsonl",
environment_variables={"API_BASE": "${azure_open_ai_connection.api_base}"},
)
)
assert run.display_name == run.name
assert "print_env_var" in run.display_name
# will respect if specified in run
base_run = pf.runs.create_or_update(
run=Run(
flow=Path(f"{FLOWS_DIR}/print_env_var"),
data=f"{DATAS_DIR}/env_var_names.jsonl",
environment_variables={"API_BASE": "${azure_open_ai_connection.api_base}"},
display_name="my_run",
)
)
assert base_run.display_name == "my_run"
run = pf.runs.create_or_update(
run=Run(
flow=Path(f"{FLOWS_DIR}/print_env_var"),
data=f"{DATAS_DIR}/env_var_names.jsonl",
environment_variables={"API_BASE": "${azure_open_ai_connection.api_base}"},
display_name="my_run_${variant_id}_${run}",
run=base_run,
)
)
assert run.display_name == f"my_run_variant_0_{base_run.name}"
run = pf.runs.create_or_update(
run=Run(
flow=Path(f"{FLOWS_DIR}/print_env_var"),
data=f"{DATAS_DIR}/env_var_names.jsonl",
environment_variables={"API_BASE": "${azure_open_ai_connection.api_base}"},
display_name="my_run_${timestamp}",
run=base_run,
)
)
assert "${timestamp}" not in run.display_name
def test_run_dump(self, azure_open_ai_connection: AzureOpenAIConnection, pf) -> None:
data_path = f"{DATAS_DIR}/webClassification3.jsonl"
run = pf.run(flow=f"{FLOWS_DIR}/web_classification", data=data_path)
# in fact, `pf.run` will internally query the run from db in `RunSubmitter`
# explicitly call ORM get here to emphasize the dump operatoin
# if no dump operation, a RunNotFoundError will be raised here
pf.runs.get(run.name)
def test_run_list(self, azure_open_ai_connection: AzureOpenAIConnection, pf) -> None:
# create a run to ensure there is at least one run in the db
data_path = f"{DATAS_DIR}/webClassification3.jsonl"
pf.run(flow=f"{FLOWS_DIR}/web_classification", data=data_path)
# not specify `max_result` here, so that if there are legacy runs in the db
# list runs API can collect them, and can somehow cover legacy schema
runs = pf.runs.list()
assert len(runs) >= 1
def test_stream_run_summary(self, azure_open_ai_connection: AzureOpenAIConnection, local_client, capfd, pf) -> None:
data_path = f"{DATAS_DIR}/webClassification3.jsonl"
run = pf.run(flow=f"{FLOWS_DIR}/web_classification", data=data_path)
local_client.runs.stream(run.name)
out, _ = capfd.readouterr()
print(out)
assert 'Run status: "Completed"' in out
assert "Output path: " in out
def test_stream_incomplete_run_summary(
self, azure_open_ai_connection: AzureOpenAIConnection, local_client, capfd, pf
) -> None:
# use wrong data to create a failed run
data_path = f"{DATAS_DIR}/webClassification3.jsonl"
name = str(uuid.uuid4())
run = pf.run(
flow=f"{FLOWS_DIR}/failed_flow",
data=data_path,
column_mapping={"text": "${data.url}"},
name=name,
)
local_client.runs.stream(run.name)
# assert error message in stream API
out, _ = capfd.readouterr()
assert 'Run status: "Completed"' in out
# won't print exception, use can get it from run._to_dict()
# assert "failed with exception" in out
def test_run_data_not_provided(self, pf):
with pytest.raises(ValueError) as e:
pf.run(
flow=f"{FLOWS_DIR}/web_classification",
)
assert "at least one of data or run must be provided" in str(e)
def test_get_details(self, azure_open_ai_connection: AzureOpenAIConnection, pf) -> None:
data_path = f"{DATAS_DIR}/webClassification3.jsonl"
run = pf.run(
flow=f"{FLOWS_DIR}/web_classification",
data=data_path,
column_mapping={"url": "${data.url}"},
)
from promptflow._sdk.operations._local_storage_operations import LocalStorageOperations
local_storage = LocalStorageOperations(run)
# there should be line_number in original DataFrame, but not in details DataFrame
# as we will set index on line_number to ensure the order
outputs = pd.read_json(local_storage._outputs_path, orient="records", lines=True)
details = pf.get_details(run)
assert "line_number" in outputs and "line_number" not in details
def test_visualize_run(self, azure_open_ai_connection: AzureOpenAIConnection, pf) -> None:
data_path = f"{DATAS_DIR}/webClassification3.jsonl"
run1 = pf.run(
flow=f"{FLOWS_DIR}/web_classification",
data=data_path,
column_mapping={"url": "${data.url}"},
)
run2 = pf.run(
flow=f"{FLOWS_DIR}/classification_accuracy_evaluation",
data=data_path,
run=run1.name,
column_mapping={
"groundtruth": "${data.answer}",
"prediction": "${run.outputs.category}",
"variant_id": "${data.variant_id}",
},
)
pf.visualize([run1, run2])
def test_incomplete_run_visualize(
self, azure_open_ai_connection: AzureOpenAIConnection, pf, capfd: pytest.CaptureFixture
) -> None:
failed_run = pf.run(
flow=f"{FLOWS_DIR}/failed_flow",
data=f"{DATAS_DIR}/webClassification1.jsonl",
column_mapping={"text": "${data.url}"},
)
# "update" run status to failed since currently all run will be completed unless there's bug
pf.runs.update(
name=failed_run.name,
status="Failed",
)
# patch logger.error to print, so that we can capture the error message using capfd
from promptflow._sdk.operations import _run_operations
_run_operations.logger.error = print
pf.visualize(failed_run)
captured = capfd.readouterr()
expected_error_message = (
f"Cannot visualize non-completed run. Run {failed_run.name!r} is not completed, the status is 'Failed'."
)
assert expected_error_message in captured.out
def test_flow_bulk_run_with_additional_includes(self, azure_open_ai_connection: AzureOpenAIConnection, pf):
data_path = f"{DATAS_DIR}/webClassification3.jsonl"
run = pf.run(flow=f"{FLOWS_DIR}/web_classification_with_additional_include", data=data_path)
additional_includes = _get_additional_includes(run.flow / "flow.dag.yaml")
snapshot_path = Path.home() / ".promptflow" / ".runs" / run.name / "snapshot"
for item in additional_includes:
assert (snapshot_path / Path(item).name).exists()
# Addition includes in snapshot is removed
additional_includes = _get_additional_includes(snapshot_path / "flow.dag.yaml")
assert not additional_includes
def test_input_mapping_source_not_found_error(self, azure_open_ai_connection: AzureOpenAIConnection, pf):
# input_mapping source not found error won't create run
name = str(uuid.uuid4())
data_path = f"{DATAS_DIR}/webClassification3.jsonl"
run = pf.run(
flow=f"{FLOWS_DIR}/web_classification",
data=data_path,
column_mapping={"not_exist": "${data.not_exist_key}"},
name=name,
)
assert_run_with_invalid_column_mapping(pf, run)
def test_input_mapping_with_dict(self, azure_open_ai_connection: AzureOpenAIConnection, pf):
data_path = f"{DATAS_DIR}/webClassification3.jsonl"
run = pf.run(
flow=f"{FLOWS_DIR}/flow_with_dict_input",
data=data_path,
column_mapping={"key": {"value": "1"}, "url": "${data.url}"},
)
outputs = pf.runs._get_outputs(run=run)
assert "dict" in outputs["output"][0]
def test_run_exist_error(self, pf):
name = str(uuid.uuid4())
data_path = f"{DATAS_DIR}/webClassification3.jsonl"
pf.run(
name=name,
flow=f"{FLOWS_DIR}/flow_with_dict_input",
data=data_path,
column_mapping={"key": {"value": "1"}, "url": "${data.url}"},
)
# create a new run won't affect original run
with pytest.raises(RunExistsError):
pf.run(
name=name,
flow=f"{FLOWS_DIR}/flow_with_dict_input",
data=data_path,
column_mapping={"key": {"value": "1"}, "url": "${data.url}"},
)
run = pf.runs.get(name)
assert run.status == RunStatus.COMPLETED
assert not os.path.exists(run._output_path / LocalStorageFilenames.EXCEPTION)
def test_run_local_storage_structure(self, local_client, pf) -> None:
run = create_run_against_multi_line_data(pf)
local_storage = LocalStorageOperations(local_client.runs.get(run.name))
run_output_path = local_storage.path
assert (Path(run_output_path) / "flow_outputs").is_dir()
assert (Path(run_output_path) / "flow_outputs" / "output.jsonl").is_file()
assert (Path(run_output_path) / "flow_artifacts").is_dir()
# 3 line runs for webClassification3.jsonl
assert len([_ for _ in (Path(run_output_path) / "flow_artifacts").iterdir()]) == 3
assert (Path(run_output_path) / "node_artifacts").is_dir()
# 5 nodes web classification flow DAG
assert len([_ for _ in (Path(run_output_path) / "node_artifacts").iterdir()]) == 5
def test_run_snapshot_with_flow_tools_json(self, local_client, pf) -> None:
run = create_run_against_multi_line_data(pf)
local_storage = LocalStorageOperations(local_client.runs.get(run.name))
assert (local_storage._snapshot_folder_path / ".promptflow").is_dir()
assert (local_storage._snapshot_folder_path / ".promptflow" / "flow.tools.json").is_file()
def test_get_metrics_format(self, local_client, pf) -> None:
run1 = create_run_against_multi_line_data(pf)
run2 = create_run_against_run(pf, run1)
# ensure the result is a flatten dict
assert local_client.runs.get_metrics(run2.name).keys() == {"accuracy"}
def test_get_detail_format(self, local_client, pf) -> None:
run = create_run_against_multi_line_data(pf)
# data is a jsonl file, so we can know the number of line runs
with open(f"{DATAS_DIR}/webClassification3.jsonl", "r") as f:
data = f.readlines()
number_of_lines = len(data)
local_storage = LocalStorageOperations(local_client.runs.get(run.name))
detail = local_storage.load_detail()
assert isinstance(detail, dict)
# flow runs
assert "flow_runs" in detail
assert isinstance(detail["flow_runs"], list)
assert len(detail["flow_runs"]) == number_of_lines
# node runs
assert "node_runs" in detail
assert isinstance(detail["node_runs"], list)
def test_run_logs(self, pf):
data_path = f"{DATAS_DIR}/webClassification3.jsonl"
run = pf.run(
flow=f"{FLOWS_DIR}/flow_with_user_output",
data=data_path,
column_mapping={"key": {"value": "1"}, "url": "${data.url}"},
)
local_storage = LocalStorageOperations(run=run)
logs = local_storage.logger.get_logs()
# For Batch run, the executor uses bulk logger to print logs, and only prints the error log of the nodes.
existing_keywords = ["execution", "execution.bulk", "WARNING", "error log"]
assert all([keyword in logs for keyword in existing_keywords])
non_existing_keywords = ["execution.flow", "user log"]
assert all([keyword not in logs for keyword in non_existing_keywords])
def test_get_detail_against_partial_fail_run(self, pf) -> None:
run = pf.run(
flow=f"{FLOWS_DIR}/partial_fail",
data=f"{FLOWS_DIR}/partial_fail/data.jsonl",
)
detail = pf.runs.get_details(name=run.name)
detail.fillna("", inplace=True)
assert len(detail) == 3
def test_flow_with_only_static_values(self, pf):
name = str(uuid.uuid4())
data_path = f"{DATAS_DIR}/webClassification3.jsonl"
with pytest.raises(UserErrorException) as e:
pf.run(
flow=f"{FLOWS_DIR}/flow_with_dict_input",
data=data_path,
column_mapping={"key": {"value": "1"}},
name=name,
)
assert "Column mapping must contain at least one mapping binding" in str(e.value)
# run should not be created
with pytest.raises(RunNotFoundError):
pf.runs.get(name=name)
def test_error_message_dump(self, pf):
failed_run = pf.run(
flow=f"{FLOWS_DIR}/failed_flow",
data=f"{DATAS_DIR}/webClassification1.jsonl",
column_mapping={"text": "${data.url}"},
)
# even if all lines failed, the bulk run's status is completed.
assert failed_run.status == "Completed"
# error messages will store in local
local_storage = LocalStorageOperations(failed_run)
assert os.path.exists(local_storage._exception_path)
exception = local_storage.load_exception()
assert "Failed to run 1/1 lines. First error message is" in exception["message"]
# line run failures will be stored in additionalInfo
assert len(exception["additionalInfo"][0]["info"]["errors"]) == 1
# show run will get error message
run = pf.runs.get(name=failed_run.name)
run_dict = run._to_dict()
assert "error" in run_dict
assert run_dict["error"] == exception
def test_system_metrics_in_properties(self, pf) -> None:
run = create_run_against_multi_line_data(pf)
assert FlowRunProperties.SYSTEM_METRICS in run.properties
assert isinstance(run.properties[FlowRunProperties.SYSTEM_METRICS], dict)
assert "total_tokens" in run.properties[FlowRunProperties.SYSTEM_METRICS]
def test_run_get_inputs(self, pf):
# inputs should be persisted when defaults are used
run = pf.run(
flow=f"{FLOWS_DIR}/default_input",
data=f"{DATAS_DIR}/webClassification1.jsonl",
)
inputs = pf.runs._get_inputs(run=run)
assert inputs == {
"line_number": [0],
"input_bool": [False],
"input_dict": [{}],
"input_list": [[]],
"input_str": ["input value from default"],
}
# inputs should be persisted when data value are used
run = pf.run(
flow=f"{FLOWS_DIR}/flow_with_dict_input",
data=f"{DATAS_DIR}/dictInput1.jsonl",
)
inputs = pf.runs._get_inputs(run=run)
assert inputs == {"key": [{"key": "value in data"}], "line_number": [0]}
# inputs should be persisted when column-mapping are used
run = pf.run(
flow=f"{FLOWS_DIR}/flow_with_dict_input",
data=f"{DATAS_DIR}/webClassification1.jsonl",
column_mapping={"key": {"value": "value in column-mapping"}, "url": "${data.url}"},
)
inputs = pf.runs._get_inputs(run=run)
assert inputs == {
"key": [{"value": "value in column-mapping"}],
"line_number": [0],
"url": ["https://www.youtube.com/watch?v=o5ZQyXaAv1g"],
}
def test_executor_logs_in_batch_run_logs(self, pf) -> None:
run = create_run_against_multi_line_data_without_llm(pf)
local_storage = LocalStorageOperations(run=run)
logs = local_storage.logger.get_logs()
# below warning is printed by executor before the batch run executed
# the warning message results from we do not use column mapping
# so it is expected to be printed here
assert "Starting run without column mapping may lead to unexpected results." in logs
def test_basic_image_flow_bulk_run(self, pf, local_client) -> None:
image_flow_path = f"{FLOWS_DIR}/python_tool_with_simple_image"
data_path = f"{image_flow_path}/image_inputs/inputs.jsonl"
result = pf.run(flow=image_flow_path, data=data_path, column_mapping={"image": "${data.image}"})
run = local_client.runs.get(name=result.name)
assert run.status == "Completed"
assert "error" not in run._to_dict()
def test_openai_vision_image_flow_bulk_run(self, pf, local_client) -> None:
image_flow_path = f"{FLOWS_DIR}/python_tool_with_openai_vision_image"
data_path = f"{image_flow_path}/inputs.jsonl"
result = pf.run(flow=image_flow_path, data=data_path, column_mapping={"image": "${data.image}"})
run = local_client.runs.get(name=result.name)
assert run.status == "Completed"
assert "error" not in run._to_dict()
def test_python_tool_with_composite_image(self, pf) -> None:
image_flow_path = f"{FLOWS_DIR}/python_tool_with_composite_image"
data_path = f"{image_flow_path}/inputs.jsonl"
result = pf.run(
flow=image_flow_path,
data=data_path,
column_mapping={
"image_list": "${data.image_list}",
"image_dict": "${data.image_dict}",
},
)
run = pf.runs.get(name=result.name)
assert run.status == "Completed"
# no error when processing lines