aiverify-moonshot 0.4.0__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.0.dist-info/METADATA +249 -0
- aiverify_moonshot-0.4.0.dist-info/RECORD +163 -0
- aiverify_moonshot-0.4.0.dist-info/WHEEL +4 -0
- aiverify_moonshot-0.4.0.dist-info/licenses/AUTHORS.md +5 -0
- aiverify_moonshot-0.4.0.dist-info/licenses/LICENSE.md +201 -0
- aiverify_moonshot-0.4.0.dist-info/licenses/NOTICES.md +3340 -0
- moonshot/__init__.py +0 -0
- moonshot/__main__.py +198 -0
- moonshot/api.py +155 -0
- moonshot/integrations/__init__.py +0 -0
- moonshot/integrations/cli/__init__.py +0 -0
- moonshot/integrations/cli/__main__.py +25 -0
- moonshot/integrations/cli/active_session_cfg.py +1 -0
- moonshot/integrations/cli/benchmark/__init__.py +0 -0
- moonshot/integrations/cli/benchmark/benchmark.py +186 -0
- moonshot/integrations/cli/benchmark/cookbook.py +545 -0
- moonshot/integrations/cli/benchmark/datasets.py +164 -0
- moonshot/integrations/cli/benchmark/metrics.py +141 -0
- moonshot/integrations/cli/benchmark/recipe.py +598 -0
- moonshot/integrations/cli/benchmark/result.py +216 -0
- moonshot/integrations/cli/benchmark/run.py +140 -0
- moonshot/integrations/cli/benchmark/runner.py +174 -0
- moonshot/integrations/cli/cli.py +64 -0
- moonshot/integrations/cli/common/__init__.py +0 -0
- moonshot/integrations/cli/common/common.py +72 -0
- moonshot/integrations/cli/common/connectors.py +325 -0
- moonshot/integrations/cli/common/display_helper.py +42 -0
- moonshot/integrations/cli/common/prompt_template.py +94 -0
- moonshot/integrations/cli/initialisation/__init__.py +0 -0
- moonshot/integrations/cli/initialisation/initialisation.py +14 -0
- moonshot/integrations/cli/redteam/__init__.py +0 -0
- moonshot/integrations/cli/redteam/attack_module.py +70 -0
- moonshot/integrations/cli/redteam/context_strategy.py +147 -0
- moonshot/integrations/cli/redteam/prompt_template.py +67 -0
- moonshot/integrations/cli/redteam/redteam.py +90 -0
- moonshot/integrations/cli/redteam/session.py +467 -0
- moonshot/integrations/web_api/.env.dev +7 -0
- moonshot/integrations/web_api/__init__.py +0 -0
- moonshot/integrations/web_api/__main__.py +56 -0
- moonshot/integrations/web_api/app.py +125 -0
- moonshot/integrations/web_api/container.py +146 -0
- moonshot/integrations/web_api/log/.gitkeep +0 -0
- moonshot/integrations/web_api/logging_conf.py +114 -0
- moonshot/integrations/web_api/routes/__init__.py +0 -0
- moonshot/integrations/web_api/routes/attack_modules.py +66 -0
- moonshot/integrations/web_api/routes/benchmark.py +116 -0
- moonshot/integrations/web_api/routes/benchmark_result.py +175 -0
- moonshot/integrations/web_api/routes/context_strategy.py +129 -0
- moonshot/integrations/web_api/routes/cookbook.py +225 -0
- moonshot/integrations/web_api/routes/dataset.py +120 -0
- moonshot/integrations/web_api/routes/endpoint.py +282 -0
- moonshot/integrations/web_api/routes/metric.py +78 -0
- moonshot/integrations/web_api/routes/prompt_template.py +128 -0
- moonshot/integrations/web_api/routes/recipe.py +219 -0
- moonshot/integrations/web_api/routes/redteam.py +609 -0
- moonshot/integrations/web_api/routes/runner.py +239 -0
- moonshot/integrations/web_api/schemas/__init__.py +0 -0
- moonshot/integrations/web_api/schemas/benchmark_runner_dto.py +13 -0
- moonshot/integrations/web_api/schemas/cookbook_create_dto.py +19 -0
- moonshot/integrations/web_api/schemas/cookbook_response_model.py +9 -0
- moonshot/integrations/web_api/schemas/dataset_response_dto.py +9 -0
- moonshot/integrations/web_api/schemas/endpoint_create_dto.py +21 -0
- moonshot/integrations/web_api/schemas/endpoint_response_model.py +11 -0
- moonshot/integrations/web_api/schemas/prompt_response_model.py +14 -0
- moonshot/integrations/web_api/schemas/prompt_template_response_model.py +10 -0
- moonshot/integrations/web_api/schemas/recipe_create_dto.py +32 -0
- moonshot/integrations/web_api/schemas/recipe_response_model.py +7 -0
- moonshot/integrations/web_api/schemas/session_create_dto.py +16 -0
- moonshot/integrations/web_api/schemas/session_prompt_dto.py +7 -0
- moonshot/integrations/web_api/schemas/session_response_model.py +38 -0
- moonshot/integrations/web_api/services/__init__.py +0 -0
- moonshot/integrations/web_api/services/attack_module_service.py +34 -0
- moonshot/integrations/web_api/services/auto_red_team_test_manager.py +86 -0
- moonshot/integrations/web_api/services/auto_red_team_test_state.py +57 -0
- moonshot/integrations/web_api/services/base_service.py +8 -0
- moonshot/integrations/web_api/services/benchmark_result_service.py +25 -0
- moonshot/integrations/web_api/services/benchmark_test_manager.py +106 -0
- moonshot/integrations/web_api/services/benchmark_test_state.py +56 -0
- moonshot/integrations/web_api/services/benchmarking_service.py +31 -0
- moonshot/integrations/web_api/services/context_strategy_service.py +22 -0
- moonshot/integrations/web_api/services/cookbook_service.py +194 -0
- moonshot/integrations/web_api/services/dataset_service.py +20 -0
- moonshot/integrations/web_api/services/endpoint_service.py +65 -0
- moonshot/integrations/web_api/services/metric_service.py +14 -0
- moonshot/integrations/web_api/services/prompt_template_service.py +39 -0
- moonshot/integrations/web_api/services/recipe_service.py +155 -0
- moonshot/integrations/web_api/services/runner_service.py +147 -0
- moonshot/integrations/web_api/services/session_service.py +350 -0
- moonshot/integrations/web_api/services/utils/exceptions_handler.py +41 -0
- moonshot/integrations/web_api/services/utils/results_formatter.py +47 -0
- moonshot/integrations/web_api/status_updater/interface/benchmark_progress_callback.py +14 -0
- moonshot/integrations/web_api/status_updater/interface/redteam_progress_callback.py +14 -0
- moonshot/integrations/web_api/status_updater/moonshot_ui_webhook.py +72 -0
- moonshot/integrations/web_api/types/types.py +99 -0
- moonshot/src/__init__.py +0 -0
- moonshot/src/api/__init__.py +0 -0
- moonshot/src/api/api_connector.py +58 -0
- moonshot/src/api/api_connector_endpoint.py +162 -0
- moonshot/src/api/api_context_strategy.py +57 -0
- moonshot/src/api/api_cookbook.py +160 -0
- moonshot/src/api/api_dataset.py +46 -0
- moonshot/src/api/api_environment_variables.py +17 -0
- moonshot/src/api/api_metrics.py +51 -0
- moonshot/src/api/api_prompt_template.py +43 -0
- moonshot/src/api/api_recipe.py +182 -0
- moonshot/src/api/api_red_teaming.py +59 -0
- moonshot/src/api/api_result.py +84 -0
- moonshot/src/api/api_run.py +74 -0
- moonshot/src/api/api_runner.py +132 -0
- moonshot/src/api/api_session.py +290 -0
- moonshot/src/configs/__init__.py +0 -0
- moonshot/src/configs/env_variables.py +187 -0
- moonshot/src/connectors/__init__.py +0 -0
- moonshot/src/connectors/connector.py +327 -0
- moonshot/src/connectors/connector_prompt_arguments.py +17 -0
- moonshot/src/connectors_endpoints/__init__.py +0 -0
- moonshot/src/connectors_endpoints/connector_endpoint.py +211 -0
- moonshot/src/connectors_endpoints/connector_endpoint_arguments.py +54 -0
- moonshot/src/cookbooks/__init__.py +0 -0
- moonshot/src/cookbooks/cookbook.py +225 -0
- moonshot/src/cookbooks/cookbook_arguments.py +34 -0
- moonshot/src/datasets/__init__.py +0 -0
- moonshot/src/datasets/dataset.py +255 -0
- moonshot/src/datasets/dataset_arguments.py +50 -0
- moonshot/src/metrics/__init__.py +0 -0
- moonshot/src/metrics/metric.py +192 -0
- moonshot/src/metrics/metric_interface.py +95 -0
- moonshot/src/prompt_templates/__init__.py +0 -0
- moonshot/src/prompt_templates/prompt_template.py +103 -0
- moonshot/src/recipes/__init__.py +0 -0
- moonshot/src/recipes/recipe.py +340 -0
- moonshot/src/recipes/recipe_arguments.py +111 -0
- moonshot/src/redteaming/__init__.py +0 -0
- moonshot/src/redteaming/attack/__init__.py +0 -0
- moonshot/src/redteaming/attack/attack_module.py +618 -0
- moonshot/src/redteaming/attack/attack_module_arguments.py +44 -0
- moonshot/src/redteaming/attack/context_strategy.py +131 -0
- moonshot/src/redteaming/context_strategy/__init__.py +0 -0
- moonshot/src/redteaming/context_strategy/context_strategy_interface.py +46 -0
- moonshot/src/redteaming/session/__init__.py +0 -0
- moonshot/src/redteaming/session/chat.py +209 -0
- moonshot/src/redteaming/session/red_teaming_progress.py +128 -0
- moonshot/src/redteaming/session/red_teaming_type.py +6 -0
- moonshot/src/redteaming/session/session.py +775 -0
- moonshot/src/results/__init__.py +0 -0
- moonshot/src/results/result.py +119 -0
- moonshot/src/results/result_arguments.py +44 -0
- moonshot/src/runners/__init__.py +0 -0
- moonshot/src/runners/runner.py +476 -0
- moonshot/src/runners/runner_arguments.py +46 -0
- moonshot/src/runners/runner_type.py +6 -0
- moonshot/src/runs/__init__.py +0 -0
- moonshot/src/runs/run.py +344 -0
- moonshot/src/runs/run_arguments.py +162 -0
- moonshot/src/runs/run_progress.py +145 -0
- moonshot/src/runs/run_status.py +10 -0
- moonshot/src/storage/__init__.py +0 -0
- moonshot/src/storage/db_interface.py +128 -0
- moonshot/src/storage/io_interface.py +31 -0
- moonshot/src/storage/storage.py +525 -0
- moonshot/src/utils/__init__.py +0 -0
- moonshot/src/utils/import_modules.py +96 -0
- moonshot/src/utils/timeit.py +25 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from pydantic import validate_call
|
|
2
|
+
|
|
3
|
+
from moonshot.src.datasets.dataset import Dataset
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# ------------------------------------------------------------------------------
|
|
7
|
+
# Datasets APIs
|
|
8
|
+
# ------------------------------------------------------------------------------
|
|
9
|
+
@validate_call
|
|
10
|
+
def api_delete_dataset(ds_id: str) -> bool:
|
|
11
|
+
"""
|
|
12
|
+
Deletes a dataset identified by its unique dataset ID.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
ds_id (str): The unique identifier for the dataset to be deleted.
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
bool: True if the dataset was successfully deleted.
|
|
19
|
+
|
|
20
|
+
Raises:
|
|
21
|
+
Exception: If the deletion process encounters an error.
|
|
22
|
+
"""
|
|
23
|
+
return Dataset.delete(ds_id)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def api_get_all_datasets() -> list[dict]:
|
|
27
|
+
"""
|
|
28
|
+
This function retrieves all available datasets and returns them as a list of dictionaries. Each dictionary
|
|
29
|
+
represents a result and contains its information.
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
list[dict]: A list of dictionaries, each representing a result.
|
|
33
|
+
"""
|
|
34
|
+
_, datasets = Dataset.get_available_items()
|
|
35
|
+
return [dataset.to_dict() for dataset in datasets]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def api_get_all_datasets_name() -> list[str]:
|
|
39
|
+
"""
|
|
40
|
+
This function retrieves all available datasets names and returns them as a list.
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
list[str]: A list of datasets names.
|
|
44
|
+
"""
|
|
45
|
+
datasets_name, _ = Dataset.get_available_items()
|
|
46
|
+
return datasets_name
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from moonshot.src.configs.env_variables import EnvironmentVars
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
# ------------------------------------------------------------------------------
|
|
5
|
+
# Environment Variables APIs
|
|
6
|
+
# ------------------------------------------------------------------------------
|
|
7
|
+
def api_set_environment_variables(env_vars: dict) -> None:
|
|
8
|
+
"""
|
|
9
|
+
Sets the environment variables for the current session.
|
|
10
|
+
|
|
11
|
+
Args:
|
|
12
|
+
env_vars (dict): A dictionary containing the environment variables to set.
|
|
13
|
+
|
|
14
|
+
Returns:
|
|
15
|
+
None
|
|
16
|
+
"""
|
|
17
|
+
EnvironmentVars.load_env(env_vars)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from pydantic import validate_call
|
|
2
|
+
|
|
3
|
+
from moonshot.src.metrics.metric import Metric
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# ------------------------------------------------------------------------------
|
|
7
|
+
# Metrics APIs
|
|
8
|
+
# ------------------------------------------------------------------------------
|
|
9
|
+
@validate_call
|
|
10
|
+
def api_delete_metric(met_id: str) -> bool:
|
|
11
|
+
"""
|
|
12
|
+
Deletes a metric identified by its unique metric ID.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
met_id (str): The unique identifier for the metric to be deleted.
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
bool: True if the metric was successfully deleted.
|
|
19
|
+
|
|
20
|
+
Raises:
|
|
21
|
+
Exception: If the deletion process encounters an error.
|
|
22
|
+
"""
|
|
23
|
+
return Metric.delete(met_id)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def api_get_all_metric() -> list[dict]:
|
|
27
|
+
"""
|
|
28
|
+
Retrieves all available metrics.
|
|
29
|
+
|
|
30
|
+
This function calls the get_available_items method from the Metric class to retrieve all available metrics.
|
|
31
|
+
It then returns a list of dictionaries, each containing the details of a metric.
|
|
32
|
+
|
|
33
|
+
Returns:
|
|
34
|
+
list[dict]: A list of dictionaries, each representing a metric's details.
|
|
35
|
+
"""
|
|
36
|
+
_, metrics_info = Metric.get_available_items()
|
|
37
|
+
return metrics_info
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def api_get_all_metric_name() -> list[str]:
|
|
41
|
+
"""
|
|
42
|
+
Retrieves all available metric names.
|
|
43
|
+
|
|
44
|
+
This function calls the get_available_items method from the Metric class to retrieve all available metrics.
|
|
45
|
+
It then extracts the names of each metric and returns a list of these names.
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
list[str]: A list of strings, each representing a metric name.
|
|
49
|
+
"""
|
|
50
|
+
metrics_names, _ = Metric.get_available_items()
|
|
51
|
+
return metrics_names
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from pydantic import validate_call
|
|
2
|
+
|
|
3
|
+
from moonshot.src.prompt_templates.prompt_template import PromptTemplate
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# ------------------------------------------------------------------------------
|
|
7
|
+
# Prompt Template APIs
|
|
8
|
+
# ------------------------------------------------------------------------------
|
|
9
|
+
def api_get_all_prompt_template_detail() -> list[dict]:
|
|
10
|
+
"""
|
|
11
|
+
Retrieves all available prompt template details and returns them as a list of dictionaries.
|
|
12
|
+
|
|
13
|
+
Returns:
|
|
14
|
+
list[dict]: A list of dictionaries, each representing the details of a prompt template.
|
|
15
|
+
"""
|
|
16
|
+
return PromptTemplate.get_all_prompt_template_details()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def api_get_all_prompt_template_name() -> list[str]:
|
|
20
|
+
"""
|
|
21
|
+
Retrieves all available prompt template names and returns them as a list.
|
|
22
|
+
|
|
23
|
+
Returns:
|
|
24
|
+
list[str]: A list of prompt template names.
|
|
25
|
+
"""
|
|
26
|
+
return PromptTemplate.get_all_prompt_template_names()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@validate_call
|
|
30
|
+
def api_delete_prompt_template(pt_id: str) -> bool:
|
|
31
|
+
"""
|
|
32
|
+
Deletes a prompt template by its identifier.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
pt_id (str): The unique identifier of the prompt template to be deleted.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
bool: True if the prompt template was successfully deleted.
|
|
39
|
+
|
|
40
|
+
Raises:
|
|
41
|
+
Exception: If the deletion process encounters an error.
|
|
42
|
+
"""
|
|
43
|
+
return PromptTemplate.delete(pt_id)
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
from pydantic import conlist, validate_call
|
|
2
|
+
|
|
3
|
+
from moonshot.src.recipes.recipe import Recipe
|
|
4
|
+
from moonshot.src.recipes.recipe_arguments import RecipeArguments
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
# ------------------------------------------------------------------------------
|
|
8
|
+
# Recipe APIs
|
|
9
|
+
# ------------------------------------------------------------------------------
|
|
10
|
+
@validate_call
|
|
11
|
+
def api_create_recipe(
|
|
12
|
+
name: str,
|
|
13
|
+
description: str,
|
|
14
|
+
tags: list[str],
|
|
15
|
+
categories: list[str],
|
|
16
|
+
datasets: list[str],
|
|
17
|
+
prompt_templates: list[str],
|
|
18
|
+
metrics: list[str],
|
|
19
|
+
attack_modules: list[str],
|
|
20
|
+
grading_scale: dict[str, list[int]],
|
|
21
|
+
) -> str:
|
|
22
|
+
"""
|
|
23
|
+
Creates a new recipe with the given parameters.
|
|
24
|
+
|
|
25
|
+
This function takes various parameters that define a recipe, creates a RecipeArguments
|
|
26
|
+
object with these parameters, and then calls the Recipe.create method to create a new
|
|
27
|
+
recipe in the system.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
name (str): The name of the recipe.
|
|
31
|
+
description (str): A description of the recipe.
|
|
32
|
+
tags (list[str]): A list of tags associated with the recipe.
|
|
33
|
+
categories (list[str]): A list of categories the recipe belongs to.
|
|
34
|
+
datasets (list[str]): A list of datasets used in the recipe.
|
|
35
|
+
prompt_templates (list[str]): A list of prompt templates for the recipe.
|
|
36
|
+
metrics (list[str]): A list of metrics to evaluate the recipe.
|
|
37
|
+
attack_modules (list[str]): A list of attack modules used in the recipe.
|
|
38
|
+
grading_scale (dict[str, list[int]]): A grading scale dictionary where the key is the grade and the
|
|
39
|
+
value is a list of integers representing the scale.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
str: The ID of the newly created recipe.
|
|
43
|
+
"""
|
|
44
|
+
rec_args = RecipeArguments(
|
|
45
|
+
id="",
|
|
46
|
+
name=name,
|
|
47
|
+
description=description,
|
|
48
|
+
tags=tags,
|
|
49
|
+
categories=categories,
|
|
50
|
+
datasets=datasets,
|
|
51
|
+
prompt_templates=prompt_templates,
|
|
52
|
+
metrics=metrics,
|
|
53
|
+
attack_modules=attack_modules,
|
|
54
|
+
grading_scale=grading_scale,
|
|
55
|
+
)
|
|
56
|
+
return Recipe.create(rec_args)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@validate_call
|
|
60
|
+
def api_read_recipe(rec_id: str) -> dict:
|
|
61
|
+
"""
|
|
62
|
+
Reads a recipe and returns its information.
|
|
63
|
+
|
|
64
|
+
This function takes a recipe ID as input, reads the corresponding recipe,
|
|
65
|
+
and returns a dictionary containing the recipe's information.
|
|
66
|
+
|
|
67
|
+
Args:
|
|
68
|
+
rec_id (str): The ID of the recipe.
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
dict: A dictionary containing the recipe's information.
|
|
72
|
+
"""
|
|
73
|
+
return Recipe.read(rec_id).to_dict()
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@validate_call
|
|
77
|
+
def api_read_recipes(rec_ids: conlist(str, min_length=1)) -> list[dict]:
|
|
78
|
+
"""
|
|
79
|
+
Reads multiple recipes and returns their information.
|
|
80
|
+
|
|
81
|
+
This function takes a list of recipe IDs as input, reads the corresponding recipes,
|
|
82
|
+
and returns a list of dictionaries containing each recipe's information.
|
|
83
|
+
|
|
84
|
+
Args:
|
|
85
|
+
rec_ids (list[str]): The IDs of the recipes.
|
|
86
|
+
|
|
87
|
+
Returns:
|
|
88
|
+
list[dict]: A list of dictionaries, each containing a recipe's information.
|
|
89
|
+
"""
|
|
90
|
+
# This function uses list comprehension to iterate over the list of recipe IDs,
|
|
91
|
+
# calling the read_recipe method for each ID and converting the result to a dictionary.
|
|
92
|
+
# The resulting list of dictionaries is then returned.
|
|
93
|
+
return [Recipe.read(rec_id).to_dict() for rec_id in rec_ids]
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@validate_call
|
|
97
|
+
def api_update_recipe(rec_id: str, **kwargs) -> bool:
|
|
98
|
+
"""
|
|
99
|
+
Updates a recipe with the given keyword arguments.
|
|
100
|
+
|
|
101
|
+
This function takes a recipe ID and arbitrary keyword arguments, checks if the recipe exists,
|
|
102
|
+
and updates the fields of the recipe with the provided values. If the recipe does not exist,
|
|
103
|
+
a RuntimeError is raised. If the update is successful, it returns True.
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
rec_id (str): The ID of the recipe to update.
|
|
107
|
+
**kwargs: Arbitrary keyword arguments representing the fields to update.
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
bool: True if the recipe was successfully updated.
|
|
111
|
+
|
|
112
|
+
Raises:
|
|
113
|
+
RuntimeError: If the recipe with the given ID does not exist.
|
|
114
|
+
"""
|
|
115
|
+
# Check if the recipe exists
|
|
116
|
+
try:
|
|
117
|
+
existing_recipe = Recipe.read(rec_id)
|
|
118
|
+
except Exception:
|
|
119
|
+
raise RuntimeError(f"Recipe with ID '{rec_id}' does not exist")
|
|
120
|
+
|
|
121
|
+
# Update the fields of the existing recipe with the provided kwargs
|
|
122
|
+
for key, value in kwargs.items():
|
|
123
|
+
if hasattr(existing_recipe, key):
|
|
124
|
+
setattr(existing_recipe, key, value)
|
|
125
|
+
|
|
126
|
+
# Perform pydantic check on the updated existing recipe
|
|
127
|
+
RecipeArguments.model_validate(existing_recipe.to_dict())
|
|
128
|
+
|
|
129
|
+
# Update the recipe
|
|
130
|
+
return Recipe.update(existing_recipe)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@validate_call
|
|
134
|
+
def api_delete_recipe(rec_id: str) -> bool:
|
|
135
|
+
"""
|
|
136
|
+
Deletes a recipe identified by its unique recipe ID.
|
|
137
|
+
|
|
138
|
+
This function takes a recipe ID, verifies the existence of the recipe, and if found, calls the delete method from
|
|
139
|
+
the Recipe class to remove the recipe from storage.
|
|
140
|
+
|
|
141
|
+
If the deletion is successful, it returns True.
|
|
142
|
+
If the recipe does not exist or an exception occurs during deletion, a RuntimeError is raised with an
|
|
143
|
+
appropriate error message.
|
|
144
|
+
|
|
145
|
+
Args:
|
|
146
|
+
rec_id (str): The unique identifier for the recipe to be deleted.
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
bool: True if the recipe was successfully deleted.
|
|
150
|
+
|
|
151
|
+
Raises:
|
|
152
|
+
RuntimeError: If the deletion process encounters an error.
|
|
153
|
+
"""
|
|
154
|
+
return Recipe.delete(rec_id)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def api_get_all_recipe() -> list[dict]:
|
|
158
|
+
"""
|
|
159
|
+
Retrieves all available recipes.
|
|
160
|
+
|
|
161
|
+
This function calls the get_available_recipes method to retrieve all available recipes. It then converts each
|
|
162
|
+
recipe into a dictionary using the to_dict method and returns a list of these dictionaries.
|
|
163
|
+
|
|
164
|
+
Returns:
|
|
165
|
+
list[dict]: A list of dictionaries, each representing a recipe.
|
|
166
|
+
"""
|
|
167
|
+
_, recipes = Recipe.get_available_items()
|
|
168
|
+
return [recipe.to_dict() for recipe in recipes]
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def api_get_all_recipe_name() -> list[str]:
|
|
172
|
+
"""
|
|
173
|
+
Retrieves all available recipe names.
|
|
174
|
+
|
|
175
|
+
This function calls the get_available_recipes method to retrieve all available recipes. It then extracts the names
|
|
176
|
+
of each recipe and returns a list of these names.
|
|
177
|
+
|
|
178
|
+
Returns:
|
|
179
|
+
list[str]: A list of strings, each representing a recipe name.
|
|
180
|
+
"""
|
|
181
|
+
recipes_names, _ = Recipe.get_available_items()
|
|
182
|
+
return recipes_names
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
from pydantic import validate_call
|
|
2
|
+
|
|
3
|
+
from moonshot.src.redteaming.attack.attack_module import AttackModule
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# ------------------------------------------------------------------------------
|
|
7
|
+
# Red teaming APIs
|
|
8
|
+
# ------------------------------------------------------------------------------
|
|
9
|
+
def api_get_all_attack_modules() -> list[str]:
|
|
10
|
+
"""
|
|
11
|
+
Retrieves all available attack module IDs.
|
|
12
|
+
|
|
13
|
+
This function calls the `get_available_items` method from the `AttackModule` class to retrieve all available
|
|
14
|
+
attack modules.
|
|
15
|
+
|
|
16
|
+
It then extracts the IDs of each attack module and returns a list of these IDs.
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
list[str]: A list of strings, each representing an attack module ID.
|
|
20
|
+
"""
|
|
21
|
+
attack_modules_ids, _ = AttackModule.get_available_items()
|
|
22
|
+
return attack_modules_ids
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def api_get_all_attack_module_metadata() -> list[dict]:
|
|
26
|
+
"""
|
|
27
|
+
Retrieves metadata for all available attack modules.
|
|
28
|
+
|
|
29
|
+
This function calls the `get_available_items` method from the `AttackModule` class to retrieve all available
|
|
30
|
+
attack modules metadata.
|
|
31
|
+
|
|
32
|
+
It then extracts the metadata for each attack module and returns a list of dictionaries, each containing the
|
|
33
|
+
metadata of an attack module.
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
list[dict]: A list of dictionaries, each representing the metadata of an attack module.
|
|
37
|
+
"""
|
|
38
|
+
_, attack_modules_metadata = AttackModule.get_available_items()
|
|
39
|
+
return attack_modules_metadata
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@validate_call
|
|
43
|
+
def api_delete_attack_module(am_id: str) -> bool:
|
|
44
|
+
"""
|
|
45
|
+
Deletes an attack module by its identifier.
|
|
46
|
+
|
|
47
|
+
This function takes an attack module ID as input and calls the delete method from the AttackModule class
|
|
48
|
+
to remove the specified attack module from storage.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
am_id (str): The unique identifier of the attack module to be deleted.
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
bool: True if the attack module was successfully deleted.
|
|
55
|
+
|
|
56
|
+
Raises:
|
|
57
|
+
Exception: If the deletion process encounters an error.
|
|
58
|
+
"""
|
|
59
|
+
return AttackModule.delete(am_id)
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
from pydantic import conlist, validate_call
|
|
2
|
+
|
|
3
|
+
from moonshot.src.results.result import Result
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# ------------------------------------------------------------------------------
|
|
7
|
+
# Result APIs
|
|
8
|
+
# ------------------------------------------------------------------------------
|
|
9
|
+
@validate_call
|
|
10
|
+
def api_read_result(res_id: str) -> dict:
|
|
11
|
+
"""
|
|
12
|
+
Reads a result and returns its information.
|
|
13
|
+
|
|
14
|
+
This function takes a result ID as input, reads the corresponding database file from the storage manager,
|
|
15
|
+
and returns a dictionary containing the result's information.
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
res_id (str): The ID of the result.
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
dict: A dictionary containing the result's information.
|
|
22
|
+
"""
|
|
23
|
+
return Result.read(res_id)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@validate_call
|
|
27
|
+
def api_read_results(res_ids: conlist(str, min_length=1)) -> list[dict]:
|
|
28
|
+
"""
|
|
29
|
+
Reads multiple results and returns their information.
|
|
30
|
+
|
|
31
|
+
This function takes a list of result IDs as input, reads the corresponding database files from the storage manager,
|
|
32
|
+
and returns a list of dictionaries, each containing a result's information.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
res_ids (conlist(str, min_length=1)): A list of result IDs.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
list[dict]: A list of dictionaries, each containing a result's information.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
return [Result.read(res_id) for res_id in res_ids]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@validate_call
|
|
45
|
+
def api_delete_result(res_id: str) -> bool:
|
|
46
|
+
"""
|
|
47
|
+
Deletes a result by its identifier.
|
|
48
|
+
|
|
49
|
+
This function takes a result ID as input and calls the delete method from the Result class
|
|
50
|
+
to remove the specified result from storage.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
res_id (str): The unique identifier of the result to be deleted.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
bool: True if the result was successfully deleted.
|
|
57
|
+
|
|
58
|
+
Raises:
|
|
59
|
+
Exception: If the deletion process encounters an error.
|
|
60
|
+
"""
|
|
61
|
+
return Result.delete(res_id)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def api_get_all_result() -> list[dict]:
|
|
65
|
+
"""
|
|
66
|
+
This function retrieves all available results and returns them as a list of dictionaries. Each dictionary
|
|
67
|
+
represents a result and contains its information.
|
|
68
|
+
|
|
69
|
+
Returns:
|
|
70
|
+
list[dict]: A list of dictionaries, each representing a result.
|
|
71
|
+
"""
|
|
72
|
+
_, results = Result.get_available_items()
|
|
73
|
+
return [result for result in results]
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def api_get_all_result_name() -> list[str]:
|
|
77
|
+
"""
|
|
78
|
+
This function retrieves all available result names and returns them as a list.
|
|
79
|
+
|
|
80
|
+
Returns:
|
|
81
|
+
list[str]: A list of result names.
|
|
82
|
+
"""
|
|
83
|
+
results_name, _ = Result.get_available_items()
|
|
84
|
+
return results_name
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
from moonshot.src.api.api_runner import api_get_all_runner, api_load_runner
|
|
2
|
+
from moonshot.src.runs.run import Run
|
|
3
|
+
from moonshot.src.runs.run_arguments import RunArguments
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# ------------------------------------------------------------------------------
|
|
7
|
+
# Run APIs
|
|
8
|
+
# ------------------------------------------------------------------------------
|
|
9
|
+
def api_get_all_run(runner_id: str = "") -> list[dict]:
|
|
10
|
+
"""
|
|
11
|
+
Retrieves all runs for a given runner ID or for all runners if no ID is provided.
|
|
12
|
+
|
|
13
|
+
This function calls an internal API to get available runs and then converts each run into a dictionary format.
|
|
14
|
+
|
|
15
|
+
Args:
|
|
16
|
+
runner_id (str, optional): The ID of the runner to retrieve runs for. If empty, runs for all runners
|
|
17
|
+
are retrieved.
|
|
18
|
+
|
|
19
|
+
Returns:
|
|
20
|
+
list[dict]: A list of dictionaries, each representing a run's data.
|
|
21
|
+
"""
|
|
22
|
+
_, runs = _api_get_available_runs(runner_id)
|
|
23
|
+
return [run.to_dict() for run in runs]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _api_get_available_runs(
|
|
27
|
+
runner_id: str = "",
|
|
28
|
+
) -> tuple[list[str], list[RunArguments]]:
|
|
29
|
+
"""
|
|
30
|
+
Retrieves available runs and their corresponding runner IDs.
|
|
31
|
+
|
|
32
|
+
This function fetches information about available runs based on the provided runner ID. If no runner ID is
|
|
33
|
+
provided, it fetches runs for all runners. It returns a tuple containing a list of runner IDs and a list of
|
|
34
|
+
RunArguments instances representing the runs.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
runner_id (str): The ID of the runner for which to retrieve run information. If empty, information for
|
|
38
|
+
all runners is retrieved.
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
tuple[list[str], list[RunArguments]]: A tuple containing a list of runner IDs and a list of RunArguments
|
|
42
|
+
instances for the corresponding runs.
|
|
43
|
+
"""
|
|
44
|
+
# Lists to hold runner IDs and Run instances.
|
|
45
|
+
runners_ids = []
|
|
46
|
+
runs = []
|
|
47
|
+
|
|
48
|
+
# Retrieve information for all runners.
|
|
49
|
+
runners_info = api_get_all_runner()
|
|
50
|
+
|
|
51
|
+
# Load the specified runner, or all runners if no ID is provided.
|
|
52
|
+
if runner_id:
|
|
53
|
+
runner_instances = (
|
|
54
|
+
[api_load_runner(runner_id)]
|
|
55
|
+
if any(runner_id == runner_info.get("id") for runner_info in runners_info)
|
|
56
|
+
else []
|
|
57
|
+
)
|
|
58
|
+
else:
|
|
59
|
+
runner_instances = [
|
|
60
|
+
api_load_runner(str(runner_info.get("id"))) for runner_info in runners_info
|
|
61
|
+
]
|
|
62
|
+
|
|
63
|
+
# Collect runs from each runner instance.
|
|
64
|
+
for runner_instance in runner_instances:
|
|
65
|
+
# Only proceed if the runner has an associated database instance.
|
|
66
|
+
if runner_instance.database_instance:
|
|
67
|
+
# Fetch all runs from the database associated with the runner.
|
|
68
|
+
all_runs = Run.get_all_runs(runner_instance.database_instance)
|
|
69
|
+
# Record the runner ID and the runs.
|
|
70
|
+
runners_ids.append(runner_instance.id)
|
|
71
|
+
runs.extend(all_runs)
|
|
72
|
+
|
|
73
|
+
# Return the runner IDs and their runs.
|
|
74
|
+
return runners_ids, runs
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
from typing import Callable
|
|
2
|
+
|
|
3
|
+
from pydantic import validate_call
|
|
4
|
+
|
|
5
|
+
from moonshot.src.runners.runner import Runner
|
|
6
|
+
from moonshot.src.runners.runner_arguments import RunnerArguments
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# ------------------------------------------------------------------------------
|
|
10
|
+
# Runner APIs
|
|
11
|
+
# ------------------------------------------------------------------------------
|
|
12
|
+
@validate_call
|
|
13
|
+
def api_create_runner(
|
|
14
|
+
name: str,
|
|
15
|
+
endpoints: list[str],
|
|
16
|
+
description: str = "",
|
|
17
|
+
progress_callback_func: Callable | None = None,
|
|
18
|
+
) -> Runner:
|
|
19
|
+
"""
|
|
20
|
+
Creates a new runner.
|
|
21
|
+
|
|
22
|
+
This function takes the name, endpoints, and an optional progress callback function to create a new Runner instance.
|
|
23
|
+
The id of the runner is generated from the name of the runner using the slugify function,
|
|
24
|
+
so it does not need to be provided.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
name (str): The name of the runner.
|
|
28
|
+
endpoints (list[str]): A list of endpoint identifiers for the runner.
|
|
29
|
+
description (str, optional): A brief description of the runner. Defaults to an empty string.
|
|
30
|
+
progress_callback_func (Callable | None, optional): An optional callback function for progress updates.
|
|
31
|
+
Defaults to None.
|
|
32
|
+
|
|
33
|
+
Returns:
|
|
34
|
+
Runner: A new Runner object.
|
|
35
|
+
"""
|
|
36
|
+
# Create a new recipe runner
|
|
37
|
+
# We do not need to provide the id.
|
|
38
|
+
# This is because during creating:
|
|
39
|
+
# 1. the id is slugify from the name and stored as id.
|
|
40
|
+
runner_args = RunnerArguments(
|
|
41
|
+
id="",
|
|
42
|
+
name=name,
|
|
43
|
+
endpoints=endpoints,
|
|
44
|
+
description=description,
|
|
45
|
+
progress_callback_func=progress_callback_func,
|
|
46
|
+
)
|
|
47
|
+
return Runner.create(runner_args)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@validate_call
|
|
51
|
+
def api_load_runner(
|
|
52
|
+
runner_id: str, progress_callback_func: Callable | None = None
|
|
53
|
+
) -> Runner:
|
|
54
|
+
"""
|
|
55
|
+
Loads a runner based on the provided runner ID.
|
|
56
|
+
|
|
57
|
+
This function retrieves the runner using the provided runner ID and then loads it.
|
|
58
|
+
It utilizes the Runner's load method to fetch and return the runner.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
runner_id (str): The ID of the runner to be loaded.
|
|
62
|
+
progress_callback_func (Callable | None): The progress callback function to be used by the runner.
|
|
63
|
+
|
|
64
|
+
Returns:
|
|
65
|
+
Runner: An initialized Runner object.
|
|
66
|
+
"""
|
|
67
|
+
return Runner.load(runner_id, progress_callback_func)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@validate_call
|
|
71
|
+
def api_read_runner(runner_id: str) -> dict:
|
|
72
|
+
"""
|
|
73
|
+
Reads a runner and returns its information.
|
|
74
|
+
|
|
75
|
+
This function takes a runner ID as input, reads the corresponding runner,
|
|
76
|
+
and returns a dictionary containing the runner's information.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
runner_id (str): The ID of the runner.
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
dict: A dictionary containing the runner's information.
|
|
83
|
+
"""
|
|
84
|
+
return Runner.read(runner_id).to_dict()
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@validate_call
|
|
88
|
+
def api_delete_runner(runner_id: str) -> bool:
|
|
89
|
+
"""
|
|
90
|
+
Deletes a runner by its identifier.
|
|
91
|
+
|
|
92
|
+
This function takes a runner ID as input and calls the delete method from the Runner class
|
|
93
|
+
to remove the specified runner from storage.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
runner_id (str): The unique identifier of the runner to be deleted.
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
bool: True if the runner was successfully deleted.
|
|
100
|
+
|
|
101
|
+
Raises:
|
|
102
|
+
Exception: If the deletion process encounters an error.
|
|
103
|
+
"""
|
|
104
|
+
return Runner.delete(runner_id)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def api_get_all_runner() -> list[dict]:
|
|
108
|
+
"""
|
|
109
|
+
Retrieves all available runners.
|
|
110
|
+
|
|
111
|
+
This function calls the get_available_items method to retrieve all available runners. It then converts each
|
|
112
|
+
runner into a dictionary using the to_dict method and returns a list of these dictionaries.
|
|
113
|
+
|
|
114
|
+
Returns:
|
|
115
|
+
list[dict]: A list of dictionaries, each representing a runner.
|
|
116
|
+
"""
|
|
117
|
+
_, runners = Runner.get_available_items()
|
|
118
|
+
return [runner.to_dict() for runner in runners]
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def api_get_all_runner_name() -> list[str]:
|
|
122
|
+
"""
|
|
123
|
+
Retrieves all available runner names.
|
|
124
|
+
|
|
125
|
+
This function calls the get_available_items method to retrieve all available runners. It then extracts the names of
|
|
126
|
+
each runner and returns a list of these names.
|
|
127
|
+
|
|
128
|
+
Returns:
|
|
129
|
+
list[str]: A list of runner names.
|
|
130
|
+
"""
|
|
131
|
+
runners_names, _ = Runner.get_available_items()
|
|
132
|
+
return runners_names
|