aiverify-moonshot 0.4.7__py3-none-any.whl → 0.4.9__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 (30) hide show
  1. {aiverify_moonshot-0.4.7.dist-info → aiverify_moonshot-0.4.9.dist-info}/METADATA +3 -3
  2. {aiverify_moonshot-0.4.7.dist-info → aiverify_moonshot-0.4.9.dist-info}/RECORD +29 -29
  3. {aiverify_moonshot-0.4.7.dist-info → aiverify_moonshot-0.4.9.dist-info}/licenses/LICENSE.md +1 -1
  4. aiverify_moonshot-0.4.9.dist-info/licenses/NOTICES.md +2506 -0
  5. moonshot/__main__.py +93 -49
  6. moonshot/api.py +12 -10
  7. moonshot/integrations/cli/benchmark/metrics.py +8 -2
  8. moonshot/integrations/cli/cli_errors.py +14 -0
  9. moonshot/integrations/cli/common/common.py +14 -8
  10. moonshot/integrations/cli/common/dataset.py +303 -65
  11. moonshot/integrations/cli/redteam/attack_module.py +30 -1
  12. moonshot/integrations/web_api/app.py +1 -1
  13. moonshot/integrations/web_api/routes/dataset.py +52 -18
  14. moonshot/integrations/web_api/schemas/cookbook_response_model.py +2 -0
  15. moonshot/integrations/web_api/schemas/dataset_create_dto.py +14 -4
  16. moonshot/integrations/web_api/schemas/recipe_response_model.py +1 -0
  17. moonshot/integrations/web_api/services/cookbook_service.py +36 -9
  18. moonshot/integrations/web_api/services/dataset_service.py +34 -9
  19. moonshot/integrations/web_api/services/recipe_service.py +33 -3
  20. moonshot/src/api/api_dataset.py +43 -11
  21. moonshot/src/bookmark/bookmark.py +16 -9
  22. moonshot/src/datasets/dataset.py +37 -45
  23. moonshot/src/datasets/dataset_arguments.py +2 -1
  24. moonshot/src/messages_constants.py +1 -0
  25. moonshot/src/redteaming/attack/attack_module.py +40 -0
  26. moonshot/src/storage/io_interface.py +18 -1
  27. moonshot/src/storage/storage.py +57 -1
  28. aiverify_moonshot-0.4.7.dist-info/licenses/NOTICES.md +0 -3340
  29. {aiverify_moonshot-0.4.7.dist-info → aiverify_moonshot-0.4.9.dist-info}/WHEEL +0 -0
  30. {aiverify_moonshot-0.4.7.dist-info → aiverify_moonshot-0.4.9.dist-info}/licenses/AUTHORS.md +0 -0
moonshot/__main__.py CHANGED
@@ -116,36 +116,56 @@ def download_nltk_resources() -> None:
116
116
  raise
117
117
 
118
118
 
119
- def moonshot_data_installation() -> None:
120
- # Code for moonshot-data installation
119
+ def moonshot_data_installation(unattended: bool, overwrite: bool) -> None:
120
+ """
121
+ Install Moonshot Data from GitHub.
122
+
123
+ This function clones the Moonshot Data repository from GitHub, installs its requirements,
124
+ and sets up the necessary environment. If the target directory already exists, it handles
125
+ the situation based on the 'unattended' and 'overwrite' flags.
126
+
127
+ Args:
128
+ unattended (bool): If True, the function will not prompt the user for input.
129
+ overwrite (bool): If True, the existing directory will be removed before installation.
130
+ """
121
131
  logger.info("Installing Moonshot Data from GitHub")
122
132
  repo = "https://github.com/aiverify-foundation/moonshot-data.git"
123
133
  folder_name = repo.split("/")[-1].replace(".git", "")
134
+ do_install = True
124
135
 
125
- # Check if the directory already exists
126
136
  if os.path.exists(folder_name):
