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,609 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
from dependency_injector.wiring import Provide, inject
|
|
4
|
+
from fastapi import APIRouter, Body, Depends, HTTPException, Query
|
|
5
|
+
|
|
6
|
+
from ..container import Container
|
|
7
|
+
from ..schemas.prompt_response_model import PromptResponseModel
|
|
8
|
+
from ..schemas.session_create_dto import SessionCreateDTO
|
|
9
|
+
from ..schemas.session_prompt_dto import SessionPromptDTO
|
|
10
|
+
from ..schemas.session_response_model import SessionMetadataModel, SessionResponseModel
|
|
11
|
+
from ..services.session_service import SessionService
|
|
12
|
+
from ..services.utils.exceptions_handler import ServiceException
|
|
13
|
+
|
|
14
|
+
router = APIRouter(tags=["Red Teaming"])
|
|
15
|
+
logger = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@router.get("/")
|
|
19
|
+
@inject
|
|
20
|
+
async def healthcheck() -> dict[str, str]:
|
|
21
|
+
"""
|
|
22
|
+
Check the health of the web API.
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
Dict[str, str]: The status message indicating the API is running.
|
|
26
|
+
"""
|
|
27
|
+
return {"status": "web api is up and running"}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@router.get("/api/v1/sessions")
|
|
31
|
+
@inject
|
|
32
|
+
async def get_all_sessions(
|
|
33
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
34
|
+
) -> list[SessionMetadataModel]:
|
|
35
|
+
"""
|
|
36
|
+
Fetches a list of all session metadata from the service layer.
|
|
37
|
+
|
|
38
|
+
This endpoint does not require any parameters and will return a list of session metadata objects.
|
|
39
|
+
Each object contains details about a specific session without including the session history.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
List[SessionMetadataModel]: A list of session metadata objects.
|
|
43
|
+
|
|
44
|
+
Raises:
|
|
45
|
+
HTTPException: 404 error if no sessions are found.
|
|
46
|
+
400 error if there is a validation issue with the request.
|
|
47
|
+
500 error for any other server-side issues.
|
|
48
|
+
"""
|
|
49
|
+
try:
|
|
50
|
+
return session_service.get_all_session()
|
|
51
|
+
|
|
52
|
+
except ServiceException as e:
|
|
53
|
+
if e.error_code == "FileNotFound":
|
|
54
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
55
|
+
elif e.error_code == "ValidationError":
|
|
56
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
57
|
+
else:
|
|
58
|
+
raise HTTPException(status_code=500, detail=e.msg)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@router.get("/api/v1/sessions/name")
|
|
62
|
+
@inject
|
|
63
|
+
async def get_all_sessions_name(
|
|
64
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
65
|
+
) -> list[str]:
|
|
66
|
+
"""
|
|
67
|
+
Retrieve the names of all sessions.
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
session_service (SessionService): The service responsible for session operations.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
List[str]: A list of session names.
|
|
74
|
+
|
|
75
|
+
Raises:
|
|
76
|
+
HTTPException: An error with status code 404 if no session names are found.
|
|
77
|
+
An error with status code 400 if there is a validation error.
|
|
78
|
+
An error with status code 500 for any other server-side error.
|
|
79
|
+
"""
|
|
80
|
+
try:
|
|
81
|
+
return session_service.get_all_sessions_names()
|
|
82
|
+
except ServiceException as e:
|
|
83
|
+
if e.error_code == "FileNotFound":
|
|
84
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
85
|
+
elif e.error_code == "ValidationError":
|
|
86
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
87
|
+
else:
|
|
88
|
+
raise HTTPException(status_code=500, detail=e.msg)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
@router.get("/api/v1/sessions/{runner_id}")
|
|
92
|
+
@inject
|
|
93
|
+
async def get_session_by_runner_id(
|
|
94
|
+
runner_id: str,
|
|
95
|
+
include_history: bool = Query(
|
|
96
|
+
default=False,
|
|
97
|
+
description="Flag to determine if session history should be included.",
|
|
98
|
+
),
|
|
99
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
100
|
+
) -> SessionResponseModel:
|
|
101
|
+
"""
|
|
102
|
+
Retrieve session data for a given runner ID, optionally including chat history.
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
runner_id (str): The unique identifier for the runner.
|
|
106
|
+
include_history (bool): A flag to determine if the session history should be included in the response.
|
|
107
|
+
session_service (SessionService): The service responsible for session operations.
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
SessionResponseModel: The session data, including metadata and optionally chat records.
|
|
111
|
+
|
|
112
|
+
Raises:
|
|
113
|
+
HTTPException: An error with status code 404 if no session is found for the runner ID.
|
|
114
|
+
An error with status code 400 if there is a validation error.
|
|
115
|
+
An error with status code 500 for any other server-side error.
|
|
116
|
+
"""
|
|
117
|
+
try:
|
|
118
|
+
session_data = session_service.get_session_by_runner_id(
|
|
119
|
+
runner_id, include_history
|
|
120
|
+
)
|
|
121
|
+
return session_data
|
|
122
|
+
except ServiceException as e:
|
|
123
|
+
if e.error_code == "FileNotFound":
|
|
124
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
125
|
+
elif e.error_code == "ValidationError":
|
|
126
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
127
|
+
else:
|
|
128
|
+
raise HTTPException(status_code=500, detail=e.msg)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
@router.post("/api/v1/sessions")
|
|
132
|
+
@inject
|
|
133
|
+
async def create_session(
|
|
134
|
+
session_dto: SessionCreateDTO,
|
|
135
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
136
|
+
) -> SessionResponseModel:
|
|
137
|
+
"""
|
|
138
|
+
Create a new session based on the provided session data transfer object (DTO).
|
|
139
|
+
|
|
140
|
+
Args:
|
|
141
|
+
session_dto (SessionCreateDTO): The DTO containing the data needed to create a session.
|
|
142
|
+
session_service (SessionService): The service responsible for session operations.
|
|
143
|
+
|
|
144
|
+
Returns:
|
|
145
|
+
SessionResponseModel: The metadata of the newly created session.
|
|
146
|
+
|
|
147
|
+
Raises:
|
|
148
|
+
HTTPException: An error with status code 404 if the session cannot be created due to a file not found.
|
|
149
|
+
An error with status code 400 if there is a validation error.
|
|
150
|
+
An error with status code 500 for any other server-side error.
|
|
151
|
+
"""
|
|
152
|
+
try:
|
|
153
|
+
new_session = session_service.create_new_session(session_dto)
|
|
154
|
+
return new_session
|
|
155
|
+
except ServiceException as e:
|
|
156
|
+
if e.error_code == "FileNotFound":
|
|
157
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
158
|
+
elif e.error_code == "ValidationError":
|
|
159
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
160
|
+
else:
|
|
161
|
+
raise HTTPException(status_code=500, detail=e.msg)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
@router.post("/api/v1/sessions/{runner_id}/prompt")
|
|
165
|
+
@inject
|
|
166
|
+
async def prompt(
|
|
167
|
+
runner_id: str,
|
|
168
|
+
user_prompt: SessionPromptDTO,
|
|
169
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
170
|
+
) -> PromptResponseModel | str:
|
|
171
|
+
"""
|
|
172
|
+
Process a user prompt for a given session and return the session's response.
|
|
173
|
+
|
|
174
|
+
This endpoint receives a prompt from the user and sends it to the specified session.
|
|
175
|
+
The session's response to the prompt is then returned to the user.
|
|
176
|
+
|
|
177
|
+
Args:
|
|
178
|
+
runner_id (str): The unique identifier for the session to which the prompt will be sent.
|
|
179
|
+
user_prompt (SessionPromptDTO): The data transfer object containing the prompt information from the user.
|
|
180
|
+
session_service (SessionService): The service responsible for session management and prompt handling.
|
|
181
|
+
|
|
182
|
+
Returns:
|
|
183
|
+
PromptResponseModel: A model representing the response to the user's prompt
|
|
184
|
+
,including any chat records generated.
|
|
185
|
+
|
|
186
|
+
Raises:
|
|
187
|
+
HTTPException: Raised with status code 404 if the session associated with the runner_id is not found.
|
|
188
|
+
Raised with status code 400 if there is a validation error with the provided prompt data.
|
|
189
|
+
Raised with status code 500 for any other server-side errors encountered while processing.
|
|
190
|
+
"""
|
|
191
|
+
try:
|
|
192
|
+
result = await session_service.send_prompt(runner_id, user_prompt)
|
|
193
|
+
return result
|
|
194
|
+
except ServiceException as e:
|
|
195
|
+
if e.error_code == "FileNotFound":
|
|
196
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
197
|
+
elif e.error_code == "ValidationError":
|
|
198
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
199
|
+
else:
|
|
200
|
+
raise HTTPException(status_code=500, detail=e.msg)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
@router.post("/api/v1/sessions/{runner_id}/cancel")
|
|
204
|
+
@inject
|
|
205
|
+
async def cancel_auto_redteam(
|
|
206
|
+
runner_id: str,
|
|
207
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
208
|
+
):
|
|
209
|
+
"""
|
|
210
|
+
Cancel the automated red team operation for a given session.
|
|
211
|
+
|
|
212
|
+
This endpoint is used to stop any ongoing automated red team operations for the session
|
|
213
|
+
associated with the provided runner_id.
|
|
214
|
+
|
|
215
|
+
Args:
|
|
216
|
+
runner_id (str): The unique identifier for the session whose automated red team operation is to be canceled.
|
|
217
|
+
session_service (SessionService): The service responsible for managing red team sessions.
|
|
218
|
+
|
|
219
|
+
Raises:
|
|
220
|
+
HTTPException: Raised with status code 404 if the session associated with the runner_id is not found.
|
|
221
|
+
Raised with status code 400 if there is a validation error with the runner_id.
|
|
222
|
+
Raised with status code 500 for any other server-side errors encountered while processing.
|
|
223
|
+
"""
|
|
224
|
+
try:
|
|
225
|
+
await session_service.cancel_auto_redteam(runner_id)
|
|
226
|
+
except ServiceException as e:
|
|
227
|
+
if e.error_code == "FileNotFound":
|
|
228
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
229
|
+
elif e.error_code == "ValidationError":
|
|
230
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
231
|
+
else:
|
|
232
|
+
raise HTTPException(status_code=500, detail=e.msg)
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
@router.delete("/api/v1/sessions/{session_id}")
|
|
236
|
+
@inject
|
|
237
|
+
async def delete_session(
|
|
238
|
+
session_id: str,
|
|
239
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
240
|
+
) -> dict[str, bool]:
|
|
241
|
+
"""
|
|
242
|
+
Delete a session by its ID.
|
|
243
|
+
|
|
244
|
+
Args:
|
|
245
|
+
session_id (str): The unique identifier of the session to delete.
|
|
246
|
+
session_service (SessionService): The service responsible for deleting sessions.
|
|
247
|
+
|
|
248
|
+
Returns:
|
|
249
|
+
Dict[str, bool]: A dictionary with a key 'success' indicating the operation result.
|
|
250
|
+
|
|
251
|
+
Raises:
|
|
252
|
+
HTTPException: An error with status code 404 if the session is not found.
|
|
253
|
+
An error with status code 400 if there is a validation error.
|
|
254
|
+
An error with status code 500 for any other server-side error.
|
|
255
|
+
"""
|
|
256
|
+
try:
|
|
257
|
+
session_service.delete_session(session_id)
|
|
258
|
+
return {"success": True}
|
|
259
|
+
except ServiceException as e:
|
|
260
|
+
if e.error_code == "FileNotFound":
|
|
261
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
262
|
+
elif e.error_code == "ValidationError":
|
|
263
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
264
|
+
else:
|
|
265
|
+
raise HTTPException(status_code=500, detail=e.msg)
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
@router.put("/api/v1/sessions/{runner_id}/prompt-template/{prompt_template_name}")
|
|
269
|
+
@inject
|
|
270
|
+
async def set_prompt_template(
|
|
271
|
+
runner_id: str,
|
|
272
|
+
prompt_template_name: str,
|
|
273
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
274
|
+
) -> dict[str, bool]:
|
|
275
|
+
"""
|
|
276
|
+
Select a prompt template for the current session.
|
|
277
|
+
|
|
278
|
+
Args:
|
|
279
|
+
runner_id (str): The unique identifier of the session.
|
|
280
|
+
prompt_template_name (str): The name of the prompt template to select.
|
|
281
|
+
session_service (SessionService): The service responsible for managing session prompt templates.
|
|
282
|
+
|
|
283
|
+
Returns:
|
|
284
|
+
Dict[str, bool]: A dictionary with a key 'success' indicating the operation result.
|
|
285
|
+
|
|
286
|
+
Raises:
|
|
287
|
+
HTTPException: An error with status code 404 if the prompt template is not found.
|
|
288
|
+
An error with status code 400 if there is a validation error.
|
|
289
|
+
An error with status code 500 for any other server-side error.
|
|
290
|
+
"""
|
|
291
|
+
try:
|
|
292
|
+
session_service.select_prompt_template(runner_id, prompt_template_name)
|
|
293
|
+
return {"success": True}
|
|
294
|
+
except ServiceException as e:
|
|
295
|
+
if e.error_code == "FileNotFound":
|
|
296
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
297
|
+
elif e.error_code == "ValidationError":
|
|
298
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
299
|
+
else:
|
|
300
|
+
raise HTTPException(status_code=500, detail=e.msg)
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
@router.delete("/api/v1/sessions/{runner_id}/prompt-template/{prompt_template_name}")
|
|
304
|
+
@inject
|
|
305
|
+
async def unset_prompt_template(
|
|
306
|
+
runner_id: str,
|
|
307
|
+
prompt_template_name: str = "",
|
|
308
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
309
|
+
) -> dict[str, bool]:
|
|
310
|
+
"""
|
|
311
|
+
Remove prompt template from the current session.
|
|
312
|
+
|
|
313
|
+
Args:
|
|
314
|
+
runner_id (str): The unique identifier of the session.
|
|
315
|
+
prompt_template_name (str): The name of the prompt template to remove.
|
|
316
|
+
session_service (SessionService): The service responsible for managing session prompt templates.
|
|
317
|
+
|
|
318
|
+
Returns:
|
|
319
|
+
Dict[str, bool]: A dictionary with a key 'success' indicating the operation result.
|
|
320
|
+
|
|
321
|
+
Raises:
|
|
322
|
+
HTTPException: An error with status code 404 if the prompt template is not found.
|
|
323
|
+
An error with status code 400 if there is a validation error.
|
|
324
|
+
An error with status code 500 for any other server-side error.
|
|
325
|
+
"""
|
|
326
|
+
try:
|
|
327
|
+
session_service.select_prompt_template(runner_id, "")
|
|
328
|
+
return {"success": True}
|
|
329
|
+
except ServiceException as e:
|
|
330
|
+
if e.error_code == "FileNotFound":
|
|
331
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
332
|
+
elif e.error_code == "ValidationError":
|
|
333
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
334
|
+
else:
|
|
335
|
+
raise HTTPException(status_code=500, detail=e.msg)
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
@router.put(
|
|
339
|
+
"/api/v1/sessions/{runner_id}/context-strategy/{ctx_strategy_name}/{num_of_prompt}"
|
|
340
|
+
)
|
|
341
|
+
@inject
|
|
342
|
+
async def set_context_strategy(
|
|
343
|
+
runner_id: str,
|
|
344
|
+
ctx_strategy_name: str,
|
|
345
|
+
num_of_prompt: int,
|
|
346
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
347
|
+
) -> dict[str, bool]:
|
|
348
|
+
"""
|
|
349
|
+
Set a context strategy for the current session.
|
|
350
|
+
|
|
351
|
+
Args:
|
|
352
|
+
runner_id (str): The unique identifier of the session.
|
|
353
|
+
ctx_strategy_name (str): The name of the context strategy to set.
|
|
354
|
+
num_of_prompt (int): The number of prompts to apply the context strategy to.
|
|
355
|
+
session_service (SessionService): The service responsible for managing session context strategies.
|
|
356
|
+
|
|
357
|
+
Returns:
|
|
358
|
+
Dict[str, bool]: A dictionary with a key 'success' indicating the operation result.
|
|
359
|
+
|
|
360
|
+
Raises:
|
|
361
|
+
HTTPException: An error with status code 404 if the context strategy is not found.
|
|
362
|
+
An error with status code 400 if there is a validation error.
|
|
363
|
+
An error with status code 500 for any other server-side error.
|
|
364
|
+
"""
|
|
365
|
+
try:
|
|
366
|
+
session_service.select_ctx_strategy(runner_id, ctx_strategy_name, num_of_prompt)
|
|
367
|
+
return {"success": True}
|
|
368
|
+
except ServiceException as e:
|
|
369
|
+
if e.error_code == "FileNotFound":
|
|
370
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
371
|
+
elif e.error_code == "ValidationError":
|
|
372
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
373
|
+
else:
|
|
374
|
+
raise HTTPException(status_code=500, detail=e.msg)
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
@router.delete(
|
|
378
|
+
"/api/v1/sessions/{runner_id}/context-strategy/{ctx_strategy_name}/{num_of_prompt}"
|
|
379
|
+
)
|
|
380
|
+
@inject
|
|
381
|
+
async def unset_context_strategy(
|
|
382
|
+
runner_id: str,
|
|
383
|
+
ctx_strategy_name: str = "",
|
|
384
|
+
num_of_prompt: int = 0,
|
|
385
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
386
|
+
) -> dict[str, bool]:
|
|
387
|
+
"""
|
|
388
|
+
Remove a context strategy from the current session.
|
|
389
|
+
|
|
390
|
+
Args:
|
|
391
|
+
runner_id (str): The unique identifier of the session.
|
|
392
|
+
ctx_strategy_name (str): The name of the context strategy to remove.
|
|
393
|
+
num_of_prompt (int): The number of prompts to apply the context strategy to, defaults to 0.
|
|
394
|
+
session_service (SessionService): The service responsible for managing session context strategies.
|
|
395
|
+
|
|
396
|
+
Returns:
|
|
397
|
+
Dict[str, bool]: A dictionary with a key 'success' indicating the operation result.
|
|
398
|
+
|
|
399
|
+
Raises:
|
|
400
|
+
HTTPException: An error with status code 404 if the context strategy is not found.
|
|
401
|
+
An error with status code 400 if there is a validation error.
|
|
402
|
+
An error with status code 500 for any other server-side error.
|
|
403
|
+
"""
|
|
404
|
+
try:
|
|
405
|
+
session_service.select_ctx_strategy(runner_id, "", 0)
|
|
406
|
+
return {"success": True}
|
|
407
|
+
except ServiceException as e:
|
|
408
|
+
if e.error_code == "FileNotFound":
|
|
409
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
410
|
+
elif e.error_code == "ValidationError":
|
|
411
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
412
|
+
else:
|
|
413
|
+
raise HTTPException(status_code=500, detail=e.msg)
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
@router.put("/api/v1/sessions/{runner_id}/attack-module/{atk_module_name}")
|
|
417
|
+
@inject
|
|
418
|
+
async def set_attack_module(
|
|
419
|
+
runner_id: str,
|
|
420
|
+
atk_module_name: str,
|
|
421
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
422
|
+
) -> dict[str, bool]:
|
|
423
|
+
"""
|
|
424
|
+
Set an attack module for the current session.
|
|
425
|
+
|
|
426
|
+
Args:
|
|
427
|
+
runner_id (str): The unique identifier of the session.
|
|
428
|
+
atk_module_name (str): The name of the attack module to set.
|
|
429
|
+
session_service (SessionService): The service responsible for managing session attack modules.
|
|
430
|
+
|
|
431
|
+
Returns:
|
|
432
|
+
Dict[str, bool]: A dictionary with a key 'success' indicating the operation result.
|
|
433
|
+
|
|
434
|
+
Raises:
|
|
435
|
+
HTTPException: An error with status code 404 if the attack module is not found.
|
|
436
|
+
An error with status code 400 if there is a validation error.
|
|
437
|
+
An error with status code 500 for any other server-side error.
|
|
438
|
+
"""
|
|
439
|
+
try:
|
|
440
|
+
session_service.select_attack_module(runner_id, atk_module_name)
|
|
441
|
+
return {"success": True}
|
|
442
|
+
except ServiceException as e:
|
|
443
|
+
if e.error_code == "FileNotFound":
|
|
444
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
445
|
+
elif e.error_code == "ValidationError":
|
|
446
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
447
|
+
else:
|
|
448
|
+
raise HTTPException(status_code=500, detail=e.msg)
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
@router.delete("/api/v1/sessions/{runner_id}/attack-module/{atk_module_name}")
|
|
452
|
+
@inject
|
|
453
|
+
async def unset_attack_module(
|
|
454
|
+
runner_id: str,
|
|
455
|
+
atk_module_name: str = "",
|
|
456
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
457
|
+
) -> dict[str, bool]:
|
|
458
|
+
"""
|
|
459
|
+
Remove an attack module from the current session.
|
|
460
|
+
|
|
461
|
+
Args:
|
|
462
|
+
runner_id (str): The unique identifier of the session.
|
|
463
|
+
atk_module_name (str): The name of the attack module to remove.
|
|
464
|
+
session_service (SessionService): The service responsible for managing session attack modules.
|
|
465
|
+
|
|
466
|
+
Returns:
|
|
467
|
+
Dict[str, bool]: A dictionary with a key 'success' indicating the operation result.
|
|
468
|
+
|
|
469
|
+
Raises:
|
|
470
|
+
HTTPException: An error with status code 404 if the attack module is not found.
|
|
471
|
+
An error with status code 400 if there is a validation error.
|
|
472
|
+
An error with status code 500 for any other server-side error.
|
|
473
|
+
"""
|
|
474
|
+
try:
|
|
475
|
+
session_service.select_attack_module(runner_id, "")
|
|
476
|
+
return {"success": True}
|
|
477
|
+
except ServiceException as e:
|
|
478
|
+
if e.error_code == "FileNotFound":
|
|
479
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
480
|
+
elif e.error_code == "ValidationError":
|
|
481
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
482
|
+
else:
|
|
483
|
+
raise HTTPException(status_code=500, detail=e.msg)
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
@router.put("/api/v1/sessions/{runner_id}/metric/{metric_name}")
|
|
487
|
+
@inject
|
|
488
|
+
async def set_metric(
|
|
489
|
+
runner_id: str,
|
|
490
|
+
metric_name: str,
|
|
491
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
492
|
+
) -> dict[str, bool]:
|
|
493
|
+
"""
|
|
494
|
+
Set a metric for the current session.
|
|
495
|
+
|
|
496
|
+
Args:
|
|
497
|
+
runner_id (str): The unique identifier of the session.
|
|
498
|
+
metric_name (str): The name of the metric to set.
|
|
499
|
+
session_service (SessionService): The service responsible for managing session metrics.
|
|
500
|
+
|
|
501
|
+
Returns:
|
|
502
|
+
Dict[str, bool]: A dictionary with a key 'success' indicating the operation result.
|
|
503
|
+
|
|
504
|
+
Raises:
|
|
505
|
+
HTTPException: An error with status code 404 if the metric is not found.
|
|
506
|
+
An error with status code 400 if there is a validation error.
|
|
507
|
+
An error with status code 500 for any other server-side error.
|
|
508
|
+
"""
|
|
509
|
+
try:
|
|
510
|
+
session_service.select_metric(runner_id, metric_name)
|
|
511
|
+
return {"success": True}
|
|
512
|
+
except ServiceException as e:
|
|
513
|
+
if e.error_code == "FileNotFound":
|
|
514
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
515
|
+
elif e.error_code == "ValidationError":
|
|
516
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
517
|
+
else:
|
|
518
|
+
raise HTTPException(status_code=500, detail=e.msg)
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
@router.delete("/api/v1/sessions/{runner_id}/metric/{metric_name}")
|
|
522
|
+
@inject
|
|
523
|
+
async def unset_metric(
|
|
524
|
+
runner_id: str,
|
|
525
|
+
metric_name: str = "",
|
|
526
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
527
|
+
) -> dict[str, bool]:
|
|
528
|
+
"""
|
|
529
|
+
Remove a metric from the current session.
|
|
530
|
+
|
|
531
|
+
Args:
|
|
532
|
+
runner_id (str): The unique identifier of the session.
|
|
533
|
+
metric_name (str): The name of the metric to remove.
|
|
534
|
+
session_service (SessionService): The service responsible for managing session metrics.
|
|
535
|
+
|
|
536
|
+
Returns:
|
|
537
|
+
Dict[str, bool]: A dictionary with a key 'success' indicating the operation result.
|
|
538
|
+
|
|
539
|
+
Raises:
|
|
540
|
+
HTTPException: An error with status code 404 if the metric is not found.
|
|
541
|
+
An error with status code 400 if there is a validation error.
|
|
542
|
+
An error with status code 500 for any other server-side error.
|
|
543
|
+
"""
|
|
544
|
+
try:
|
|
545
|
+
session_service.select_metric(runner_id, "")
|
|
546
|
+
return {"success": True}
|
|
547
|
+
except ServiceException as e:
|
|
548
|
+
if e.error_code == "FileNotFound":
|
|
549
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
550
|
+
elif e.error_code == "ValidationError":
|
|
551
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
552
|
+
else:
|
|
553
|
+
raise HTTPException(status_code=500, detail=e.msg)
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
@router.put("/api/v1/sessions/{runner_id}/system-prompt")
|
|
557
|
+
@inject
|
|
558
|
+
async def set_system_prompts(
|
|
559
|
+
runner_id: str,
|
|
560
|
+
system_prompt: str = Body(..., embed=True),
|
|
561
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
562
|
+
) -> dict[str, bool]:
|
|
563
|
+
try:
|
|
564
|
+
session_service.update_system_prompt(runner_id, system_prompt)
|
|
565
|
+
return {"success": True}
|
|
566
|
+
except ServiceException as e:
|
|
567
|
+
if e.error_code == "FileNotFound":
|
|
568
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
569
|
+
elif e.error_code == "ValidationError":
|
|
570
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
571
|
+
else:
|
|
572
|
+
raise HTTPException(status_code=500, detail=e.msg)
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
@router.delete("/api/v1/sessions/{runner_id}/system-prompt")
|
|
576
|
+
@inject
|
|
577
|
+
async def unset_system_prompts(
|
|
578
|
+
runner_id: str,
|
|
579
|
+
system_prompt: str = "",
|
|
580
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
581
|
+
) -> dict[str, bool]:
|
|
582
|
+
try:
|
|
583
|
+
session_service.update_system_prompt(runner_id, "")
|
|
584
|
+
return {"success": True}
|
|
585
|
+
except ServiceException as e:
|
|
586
|
+
if e.error_code == "FileNotFound":
|
|
587
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
588
|
+
elif e.error_code == "ValidationError":
|
|
589
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
590
|
+
else:
|
|
591
|
+
raise HTTPException(status_code=500, detail=e.msg)
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
@router.get("/api/v1/sessions/{runner_id}/close")
|
|
595
|
+
@inject
|
|
596
|
+
async def close_session(
|
|
597
|
+
runner_id: str,
|
|
598
|
+
session_service: SessionService = Depends(Provide[Container.session_service]),
|
|
599
|
+
):
|
|
600
|
+
try:
|
|
601
|
+
await session_service.end_session(runner_id)
|
|
602
|
+
return {"success": True}
|
|
603
|
+
except ServiceException as e:
|
|
604
|
+
if e.error_code == "FileNotFound":
|
|
605
|
+
raise HTTPException(status_code=404, detail=e.msg)
|
|
606
|
+
elif e.error_code == "ValidationError":
|
|
607
|
+
raise HTTPException(status_code=400, detail=e.msg)
|
|
608
|
+
else:
|
|
609
|
+
raise HTTPException(status_code=500, detail=e.msg)
|