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.
Files changed (163) hide show
  1. aiverify_moonshot-0.4.0.dist-info/METADATA +249 -0
  2. aiverify_moonshot-0.4.0.dist-info/RECORD +163 -0
  3. aiverify_moonshot-0.4.0.dist-info/WHEEL +4 -0
  4. aiverify_moonshot-0.4.0.dist-info/licenses/AUTHORS.md +5 -0
  5. aiverify_moonshot-0.4.0.dist-info/licenses/LICENSE.md +201 -0
  6. aiverify_moonshot-0.4.0.dist-info/licenses/NOTICES.md +3340 -0
  7. moonshot/__init__.py +0 -0
  8. moonshot/__main__.py +198 -0
  9. moonshot/api.py +155 -0
  10. moonshot/integrations/__init__.py +0 -0
  11. moonshot/integrations/cli/__init__.py +0 -0
  12. moonshot/integrations/cli/__main__.py +25 -0
  13. moonshot/integrations/cli/active_session_cfg.py +1 -0
  14. moonshot/integrations/cli/benchmark/__init__.py +0 -0
  15. moonshot/integrations/cli/benchmark/benchmark.py +186 -0
  16. moonshot/integrations/cli/benchmark/cookbook.py +545 -0
  17. moonshot/integrations/cli/benchmark/datasets.py +164 -0
  18. moonshot/integrations/cli/benchmark/metrics.py +141 -0
  19. moonshot/integrations/cli/benchmark/recipe.py +598 -0
  20. moonshot/integrations/cli/benchmark/result.py +216 -0
  21. moonshot/integrations/cli/benchmark/run.py +140 -0
  22. moonshot/integrations/cli/benchmark/runner.py +174 -0
  23. moonshot/integrations/cli/cli.py +64 -0
  24. moonshot/integrations/cli/common/__init__.py +0 -0
  25. moonshot/integrations/cli/common/common.py +72 -0
  26. moonshot/integrations/cli/common/connectors.py +325 -0
  27. moonshot/integrations/cli/common/display_helper.py +42 -0
  28. moonshot/integrations/cli/common/prompt_template.py +94 -0
  29. moonshot/integrations/cli/initialisation/__init__.py +0 -0
  30. moonshot/integrations/cli/initialisation/initialisation.py +14 -0
  31. moonshot/integrations/cli/redteam/__init__.py +0 -0
  32. moonshot/integrations/cli/redteam/attack_module.py +70 -0
  33. moonshot/integrations/cli/redteam/context_strategy.py +147 -0
  34. moonshot/integrations/cli/redteam/prompt_template.py +67 -0
  35. moonshot/integrations/cli/redteam/redteam.py +90 -0
  36. moonshot/integrations/cli/redteam/session.py +467 -0
  37. moonshot/integrations/web_api/.env.dev +7 -0
  38. moonshot/integrations/web_api/__init__.py +0 -0
  39. moonshot/integrations/web_api/__main__.py +56 -0
  40. moonshot/integrations/web_api/app.py +125 -0
  41. moonshot/integrations/web_api/container.py +146 -0
  42. moonshot/integrations/web_api/log/.gitkeep +0 -0
  43. moonshot/integrations/web_api/logging_conf.py +114 -0
  44. moonshot/integrations/web_api/routes/__init__.py +0 -0
  45. moonshot/integrations/web_api/routes/attack_modules.py +66 -0
  46. moonshot/integrations/web_api/routes/benchmark.py +116 -0
  47. moonshot/integrations/web_api/routes/benchmark_result.py +175 -0
  48. moonshot/integrations/web_api/routes/context_strategy.py +129 -0
  49. moonshot/integrations/web_api/routes/cookbook.py +225 -0
  50. moonshot/integrations/web_api/routes/dataset.py +120 -0
  51. moonshot/integrations/web_api/routes/endpoint.py +282 -0
  52. moonshot/integrations/web_api/routes/metric.py +78 -0
  53. moonshot/integrations/web_api/routes/prompt_template.py +128 -0
  54. moonshot/integrations/web_api/routes/recipe.py +219 -0
  55. moonshot/integrations/web_api/routes/redteam.py +609 -0
  56. moonshot/integrations/web_api/routes/runner.py +239 -0
  57. moonshot/integrations/web_api/schemas/__init__.py +0 -0
  58. moonshot/integrations/web_api/schemas/benchmark_runner_dto.py +13 -0
  59. moonshot/integrations/web_api/schemas/cookbook_create_dto.py +19 -0
  60. moonshot/integrations/web_api/schemas/cookbook_response_model.py +9 -0
  61. moonshot/integrations/web_api/schemas/dataset_response_dto.py +9 -0
  62. moonshot/integrations/web_api/schemas/endpoint_create_dto.py +21 -0
  63. moonshot/integrations/web_api/schemas/endpoint_response_model.py +11 -0
  64. moonshot/integrations/web_api/schemas/prompt_response_model.py +14 -0
  65. moonshot/integrations/web_api/schemas/prompt_template_response_model.py +10 -0
  66. moonshot/integrations/web_api/schemas/recipe_create_dto.py +32 -0
  67. moonshot/integrations/web_api/schemas/recipe_response_model.py +7 -0
  68. moonshot/integrations/web_api/schemas/session_create_dto.py +16 -0
  69. moonshot/integrations/web_api/schemas/session_prompt_dto.py +7 -0
  70. moonshot/integrations/web_api/schemas/session_response_model.py +38 -0
  71. moonshot/integrations/web_api/services/__init__.py +0 -0
  72. moonshot/integrations/web_api/services/attack_module_service.py +34 -0
  73. moonshot/integrations/web_api/services/auto_red_team_test_manager.py +86 -0
  74. moonshot/integrations/web_api/services/auto_red_team_test_state.py +57 -0
  75. moonshot/integrations/web_api/services/base_service.py +8 -0
  76. moonshot/integrations/web_api/services/benchmark_result_service.py +25 -0
  77. moonshot/integrations/web_api/services/benchmark_test_manager.py +106 -0
  78. moonshot/integrations/web_api/services/benchmark_test_state.py +56 -0
  79. moonshot/integrations/web_api/services/benchmarking_service.py +31 -0
  80. moonshot/integrations/web_api/services/context_strategy_service.py +22 -0
  81. moonshot/integrations/web_api/services/cookbook_service.py +194 -0
  82. moonshot/integrations/web_api/services/dataset_service.py +20 -0
  83. moonshot/integrations/web_api/services/endpoint_service.py +65 -0
  84. moonshot/integrations/web_api/services/metric_service.py +14 -0
  85. moonshot/integrations/web_api/services/prompt_template_service.py +39 -0
  86. moonshot/integrations/web_api/services/recipe_service.py +155 -0
  87. moonshot/integrations/web_api/services/runner_service.py +147 -0
  88. moonshot/integrations/web_api/services/session_service.py +350 -0
  89. moonshot/integrations/web_api/services/utils/exceptions_handler.py +41 -0
  90. moonshot/integrations/web_api/services/utils/results_formatter.py +47 -0
  91. moonshot/integrations/web_api/status_updater/interface/benchmark_progress_callback.py +14 -0
  92. moonshot/integrations/web_api/status_updater/interface/redteam_progress_callback.py +14 -0
  93. moonshot/integrations/web_api/status_updater/moonshot_ui_webhook.py +72 -0
  94. moonshot/integrations/web_api/types/types.py +99 -0
  95. moonshot/src/__init__.py +0 -0
  96. moonshot/src/api/__init__.py +0 -0
  97. moonshot/src/api/api_connector.py +58 -0
  98. moonshot/src/api/api_connector_endpoint.py +162 -0
  99. moonshot/src/api/api_context_strategy.py +57 -0
  100. moonshot/src/api/api_cookbook.py +160 -0
  101. moonshot/src/api/api_dataset.py +46 -0
  102. moonshot/src/api/api_environment_variables.py +17 -0
  103. moonshot/src/api/api_metrics.py +51 -0
  104. moonshot/src/api/api_prompt_template.py +43 -0
  105. moonshot/src/api/api_recipe.py +182 -0
  106. moonshot/src/api/api_red_teaming.py +59 -0
  107. moonshot/src/api/api_result.py +84 -0
  108. moonshot/src/api/api_run.py +74 -0
  109. moonshot/src/api/api_runner.py +132 -0
  110. moonshot/src/api/api_session.py +290 -0
  111. moonshot/src/configs/__init__.py +0 -0
  112. moonshot/src/configs/env_variables.py +187 -0
  113. moonshot/src/connectors/__init__.py +0 -0
  114. moonshot/src/connectors/connector.py +327 -0
  115. moonshot/src/connectors/connector_prompt_arguments.py +17 -0
  116. moonshot/src/connectors_endpoints/__init__.py +0 -0
  117. moonshot/src/connectors_endpoints/connector_endpoint.py +211 -0
  118. moonshot/src/connectors_endpoints/connector_endpoint_arguments.py +54 -0
  119. moonshot/src/cookbooks/__init__.py +0 -0
  120. moonshot/src/cookbooks/cookbook.py +225 -0
  121. moonshot/src/cookbooks/cookbook_arguments.py +34 -0
  122. moonshot/src/datasets/__init__.py +0 -0
  123. moonshot/src/datasets/dataset.py +255 -0
  124. moonshot/src/datasets/dataset_arguments.py +50 -0
  125. moonshot/src/metrics/__init__.py +0 -0
  126. moonshot/src/metrics/metric.py +192 -0
  127. moonshot/src/metrics/metric_interface.py +95 -0
  128. moonshot/src/prompt_templates/__init__.py +0 -0
  129. moonshot/src/prompt_templates/prompt_template.py +103 -0
  130. moonshot/src/recipes/__init__.py +0 -0
  131. moonshot/src/recipes/recipe.py +340 -0
  132. moonshot/src/recipes/recipe_arguments.py +111 -0
  133. moonshot/src/redteaming/__init__.py +0 -0
  134. moonshot/src/redteaming/attack/__init__.py +0 -0
  135. moonshot/src/redteaming/attack/attack_module.py +618 -0
  136. moonshot/src/redteaming/attack/attack_module_arguments.py +44 -0
  137. moonshot/src/redteaming/attack/context_strategy.py +131 -0
  138. moonshot/src/redteaming/context_strategy/__init__.py +0 -0
  139. moonshot/src/redteaming/context_strategy/context_strategy_interface.py +46 -0
  140. moonshot/src/redteaming/session/__init__.py +0 -0
  141. moonshot/src/redteaming/session/chat.py +209 -0
  142. moonshot/src/redteaming/session/red_teaming_progress.py +128 -0
  143. moonshot/src/redteaming/session/red_teaming_type.py +6 -0
  144. moonshot/src/redteaming/session/session.py +775 -0
  145. moonshot/src/results/__init__.py +0 -0
  146. moonshot/src/results/result.py +119 -0
  147. moonshot/src/results/result_arguments.py +44 -0
  148. moonshot/src/runners/__init__.py +0 -0
  149. moonshot/src/runners/runner.py +476 -0
  150. moonshot/src/runners/runner_arguments.py +46 -0
  151. moonshot/src/runners/runner_type.py +6 -0
  152. moonshot/src/runs/__init__.py +0 -0
  153. moonshot/src/runs/run.py +344 -0
  154. moonshot/src/runs/run_arguments.py +162 -0
  155. moonshot/src/runs/run_progress.py +145 -0
  156. moonshot/src/runs/run_status.py +10 -0
  157. moonshot/src/storage/__init__.py +0 -0
  158. moonshot/src/storage/db_interface.py +128 -0
  159. moonshot/src/storage/io_interface.py +31 -0
  160. moonshot/src/storage/storage.py +525 -0
  161. moonshot/src/utils/__init__.py +0 -0
  162. moonshot/src/utils/import_modules.py +96 -0
  163. moonshot/src/utils/timeit.py +25 -0
