aiverify-moonshot 0.4.2__py3-none-any.whl → 0.4.4__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.
- {aiverify_moonshot-0.4.2.dist-info → aiverify_moonshot-0.4.4.dist-info}/METADATA +12 -10
- {aiverify_moonshot-0.4.2.dist-info → aiverify_moonshot-0.4.4.dist-info}/RECORD +35 -31
- moonshot/api.py +2 -0
- moonshot/integrations/cli/benchmark/cookbook.py +36 -28
- moonshot/integrations/cli/benchmark/datasets.py +56 -47
- moonshot/integrations/cli/benchmark/metrics.py +39 -30
- moonshot/integrations/cli/benchmark/recipe.py +63 -73
- moonshot/integrations/cli/benchmark/result.py +62 -54
- moonshot/integrations/cli/benchmark/run.py +75 -66
- moonshot/integrations/cli/common/common.py +8 -0
- moonshot/integrations/cli/common/connectors.py +101 -85
- moonshot/integrations/cli/common/dataset.py +66 -0
- moonshot/integrations/cli/common/prompt_template.py +30 -27
- moonshot/integrations/cli/redteam/attack_module.py +45 -30
- moonshot/integrations/cli/redteam/context_strategy.py +36 -30
- moonshot/integrations/cli/redteam/session.py +101 -76
- moonshot/integrations/cli/utils/process_data.py +52 -0
- moonshot/integrations/web_api/app.py +1 -1
- moonshot/integrations/web_api/routes/dataset.py +46 -1
- moonshot/integrations/web_api/schemas/dataset_create_dto.py +18 -0
- moonshot/integrations/web_api/schemas/recipe_create_dto.py +0 -2
- moonshot/integrations/web_api/services/bookmark_service.py +6 -2
- moonshot/integrations/web_api/services/dataset_service.py +25 -0
- moonshot/integrations/web_api/services/recipe_service.py +0 -1
- moonshot/src/api/api_dataset.py +35 -0
- moonshot/src/api/api_recipe.py +0 -3
- moonshot/src/datasets/dataset.py +116 -0
- moonshot/src/recipes/recipe.py +0 -15
- moonshot/src/recipes/recipe_arguments.py +0 -4
- moonshot/src/utils/log.py +12 -6
- moonshot/src/utils/pagination.py +25 -0
- {aiverify_moonshot-0.4.2.dist-info → aiverify_moonshot-0.4.4.dist-info}/WHEEL +0 -0
- {aiverify_moonshot-0.4.2.dist-info → aiverify_moonshot-0.4.4.dist-info}/licenses/AUTHORS.md +0 -0
- {aiverify_moonshot-0.4.2.dist-info → aiverify_moonshot-0.4.4.dist-info}/licenses/LICENSE.md +0 -0
- {aiverify_moonshot-0.4.2.dist-info → aiverify_moonshot-0.4.4.dist-info}/licenses/NOTICES.md +0 -0
|
@@ -18,7 +18,7 @@ from moonshot.api import (
|
|
|
18
18
|
api_update_recipe,
|
|
19
19
|
)
|
|
20
20
|
from moonshot.integrations.cli.common.display_helper import display_view_list_format
|
|
21
|
-
from moonshot.
|
|
21
|
+
from moonshot.integrations.cli.utils.process_data import filter_data
|
|
22
22
|
|
|
23
23
|
console = Console()
|
|
24
24
|
|
|
@@ -37,8 +37,7 @@ def add_recipe(args) -> None:
|
|
|
37
37
|
|
|
38
38
|
Args:
|
|
39
39
|
args (argparse.Namespace): The arguments provided to the command line interface.
|
|
40
|
-
Expected keys are name, description, tags, categories, dataset, prompt_templates, metrics
|
|
41
|
-
and grading_scale.
|
|
40
|
+
Expected keys are name, description, tags, categories, dataset, prompt_templates, metrics and grading_scale.
|
|
42
41
|
|
|
43
42
|
Returns:
|
|
44
43
|
None
|
|
@@ -54,9 +53,6 @@ def add_recipe(args) -> None:
|
|
|
54
53
|
literal_eval(args.prompt_templates) if args.prompt_templates else []
|
|
55
54
|
)
|
|
56
55
|
metrics = literal_eval(args.metrics)
|
|
57
|
-
attack_modules = (
|
|
58
|
-
literal_eval(args.attack_modules) if args.attack_modules else []
|
|
59
|
-
)
|
|
60
56
|
grading_scale = literal_eval(args.grading_scale) if args.grading_scale else {}
|
|
61
57
|
|
|
62
58
|
new_recipe_id = api_create_recipe(
|
|
@@ -67,7 +63,6 @@ def add_recipe(args) -> None:
|
|
|
67
63
|
datasets,
|
|
68
64
|
prompt_templates,
|
|
69
65
|
metrics,
|
|
70
|
-
attack_modules,
|
|
71
66
|
grading_scale,
|
|
72
67
|
)
|
|
73
68
|
print(f"[add_recipe]: Recipe ({new_recipe_id}) created.")
|
|
@@ -81,29 +76,31 @@ def list_recipes(args) -> list | None:
|
|
|
81
76
|
|
|
82
77
|
This function retrieves all available recipes by calling the api_get_all_recipe function from the
|
|
83
78
|
moonshot.api module.
|
|
84
|
-
It then displays the retrieved recipes using the
|
|
79
|
+
It then displays the retrieved recipes using the _display_recipes function.
|
|
85
80
|
|
|
86
81
|
Args:
|
|
87
82
|
args: A namespace object from argparse. It should have an optional attribute:
|
|
88
83
|
find (str): Optional field to find recipe(s) with a keyword.
|
|
84
|
+
pagination (str): Optional field to paginate recipes.
|
|
89
85
|
|
|
90
86
|
Returns:
|
|
91
87
|
list | None: A list of Recipe or None if there is no result.
|
|
92
88
|
"""
|
|
89
|
+
|
|
93
90
|
try:
|
|
94
91
|
recipes_list = api_get_all_recipe()
|
|
95
92
|
keyword = args.find.lower() if args.find else ""
|
|
96
|
-
if
|
|
97
|
-
|
|
93
|
+
pagination = literal_eval(args.pagination) if args.pagination else ()
|
|
94
|
+
|
|
95
|
+
if recipes_list:
|
|
96
|
+
filtered_recipes_list = filter_data(recipes_list, keyword, pagination)
|
|
98
97
|
if filtered_recipes_list:
|
|
99
|
-
|
|
98
|
+
_display_recipes(filtered_recipes_list)
|
|
100
99
|
return filtered_recipes_list
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
display_recipes(recipes_list)
|
|
106
|
-
return recipes_list
|
|
100
|
+
|
|
101
|
+
console.print("[red]There are no recipes found.[/red]")
|
|
102
|
+
return None
|
|
103
|
+
|
|
107
104
|
except Exception as e:
|
|
108
105
|
print(f"[list_recipes]: {str(e)}")
|
|
109
106
|
|
|
@@ -125,7 +122,7 @@ def view_recipe(args) -> None:
|
|
|
125
122
|
"""
|
|
126
123
|
try:
|
|
127
124
|
recipe_info = api_read_recipe(args.recipe)
|
|
128
|
-
|
|
125
|
+
_display_recipes([recipe_info])
|
|
129
126
|
except Exception as e:
|
|
130
127
|
print(f"[view_recipe]: {str(e)}")
|
|
131
128
|
|
|
@@ -311,7 +308,7 @@ def display_view_statistics_format(title: str, stats: dict) -> str:
|
|
|
311
308
|
return f"[blue]{title}[/blue]: nil"
|
|
312
309
|
|
|
313
310
|
|
|
314
|
-
def
|
|
311
|
+
def _display_recipes(recipes_list: list) -> None:
|
|
315
312
|
"""
|
|
316
313
|
Display the list of recipes in a tabular format.
|
|
317
314
|
|
|
@@ -323,54 +320,48 @@ def display_recipes(recipes_list: list) -> None:
|
|
|
323
320
|
Args:
|
|
324
321
|
recipes_list (list): A list of dictionaries, where each dictionary contains the details of a recipe.
|
|
325
322
|
"""
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
323
|
+
table = Table(
|
|
324
|
+
title="List of Recipes", show_lines=True, expand=True, header_style="bold"
|
|
325
|
+
)
|
|
326
|
+
table.add_column("No.", width=2)
|
|
327
|
+
table.add_column("Recipe", justify="left", width=78)
|
|
328
|
+
table.add_column("Contains", justify="left", width=20, overflow="fold")
|
|
329
|
+
for idx, recipe in enumerate(recipes_list, 1):
|
|
330
|
+
(
|
|
331
|
+
id,
|
|
332
|
+
name,
|
|
333
|
+
description,
|
|
334
|
+
tags,
|
|
335
|
+
categories,
|
|
336
|
+
datasets,
|
|
337
|
+
prompt_templates,
|
|
338
|
+
metrics,
|
|
339
|
+
grading_scale,
|
|
340
|
+
stats,
|
|
341
|
+
*other_args,
|
|
342
|
+
) = recipe.values()
|
|
343
|
+
idx = recipe.get("idx", idx)
|
|
344
|
+
tags_info = display_view_list_format("Tags", tags)
|
|
345
|
+
categories_info = display_view_list_format("Categories", categories)
|
|
346
|
+
datasets_info = display_view_list_format("Datasets", datasets)
|
|
347
|
+
prompt_templates_info = display_view_list_format(
|
|
348
|
+
"Prompt Templates", prompt_templates
|
|
329
349
|
)
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
id,
|
|
336
|
-
name,
|
|
337
|
-
description,
|
|
338
|
-
tags,
|
|
339
|
-
categories,
|
|
340
|
-
datasets,
|
|
341
|
-
prompt_templates,
|
|
342
|
-
metrics,
|
|
343
|
-
attack_strategies,
|
|
344
|
-
grading_scale,
|
|
345
|
-
stats,
|
|
346
|
-
) = recipe.values()
|
|
347
|
-
|
|
348
|
-
tags_info = display_view_list_format("Tags", tags)
|
|
349
|
-
categories_info = display_view_list_format("Categories", categories)
|
|
350
|
-
datasets_info = display_view_list_format("Datasets", datasets)
|
|
351
|
-
prompt_templates_info = display_view_list_format(
|
|
352
|
-
"Prompt Templates", prompt_templates
|
|
353
|
-
)
|
|
354
|
-
metrics_info = display_view_list_format("Metrics", metrics)
|
|
355
|
-
attack_strategies_info = display_view_list_format(
|
|
356
|
-
"Attack Strategies", attack_strategies
|
|
357
|
-
)
|
|
358
|
-
grading_scale_info = display_view_grading_scale_format(
|
|
359
|
-
"Grading Scale", grading_scale
|
|
360
|
-
)
|
|
361
|
-
stats_info = display_view_statistics_format("Statistics", stats)
|
|
350
|
+
metrics_info = display_view_list_format("Metrics", metrics)
|
|
351
|
+
grading_scale_info = display_view_grading_scale_format(
|
|
352
|
+
"Grading Scale", grading_scale
|
|
353
|
+
)
|
|
354
|
+
stats_info = display_view_statistics_format("Statistics", stats)
|
|
362
355
|
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
356
|
+
recipe_info = (
|
|
357
|
+
f"[red]id: {id}[/red]\n\n[blue]{name}[/blue]\n{description}\n\n"
|
|
358
|
+
f"{tags_info}\n\n{categories_info}\n\n{grading_scale_info}\n\n{stats_info}"
|
|
359
|
+
)
|
|
360
|
+
contains_info = f"{datasets_info}\n\n{prompt_templates_info}\n\n{metrics_info}"
|
|
368
361
|
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
else:
|
|
373
|
-
console.print("[red]There are no recipes found.[/red]")
|
|
362
|
+
table.add_section()
|
|
363
|
+
table.add_row(str(idx), recipe_info, contains_info)
|
|
364
|
+
console.print(table)
|
|
374
365
|
|
|
375
366
|
|
|
376
367
|
def show_recipe_results(recipes, endpoints, recipe_results, duration):
|
|
@@ -499,7 +490,6 @@ add_recipe_args = cmd2.Cmd2ArgumentParser(
|
|
|
499
490
|
"\"['bertscore','bleuscore']\" "
|
|
500
491
|
"-p \"['analogical-similarity','mmlu']\" "
|
|
501
492
|
"-t \"['tag1','tag2']\" "
|
|
502
|
-
"-a \"['charswap_attack']\" "
|
|
503
493
|
"-g \"{'A':[80,100],'B':[60,79],'C':[40,59],'D':[20,39],'E':[0,19]}\" ",
|
|
504
494
|
)
|
|
505
495
|
add_recipe_args.add_argument("name", type=str, help="Name of the new recipe")
|
|
@@ -527,13 +517,6 @@ add_recipe_args.add_argument(
|
|
|
527
517
|
add_recipe_args.add_argument(
|
|
528
518
|
"metrics", type=str, help="List of metrics to be included in the new recipe"
|
|
529
519
|
)
|
|
530
|
-
add_recipe_args.add_argument(
|
|
531
|
-
"-a",
|
|
532
|
-
"--attack_modules",
|
|
533
|
-
type=str,
|
|
534
|
-
help="List of attack modules to be included in the new recipe",
|
|
535
|
-
nargs="?",
|
|
536
|
-
)
|
|
537
520
|
add_recipe_args.add_argument(
|
|
538
521
|
"-g",
|
|
539
522
|
"--grading_scale",
|
|
@@ -553,7 +536,6 @@ update_recipe_args = cmd2.Cmd2ArgumentParser(
|
|
|
553
536
|
" datasets: A list of datasets used in the recipe. \n"
|
|
554
537
|
" prompt_templates: A list of prompt templates for the recipe. \n"
|
|
555
538
|
" metrics: A list of metrics to evaluate the recipe. \n"
|
|
556
|
-
" attack_modules: A list of attack modules used in the recipe.\n"
|
|
557
539
|
" grading_scale: A list of grading scale used in the recipe. \n\n"
|
|
558
540
|
"Example command:\n"
|
|
559
541
|
" update_recipe my-new-recipe \"[('name', 'My Updated Recipe'), ('tags', ['fairness', 'bbq'])]\" ",
|
|
@@ -626,3 +608,11 @@ list_recipes_args.add_argument(
|
|
|
626
608
|
help="Optional field to find recipe(s) with keyword",
|
|
627
609
|
nargs="?",
|
|
628
610
|
)
|
|
611
|
+
|
|
612
|
+
list_recipes_args.add_argument(
|
|
613
|
+
"-p",
|
|
614
|
+
"--pagination",
|
|
615
|
+
type=str,
|
|
616
|
+
help="Optional tuple to paginate recipes(s). E.g. (2,10) returns 2nd page with 10 items in each page.",
|
|
617
|
+
nargs="?",
|
|
618
|
+
)
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
from ast import literal_eval
|
|
2
|
+
|
|
1
3
|
import cmd2
|
|
2
4
|
from rich.console import Console
|
|
3
5
|
from rich.table import Table
|
|
@@ -9,7 +11,7 @@ from moonshot.integrations.cli.common.display_helper import (
|
|
|
9
11
|
display_view_list_format,
|
|
10
12
|
display_view_str_format,
|
|
11
13
|
)
|
|
12
|
-
from moonshot.
|
|
14
|
+
from moonshot.integrations.cli.utils.process_data import filter_data
|
|
13
15
|
|
|
14
16
|
console = Console()
|
|
15
17
|
|
|
@@ -28,24 +30,26 @@ def list_results(args) -> list | None:
|
|
|
28
30
|
Args:
|
|
29
31
|
args: A namespace object from argparse. It should have an optional attribute:
|
|
30
32
|
find (str): Optional field to find result(s) with a keyword.
|
|
33
|
+
pagination (str): Optional field to paginate results.
|
|
31
34
|
|
|
32
35
|
Returns:
|
|
33
36
|
list | None: A list of Result or None if there is no result.
|
|
34
37
|
"""
|
|
38
|
+
|
|
35
39
|
try:
|
|
36
40
|
results_list = api_get_all_result()
|
|
37
41
|
keyword = args.find.lower() if args.find else ""
|
|
38
|
-
if
|
|
39
|
-
|
|
42
|
+
pagination = literal_eval(args.pagination) if args.pagination else ()
|
|
43
|
+
|
|
44
|
+
if results_list:
|
|
45
|
+
filtered_results_list = filter_data(results_list, keyword, pagination)
|
|
40
46
|
if filtered_results_list:
|
|
41
|
-
|
|
47
|
+
_display_results(filtered_results_list)
|
|
42
48
|
return filtered_results_list
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
display_results(results_list)
|
|
48
|
-
return results_list
|
|
49
|
+
|
|
50
|
+
console.print("[red]There are no results found.[/red]")
|
|
51
|
+
return None
|
|
52
|
+
|
|
49
53
|
except Exception as e:
|
|
50
54
|
print(f"[list_results]: {str(e)}")
|
|
51
55
|
|
|
@@ -111,7 +115,7 @@ def delete_result(args) -> None:
|
|
|
111
115
|
# ------------------------------------------------------------------------------
|
|
112
116
|
# Helper functions: Display on cli
|
|
113
117
|
# ------------------------------------------------------------------------------
|
|
114
|
-
def
|
|
118
|
+
def _display_results(results_list):
|
|
115
119
|
"""
|
|
116
120
|
Display a list of results.
|
|
117
121
|
|
|
@@ -124,50 +128,46 @@ def display_results(results_list):
|
|
|
124
128
|
Returns:
|
|
125
129
|
None
|
|
126
130
|
"""
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
131
|
+
table = Table(
|
|
132
|
+
title="List of Results", show_lines=True, expand=True, header_style="bold"
|
|
133
|
+
)
|
|
134
|
+
table.add_column("No.", width=2)
|
|
135
|
+
table.add_column("Result", justify="left", width=78)
|
|
136
|
+
table.add_column("Contains", justify="left", width=20, overflow="fold")
|
|
137
|
+
for idx, result in enumerate(results_list, 1):
|
|
138
|
+
metadata, results, *other_args = result.values()
|
|
139
|
+
|
|
140
|
+
id = metadata["id"]
|
|
141
|
+
start_time = metadata["start_time"]
|
|
142
|
+
end_time = metadata["end_time"]
|
|
143
|
+
duration = metadata["duration"]
|
|
144
|
+
status = metadata["status"]
|
|
145
|
+
recipes = metadata["recipes"]
|
|
146
|
+
cookbooks = metadata["cookbooks"]
|
|
147
|
+
endpoints = metadata["endpoints"]
|
|
148
|
+
num_of_prompts = metadata["num_of_prompts"]
|
|
149
|
+
random_seed = metadata["random_seed"]
|
|
150
|
+
system_prompt = metadata["system_prompt"]
|
|
151
|
+
idx = result.get("idx", idx)
|
|
152
|
+
|
|
153
|
+
duration_info = f"[blue]Period:[/blue] {start_time} - {end_time} ({duration}s)"
|
|
154
|
+
status_info = display_view_str_format("Status", status)
|
|
155
|
+
recipes_info = display_view_list_format("Recipes", recipes)
|
|
156
|
+
cookbooks_info = display_view_list_format("Cookbooks", cookbooks)
|
|
157
|
+
endpoints_info = display_view_list_format("Endpoints", endpoints)
|
|
158
|
+
prompts_info = display_view_str_format("Number of Prompts", num_of_prompts)
|
|
159
|
+
seed_info = display_view_str_format("Seed", random_seed)
|
|
160
|
+
system_prompt_info = display_view_str_format("System Prompt", system_prompt)
|
|
161
|
+
|
|
162
|
+
result_info = f"[red]id: {id}[/red]\n\n{duration_info}\n\n{status_info}"
|
|
163
|
+
contains_info = (
|
|
164
|
+
f"{recipes_info}\n\n{cookbooks_info}\n\n{endpoints_info}\n\n{prompts_info}"
|
|
165
|
+
f"\n\n{seed_info}\n\n{system_prompt_info}"
|
|
130
166
|
)
|
|
131
|
-
|
|
132
|
-
table.
|
|
133
|
-
table.
|
|
134
|
-
|
|
135
|
-
metadata, results = result.values()
|
|
136
|
-
|
|
137
|
-
id = metadata["id"]
|
|
138
|
-
start_time = metadata["start_time"]
|
|
139
|
-
end_time = metadata["end_time"]
|
|
140
|
-
duration = metadata["duration"]
|
|
141
|
-
status = metadata["status"]
|
|
142
|
-
recipes = metadata["recipes"]
|
|
143
|
-
cookbooks = metadata["cookbooks"]
|
|
144
|
-
endpoints = metadata["endpoints"]
|
|
145
|
-
num_of_prompts = metadata["num_of_prompts"]
|
|
146
|
-
random_seed = metadata["random_seed"]
|
|
147
|
-
system_prompt = metadata["system_prompt"]
|
|
148
|
-
|
|
149
|
-
duration_info = (
|
|
150
|
-
f"[blue]Period:[/blue] {start_time} - {end_time} ({duration}s)"
|
|
151
|
-
)
|
|
152
|
-
status_info = display_view_str_format("Status", status)
|
|
153
|
-
recipes_info = display_view_list_format("Recipes", recipes)
|
|
154
|
-
cookbooks_info = display_view_list_format("Cookbooks", cookbooks)
|
|
155
|
-
endpoints_info = display_view_list_format("Endpoints", endpoints)
|
|
156
|
-
prompts_info = display_view_str_format("Number of Prompts", num_of_prompts)
|
|
157
|
-
seed_info = display_view_str_format("Seed", random_seed)
|
|
158
|
-
system_prompt_info = display_view_str_format("System Prompt", system_prompt)
|
|
159
|
-
|
|
160
|
-
result_info = f"[red]id: {id}[/red]\n\n{duration_info}\n\n{status_info}"
|
|
161
|
-
contains_info = (
|
|
162
|
-
f"{recipes_info}\n\n{cookbooks_info}\n\n{endpoints_info}\n\n{prompts_info}"
|
|
163
|
-
f"\n\n{seed_info}\n\n{system_prompt_info}"
|
|
164
|
-
)
|
|
165
|
-
|
|
166
|
-
table.add_section()
|
|
167
|
-
table.add_row(str(result_id), result_info, contains_info)
|
|
168
|
-
console.print(table)
|
|
169
|
-
else:
|
|
170
|
-
console.print("[red]There are no results found.[/red]")
|
|
167
|
+
|
|
168
|
+
table.add_section()
|
|
169
|
+
table.add_row(str(idx), result_info, contains_info)
|
|
170
|
+
console.print(table)
|
|
171
171
|
|
|
172
172
|
|
|
173
173
|
def display_view_recipe_result(result_info):
|
|
@@ -244,3 +244,11 @@ list_results_args.add_argument(
|
|
|
244
244
|
help="Optional field to find result(s) with keyword",
|
|
245
245
|
nargs="?",
|
|
246
246
|
)
|
|
247
|
+
|
|
248
|
+
list_results_args.add_argument(
|
|
249
|
+
"-p",
|
|
250
|
+
"--pagination",
|
|
251
|
+
type=str,
|
|
252
|
+
help="Optional tuple to paginate result(s). E.g. (2,10) returns 2nd page with 10 items in each page.",
|
|
253
|
+
nargs="?",
|
|
254
|
+
)
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
from ast import literal_eval
|
|
2
|
+
|
|
1
3
|
import cmd2
|
|
2
4
|
from rich.console import Console
|
|
3
5
|
from rich.table import Table
|
|
@@ -7,7 +9,7 @@ from moonshot.integrations.cli.common.display_helper import (
|
|
|
7
9
|
display_view_list_format,
|
|
8
10
|
display_view_str_format,
|
|
9
11
|
)
|
|
10
|
-
from moonshot.
|
|
12
|
+
from moonshot.integrations.cli.utils.process_data import filter_data
|
|
11
13
|
|
|
12
14
|
console = Console()
|
|
13
15
|
|
|
@@ -20,31 +22,33 @@ def list_runs(args) -> list | None:
|
|
|
20
22
|
List all runs.
|
|
21
23
|
|
|
22
24
|
This function retrieves all available runs by calling the api_get_all_run function from the
|
|
23
|
-
moonshot.api module. It then calls the
|
|
25
|
+
moonshot.api module. It then calls the _display_runs function to present the retrieved run information
|
|
24
26
|
in a user-friendly format on the command line interface. If an exception occurs during the retrieval
|
|
25
27
|
or display process, it prints an error message.
|
|
26
28
|
|
|
27
29
|
Args:
|
|
28
30
|
args: A namespace object from argparse. It should have an optional attribute:
|
|
29
31
|
find (str): Optional field to find run(s) with a keyword.
|
|
32
|
+
pagination (str): Optional field to paginate runs.
|
|
30
33
|
|
|
31
34
|
Returns:
|
|
32
35
|
list | None: A list of Run or None if there is no result.
|
|
33
36
|
"""
|
|
37
|
+
|
|
34
38
|
try:
|
|
35
39
|
runner_run_info = api_get_all_run()
|
|
36
40
|
keyword = args.find.lower() if args.find else ""
|
|
37
|
-
if
|
|
38
|
-
|
|
41
|
+
pagination = literal_eval(args.pagination) if args.pagination else ()
|
|
42
|
+
|
|
43
|
+
if runner_run_info:
|
|
44
|
+
filtered_runs_list = filter_data(runner_run_info, keyword, pagination)
|
|
39
45
|
if filtered_runs_list:
|
|
40
|
-
|
|
46
|
+
_display_runs(filtered_runs_list)
|
|
41
47
|
return filtered_runs_list
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
display_runs(runner_run_info)
|
|
47
|
-
return runner_run_info
|
|
48
|
+
|
|
49
|
+
console.print("[red]There are no runs found.[/red]")
|
|
50
|
+
return None
|
|
51
|
+
|
|
48
52
|
except Exception as e:
|
|
49
53
|
print(f"[list_runs]: {str(e)}")
|
|
50
54
|
|
|
@@ -54,7 +58,7 @@ def view_run(args) -> None:
|
|
|
54
58
|
View the details of a specific run.
|
|
55
59
|
|
|
56
60
|
This function retrieves and displays information about a specific run associated with a runner. It uses the runner
|
|
57
|
-
identifier provided in the arguments to fetch the data and then calls the
|
|
61
|
+
identifier provided in the arguments to fetch the data and then calls the _display_runs function to present it in a
|
|
58
62
|
user-friendly format.
|
|
59
63
|
|
|
60
64
|
Args:
|
|
@@ -66,7 +70,7 @@ def view_run(args) -> None:
|
|
|
66
70
|
"""
|
|
67
71
|
try:
|
|
68
72
|
runner_run_info = api_get_all_run(args.runner_id)
|
|
69
|
-
|
|
73
|
+
_display_runs(runner_run_info)
|
|
70
74
|
except Exception as e:
|
|
71
75
|
print(f"[view_run]: {str(e)}")
|
|
72
76
|
|
|
@@ -74,7 +78,7 @@ def view_run(args) -> None:
|
|
|
74
78
|
# ------------------------------------------------------------------------------
|
|
75
79
|
# Helper functions: Display on cli
|
|
76
80
|
# ------------------------------------------------------------------------------
|
|
77
|
-
def
|
|
81
|
+
def _display_runs(runs_list: list):
|
|
78
82
|
"""
|
|
79
83
|
Display a list of runs in a table format.
|
|
80
84
|
|
|
@@ -90,59 +94,56 @@ def display_runs(runs_list: list):
|
|
|
90
94
|
Returns:
|
|
91
95
|
None
|
|
92
96
|
"""
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
97
|
+
table = Table(
|
|
98
|
+
title="List of Runs", show_lines=True, expand=True, header_style="bold"
|
|
99
|
+
)
|
|
100
|
+
table.add_column("No.", width=2)
|
|
101
|
+
table.add_column("Run", justify="left", width=78)
|
|
102
|
+
table.add_column("Contains", justify="left", width=20, overflow="fold")
|
|
103
|
+
for idx, run in enumerate(runs_list, 1):
|
|
104
|
+
(
|
|
105
|
+
run_id,
|
|
106
|
+
runner_id,
|
|
107
|
+
runner_type,
|
|
108
|
+
runner_args,
|
|
109
|
+
endpoints,
|
|
110
|
+
results_file,
|
|
111
|
+
start_time,
|
|
112
|
+
end_time,
|
|
113
|
+
duration,
|
|
114
|
+
error_messages,
|
|
115
|
+
raw_results,
|
|
116
|
+
results,
|
|
117
|
+
status,
|
|
118
|
+
*other_args,
|
|
119
|
+
) = run.values()
|
|
120
|
+
|
|
121
|
+
duration_info = f"[blue]Period:[/blue] {start_time} - {end_time} ({duration}s)"
|
|
122
|
+
run_id = display_view_str_format("Run ID", run_id)
|
|
123
|
+
runner_id = display_view_str_format("Runner ID", runner_id)
|
|
124
|
+
runner_type = display_view_str_format("Runner Type", runner_type)
|
|
125
|
+
runner_args = display_view_str_format("Runner Args", runner_args)
|
|
126
|
+
status_info = display_view_str_format("Status", status)
|
|
127
|
+
results_info = display_view_str_format("Results File", results_file)
|
|
128
|
+
endpoints_info = display_view_list_format("Endpoints", endpoints)
|
|
129
|
+
error_messages_info = display_view_list_format("Error Messages", error_messages)
|
|
130
|
+
|
|
131
|
+
has_raw_results = bool(raw_results)
|
|
132
|
+
has_results = bool(results)
|
|
133
|
+
idx = run.get("idx", idx)
|
|
134
|
+
|
|
135
|
+
result_info = (
|
|
136
|
+
f"[red]{runner_id}[/red]\n\n{run_id}\n\n{duration_info}\n\n{status_info}"
|
|
137
|
+
)
|
|
138
|
+
contains_info = (
|
|
139
|
+
f"{results_info}\n\n{error_messages_info}\n\n{endpoints_info}\n\n"
|
|
140
|
+
f"[blue]Has Raw Results: {has_raw_results}[/blue]\n\n"
|
|
141
|
+
f"[blue]Has Results: {has_results}[/blue]"
|
|
96
142
|
)
|
|
97
|
-
|
|
98
|
-
table.
|
|
99
|
-
table.
|
|
100
|
-
|
|
101
|
-
(
|
|
102
|
-
run_id,
|
|
103
|
-
runner_id,
|
|
104
|
-
runner_type,
|
|
105
|
-
runner_args,
|
|
106
|
-
endpoints,
|
|
107
|
-
results_file,
|
|
108
|
-
start_time,
|
|
109
|
-
end_time,
|
|
110
|
-
duration,
|
|
111
|
-
error_messages,
|
|
112
|
-
raw_results,
|
|
113
|
-
results,
|
|
114
|
-
status,
|
|
115
|
-
) = run.values()
|
|
116
|
-
|
|
117
|
-
duration_info = (
|
|
118
|
-
f"[blue]Period:[/blue] {start_time} - {end_time} ({duration}s)"
|
|
119
|
-
)
|
|
120
|
-
run_id = display_view_str_format("Run ID", run_id)
|
|
121
|
-
runner_id = display_view_str_format("Runner ID", runner_id)
|
|
122
|
-
runner_type = display_view_str_format("Runner Type", runner_type)
|
|
123
|
-
runner_args = display_view_str_format("Runner Args", runner_args)
|
|
124
|
-
status_info = display_view_str_format("Status", status)
|
|
125
|
-
results_info = display_view_str_format("Results File", results_file)
|
|
126
|
-
endpoints_info = display_view_list_format("Endpoints", endpoints)
|
|
127
|
-
error_messages_info = display_view_list_format(
|
|
128
|
-
"Error Messages", error_messages
|
|
129
|
-
)
|
|
130
|
-
|
|
131
|
-
has_raw_results = bool(raw_results)
|
|
132
|
-
has_results = bool(results)
|
|
133
|
-
|
|
134
|
-
result_info = f"[red]{runner_id}[/red]\n\n{run_id}\n\n{duration_info}\n\n{status_info}"
|
|
135
|
-
contains_info = (
|
|
136
|
-
f"{results_info}\n\n{error_messages_info}\n\n{endpoints_info}\n\n"
|
|
137
|
-
f"[blue]Has Raw Results: {has_raw_results}[/blue]\n\n"
|
|
138
|
-
f"[blue]Has Results: {has_results}[/blue]"
|
|
139
|
-
)
|
|
140
|
-
|
|
141
|
-
table.add_section()
|
|
142
|
-
table.add_row(str(run_number), result_info, contains_info)
|
|
143
|
-
console.print(table)
|
|
144
|
-
else:
|
|
145
|
-
console.print("[red]There are no results found.[/red]")
|
|
143
|
+
|
|
144
|
+
table.add_section()
|
|
145
|
+
table.add_row(str(idx), result_info, contains_info)
|
|
146
|
+
console.print(table)
|
|
146
147
|
|
|
147
148
|
|
|
148
149
|
# ------------------------------------------------------------------------------
|
|
@@ -169,3 +170,11 @@ list_runs_args.add_argument(
|
|
|
169
170
|
help="Optional field to find run(s) with keyword",
|
|
170
171
|
nargs="?",
|
|
171
172
|
)
|
|
173
|
+
|
|
174
|
+
list_runs_args.add_argument(
|
|
175
|
+
"-p",
|
|
176
|
+
"--pagination",
|
|
177
|
+
type=str,
|
|
178
|
+
help="Optional tuple to paginate run(s). E.g. (2,10) returns 2nd page with 10 items in each page.",
|
|
179
|
+
nargs="?",
|
|
180
|
+
)
|
|
@@ -2,6 +2,10 @@ import argparse
|
|
|
2
2
|
|
|
3
3
|
import cmd2
|
|
4
4
|
|
|
5
|
+
from moonshot.integrations.cli.common.dataset import (
|
|
6
|
+
add_dataset,
|
|
7
|
+
add_dataset_args
|
|
8
|
+
)
|
|
5
9
|
from moonshot.integrations.cli.common.connectors import (
|
|
6
10
|
add_endpoint,
|
|
7
11
|
add_endpoint_args,
|
|
@@ -56,6 +60,10 @@ class CommonCommandSet(cmd2.CommandSet):
|
|
|
56
60
|
def do_add_endpoint(self, args: argparse.Namespace) -> None:
|
|
57
61
|
add_endpoint(args)
|
|
58
62
|
|
|
63
|
+
@cmd2.with_argparser(add_dataset_args)
|
|
64
|
+
def do_add_dataset(self, args:argparse.Namespace) -> None:
|
|
65
|
+
add_dataset(args)
|
|
66
|
+
|
|
59
67
|
# ------------------------------------------------------------------------------
|
|
60
68
|
# Delete contents
|
|
61
69
|
# ------------------------------------------------------------------------------
|