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,216 @@
1
+ import cmd2
2
+ from rich.console import Console
3
+ from rich.table import Table
4
+
5
+ from moonshot.api import api_delete_result, api_get_all_result, api_read_result
6
+ from moonshot.integrations.cli.benchmark.cookbook import show_cookbook_results
7
+ from moonshot.integrations.cli.benchmark.recipe import show_recipe_results
8
+ from moonshot.integrations.cli.common.display_helper import (
9
+ display_view_list_format,
10
+ display_view_str_format,
11
+ )
12
+
13
+ console = Console()
14
+
15
+
16
+ # ------------------------------------------------------------------------------
17
+ # CLI Functions
18
+ # ------------------------------------------------------------------------------
19
+ def list_results() -> None:
20
+ """
21
+ List all available results.
22
+
23
+ This function retrieves all available results by calling the api_get_all_result_name function from the
24
+ moonshot.api module. It then creates a table with the result id and name. If there are no results, it prints a
25
+ message indicating that no results were found.
26
+
27
+ Returns:
28
+ None
29
+ """
30
+ try:
31
+ results_list = api_get_all_result()
32
+ display_results(results_list)
33
+ except Exception as e:
34
+ print(f"[list_results]: {str(e)}")
35
+
36
+
37
+ def view_result(args) -> None:
38
+ """
39
+ View a specific result.
40
+
41
+ This function retrieves a specific result by calling the api_read_result function from the
42
+ moonshot.api module using the result filename provided in the args.
43
+ It then checks if the result filename starts with "cookbook". If it does, it displays the result using the
44
+ display_view_cookbook_result function. Otherwise, it uses the display_view_recipe_result function.
45
+
46
+ Args:
47
+ args: A namespace object from argparse. It should have the following attribute:
48
+ result_filename (str): The filename of the result to view.
49
+
50
+ Returns:
51
+ None
52
+ """
53
+ try:
54
+ result_info = api_read_result(args.result_filename)
55
+ if result_info["metadata"].get("cookbooks"):
56
+ display_view_cookbook_result(result_info)
57
+ elif result_info["metadata"].get("recipes"):
58
+ display_view_recipe_result(result_info)
59
+ else:
60
+ print("[view_result]: Unable to determine cookbook or recipe")
61
+ except Exception as e:
62
+ print(f"[view_result]: {str(e)}")
63
+
64
+
65
+ def delete_result(args) -> None:
66
+ """
67
+ Delete a result.
68
+
69
+ This function deletes a result with the specified identifier. It prompts the user for confirmation before proceeding
70
+ with the deletion. If the user confirms, it calls the api_delete_result function from the moonshot.api module to
71
+ delete the result. If the deletion is successful, it prints a confirmation message. If an exception occurs, it
72
+ prints an error message.
73
+
74
+ Args:
75
+ args: A namespace object from argparse. It should have the following attribute:
76
+ result (str): The identifier of the result to delete.
77
+
78
+ Returns:
79
+ None
80
+ """
81
+ # Confirm with the user before deleting a result
82
+ confirmation = console.input(
83
+ "[bold red]Are you sure you want to delete the result (y/N)? [/]"
84
+ )
85
+ if confirmation.lower() != "y":
86
+ console.print("[bold yellow]Result deletion cancelled.[/]")
87
+ return
88
+ try:
89
+ api_delete_result(args.result)
90
+ print("[delete_result]: Result deleted.")
91
+ except Exception as e:
92
+ print(f"[delete_result]: {str(e)}")
93
+
94
+
95
+ # ------------------------------------------------------------------------------
96
+ # Helper functions: Display on cli
97
+ # ------------------------------------------------------------------------------
98
+ def display_results(results_list):
99
+ """
100
+ Display a list of results.
101
+
102
+ This function takes a list of results and displays them in a table format. If the list is empty, it prints a
103
+ message indicating that no results were found.
104
+
105
+ Args:
106
+ results_list (list): A list of results. Each result is a dictionary with keys 'id' and 'name'.
107
+
108
+ Returns:
109
+ None
110
+ """
111
+ if results_list:
112
+ table = Table(
113
+ title="List of Results", show_lines=True, expand=True, header_style="bold"
114
+ )
115
+ table.add_column("No.", width=2)
116
+ table.add_column("Result", justify="left", width=78)
117
+ table.add_column("Contains", justify="left", width=20, overflow="fold")
118
+ for result_id, result in enumerate(results_list, 1):
119
+ metadata, results = result.values()
120
+
121
+ id = metadata["id"]
122
+ start_time = metadata["start_time"]
123
+ end_time = metadata["end_time"]
124
+ duration = metadata["duration"]
125
+ status = metadata["status"]
126
+ recipes = metadata["recipes"]
127
+ cookbooks = metadata["cookbooks"]
128
+ endpoints = metadata["endpoints"]
129
+ num_of_prompts = metadata["num_of_prompts"]
130
+ random_seed = metadata["random_seed"]
131
+ system_prompt = metadata["system_prompt"]
132
+
133
+ duration_info = (
134
+ f"[blue]Period:[/blue] {start_time} - {end_time} ({duration}s)"
135
+ )
136
+ status_info = display_view_str_format("Status", status)
137
+ recipes_info = display_view_list_format("Recipes", recipes)
138
+ cookbooks_info = display_view_list_format("Cookbooks", cookbooks)
139
+ endpoints_info = display_view_list_format("Endpoints", endpoints)
140
+ prompts_info = display_view_str_format("Number of Prompts", num_of_prompts)
141
+ seed_info = display_view_str_format("Seed", random_seed)
142
+ system_prompt_info = display_view_str_format("System Prompt", system_prompt)
143
+
144
+ result_info = f"[red]id: {id}[/red]\n\n{duration_info}\n\n{status_info}"
145
+ contains_info = (
146
+ f"{recipes_info}\n\n{cookbooks_info}\n\n{endpoints_info}\n\n{prompts_info}"
147
+ f"\n\n{seed_info}\n\n{system_prompt_info}"
148
+ )
149
+
150
+ table.add_section()
151
+ table.add_row(str(result_id), result_info, contains_info)
152
+ console.print(table)
153
+ else:
154
+ console.print("[red]There are no results found.[/red]")
155
+
156
+
157
+ def display_view_recipe_result(result_info):
158
+ """
159
+ Display the recipe result.
160
+
161
+ This function takes the result file and result info as arguments. It converts the result info into a dictionary
162
+ using the convert_string_tuples_in_dict function. It then retrieves the recipes, endpoints, and duration from the
163
+ converted result info. Finally, it calls the show_recipe_results function from the
164
+ moonshot.integrations.cli.benchmark.recipe module to display the recipe results.
165
+
166
+ Args:
167
+ result_info (dict): The result info.
168
+
169
+ Returns:
170
+ None
171
+ """
172
+ recipes = result_info["metadata"]["recipes"]
173
+ endpoints = result_info["metadata"]["endpoints"]
174
+ duration = result_info["metadata"]["duration"]
175
+ show_recipe_results(recipes, endpoints, result_info, duration)
176
+
177
+
178
+ def display_view_cookbook_result(result_info):
179
+ """
180
+ Display the cookbook result.
181
+
182
+ This function takes the result file and result info as arguments. It converts the result info into a dictionary
183
+ using the convert_string_tuples_in_dict function. It then retrieves the cookbooks, endpoints, and duration from the
184
+ converted result info. Finally, it calls the show_cookbook_results function from the
185
+ moonshot.integrations.cli.benchmark.cookbook module to display the cookbook results.
186
+
187
+ Args:
188
+ result_info (dict): The result info.
189
+
190
+ Returns:
191
+ None
192
+ """
193
+ cookbooks = result_info["metadata"]["cookbooks"]
194
+ endpoints = result_info["metadata"]["endpoints"]
195
+ duration = result_info["metadata"]["duration"]
196
+ show_cookbook_results(cookbooks, endpoints, result_info, duration)
197
+
198
+
199
+ # ------------------------------------------------------------------------------
200
+ # Cmd2 Arguments Parsers
201
+ # ------------------------------------------------------------------------------
202
+ # View result arguments
203
+ view_result_args = cmd2.Cmd2ArgumentParser(
204
+ description="View a result file.",
205
+ epilog="Example:\n view_result my-new-cookbook-runner",
206
+ )
207
+ view_result_args.add_argument(
208
+ "result_filename", type=str, help="Name of the result file"
209
+ )
210
+
211
+ # Delete result arguments
212
+ delete_result_args = cmd2.Cmd2ArgumentParser(
213
+ description="Delete a result.",
214
+ epilog="Example:\n delete_result my-new-cookbook-runner",
215
+ )
216
+ delete_result_args.add_argument("result", type=str, help="Name of the result")
@@ -0,0 +1,140 @@
1
+ import cmd2
2
+ from rich.console import Console
3
+ from rich.table import Table
4
+
5
+ from moonshot.api import api_get_all_run
6
+ from moonshot.integrations.cli.common.display_helper import (
7
+ display_view_list_format,
8
+ display_view_str_format,
9
+ )
10
+
11
+ console = Console()
12
+
13
+
14
+ # ------------------------------------------------------------------------------
15
+ # CLI Functions
16
+ # ------------------------------------------------------------------------------
17
+ def list_runs() -> None:
18
+ """
19
+ List all runs.
20
+
21
+ This function retrieves all available runs by calling the api_get_all_run function from the
22
+ moonshot.api module. It then calls the display_runs function to present the retrieved run information
23
+ in a user-friendly format on the command line interface. If an exception occurs during the retrieval
24
+ or display process, it prints an error message.
25
+
26
+ Returns:
27
+ None
28
+ """
29
+ try:
30
+ runner_run_info = api_get_all_run()
31
+ display_runs(runner_run_info)
32
+ except Exception as e:
33
+ print(f"[list_runs]: {str(e)}")
34
+
35
+
36
+ def view_run(args) -> None:
37
+ """
38
+ View the details of a specific run.
39
+
40
+ This function retrieves and displays information about a specific run associated with a runner. It uses the runner
41
+ identifier provided in the arguments to fetch the data and then calls the display_runs function to present it in a
42
+ user-friendly format.
43
+
44
+ Args:
45
+ args: A namespace object from argparse. It should have the following attribute:
46
+ runner (str): The identifier of the runner whose runs are to be viewed.
47
+
48
+ Returns:
49
+ None
50
+ """
51
+ try:
52
+ runner_run_info = api_get_all_run(args.runner_id)
53
+ display_runs(runner_run_info)
54
+ except Exception as e:
55
+ print(f"[view_run]: {str(e)}")
56
+
57
+
58
+ # ------------------------------------------------------------------------------
59
+ # Helper functions: Display on cli
60
+ # ------------------------------------------------------------------------------
61
+ def display_runs(runs_list: list):
62
+ """
63
+ Display a list of runs in a table format.
64
+
65
+ This function takes a list of run information and displays it in a table format using the rich library's
66
+ Table object.
67
+
68
+ Each run's details are formatted and added as a row in the table.
69
+ If there are no runs to display, a message is printed to indicate that no results were found.
70
+
71
+ Args:
72
+ runs_list (list): A list of dictionaries, where each dictionary contains details of a run.
73
+
74
+ Returns:
75
+ None
76
+ """
77
+ if runs_list:
78
+ table = Table(
79
+ title="List of Runs", show_lines=True, expand=True, header_style="bold"
80
+ )
81
+ table.add_column("No.", width=2)
82
+ table.add_column("Run", justify="left", width=78)
83
+ table.add_column("Contains", justify="left", width=20, overflow="fold")
84
+ for run_number, run in enumerate(runs_list, 1):
85
+ (
86
+ run_id,
87
+ runner_id,
88
+ runner_type,
89
+ runner_args,
90
+ endpoints,
91
+ results_file,
92
+ start_time,
93
+ end_time,
94
+ duration,
95
+ error_messages,
96
+ raw_results,
97
+ results,
98
+ status,
99
+ ) = run.values()
100
+
101
+ duration_info = (
102
+ f"[blue]Period:[/blue] {start_time} - {end_time} ({duration}s)"
103
+ )
104
+ run_id = display_view_str_format("Run ID", run_id)
105
+ runner_id = display_view_str_format("Runner ID", runner_id)
106
+ runner_type = display_view_str_format("Runner Type", runner_type)
107
+ runner_args = display_view_str_format("Runner Args", runner_args)
108
+ status_info = display_view_str_format("Status", status)
109
+ results_info = display_view_str_format("Results File", results_file)
110
+ endpoints_info = display_view_list_format("Endpoints", endpoints)
111
+ error_messages_info = display_view_list_format(
112
+ "Error Messages", error_messages
113
+ )
114
+
115
+ has_raw_results = bool(raw_results)
116
+ has_results = bool(results)
117
+
118
+ result_info = f"[red]{runner_id}[/red]\n\n{run_id}\n\n{duration_info}\n\n{status_info}"
119
+ contains_info = (
120
+ f"{results_info}\n\n{error_messages_info}\n\n{endpoints_info}\n\n"
121
+ f"[blue]Has Raw Results: {has_raw_results}[/blue]\n\n"
122
+ f"[blue]Has Results: {has_results}[/blue]"
123
+ )
124
+
125
+ table.add_section()
126
+ table.add_row(str(run_number), result_info, contains_info)
127
+ console.print(table)
128
+ else:
129
+ console.print("[red]There are no results found.[/red]")
130
+
131
+
132
+ # ------------------------------------------------------------------------------
133
+ # Cmd2 Arguments Parsers
134
+ # ------------------------------------------------------------------------------
135
+ # View run arguments
136
+ view_run_args = cmd2.Cmd2ArgumentParser(
137
+ description="View a runner runs.",
138
+ epilog="Example:\n view_run my-new-cookbook-runner",
139
+ )
140
+ view_run_args.add_argument("runner_id", type=str, help="Name of the runner")
@@ -0,0 +1,174 @@
1
+ import cmd2
2
+ from rich.console import Console
3
+ from rich.table import Table
4
+
5
+ from moonshot.api import (
6
+ api_delete_runner,
7
+ api_get_all_run,
8
+ api_get_all_runner,
9
+ api_get_available_session_info,
10
+ api_load_session,
11
+ api_read_runner,
12
+ )
13
+ from moonshot.integrations.cli.common.display_helper import (
14
+ display_view_list_format,
15
+ display_view_str_format,
16
+ )
17
+
18
+ console = Console()
19
+
20
+
21
+ # ------------------------------------------------------------------------------
22
+ # CLI Functions
23
+ # ------------------------------------------------------------------------------
24
+ def list_runners() -> None:
25
+ """
26
+ List all runners.
27
+
28
+ This function retrieves and displays information about all runners, including their associated runs and session
29
+ information. It fetches the data using the api_get_all_runner, api_get_all_run, and api_get_available_session_info
30
+ functions, then calls the display_runners function to present it in a user-friendly format.
31
+
32
+ Returns:
33
+ None
34
+ """
35
+ try:
36
+ runner_info = api_get_all_runner()
37
+ runner_run_info = api_get_all_run()
38
+ _, runner_session_info = api_get_available_session_info()
39
+ display_runners(runner_info, runner_run_info, runner_session_info)
40
+ except Exception as e:
41
+ print(f"[list_runners]: {str(e)}")
42
+
43
+
44
+ def view_runner(args) -> None:
45
+ """
46
+ View a specific runner.
47
+
48
+ This function retrieves and displays information about a specific runner, including its associated runs and session
49
+ information. It uses the runner identifier provided in the arguments to fetch the data and then calls the
50
+ display_runners function to present it in a user-friendly format.
51
+
52
+ Args:
53
+ args: A namespace object from argparse. It should have the following attribute:
54
+ runner (str): The identifier of the runner to view.
55
+
56
+ Returns:
57
+ None
58
+ """
59
+ try:
60
+ runner_info = api_read_runner(args.runner)
61
+ runner_run_info = api_get_all_run(args.runner)
62
+ runner_session_info = api_load_session(args.runner)
63
+ display_runners([runner_info], runner_run_info, [runner_session_info])
64
+ except Exception as e:
65
+ print(f"[view_runner]: {str(e)}")
66
+
67
+
68
+ def delete_runner(args) -> None:
69
+ """
70
+ Delete a runner.
71
+
72
+ This function deletes a runner with the specified identifier. It prompts the user for confirmation before proceeding
73
+ with the deletion. If the user confirms, it calls the api_delete_runner function from the moonshot.api module to
74
+ delete the runner. If the deletion is successful, it prints a confirmation message. If an exception occurs, it
75
+ prints an error message.
76
+
77
+ Args:
78
+ args: A namespace object from argparse. It should have the following attribute:
79
+ runner (str): The identifier of the runner to delete.
80
+
81
+ Returns:
82
+ None
83
+ """
84
+ # Confirm with the user before deleting a runner
85
+ confirmation = console.input(
86
+ "[bold red]Are you sure you want to delete the runner (y/N)? [/]"
87
+ )
88
+ if confirmation.lower() != "y":
89
+ console.print("[bold yellow]Runner deletion cancelled.[/]")
90
+ return
91
+ try:
92
+ api_delete_runner(args.runner)
93
+ print("[delete_runner]: Runner deleted.")
94
+ except Exception as e:
95
+ print(f"[delete_runner]: {str(e)}")
96
+
97
+
98
+ # ------------------------------------------------------------------------------
99
+ # Helper functions: Display on cli
100
+ # ------------------------------------------------------------------------------
101
+ def display_runners(
102
+ runner_list: list, runner_run_info_list: list, runner_session_info_list: list
103
+ ) -> None:
104
+ """
105
+ Display runners in a table format.
106
+
107
+ This function takes lists of runner information, run information, and session information, then displays them in a
108
+ table format on the command line interface. Each runner is listed with details such as the runner's ID, name,
109
+ description, number of runs, number of sessions, database file, and endpoints.
110
+
111
+ Args:
112
+ runner_list: A list of dictionaries, where each dictionary contains information about a runner.
113
+
114
+ runner_run_info_list: A list of dictionaries, where each dictionary contains information about a run
115
+ associated with a runner.
116
+
117
+ runner_session_info_list: A list of dictionaries, where each dictionary contains information about a session
118
+ associated with a runner.
119
+
120
+ Returns:
121
+ None
122
+ """
123
+ if runner_list:
124
+ table = Table(
125
+ title="List of Runners", show_lines=True, expand=True, header_style="bold"
126
+ )
127
+ table.add_column("No.", width=2)
128
+ table.add_column("Runner", justify="left", width=78)
129
+ table.add_column("Contains", justify="left", width=20, overflow="fold")
130
+ for runner_id, runner in enumerate(runner_list, 1):
131
+ (id, name, db_file, endpoints, description) = runner.values()
132
+
133
+ db_info = display_view_str_format("Database", db_file)
134
+ endpoints_info = display_view_list_format("Endpoints", endpoints)
135
+
136
+ runs_count = sum(
137
+ run_info["runner_id"] == id for run_info in runner_run_info_list
138
+ )
139
+ # Handle the case where session_info can be None
140
+ sessions_count = sum(
141
+ session_info is not None and session_info["session_id"] == id
142
+ for session_info in runner_session_info_list
143
+ )
144
+
145
+ runner_info = (
146
+ f"[red]id: {id}[/red]\n\n[blue]{name}[/blue]\n{description}\n"
147
+ f"[blue]Number of Runs:[/blue] {runs_count}\n"
148
+ f"[blue]Number of Sessions:[/blue] {sessions_count}"
149
+ )
150
+ contains_info = f"{db_info}\n\n{endpoints_info}"
151
+
152
+ table.add_section()
153
+ table.add_row(str(runner_id), runner_info, contains_info)
154
+ console.print(table)
155
+ else:
156
+ console.print("[red]There are no runners found.[/red]")
157
+
158
+
159
+ # ------------------------------------------------------------------------------
160
+ # Cmd2 Arguments Parsers
161
+ # ------------------------------------------------------------------------------
162
+ # View runner arguments
163
+ view_runner_args = cmd2.Cmd2ArgumentParser(
164
+ description="View a runner.",
165
+ epilog="Example:\n view_runner my-new-cookbook-runner",
166
+ )
167
+ view_runner_args.add_argument("runner", type=str, help="Name of the runner")
168
+
169
+ # Delete runner arguments
170
+ delete_runner_args = cmd2.Cmd2ArgumentParser(
171
+ description="Delete a runner.",
172
+ epilog="Example:\n delete_runner my-new-cookbook-runner",
173
+ )
174
+ delete_runner_args.add_argument("runner", type=str, help="Name of the runner")
@@ -0,0 +1,64 @@
1
+ import cmd2
2
+ from cmd2 import Statement
3
+ from rich.console import Console
4
+
5
+ from moonshot.integrations.cli.active_session_cfg import active_session
6
+ from moonshot.integrations.cli.benchmark.benchmark import BenchmarkCommandSet
7
+ from moonshot.integrations.cli.common.common import CommonCommandSet
8
+ from moonshot.integrations.cli.initialisation.initialisation import (
9
+ InitialisationCommandSet,
10
+ )
11
+ from moonshot.integrations.cli.redteam.redteam import RedTeamCommandSet
12
+ from moonshot.integrations.cli.redteam.session import (
13
+ manual_red_teaming,
14
+ update_chat_display,
15
+ )
16
+
17
+ console = Console()
18
+
19
+
20
+ class CommandLineInterface(cmd2.Cmd):
21
+ def __init__(self):
22
+ super().__init__(terminators=[])
23
+ self.prompt = "moonshot > "
24
+ self.welcome()
25
+
26
+ def welcome(self) -> None:
27
+ """
28
+ Display Project Moonshot logo
29
+ """
30
+ logo = " _____ _ _ __ __ _ _ \n"
31
+ logo += " | __ \\ (_) | | | \\/ | | | | | \n"
32
+ logo += " | |__) | __ ___ _ ___ ___| |_ | \\ / | ___ ___ _ __ ___| |__ ___ | |_ \n"
33
+ logo += " | ___/ '__/ _ \\| |/ _ \\/ __| __| | |\\/| |/ _ \\ / _ \\| '_ \\/ __| '_ \\ / _ \\| __|\n"
34
+ logo += " | | | | | (_) | | __/ (__| |_ | | | | (_) | (_) | | | \\__ \\ | | | (_) | |_ \n"
35
+ logo += " |_| |_| \\___/| |\\___|\\___|\\__| |_| |_|\\___/ \\___/|_| |_|___/_| |_|\\___/ \\__|\n"
36
+ logo += " _/ | \n"
37
+ logo += " |__/ \n"
38
+ logo += "\n"
39
+ print(logo)
40
+
41
+ def default(self, statement: Statement) -> None:
42
+ # if there is an active session, anything entered other than recognised commands
43
+ # will be considered prompts
44
+ if active_session:
45
+ user_prompt = statement.command + " " + statement
46
+ user_prompt = user_prompt.strip()
47
+ manual_red_teaming(user_prompt)
48
+ # Update chat display with response
49
+ update_chat_display()
50
+
51
+ def postcmd(self, stop, line):
52
+ if active_session:
53
+ if active_session["context_strategy"]:
54
+ cs_prompt = f"CS: ({active_session['context_strategy']},{active_session['cs_num_of_prev_prompts']})] > "
55
+ else:
56
+ cs_prompt = f"CS: {active_session['context_strategy']}]> "
57
+ self.prompt = (
58
+ f"moonshot ({active_session['session_id']}) "
59
+ f"[PT: {active_session['prompt_template']}, "
60
+ f"{cs_prompt}"
61
+ )
62
+ else:
63
+ self.prompt = "moonshot > "
64
+ return stop
File without changes
@@ -0,0 +1,72 @@
1
+ import argparse
2
+
3
+ import cmd2
4
+
5
+ from moonshot.integrations.cli.common.connectors import (
6
+ add_endpoint,
7
+ add_endpoint_args,
8
+ delete_endpoint,
9
+ delete_endpoint_args,
10
+ list_connector_types,
11
+ list_endpoints,
12
+ update_endpoint,
13
+ update_endpoint_args,
14
+ view_endpoint,
15
+ view_endpoint_args,
16
+ )
17
+ from moonshot.integrations.cli.common.prompt_template import (
18
+ delete_prompt_template,
19
+ delete_prompt_template_args,
20
+ list_prompt_templates,
21
+ )
22
+
23
+
24
+ @cmd2.with_default_category("Moonshot Common")
25
+ class CommonCommandSet(cmd2.CommandSet):
26
+ def __init__(self):
27
+ super().__init__()
28
+
29
+ # ------------------------------------------------------------------------------
30
+ # List contents
31
+ # ------------------------------------------------------------------------------
32
+
33
+ def do_list_connector_types(self, _: cmd2.Statement) -> None:
34
+ list_connector_types()
35
+
36
+ def do_list_endpoints(self, _: cmd2.Statement) -> None:
37
+ list_endpoints()
38
+
39
+ def do_list_prompt_templates(self, _: cmd2.Statement) -> None:
40
+ list_prompt_templates()
41
+
42
+ @cmd2.with_argparser(delete_prompt_template_args)
43
+ def do_delete_prompt_template(self, args: argparse.Namespace) -> None:
44
+ delete_prompt_template(args)
45
+
46
+ # ------------------------------------------------------------------------------
47
+ # Add contents
48
+ # ------------------------------------------------------------------------------
49
+ @cmd2.with_argparser(add_endpoint_args)
50
+ def do_add_endpoint(self, args: argparse.Namespace) -> None:
51
+ add_endpoint(args)
52
+
53
+ # ------------------------------------------------------------------------------
54
+ # Delete contents
55
+ # ------------------------------------------------------------------------------
56
+ @cmd2.with_argparser(delete_endpoint_args)
57
+ def do_delete_endpoint(self, args: argparse.Namespace) -> None:
58
+ delete_endpoint(args)
59
+
60
+ # ------------------------------------------------------------------------------
61
+ # Update contents
62
+ # ------------------------------------------------------------------------------
63
+ @cmd2.with_argparser(update_endpoint_args)
64
+ def do_update_endpoint(self, args: argparse.Namespace) -> None:
65
+ update_endpoint(args)
66
+
67
+ # ------------------------------------------------------------------------------
68
+ # View contents
69
+ # ------------------------------------------------------------------------------
70
+ @cmd2.with_argparser(view_endpoint_args)
71
+ def do_view_endpoint(self, args: argparse.Namespace) -> None:
72
+ view_endpoint(args)