@@ -0,0 +1,164 @@
1
+ import cmd2
2
+ from rich.console import Console
3
+ from rich.table import Table
4
+
5
+ from moonshot.api import (
6
+ api_delete_dataset,
7
+ api_get_all_datasets,
8
+ api_get_all_datasets_name,
9
+ )
10
+ from moonshot.integrations.cli.common.display_helper import display_view_str_format
11
+
12
+ console = Console()
13
+
14
+
15
+ # ------------------------------------------------------------------------------
16
+ # CLI Functions
17
+ # ------------------------------------------------------------------------------
18
+ def list_datasets() -> None:
19
+ """
20
+ List all available datasets.
21
+
22
+ This function retrieves all available datasets by calling the api_get_all_datasets function from the
23
+ moonshot.api module. It then displays the datasets using the display_datasets function. If an exception occurs,
24
+ it prints an error message.
25
+
26
+ Returns:
27
+ None
28
+ """
29
+ try:
30
+ print("Listing datasets may take a while...")
31
+ datasets_list = api_get_all_datasets()
32
+ display_datasets(datasets_list)
33
+ except Exception as e:
34
+ print(f"[list_datasets]: {str(e)}")
35
+
36
+
37
+ def view_dataset(args) -> None:
38
+ """
39
+ View a specific dataset.
40
+
41
+ This function retrieves all available datasets and their names by calling the api_get_all_datasets and
42
+ api_get_all_datasets_name functions. It then finds the dataset with the name specified in args.dataset_filename
43
+ and displays it using the display_datasets function. If an exception occurs, it prints an error message.
44
+
45
+ Args:
46
+ args: A namespace object from argparse. It should have the following attribute:
47
+ dataset_filename (str): The name of the dataset to view.
48
+
49
+ Returns:
50
+ None
51
+ """
52
+ try:
53
+ print("Viewing datasets may take a while...")
54
+ datasets_list = api_get_all_datasets()
55
+ datasets_name_list = api_get_all_datasets_name()
56
+
57
+ # Find the index of the dataset with the name args.dataset_filename
58
+ dataset_index = datasets_name_list.index(args.dataset_filename)
59
+ # Pass the corresponding dataset from datasets_list to display_datasets
60
+ display_datasets([datasets_list[dataset_index]])
61
+
62
+ except Exception as e:
63
+ print(f"[view_dataset]: {str(e)}")
64
+
65
+
66
+ def delete_dataset(args) -> None:
67
+ """
68
+ Delete a dataset.
69
+
70
+ This function deletes a dataset with the specified name. It prompts the user for confirmation before proceeding
71
+ with the deletion. If the user confirms, it calls the api_delete_dataset function from the moonshot.api module to
72
+ delete the dataset. If the deletion is successful, it prints a confirmation message. If an exception occurs, it
73
+ prints an error message.
74
+
75
+ Args:
76
+ args: A namespace object from argparse. It should have the following attribute:
77
+ dataset_name (str): The name of the dataset to delete.
78
+
79
+ Returns:
80
+ None
81
+ """
82
+ # Confirm with the user before deleting a dataset
83
+ confirmation = console.input(
84
+ "[bold red]Are you sure you want to delete the dataset (y/N)? [/]"
85
+ )
86
+ if confirmation.lower() != "y":
87
+ console.print("[bold yellow]Dataset deletion cancelled.[/]")
88
+ return
89
+ try:
90
+ api_delete_dataset(args.dataset)
91
+ print("[delete_dataset]: Dataset deleted.")
92
+ except Exception as e:
93
+ print(f"[delete_dataset]: {str(e)}")
94
+
95
+
96
+ # ------------------------------------------------------------------------------
97
+ # Helper functions: Display on cli
98
+ # ------------------------------------------------------------------------------
99
+ def display_datasets(datasets_list: list):
100
+ """
101
+ Displays a list of datasets in a table format.
102
+
103
+ This function takes a list of datasets and displays them in a table format with each dataset's name, description,
104
+ and other relevant details. If the list is empty, it prints a message indicating that no datasets are found.
105
+
106
+ Args:
107
+ datasets_list (list): A list of dictionaries, where each dictionary contains the details of a dataset.
108
+
109
+ Returns:
110
+ None
111
+ """
112
+ if datasets_list:
113
+ table = Table(
114
+ title="List of Datasets", show_lines=True, expand=True, header_style="bold"
115
+ )
116
+ table.add_column("No.", width=2)
117
+ table.add_column("Dataset", justify="left", width=78)
118
+ for dataset_no, dataset in enumerate(datasets_list, 1):
119
+ (
120
+ id,
121
+ name,
122
+ description,
123
+ _,
124
+ num_of_dataset_prompts,
125
+ created_date,
126
+ reference,
127
+ license,
128
+ ) = dataset.values()
129
+
130
+ prompt_info = display_view_str_format("Prompts", num_of_dataset_prompts)
131
+ created_date_info = display_view_str_format("Created Date", created_date)
132
+ license_info = display_view_str_format("License", license)
133
+ reference_info = display_view_str_format("Reference", reference)
134
+
135
+ dataset_info = (
136
+ f"[red]{id}[/red]\n\n[blue]{name}[/blue]\n{description}\n\n"
137
+ f"{prompt_info}\n\n{created_date_info}\n\n{license_info}\n\n{reference_info}"
138
+ )
139
+
140
+ table.add_section()
141
+ table.add_row(str(dataset_no), dataset_info)
142
+ console.print(table)
143
+ else:
144
+ console.print("[red]There are no datasets found.[/red]")
145
+
146
+
147
+ # ------------------------------------------------------------------------------
148
+ # Cmd2 Arguments Parsers
149
+ # ------------------------------------------------------------------------------
150
+ # View dataset arguments
151
+ view_dataset_args = cmd2.Cmd2ArgumentParser(
152
+ description="View a dataset file.",
153
+ epilog="Example:\n view_dataset bbq-lite-age-ambiguous",
154
+ )
155
+ view_dataset_args.add_argument(
156
+ "dataset_filename", type=str, help="Name of the dataset file"
157
+ )
158
+
159
+ # Delete dataset arguments
160
+ delete_dataset_args = cmd2.Cmd2ArgumentParser(
161
+ description="Delete a dataset.",
162
+ epilog="Example:\n delete_dataset bbq-lite-age-ambiguous",
163
+ )
164
+ delete_dataset_args.add_argument("dataset", type=str, help="Name of the dataset")
@@ -0,0 +1,141 @@
1
+ import cmd2
2
+ from rich.console import Console
3
+ from rich.table import Table
4
+
5
+ from moonshot.api import api_delete_metric, api_get_all_metric, api_get_all_metric_name
6
+
7
+ console = Console()
8
+
9
+
10
+ # ------------------------------------------------------------------------------
11
+ # CLI Functions
12
+ # ------------------------------------------------------------------------------
13
+ def list_metrics() -> None:
14
+ """
15
+ List all available metrics.
16
+
17
+ This function retrieves all available metrics by calling the api_get_all_metric function from the
18
+ moonshot.api module. It then displays the metrics using the display_metrics function. If an exception occurs,
19
+ it prints an error message.
20
+
21
+ Returns:
22
+ None
23
+ """
24
+ try:
25
+ print("Listing metrics may take a while...")
26
+ metrics_list = api_get_all_metric()
27
+ display_metrics(metrics_list)
28
+ except Exception as e:
29
+ print(f"[list_metrics]: {str(e)}")
30
+
31
+
32
+ def view_metric(args) -> None:
33
+ """
34
+ View a specific metric.
35
+
36
+ This function retrieves all available metrics and their names by calling the api_get_all_metric and
37
+ api_get_all_metric_name functions. It then finds the metric with the name specified in args.metric_filename
38
+ and displays it using the display_metrics function. If an exception occurs, it prints an error message.
39
+
40
+ Args:
41
+ args: A namespace object from argparse. It should have the following attribute:
42
+ metric_filename (str): The name of the metric to view.
43
+
44
+ Returns:
45
+ None
46
+ """
47
+ try:
48
+ print("Viewing metrics may take a while...")
49
+ metrics_list = api_get_all_metric()
50
+ metrics_name_list = api_get_all_metric_name()
51
+
52
+ # Find the index of the metric with the name args.metric_filename
53
+ metric_index = metrics_name_list.index(args.metric_filename)
54
+ # Pass the corresponding metric from metrics_list to display_metrics
55
+ display_metrics([metrics_list[metric_index]])
56
+
57
+ except Exception as e:
58
+ print(f"[view_metric]: {str(e)}")
59
+
60
+
61
+ def delete_metric(args) -> None:
62
+ """
63
+ Delete a metric.
64
+
65
+ This function deletes a metric with the specified identifier. It prompts the user for confirmation before proceeding
66
+ with the deletion. If the user confirms, it calls the api_delete_metric function from the moonshot.api module to
67
+ delete the metric. If the deletion is successful, it prints a confirmation message. If an exception occurs, it
68
+ prints an error message.
69
+
70
+ Args:
71
+ args: A namespace object from argparse. It should have the following attribute:
72
+ metric (str): The identifier of the metric to delete.
73
+
74
+ Returns:
75
+ None
76
+ """
77
+ # Confirm with the user before deleting a metric
78
+ confirmation = console.input(
79
+ "[bold red]Are you sure you want to delete the metric (y/N)? [/]"
80
+ )
81
+ if confirmation.lower() != "y":
82
+ console.print("[bold yellow]Metric deletion cancelled.[/]")
83
+ return
84
+ try:
85
+ api_delete_metric(args.metric)
86
+ print("[delete_metric]: Metric deleted.")
87
+ except Exception as e:
88
+ print(f"[delete_metric]: {str(e)}")
89
+
90
+
91
+ # ------------------------------------------------------------------------------
92
+ # Helper functions: Display on cli
93
+ # ------------------------------------------------------------------------------
94
+ def display_metrics(metrics_list: list):
95
+ """
96
+ Displays a list of metrics in a table format.
97
+
98
+ This function takes a list of metrics and displays them in a table format with each metric's ID, name, and
99
+ description. If the list is empty, it prints a message indicating that no metrics are found.
100
+
101
+ Args:
102
+ metrics_list (list): A list of dictionaries, where each dictionary contains the details of a metric.
103
+
104
+ Returns:
105
+ None
106
+ """
107
+ if metrics_list:
108
+ table = Table(
109
+ title="List of Metrics", show_lines=True, expand=True, header_style="bold"
110
+ )
111
+ table.add_column("No.", width=2)
112
+ table.add_column("Metric", justify="left", width=78)
113
+ for metric_no, metric in enumerate(metrics_list, 1):
114
+ id, name, description = metric.values()
115
+ result_info = f"[red]id: {id}[/red]\n\n[blue]{name}[/blue]\n{description}"
116
+
117
+ table.add_section()
118
+ table.add_row(str(metric_no), result_info)
119
+ console.print(table)
120
+ else:
121
+ console.print("[red]There are no metrics found.[/red]")
122
+
123
+
124
+ # ------------------------------------------------------------------------------
125
+ # Cmd2 Arguments Parsers
126
+ # ------------------------------------------------------------------------------
127
+ # View metric arguments
128
+ view_metric_args = cmd2.Cmd2ArgumentParser(
129
+ description="View a metric file.",
130
+ epilog="Example:\n view_metric my-new-metric",
131
+ )
132
+ view_metric_args.add_argument(
133
+ "metric_filename", type=str, help="Name of the metric file"
134
+ )
135
+
136
+ # Delete metric arguments
137
+ delete_metric_args = cmd2.Cmd2ArgumentParser(
138
+ description="Delete a metric.",
139
+ epilog="Example:\n delete_metric my-new-metric",
140
+ )
141
+ delete_metric_args.add_argument("metric", type=str, help="Name of the metric")