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,290 @@
|
|
|
1
|
+
from operator import itemgetter
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
from pydantic import validate_call
|
|
5
|
+
|
|
6
|
+
from moonshot.src.api.api_runner import api_get_all_runner, api_load_runner
|
|
7
|
+
from moonshot.src.configs.env_variables import EnvVariables
|
|
8
|
+
from moonshot.src.redteaming.session.session import Session
|
|
9
|
+
from moonshot.src.runners.runner_type import RunnerType
|
|
10
|
+
from moonshot.src.storage.db_interface import DBInterface
|
|
11
|
+
from moonshot.src.storage.storage import Storage
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# ------------------------------------------------------------------------------
|
|
15
|
+
# Session and Chat APIs
|
|
16
|
+
# ------------------------------------------------------------------------------
|
|
17
|
+
@validate_call
|
|
18
|
+
def api_load_session(runner_id: str) -> dict | None:
|
|
19
|
+
"""
|
|
20
|
+
Loads the session details for a specific runner.
|
|
21
|
+
|
|
22
|
+
This function calls the `Session.load` method to retrieve the session details associated with the
|
|
23
|
+
specified runner ID.
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
runner_id (str): The unique identifier of the runner for which the session details are to be loaded.
|
|
27
|
+
|
|
28
|
+
Returns:
|
|
29
|
+
dict | None: A dictionary containing the session details if available, otherwise None.
|
|
30
|
+
"""
|
|
31
|
+
return Session.load(api_load_runner(runner_id).database_instance)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@validate_call
|
|
35
|
+
def api_create_session(
|
|
36
|
+
runner_id: str, database_instance: Any, endpoints: list[str], runner_args: dict
|
|
37
|
+
) -> Session:
|
|
38
|
+
"""
|
|
39
|
+
Creates a new session for a specific runner.
|
|
40
|
+
|
|
41
|
+
This function creates a new session by calling the `Session` constructor with the provided arguments.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
runner_id (str): The unique identifier of the runner for which the session is to be created.
|
|
45
|
+
database_instance (Any): The database instance to be used for the session.
|
|
46
|
+
endpoints (list): A list of endpoints for the session.
|
|
47
|
+
runner_args (dict): A dictionary of arguments for the runner.
|
|
48
|
+
|
|
49
|
+
Returns:
|
|
50
|
+
Session
|
|
51
|
+
"""
|
|
52
|
+
if isinstance(database_instance, DBInterface):
|
|
53
|
+
if runner_id:
|
|
54
|
+
session_instance = Session(
|
|
55
|
+
runner_id,
|
|
56
|
+
RunnerType.REDTEAM,
|
|
57
|
+
{**runner_args},
|
|
58
|
+
database_instance,
|
|
59
|
+
endpoints,
|
|
60
|
+
Storage.get_filepath(
|
|
61
|
+
EnvVariables.RESULTS.name, runner_id, "json", True
|
|
62
|
+
),
|
|
63
|
+
)
|
|
64
|
+
return session_instance
|
|
65
|
+
else:
|
|
66
|
+
raise RuntimeError(
|
|
67
|
+
"[Session] Failed to initialise Session. String should have at least 1 character."
|
|
68
|
+
)
|
|
69
|
+
else:
|
|
70
|
+
raise RuntimeError(
|
|
71
|
+
"[Session] Failed to initialise Session. No database instance provided."
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def api_get_all_session_names() -> list[str]:
|
|
76
|
+
"""
|
|
77
|
+
Retrieves a list of all session names.
|
|
78
|
+
|
|
79
|
+
This function calls the `api_get_available_session_info` method to obtain the available session information
|
|
80
|
+
and returns a list of session names.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
list[str]: A list of strings, each denoting a session name.
|
|
84
|
+
"""
|
|
85
|
+
session_names, _ = api_get_available_session_info()
|
|
86
|
+
return session_names
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def api_get_available_session_info() -> tuple[list, list]:
|
|
90
|
+
"""
|
|
91
|
+
Retrieves the IDs and database instances of runners with active sessions.
|
|
92
|
+
|
|
93
|
+
This function retrieves the IDs and database instances of runners with active sessions by querying all runners
|
|
94
|
+
and checking if each runner has an active session. It returns a tuple containing a list of runner IDs and a list
|
|
95
|
+
of corresponding session metadata for runners with active sessions.
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
tuple[list[str], list[str]]: A tuple containing a list of runner IDs and a list of corresponding session
|
|
99
|
+
metadata for runners with active sessions.
|
|
100
|
+
"""
|
|
101
|
+
runners_info = api_get_all_runner()
|
|
102
|
+
runner_instances = [
|
|
103
|
+
api_load_runner(str(runner_info.get("id"))) for runner_info in runners_info
|
|
104
|
+
]
|
|
105
|
+
|
|
106
|
+
runner_ids = []
|
|
107
|
+
session_metadata_list = []
|
|
108
|
+
|
|
109
|
+
for runner_instance in runner_instances:
|
|
110
|
+
if runner_instance.database_instance:
|
|
111
|
+
session_metadata = Session.load(runner_instance.database_instance)
|
|
112
|
+
if session_metadata is not None:
|
|
113
|
+
runner_ids.append(runner_instance.id)
|
|
114
|
+
session_metadata_list.append(session_metadata)
|
|
115
|
+
return runner_ids, session_metadata_list
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def api_get_all_session_metadata() -> list:
|
|
119
|
+
"""
|
|
120
|
+
Retrieves metadata for all sessions.
|
|
121
|
+
|
|
122
|
+
This function retrieves the metadata for all active sessions by calling the `api_get_available_session_info` method.
|
|
123
|
+
|
|
124
|
+
Returns:
|
|
125
|
+
list: A list containing the metadata for all active sessions, sorted by created datetime in descending order.
|
|
126
|
+
"""
|
|
127
|
+
_, session_metadata_list = api_get_available_session_info()
|
|
128
|
+
return sorted(
|
|
129
|
+
session_metadata_list, key=itemgetter("created_datetime"), reverse=True
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@validate_call
|
|
134
|
+
def api_update_context_strategy(runner_id: str, context_strategy: str) -> bool:
|
|
135
|
+
"""
|
|
136
|
+
Updates the context strategy for a specific runner.
|
|
137
|
+
|
|
138
|
+
This function updates the context strategy for a specific runner identified by the given runner_id. It calls the
|
|
139
|
+
`Session.update_context_strategy` method with the runner's database instance,
|
|
140
|
+
runner_id, and the new context_strategy.
|
|
141
|
+
|
|
142
|
+
Args:
|
|
143
|
+
runner_id (str): The ID of the runner for which the context strategy needs to be updated.
|
|
144
|
+
context_strategy (str): The new context strategy to be set for the runner.
|
|
145
|
+
|
|
146
|
+
Returns:
|
|
147
|
+
bool: The status on whether the context strategy is updated successfully.
|
|
148
|
+
"""
|
|
149
|
+
return Session.update_context_strategy(
|
|
150
|
+
api_load_runner(runner_id).database_instance, runner_id, context_strategy
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
@validate_call
|
|
155
|
+
def api_update_cs_num_of_prev_prompts(runner_id: str, num_of_prev_prompts: int) -> bool:
|
|
156
|
+
"""
|
|
157
|
+
Updates the number of previous prompts used in a context strategy for a specific runner.
|
|
158
|
+
|
|
159
|
+
This function updates the number of previous prompts used in a context strategy for a specific runner identified by
|
|
160
|
+
the given runner_id. It calls the `Session.update_cs_num_of_prev_prompts` method with the runner's database
|
|
161
|
+
instance, runner_id, and the new num_of_prev_prompts.
|
|
162
|
+
|
|
163
|
+
Args:
|
|
164
|
+
runner_id (str): The ID of the runner for which the number of previous prompts needs to be updated.
|
|
165
|
+
num_of_prev_prompts (int): The new number of previous prompts to be set for the runner.
|
|
166
|
+
|
|
167
|
+
Returns:
|
|
168
|
+
bool: The status on whether the number of prompts for context strategy is updated successfully.
|
|
169
|
+
"""
|
|
170
|
+
return Session.update_cs_num_of_prev_prompts(
|
|
171
|
+
api_load_runner(runner_id).database_instance, runner_id, num_of_prev_prompts
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
@validate_call
|
|
176
|
+
def api_update_prompt_template(runner_id: str, prompt_template: str) -> bool:
|
|
177
|
+
"""
|
|
178
|
+
Updates the prompt template for a specific runner.
|
|
179
|
+
|
|
180
|
+
This function updates the prompt template for a specific runner identified by the given runner_id. It calls the
|
|
181
|
+
`Session.update_prompt_template` method with the runner's database instance,
|
|
182
|
+
runner_id, and the new prompt_template.
|
|
183
|
+
|
|
184
|
+
Args:
|
|
185
|
+
runner_id (str): The ID of the runner for which the prompt template needs to be updated.
|
|
186
|
+
prompt_template (str): The new prompt template to be set for the runner.
|
|
187
|
+
|
|
188
|
+
Returns:
|
|
189
|
+
bool: The status on whether the prompt template is updated successfully.
|
|
190
|
+
"""
|
|
191
|
+
return Session.update_prompt_template(
|
|
192
|
+
api_load_runner(runner_id).database_instance, runner_id, prompt_template
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
@validate_call
|
|
197
|
+
def api_update_metric(runner_id: str, metric_id: str) -> bool:
|
|
198
|
+
"""
|
|
199
|
+
Updates the metric for a specific runner.
|
|
200
|
+
|
|
201
|
+
This function updates the metric for a specific runner identified by the given runner_id. It calls the
|
|
202
|
+
`Session.update_metric` method with the runner's database instance,
|
|
203
|
+
runner_id, and the new metric_id.
|
|
204
|
+
|
|
205
|
+
Args:
|
|
206
|
+
runner_id (str): The ID of the runner for which the metric needs to be updated.
|
|
207
|
+
metric_id (str): The new metric to be set for the runner.
|
|
208
|
+
|
|
209
|
+
Returns:
|
|
210
|
+
bool: The status on whether the metric is updated successfully.
|
|
211
|
+
"""
|
|
212
|
+
return Session.update_metric(
|
|
213
|
+
api_load_runner(runner_id).database_instance, runner_id, metric_id
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
@validate_call
|
|
218
|
+
def api_update_system_prompt(runner_id: str, system_prompt: str) -> bool:
|
|
219
|
+
"""
|
|
220
|
+
Updates the system prompt for a specific runner.
|
|
221
|
+
|
|
222
|
+
This function updates the system prompt for a specific runner identified by the given runner_id. It calls the
|
|
223
|
+
`Session.update_system_prompt` method with the runner's database instance,
|
|
224
|
+
runner_id, and the new system_prompt.
|
|
225
|
+
|
|
226
|
+
Args:
|
|
227
|
+
runner_id (str): The ID of the runner for which the system prompt needs to be updated.
|
|
228
|
+
system_prompt (str): The new system prompt to be set for the runner.
|
|
229
|
+
|
|
230
|
+
Returns:
|
|
231
|
+
bool: The status on whether the system prompt is updated successfully.
|
|
232
|
+
"""
|
|
233
|
+
return Session.update_system_prompt(
|
|
234
|
+
api_load_runner(runner_id).database_instance, runner_id, system_prompt
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
@validate_call
|
|
239
|
+
def api_update_attack_module(runner_id: str, attack_module_id: str) -> bool:
|
|
240
|
+
"""
|
|
241
|
+
Updates the attack module for a specific runner.
|
|
242
|
+
|
|
243
|
+
This function updates the attack module for a specific runner identified by the given runner_id. It calls the
|
|
244
|
+
`Session.update_attack_module` method with the runner's database instance,
|
|
245
|
+
runner_id, and the new attack_module_id.
|
|
246
|
+
|
|
247
|
+
Args:
|
|
248
|
+
runner_id (str): The ID of the runner for which the attack module needs to be updated.
|
|
249
|
+
attack_module_id (str): The new attack module to be set for the runner.
|
|
250
|
+
|
|
251
|
+
Returns:
|
|
252
|
+
bool: The status on whether the attack module is updated successfully.
|
|
253
|
+
"""
|
|
254
|
+
return Session.update_attack_module(
|
|
255
|
+
api_load_runner(runner_id).database_instance, runner_id, attack_module_id
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
@validate_call
|
|
260
|
+
def api_delete_session(runner_id: str) -> bool:
|
|
261
|
+
"""
|
|
262
|
+
Deletes the session for a specific runner.
|
|
263
|
+
|
|
264
|
+
This function deletes the session for the runner identified by the given runner_id. It calls the
|
|
265
|
+
`Session.delete` method with the runner's database instance.
|
|
266
|
+
|
|
267
|
+
Args:
|
|
268
|
+
runner_id (str): The ID of the runner for which the session needs to be deleted.
|
|
269
|
+
|
|
270
|
+
Returns:
|
|
271
|
+
bool: The status on whether the session is deleted successfully.
|
|
272
|
+
"""
|
|
273
|
+
return Session.delete(api_load_runner(runner_id).database_instance)
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
@validate_call
|
|
277
|
+
def api_get_all_chats_from_session(runner_id: str) -> dict | None:
|
|
278
|
+
"""
|
|
279
|
+
Retrieves all chat messages from a specific session.
|
|
280
|
+
|
|
281
|
+
This function retrieves all chat messages from the session associated with the specified runner ID.
|
|
282
|
+
It calls the `Session.get_session_chats` method with the runner's database instance.
|
|
283
|
+
|
|
284
|
+
Args:
|
|
285
|
+
runner_id (str): The unique identifier of the runner for which the chat messages are to be retrieved.
|
|
286
|
+
|
|
287
|
+
Returns:
|
|
288
|
+
dict | None: A dictionary containing all chat messages if available, otherwise None.
|
|
289
|
+
"""
|
|
290
|
+
return Session.get_session_chats(api_load_runner(runner_id).database_instance)
|
|
File without changes
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import importlib.resources
|
|
2
|
+
from enum import Enum
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
__app_name__ = "moonshot"
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class EnvVariables(Enum):
|
|
9
|
+
ATTACK_MODULES = "ATTACK_MODULES"
|
|
10
|
+
CONNECTORS = "CONNECTORS"
|
|
11
|
+
CONNECTORS_ENDPOINTS = "CONNECTORS_ENDPOINTS"
|
|
12
|
+
CONTEXT_STRATEGY = "CONTEXT_STRATEGY"
|
|
13
|
+
COOKBOOKS = "COOKBOOKS"
|
|
14
|
+
DATABASES = "DATABASES"
|
|
15
|
+
DATABASES_MODULES = "DATABASES_MODULES"
|
|
16
|
+
DATASETS = "DATASETS"
|
|
17
|
+
IO_MODULES = "IO_MODULES"
|
|
18
|
+
METRICS = "METRICS"
|
|
19
|
+
PROMPT_TEMPLATES = "PROMPT_TEMPLATES"
|
|
20
|
+
RECIPES = "RECIPES"
|
|
21
|
+
RESULTS = "RESULTS"
|
|
22
|
+
RESULTS_MODULES = "RESULTS_MODULES"
|
|
23
|
+
RUNNERS = "RUNNERS"
|
|
24
|
+
RUNNERS_MODULES = "RUNNERS_MODULES"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class EnvironmentVars:
|
|
28
|
+
env_vars = {}
|
|
29
|
+
|
|
30
|
+
ATTACK_MODULES = [
|
|
31
|
+
env_vars.get(EnvVariables.ATTACK_MODULES.value),
|
|
32
|
+
str(importlib.resources.files(__app_name__).joinpath("data/attack-modules")),
|
|
33
|
+
]
|
|
34
|
+
CONNECTORS = [
|
|
35
|
+
env_vars.get(EnvVariables.CONNECTORS.value),
|
|
36
|
+
str(importlib.resources.files(__app_name__).joinpath("data/connectors")),
|
|
37
|
+
]
|
|
38
|
+
CONNECTORS_ENDPOINTS = [
|
|
39
|
+
env_vars.get(EnvVariables.CONNECTORS_ENDPOINTS.value),
|
|
40
|
+
str(
|
|
41
|
+
importlib.resources.files(__app_name__).joinpath(
|
|
42
|
+
"data/connectors-endpoints"
|
|
43
|
+
)
|
|
44
|
+
),
|
|
45
|
+
]
|
|
46
|
+
CONTEXT_STRATEGY = [
|
|
47
|
+
env_vars.get(EnvVariables.CONTEXT_STRATEGY.value),
|
|
48
|
+
str(importlib.resources.files(__app_name__).joinpath("data/context-strategy")),
|
|
49
|
+
]
|
|
50
|
+
COOKBOOKS = [
|
|
51
|
+
env_vars.get(EnvVariables.COOKBOOKS.value),
|
|
52
|
+
str(importlib.resources.files(__app_name__).joinpath("data/cookbooks")),
|
|
53
|
+
]
|
|
54
|
+
DATABASES = [
|
|
55
|
+
env_vars.get(EnvVariables.DATABASES.value),
|
|
56
|
+
str(
|
|
57
|
+
importlib.resources.files(__app_name__).joinpath(
|
|
58
|
+
"data/generated-outputs/databases"
|
|
59
|
+
)
|
|
60
|
+
),
|
|
61
|
+
]
|
|
62
|
+
DATABASES_MODULES = [
|
|
63
|
+
env_vars.get(EnvVariables.DATABASES_MODULES.value),
|
|
64
|
+
str(importlib.resources.files(__app_name__).joinpath("data/databases-modules")),
|
|
65
|
+
]
|
|
66
|
+
DATASETS = [
|
|
67
|
+
env_vars.get(EnvVariables.DATASETS.value),
|
|
68
|
+
str(importlib.resources.files(__app_name__).joinpath("data/datasets")),
|
|
69
|
+
]
|
|
70
|
+
IO_MODULES = [
|
|
71
|
+
env_vars.get(EnvVariables.IO_MODULES.value),
|
|
72
|
+
str(importlib.resources.files(__app_name__).joinpath("data/io-modules")),
|
|
73
|
+
]
|
|
74
|
+
METRICS = [
|
|
75
|
+
env_vars.get(EnvVariables.METRICS.value),
|
|
76
|
+
str(importlib.resources.files(__app_name__).joinpath("data/metrics")),
|
|
77
|
+
]
|
|
78
|
+
PROMPT_TEMPLATES = [
|
|
79
|
+
env_vars.get(EnvVariables.PROMPT_TEMPLATES.value),
|
|
80
|
+
str(importlib.resources.files(__app_name__).joinpath("data/prompt-templates")),
|
|
81
|
+
]
|
|
82
|
+
RECIPES = [
|
|
83
|
+
env_vars.get(EnvVariables.RECIPES.value),
|
|
84
|
+
str(importlib.resources.files(__app_name__).joinpath("data/recipes")),
|
|
85
|
+
]
|
|
86
|
+
RESULTS = [
|
|
87
|
+
env_vars.get(EnvVariables.RESULTS.value),
|
|
88
|
+
str(
|
|
89
|
+
importlib.resources.files(__app_name__).joinpath(
|
|
90
|
+
"data/generated-outputs/results"
|
|
91
|
+
)
|
|
92
|
+
),
|
|
93
|
+
]
|
|
94
|
+
RESULTS_MODULES = [
|
|
95
|
+
env_vars.get(EnvVariables.RESULTS_MODULES.value),
|
|
96
|
+
str(importlib.resources.files(__app_name__).joinpath("data/results-modules")),
|
|
97
|
+
]
|
|
98
|
+
RUNNERS = [
|
|
99
|
+
env_vars.get(EnvVariables.RUNNERS.value),
|
|
100
|
+
str(
|
|
101
|
+
importlib.resources.files(__app_name__).joinpath(
|
|
102
|
+
"data/generated-outputs/runners"
|
|
103
|
+
)
|
|
104
|
+
),
|
|
105
|
+
]
|
|
106
|
+
RUNNERS_MODULES = [
|
|
107
|
+
env_vars.get(EnvVariables.RUNNERS_MODULES.value),
|
|
108
|
+
str(importlib.resources.files(__app_name__).joinpath("data/runners-modules")),
|
|
109
|
+
]
|
|
110
|
+
|
|
111
|
+
@staticmethod
|
|
112
|
+
def load_env(env_dict: dict | None = None) -> None:
|
|
113
|
+
"""
|
|
114
|
+
This method is used to load environment variables from a provided dictionary.
|
|
115
|
+
If the dictionary is not provided, it defaults to an empty dictionary.
|
|
116
|
+
|
|
117
|
+
The method updates the class attributes with the corresponding values from the dictionary.
|
|
118
|
+
If a key from the class attributes is not found in the dictionary, it will not be updated and a
|
|
119
|
+
message will be printed.
|
|
120
|
+
|
|
121
|
+
Args:
|
|
122
|
+
env_dict (dict | None): A dictionary containing the environment variables to be loaded.
|
|
123
|
+
If None, an empty dictionary will be used.
|
|
124
|
+
"""
|
|
125
|
+
if env_dict is None:
|
|
126
|
+
env_dict = dict()
|
|
127
|
+
|
|
128
|
+
keys = [e.value for e in EnvVariables]
|
|
129
|
+
unset_keys = []
|
|
130
|
+
for key in keys:
|
|
131
|
+
if key in env_dict:
|
|
132
|
+
given_path = Path(env_dict[key])
|
|
133
|
+
if given_path.exists():
|
|
134
|
+
EnvironmentVars.__dict__[key][0] = str(given_path)
|
|
135
|
+
else:
|
|
136
|
+
print(
|
|
137
|
+
f"Unable to set {key}. The provided path {given_path} does not exist. ",
|
|
138
|
+
"The stock set will be used.",
|
|
139
|
+
)
|
|
140
|
+
else:
|
|
141
|
+
unset_keys.append(key)
|
|
142
|
+
if unset_keys:
|
|
143
|
+
print(
|
|
144
|
+
f"Unable to retrieve the following environment variables: {unset_keys}. The stock set will be used."
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
@staticmethod
|
|
148
|
+
def get_file_path(file_type: str, file_name: str, ignore_existance: bool) -> str:
|
|
149
|
+
"""
|
|
150
|
+
This method is used to get the file path for a given file type and file name.
|
|
151
|
+
If the ignore existance flag is set to True, it returns the file path even if the file does not exist.
|
|
152
|
+
|
|
153
|
+
Args:
|
|
154
|
+
file_type (str): The type of the file (e.g., 'recipe', 'cookbook').
|
|
155
|
+
file_name (str): The name of the file.
|
|
156
|
+
ignore_existance (bool): A flag indicating whether to return the file path
|
|
157
|
+
even if the file does not exist.
|
|
158
|
+
|
|
159
|
+
Returns:
|
|
160
|
+
str: The file path of the file.
|
|
161
|
+
"""
|
|
162
|
+
for directory in EnvironmentVars.get_file_directory(file_type):
|
|
163
|
+
file_path = Path(directory) / file_name
|
|
164
|
+
if ignore_existance:
|
|
165
|
+
return str(file_path)
|
|
166
|
+
else:
|
|
167
|
+
if Path(file_path).exists():
|
|
168
|
+
return str(file_path)
|
|
169
|
+
return ""
|
|
170
|
+
|
|
171
|
+
@staticmethod
|
|
172
|
+
def get_file_directory(file_type: str) -> list[str]:
|
|
173
|
+
"""
|
|
174
|
+
This method retrieves the directory paths associated with a specified file type.
|
|
175
|
+
|
|
176
|
+
Args:
|
|
177
|
+
file_type (str): The type of file for which the directory paths are to be retrieved.
|
|
178
|
+
|
|
179
|
+
Returns:
|
|
180
|
+
list[str]: A list containing the directory paths associated with the given file type.
|
|
181
|
+
"""
|
|
182
|
+
path_from_user, path_from_resource = getattr(EnvironmentVars, file_type)
|
|
183
|
+
|
|
184
|
+
if path_from_user is not None:
|
|
185
|
+
return [path_from_user, path_from_resource]
|
|
186
|
+
else:
|
|
187
|
+
return [path_from_resource]
|
|
File without changes
|