127
- logger.warning(f"Directory {folder_name} already exists.")
128
- user_input = (
129
- input(
130
- f"Directory {folder_name} already exists. Do you want to remove it and reinstall? (Y/n): "
131
- )
132
- .strip()
133
- .lower()
134
- )
135
- if user_input == "y":
136
- logger.info(f"Removing directory {folder_name}.")
137
- # Remove the existing directory
137
+ if overwrite:
138
+ logger.info(f"Removing directory {folder_name} due to --overwrite flag.")
138
139
  shutil.rmtree(folder_name)
139
140
  else:
140
- logger.info("Exiting function without removing the directory.")
141
- return
142
-
143
- logger.info(f"Cloning {repo}")
144
- # Clone the repository
145
- run_subprocess(["git", "clone", repo], check=True)
146
-
147
- # Create .env to point to installed folder
148
- ms_lib_env_file(folder_name)
141
+ logger.warning(f"Directory {folder_name} already exists.")
142
+ if not unattended:
143
+ user_input = (
144
+ input(
145
+ f"Directory {folder_name} already exists. Do you want to remove it and reinstall? (Y/n): "
146
+ )
147
+ .strip()
148
+ .lower()
149
+ )
150
+ if user_input == "y":
151
+ logger.info(f"Removing directory {folder_name}.")
152
+ shutil.rmtree(folder_name)
153
+ else:
154
+ logger.info("Skipping removal of directory.")
155
+ do_install = False
156
+ else:
157
+ do_install = False
158
+ logger.info(
159
+ "Unattended mode detected. To reinstall, please use the --overwrite flag."
160
+ )
161
+
162
+ if do_install:
163
+ logger.info(f"Cloning {repo}")
164
+ # Clone the repository
165
+ run_subprocess(["git", "clone", repo], check=True)
166
+
167
+ # Create .env to point to installed folder
168
+ ms_lib_env_file(folder_name)
149
169
 
150
170
  # Change directory to the folder
151
171
  os.chdir(folder_name)
@@ -159,7 +179,6 @@ def moonshot_data_installation() -> None:
159
179
  # Change back to the base directory
160
180
  os.chdir("..")
161
181
 
162
-
163
182
  def check_node() -> bool:
164
183
  """
165
184
  Check if Node.js is installed on the user's machine.
@@ -176,36 +195,52 @@ def check_node() -> bool:
176
195
  return False
177
196
 
178
197
 
179
- def moonshot_ui_installation() -> None:
198
+ def moonshot_ui_installation(unattended: bool, overwrite: bool) -> None:
199
+ """
200
+ Install Moonshot UI from GitHub.
201
+ """
180
202
  if not check_node():
181
203
  logger.error("Node.js is not installed. Please install Node.js to proceed.")
182
204
  return
183
205
 
184
- # Code for moonshot-ui installation
206
+ logger.info("Installing Moonshot UI from GitHub")
185
207
  repo = "https://github.com/aiverify-foundation/moonshot-ui.git"
186
208
  folder_name = repo.split("/")[-1].replace(".git", "")
209
+ do_install = True
187
210
 
188
- # Check if the directory already exists
189
211
  if os.path.exists(folder_name):
190
- logger.warning(f"Directory {folder_name} already exists.")
191
- user_input = (
192
- input(
193
- f"Directory {folder_name} already exists. Do you want to remove it and reinstall? (Y/n): "
194
- )
195
- .strip()
196
- .lower()
197
- )
198
- if user_input == "y":
199
- logger.info(f"Removing directory {folder_name}.")
200
- # Remove the existing directory
212
+ if overwrite:
213
+ logger.info(f"Removing directory {folder_name} due to --overwrite flag.")
201
214
  shutil.rmtree(folder_name)
202
215
  else:
203
- logger.info("Exiting function without removing the directory.")
204
- return
205
-
206
- logger.info(f"Cloning {repo}")
207
- # Clone the repository
208
- run_subprocess(["git", "clone", repo], check=True)
216
+ logger.warning(f"Directory {folder_name} already exists.")
217
+ if not unattended:
218
+ user_input = (
219
+ input(
220
+ f"Directory {folder_name} already exists. Do you want to remove it and reinstall? (Y/n): "
221
+ )
222
+ .strip()
223
+ .lower()
224
+ )
225
+ if user_input == "y":
226
+ logger.info(f"Removing directory {folder_name}.")
227
+ shutil.rmtree(folder_name)
228
+ else:
229
+ logger.info("Skipping removal of directory.")
230
+ do_install = False
231
+ else:
232
+ do_install = False
233
+ logger.info(
234
+ "Unattended mode detected. To reinstall, please use the --overwrite flag."
235
+ )
236
+
237
+ if do_install:
238
+ logger.info(f"Cloning {repo}")
239
+ # Clone the repository
240
+ run_subprocess(["git", "clone", repo], check=True)
241
+
242
+ # Create .env for UI
243
+ ms_ui_env_file(folder_name)
209
244
 
210
245
  # Change directory to the folder
211
246
  os.chdir(folder_name)
@@ -219,9 +254,6 @@ def moonshot_ui_installation() -> None:
219
254
  # Change back to the base directory
220
255
  os.chdir("..")
221
256
 
222
- # Create .env for ui
223
- ms_ui_env_file(folder_name)
224
-
225
257
 
226
258
  def run_moonshot_ui() -> None:
227
259
  """
