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,219 @@
1
+ from typing import Optional
2
+
3
+ from dependency_injector.wiring import Provide, inject
4
+ from fastapi import APIRouter, Depends, HTTPException, Query
5
+
6
+ from ..container import Container
7
+ from ..schemas.recipe_create_dto import RecipeCreateDTO, RecipeUpdateDTO
8
+ from ..schemas.recipe_response_model import RecipeResponseModel
9
+ from ..services.recipe_service import RecipeService
10
+ from ..services.utils.exceptions_handler import ServiceException
11
+
12
+ router = APIRouter(tags=["Recipe"])
13
+
14
+
15
+ @router.post("/api/v1/recipes")
16
+ @inject
17
+ def create_recipe(
18
+ recipe_data: RecipeCreateDTO,
19
+ recipe_service: RecipeService = Depends(Provide[Container.recipe_service]),
20
+ ) -> dict[str, str]:
21
+ """
22
+ Endpoint to add a new recipe to the database.
23
+
24
+ Parameters:
25
+ recipe_data (RecipeCreateDTO): The data transfer object containing the recipe details.
26
+ recipe_service (RecipeService): The service layer responsible for the creation logic.
27
+
28
+ Returns:
29
+ dict[str, str]: A dictionary with a message indicating the successful creation of the recipe.
30
+
31
+ Raises:
32
+ HTTPException: 404 if the recipe cannot be created because the file is not found.
33
+ 400 if there is a validation error with the provided data.
34
+ 500 for any other internal server error.
35
+ """
36
+ try:
37
+ recipe_service.create_recipe(recipe_data)
38
+ return {"message": "Recipe created successfully"}
39
+ except ServiceException as e:
40
+ if e.error_code == "FileNotFound":
41
+ raise HTTPException(
42
+ status_code=404, detail=f"Failed to create recipe: {e.msg}"
43
+ )
44
+ elif e.error_code == "ValidationError":
45
+ raise HTTPException(
46
+ status_code=400, detail=f"Failed to create recipe: {e.msg}"
47
+ )
48
+ else:
49
+ raise HTTPException(
50
+ status_code=500, detail=f"Failed to create recipe: {e.msg}"
51
+ )
52
+
53
+
54
+ @router.get("/api/v1/recipes")
55
+ @inject
56
+ def get_all_recipes(
57
+ ids: Optional[str] = Query(None, description="Get recipes to query"),
58
+ tags: Optional[str] = Query(None, description="Filter recipes by tags"),
59
+ categories: str = Query(None, description="Filter recipes by categories"),
60
+ sort_by: Optional[str] = Query(
61
+ None, description="Sort recipes by a specific field"
62
+ ),
63
+ count: bool = Query(False, description="Whether to include the count of recipes"),
64
+ recipe_service: RecipeService = Depends(Provide[Container.recipe_service]),
65
+ ) -> list[RecipeResponseModel]:
66
+ """
67
+ Endpoint to retrieve all recipes from the database, with optional filters, sorting, and count inclusion.
68
+
69
+ Parameters:
70
+ ids (str, optional): Filter to retrieve recipes by list of comma separated recipe ids.
71
+ tags (str, optional): Filter to retrieve recipes by tags.
72
+ categories (str, optional): Filter to retrieve recipes by categories.
73
+ sort_by (str, optional): Parameter to sort recipes by a specific field.
74
+ count (bool, optional): Flag to indicate whether to include the count of recipes in the response.
75
+ recipe_service (RecipeService): The service layer responsible for the retrieval logic.
76
+
77
+ Returns:
78
+ list[RecipeResponseModel]: A list of recipe, filtered, sorted, and with counts.
79
+
80
+ Raises:
81
+ HTTPException: 404 if no recipes are found.
82
+ 400 if there is a validation error with the provided data.
83
+ 500 for any other internal server error.
84
+ """
85
+ try:
86
+ recipes = recipe_service.get_all_recipes(
87
+ tags=tags, categories=categories, sort_by=sort_by, count=count, ids=ids
88
+ )
89
+ return recipes
90
+ except ServiceException as e:
91
+ if e.error_code == "FileNotFound":
92
+ raise HTTPException(
93
+ status_code=404, detail=f"Failed to retrieve recipes: {e.msg}"
94
+ )
95
+ elif e.error_code == "ValidationError":
96
+ raise HTTPException(
97
+ status_code=400, detail=f"Failed to retrieve recipes: {e.msg}"
98
+ )
99
+ else:
100
+ raise HTTPException(
101
+ status_code=500, detail=f"Failed to retrieve recipes: {e.msg}"
102
+ )
103
+
104
+
105
+ @router.get("/api/v1/recipes/name")
106
+ @inject
107
+ def get_all_recipes_name(
108
+ recipe_service: RecipeService = Depends(Provide[Container.recipe_service]),
109
+ ) -> list[str]:
110
+ """
111
+ Endpoint to retrieve all recipe names from the database.
112
+
113
+ Parameters:
114
+ recipe_service (RecipeService): The service layer responsible for retrieving recipe names.
115
+
116
+ Returns:
117
+ list[str]: A list of recipe names.
118
+
119
+ Raises:
120
+ HTTPException: 404 if no recipe names are found.
121
+ 400 if there is a validation error with the provided data.
122
+ 500 for any other internal server error.
123
+ """
124
+ try:
125
+ recipes = recipe_service.get_all_recipes_name()
126
+ return recipes
127
+ except ServiceException as e:
128
+ if e.error_code == "FileNotFound":
129
+ raise HTTPException(
130
+ status_code=404, detail=f"Failed to retrieve recipe names: {e.msg}"
131
+ )
132
+ elif e.error_code == "ValidationError":
133
+ raise HTTPException(
134
+ status_code=400, detail=f"Failed to retrieve recipe names: {e.msg}"
135
+ )
136
+ else:
137
+ raise HTTPException(
138
+ status_code=500, detail=f"Failed to retrieve recipe names: {e.msg}"
139
+ )
140
+
141
+
142
+ @router.put("/api/v1/recipes/{recipe_id}")
143
+ @inject
144
+ async def update_recipe(
145
+ recipe_data: RecipeUpdateDTO,
146
+ recipe_id: str,
147
+ recipe_service: RecipeService = Depends(Provide[Container.recipe_service]),
148
+ ) -> dict[str, str]:
149
+ """
150
+ Endpoint to update an existing recipe in the database by its ID.
151
+
152
+ Parameters:
153
+ recipe_data (RecipeCreateDTO): The data transfer object containing the updated recipe details.
154
+ recipe_id (str): The unique identifier of the recipe to update.
155
+ recipe_service (RecipeService): The service layer responsible for the update logic.
156
+
157
+ Returns:
158
+ dict[str, str]: A dictionary with a message indicating the successful update of the recipe.
159
+
160
+ Raises:
161
+ HTTPException: 404 if the recipe to be updated cannot be found.
162
+ 400 if there is a validation error with the provided data.
163
+ 500 for any other internal server error.
164
+ """
165
+ try:
166
+ recipe_service.update_recipe(recipe_data, recipe_id)
167
+ return {"message": "Recipe updated successfully"}
168
+ except ServiceException as e:
169
+ if e.error_code == "FileNotFound":
170
+ raise HTTPException(
171
+ status_code=404, detail=f"Failed to update recipe: {e.msg}"
172
+ )
173
+ elif e.error_code == "ValidationError":
174
+ raise HTTPException(
175
+ status_code=400, detail=f"Failed to update recipe: {e.msg}"
176
+ )
177
+ else:
178
+ raise HTTPException(
179
+ status_code=500, detail=f"Failed to update recipe: {e.msg}"
180
+ )
181
+
182
+
183
+ @router.delete("/api/v1/recipes/{recipe_id}")
184
+ @inject
185
+ def delete_recipe(
186
+ recipe_id: str,
187
+ recipe_service: RecipeService = Depends(Provide[Container.recipe_service]),
188
+ ) -> dict[str, str]:
189
+ """
190
+ Endpoint to delete a recipe from the database by its ID.
191
+
192
+ Parameters:
193
+ recipe_id (str): The unique identifier of the recipe to delete.
194
+ recipe_service (RecipeService): The service layer responsible for the deletion logic.
195
+
196
+ Returns:
197
+ dict[str, str]: A dictionary with a message indicating the successful deletion of the recipe.
198
+
199
+ Raises:
200
+ HTTPException: 404 if the recipe to be deleted cannot be found.
201
+ 400 if there is a validation error with the provided data.
202
+ 500 for any other internal server error.
203
+ """
204
+ try:
205
+ recipe_service.delete_recipe(recipe_id)
206
+ return {"message": "Recipe deleted successfully"}
207
+ except ServiceException as e:
208
+ if e.error_code == "FileNotFound":
209
+ raise HTTPException(
210
+ status_code=404, detail=f"Failed to delete recipe: {e.msg}"
211
+ )
212
+ elif e.error_code == "ValidationError":
213
+ raise HTTPException(
214
+ status_code=400, detail=f"Failed to delete recipe: {e.msg}"
215
+ )
216
+ else:
217
+ raise HTTPException(
218
+ status_code=500, detail=f"Failed to delete recipe: {e.msg}"
219
+ )