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,147 @@
1
+ import argparse
2
+
3
+ import cmd2
4
+ from rich.console import Console
5
+ from rich.table import Table
6
+
7
+ from moonshot.api import (
8
+ api_delete_context_strategy,
9
+ api_get_all_context_strategy_metadata,
10
+ api_update_context_strategy,
11
+ )
12
+ from moonshot.integrations.cli.active_session_cfg import active_session
13
+ from moonshot.src.redteaming.session.session import Session
14
+
15
+ console = Console()
16
+
17
+
18
+ def use_context_strategy(args: argparse.Namespace) -> None:
19
+ """
20
+ Use a context strategy for process the user's prompt (i.e. summarise past 3 prompts and add
21
+ it to the current user's prompt)
22
+
23
+ Args:
24
+ args: A namespace with the context strategy parameters. Expected to have 'context_strategy'.
25
+ """
26
+ new_context_strategy_name = args.context_strategy
27
+ num_of_prev_prompts = (
28
+ args.num_of_prev_prompts
29
+ if args.num_of_prev_prompts
30
+ else Session.DEFAULT_CONTEXT_STRATEGY_PROMPT
31
+ )
32
+
33
+ # Check if current session exists. If it does, update context strategy and number of previous prompts
34
+ if active_session:
35
+ active_session["context_strategy"] = new_context_strategy_name
36
+ active_session["cs_num_of_prev_prompts"] = num_of_prev_prompts
37
+ try:
38
+ api_update_context_strategy(
39
+ active_session["session_id"], new_context_strategy_name
40
+ )
41
+ print(
42
+ f"Updated session: {active_session['session_id']}. "
43
+ f"Context Strategy: {active_session['context_strategy']}."
44
+ f"No. of previous prompts for Context Strategy: {active_session['cs_num_of_prev_prompts']}."
45
+ )
46
+ except Exception as e:
47
+ print(f"[use_context_strategy]: {str(e)}")
48
+
49
+ else:
50
+ print(
51
+ "There is no active session. Activate a session to send a prompt with a context strategy."
52
+ )
53
+
54
+
55
+ def list_context_strategies() -> None:
56
+ """
57
+ List all context strategies available.
58
+ """
59
+ context_strategy_metadata_list = api_get_all_context_strategy_metadata()
60
+ if context_strategy_metadata_list:
61
+ table = Table(
62
+ title="Context Strategy List",
63
+ show_lines=True,
64
+ expand=True,
65
+ header_style="bold",
66
+ )
67
+ table.add_column("No.", justify="left", width=2)
68
+ table.add_column("Context Strategy Information", justify="left", width=98)
69
+ for context_strategy_index, context_strategy_data in enumerate(
70
+ context_strategy_metadata_list, 1
71
+ ):
72
+ context_strategy_data_str = ""
73
+ for k, v in context_strategy_data.items():
74
+ context_strategy_data_str += f"[blue]{k.capitalize()}:[/blue] {v}\n\n"
75
+ table.add_row(str(context_strategy_index), context_strategy_data_str)
76
+ console.print(table)
77
+ else:
78
+ console.print("[red]There are no context strategies found.[/red]", style="bold")
79
+
80
+
81
+ def clear_context_strategy() -> None:
82
+ """
83
+ Resets the context in a session.
84
+ """
85
+ # Check if current session exists
86
+ if active_session:
87
+ try:
88
+ api_update_context_strategy(active_session["session_id"], "")
89
+ active_session["context_strategy"] = ""
90
+ print("Cleared context strategy.")
91
+ except Exception as e:
92
+ print(f"[clear_context_strategy: {str(e)}]")
93
+ else:
94
+ print(
95
+ "There is no active session. Activate a session to send a prompt with a context strategy."
96
+ )
97
+
98
+
99
+ def delete_context_strategy(args) -> None:
100
+ """
101
+ Deletes a context strategy after confirming with the user.
102
+
103
+ Args:
104
+ args (object): The arguments object. It should have a 'context_strategy' attribute
105
+ which is the ID of the context strategy to delete.
106
+ """
107
+ # Confirm with the user before deleting a context strategy
108
+ confirmation = console.input(
109
+ "[bold red]Are you sure you want to delete the context strategy (y/N)? [/]"
110
+ )
111
+ if confirmation.lower() != "y":
112
+ console.print("[bold yellow]Context strategy deletion cancelled.[/]")
113
+ return
114
+ try:
115
+ api_delete_context_strategy(args.context_strategy)
116
+ print("[delete_context_strategy]: Context strategy deleted.")
117
+ except Exception as e:
118
+ print(f"[delete_context_strategy]: {str(e)}")
119
+
120
+
121
+ # Use context strategy arguments
122
+ use_context_strategy_args = cmd2.Cmd2ArgumentParser(
123
+ description="Use a context strategy.",
124
+ epilog="Example:\n use_context_strategy my_strategy_one",
125
+ )
126
+ use_context_strategy_args.add_argument(
127
+ "context_strategy",
128
+ type=str,
129
+ help="The ID of the context strategy to use",
130
+ )
131
+ use_context_strategy_args.add_argument(
132
+ "-n",
133
+ "--num_of_prev_prompts",
134
+ type=int,
135
+ help="The number of previous prompts to use with the context strategy",
136
+ nargs="?",
137
+ )
138
+
139
+ # Delete context strategy arguments
140
+ delete_context_strategy_args = cmd2.Cmd2ArgumentParser(
141
+ description="Delete a context strategy.",
142
+ epilog="Example:\n delete_context_strategy add_previous_prompt",
143
+ )
144
+
145
+ delete_context_strategy_args.add_argument(
146
+ "context_strategy", type=str, help="The ID of the context strategy to delete"
147
+ )
@@ -0,0 +1,67 @@
1
+ import argparse
2
+
3
+ import cmd2
4
+ from rich.console import Console
5
+
6
+ from moonshot.api import api_update_prompt_template
7
+ from moonshot.integrations.cli.active_session_cfg import active_session
8
+
9
+ console = Console()
10
+
11
+
12
+ def use_prompt_template(args: argparse.Namespace) -> None:
13
+ """
14
+ Use a prompt template by specifying its name while user is in a session.
15
+
16
+ Args:
17
+ args: A namespace with the prompt template parameters. Expected to have 'prompt_template'.
18
+ """
19
+ new_prompt_template_name = args.prompt_template
20
+
21
+ # Check if current session exists
22
+ if active_session:
23
+ active_session["prompt_template"] = new_prompt_template_name
24
+ try:
25
+ api_update_prompt_template(
26
+ active_session["session_id"], new_prompt_template_name
27
+ )
28
+ print(
29
+ f"Updated session: {active_session['session_id']}. "
30
+ f"Prompt Template: {active_session['prompt_template']}."
31
+ )
32
+ except Exception as e:
33
+ print(f"[use_prompt_template]: {str(e)}")
34
+ else:
35
+ print(
36
+ "There is no active session. Activate a session to send a prompt with a prompt template."
37
+ )
38
+
39
+
40
+ def clear_prompt_template() -> None:
41
+ """
42
+ Resets the prompt template in a session.
43
+ """
44
+ # Check if current session exists
45
+ if active_session:
46
+ try:
47
+ api_update_prompt_template(active_session["session_id"], "")
48
+ active_session["prompt_template"] = ""
49
+ print("Cleared prompt template.")
50
+ except Exception as e:
51
+ print(f"[clear_prompt_template: {str(e)}]")
52
+ else:
53
+ print(
54
+ "There is no active session. Activate a session to send a prompt with a prompt template."
55
+ )
56
+
57
+
58
+ # Use prompt template arguments
59
+ use_prompt_template_args = cmd2.Cmd2ArgumentParser(
60
+ description="Use a prompt template.",
61
+ epilog="Example:\n use_prompt_template 'analogical-similarity'",
62
+ )
63
+ use_prompt_template_args.add_argument(
64
+ "prompt_template",
65
+ type=str,
66
+ help="Name of the prompt template",
67
+ )
@@ -0,0 +1,90 @@
1
+ import argparse
2
+
3
+ import cmd2
4
+
5
+ from moonshot.integrations.cli.redteam.attack_module import (
6
+ delete_attack_module,
7
+ delete_attack_module_args,
8
+ list_attack_modules,
9
+ )
10
+ from moonshot.integrations.cli.redteam.context_strategy import (
11
+ clear_context_strategy,
12
+ delete_context_strategy,
13
+ delete_context_strategy_args,
14
+ list_context_strategies,
15
+ use_context_strategy,
16
+ use_context_strategy_args,
17
+ )
18
+ from moonshot.integrations.cli.redteam.prompt_template import (
19
+ clear_prompt_template,
20
+ use_prompt_template,
21
+ use_prompt_template_args,
22
+ )
23
+ from moonshot.integrations.cli.redteam.session import (
24
+ automated_rt_session_args,
25
+ delete_session,
26
+ delete_session_args,
27
+ end_session,
28
+ list_sessions,
29
+ new_session,
30
+ new_session_args,
31
+ run_attack_module,
32
+ use_session,
33
+ use_session_args,
34
+ )
35
+
36
+
37
+ @cmd2.with_default_category("Moonshot RedTeaming")
38
+ class RedTeamCommandSet(cmd2.CommandSet):
39
+ def __init__(self):
40
+ super().__init__()
41
+
42
+ @cmd2.with_argparser(new_session_args)
43
+ def do_new_session(self, args: argparse.Namespace) -> None:
44
+ new_session(args)
45
+
46
+ def do_end_session(self, _: cmd2.Statement) -> None:
47
+ end_session()
48
+
49
+ def do_list_sessions(self, _: cmd2.Statement) -> None:
50
+ list_sessions()
51
+
52
+ @cmd2.with_argparser(use_session_args)
53
+ def do_use_session(self, args: argparse.Namespace) -> None:
54
+ use_session(args)
55
+
56
+ @cmd2.with_argparser(use_prompt_template_args)
57
+ def do_use_prompt_template(self, args: argparse.Namespace) -> None:
58
+ use_prompt_template(args)
59
+
60
+ def do_clear_prompt_template(self, _: cmd2.Statement) -> None:
61
+ clear_prompt_template()
62
+
63
+ def do_list_context_strategies(self, _: cmd2.Statement) -> None:
64
+ list_context_strategies()
65
+
66
+ @cmd2.with_argparser(use_context_strategy_args)
67
+ def do_use_context_strategy(self, args: argparse.Namespace) -> None:
68
+ use_context_strategy(args)
69
+
70
+ def do_clear_context_strategy(self, _: cmd2.Statement) -> None:
71
+ clear_context_strategy()
72
+
73
+ @cmd2.with_argparser(automated_rt_session_args)
74
+ def do_run_attack_module(self, args: argparse.Namespace) -> None:
75
+ run_attack_module(args)
76
+
77
+ def do_list_attack_modules(self, _: cmd2.Statement) -> None:
78
+ list_attack_modules()
79
+
80
+ @cmd2.with_argparser(delete_session_args)
81
+ def do_delete_session(self, args: argparse.Namespace) -> None:
82
+ delete_session(args)
83
+
84
+ @cmd2.with_argparser(delete_context_strategy_args)
85
+ def do_delete_context_strategy(self, args: argparse.Namespace) -> None:
86
+ delete_context_strategy(args)
87
+
88
+ @cmd2.with_argparser(delete_attack_module_args)
89
+ def do_delete_attack_module(self, args: argparse.Namespace) -> None:
90
+ delete_attack_module(args)