@@ -262,15 +294,27 @@ def main() -> None:
262
294
  parser.add_argument(
263
295
  "-e", "--env", type=str, help="Path to the .env file", default=".env"
264
296
  )
297
+ parser.add_argument(
298
+ "-u",
299
+ "--unattended",
300
+ action="store_true",
301
+ help="Perform action without user interaction",
302
+ )
303
+ parser.add_argument(
304
+ "-o",
305
+ "--overwrite",
306
+ action="store_true",
307
+ help="Force delete and reinstall the specified module",
308
+ )
265
309
 
266
310
  args = parser.parse_args()
267
311
 
268
312
  # Handle installations based on the -i include arguments
269
313
  if "moonshot-data" in args.install:
270
- moonshot_data_installation()
314
+ moonshot_data_installation(args.unattended, args.overwrite)
271
315
 
272
316
  if "moonshot-ui" in args.install:
273
- moonshot_ui_installation()
317
+ moonshot_ui_installation(args.unattended, args.overwrite)
274
318
 
275
319
  # If mode is not specified, skip running any modes
276
320
  if args.mode is None:
moonshot/api.py CHANGED
@@ -1,3 +1,11 @@
1
+ from moonshot.src.api.api_bookmark import (
2
+ api_delete_all_bookmark,
3
+ api_delete_bookmark,
4
+ api_export_bookmarks,
5
+ api_get_all_bookmarks,
6
+ api_get_bookmark,
7
+ api_insert_bookmark,
8
+ )
1
9
  from moonshot.src.api.api_connector import (
2
10
  api_create_connector_from_endpoint,
3
11
  api_create_connectors_from_endpoints,
@@ -26,10 +34,11 @@ from moonshot.src.api.api_cookbook import (
26
34
  api_update_cookbook,
27
35
  )
28
36
  from moonshot.src.api.api_dataset import (
37
+ api_convert_dataset,
29
38
  api_delete_dataset,
39
+ api_download_dataset,
30
40
  api_get_all_datasets,
31
41
  api_get_all_datasets_name,
32
- api_create_datasets
33
42
  )
34
43
  from moonshot.src.api.api_environment_variables import api_set_environment_variables
35
44
  from moonshot.src.api.api_metrics import (
@@ -87,14 +96,6 @@ from moonshot.src.api.api_session import (
87
96
  api_update_prompt_template,
88
97
  api_update_system_prompt,
89
98
  )
90
- from moonshot.src.api.api_bookmark import (
91
- api_get_all_bookmarks,
92
- api_get_bookmark,
93
- api_insert_bookmark,
94
- api_delete_bookmark,
95
- api_delete_all_bookmark,
96
- api_export_bookmarks,
97
- )
98
99
 
99
100
  __all__ = [
100
101
  "api_create_connector_from_endpoint",
@@ -116,7 +117,8 @@ __all__ = [
116
117
  "api_read_cookbook",
117
118
  "api_read_cookbooks",
118
119
  "api_update_cookbook",
119
- "api_create_datasets",
120
+ "api_convert_dataset",
121
+ "api_download_dataset",
120
122
  "api_delete_dataset",
121
123
  "api_get_all_datasets",
122
124
  "api_get_all_datasets_name",
@@ -170,9 +170,15 @@ def _display_metrics(metrics_list: list):
170
170
  table.add_column("No.", width=2)
171
171
  table.add_column("Metric", justify="left", width=78)
172
172
  for idx, metric in enumerate(metrics_list, 1):
173
- id, name, description, *other_args = metric.values()
173
+ id, name, description, endpoints, configurations, *other_args = metric.values()
174
174
  idx = metric.get("idx", idx)
175
- result_info = f"[red]id: {id}[/red]\n\n[blue]{name}[/blue]\n{description}"
175
+ result_info = (
176
+ f"[red]Id: {id}[/red]\n\n"
177
+ f"[blue]Name: {name}[/blue]\n\n"
178
+ f"[blue]Description: {description}[/blue]\n\n"
179
+ f"[blue]Endpoints: {endpoints}[/blue]\n\n"
180
+ f"[blue]Configurations:{configurations}[/blue]\n\n"
181
+ )
176
182
 
177
183
  table.add_section()
178
184
  table.add_row(str(idx), result_info)
@@ -509,3 +509,17 @@ ERROR_RED_TEAMING_ADD_BOOKMARK_NO_ACTIVE_SESSION = (
509
509
  ERROR_RED_TEAMING_USE_BOOKMARK_NO_ACTIVE_SESSION = (
510
510
  "There is no active session. Activate a session to bookmark a prompt."
511
511
  )
512
+
513
+
514
+ # ------------------------------------------------------------------------------
515
+ # Redteaming - list_attack_modules
516
+ # ------------------------------------------------------------------------------
517
+ ERROR_RED_TEAMING_LIST_ATTACK_MODULES_FIND_VALIDATION = (
518
+ "Invalid type for parameter: find. Expecting type str."
519
+ )
520
+ ERROR_RED_TEAMING_LIST_ATTACK_MODULES_PAGINATION_VALIDATION = (
521
+ "Invalid type for parameter: pagination. Expecting type str."
522
+ )
523
+ ERROR_RED_TEAMING_LIST_ATTACK_MODULES_PAGINATION_VALIDATION_1 = (
524
+ "The 'pagination' argument must be a tuple of two integers."
525
+ )
@@ -2,10 +2,6 @@ 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
- )
9
5
  from moonshot.integrations.cli.common.connectors import (
10
6
  add_endpoint,
11
7
  add_endpoint_args,
@@ -20,6 +16,12 @@ from moonshot.integrations.cli.common.connectors import (
20
16
  view_endpoint,
21
17
  view_endpoint_args,
22
18
  )
19
+ from moonshot.integrations.cli.common.dataset import (
20
+ convert_dataset,
21
+ convert_dataset_args,
22
+ download_dataset,
23
+ download_dataset_args,
24
+ )
23
25
  from moonshot.integrations.cli.common.prompt_template import (
24
26
  delete_prompt_template,
25
27
  delete_prompt_template_args,
@@ -60,10 +62,14 @@ class CommonCommandSet(cmd2.CommandSet):
60
62
  def do_add_endpoint(self, args: argparse.Namespace) -> None:
61
63
  add_endpoint(args)
62
64
 
63
- @cmd2.with_argparser(add_dataset_args)
64
- def do_add_dataset(self, args:argparse.Namespace) -> None:
65
- add_dataset(args)
66
-
65
+ @cmd2.with_argparser(convert_dataset_args)
66
+ def do_convert_dataset(self, args: argparse.Namespace) -> None:
67
+ convert_dataset(args)
68
+
69
+ @cmd2.with_argparser(download_dataset_args)
70
+ def do_download_dataset(self, args: argparse.Namespace) -> None:
71
+ download_dataset(args)
72
+
67
73
  # ------------------------------------------------------------------------------
68
74
  # Delete contents
69
75
  # ------------------------------------------------------------------------------