azure-ai-evaluation 1.0.1__py3-none-any.whl → 1.13.5__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.
Potentially problematic release.
This version of azure-ai-evaluation might be problematic. Click here for more details.
- azure/ai/evaluation/__init__.py +85 -14
- azure/ai/evaluation/_aoai/__init__.py +10 -0
- azure/ai/evaluation/_aoai/aoai_grader.py +140 -0
- azure/ai/evaluation/_aoai/label_grader.py +68 -0
- azure/ai/evaluation/_aoai/python_grader.py +86 -0
- azure/ai/evaluation/_aoai/score_model_grader.py +94 -0
- azure/ai/evaluation/_aoai/string_check_grader.py +66 -0
- azure/ai/evaluation/_aoai/text_similarity_grader.py +80 -0
- azure/ai/evaluation/_azure/__init__.py +3 -0
- azure/ai/evaluation/_azure/_clients.py +204 -0
- azure/ai/evaluation/_azure/_envs.py +207 -0
- azure/ai/evaluation/_azure/_models.py +227 -0
- azure/ai/evaluation/_azure/_token_manager.py +129 -0
- azure/ai/evaluation/_common/__init__.py +9 -1
- azure/ai/evaluation/_common/constants.py +124 -2
- azure/ai/evaluation/_common/evaluation_onedp_client.py +169 -0
- azure/ai/evaluation/_common/onedp/__init__.py +32 -0
- azure/ai/evaluation/_common/onedp/_client.py +166 -0
- azure/ai/evaluation/_common/onedp/_configuration.py +72 -0
- azure/ai/evaluation/_common/onedp/_model_base.py +1232 -0
- azure/ai/evaluation/_common/onedp/_patch.py +21 -0
- azure/ai/evaluation/_common/onedp/_serialization.py +2032 -0
- azure/ai/evaluation/_common/onedp/_types.py +21 -0
- azure/ai/evaluation/_common/onedp/_utils/__init__.py +6 -0
- azure/ai/evaluation/_common/onedp/_utils/model_base.py +1232 -0
- azure/ai/evaluation/_common/onedp/_utils/serialization.py +2032 -0
- azure/ai/evaluation/_common/onedp/_validation.py +66 -0
- azure/ai/evaluation/_common/onedp/_vendor.py +50 -0
- azure/ai/evaluation/_common/onedp/_version.py +9 -0
- azure/ai/evaluation/_common/onedp/aio/__init__.py +29 -0
- azure/ai/evaluation/_common/onedp/aio/_client.py +168 -0
- azure/ai/evaluation/_common/onedp/aio/_configuration.py +72 -0
- azure/ai/evaluation/_common/onedp/aio/_patch.py +21 -0
- azure/ai/evaluation/_common/onedp/aio/operations/__init__.py +49 -0
- azure/ai/evaluation/_common/onedp/aio/operations/_operations.py +7143 -0
- azure/ai/evaluation/_common/onedp/aio/operations/_patch.py +21 -0
- azure/ai/evaluation/_common/onedp/models/__init__.py +358 -0
- azure/ai/evaluation/_common/onedp/models/_enums.py +447 -0
- azure/ai/evaluation/_common/onedp/models/_models.py +5963 -0
- azure/ai/evaluation/_common/onedp/models/_patch.py +21 -0
- azure/ai/evaluation/_common/onedp/operations/__init__.py +49 -0
- azure/ai/evaluation/_common/onedp/operations/_operations.py +8951 -0
- azure/ai/evaluation/_common/onedp/operations/_patch.py +21 -0
- azure/ai/evaluation/_common/onedp/py.typed +1 -0
- azure/ai/evaluation/_common/onedp/servicepatterns/__init__.py +1 -0
- azure/ai/evaluation/_common/onedp/servicepatterns/aio/__init__.py +1 -0
- azure/ai/evaluation/_common/onedp/servicepatterns/aio/operations/__init__.py +25 -0
- azure/ai/evaluation/_common/onedp/servicepatterns/aio/operations/_operations.py +34 -0
- azure/ai/evaluation/_common/onedp/servicepatterns/aio/operations/_patch.py +20 -0
- azure/ai/evaluation/_common/onedp/servicepatterns/buildingblocks/__init__.py +1 -0
- azure/ai/evaluation/_common/onedp/servicepatterns/buildingblocks/aio/__init__.py +1 -0
- azure/ai/evaluation/_common/onedp/servicepatterns/buildingblocks/aio/operations/__init__.py +22 -0
- azure/ai/evaluation/_common/onedp/servicepatterns/buildingblocks/aio/operations/_operations.py +29 -0
- azure/ai/evaluation/_common/onedp/servicepatterns/buildingblocks/aio/operations/_patch.py +20 -0
- azure/ai/evaluation/_common/onedp/servicepatterns/buildingblocks/operations/__init__.py +22 -0
- azure/ai/evaluation/_common/onedp/servicepatterns/buildingblocks/operations/_operations.py +29 -0
- azure/ai/evaluation/_common/onedp/servicepatterns/buildingblocks/operations/_patch.py +20 -0
- azure/ai/evaluation/_common/onedp/servicepatterns/operations/__init__.py +25 -0
- azure/ai/evaluation/_common/onedp/servicepatterns/operations/_operations.py +34 -0
- azure/ai/evaluation/_common/onedp/servicepatterns/operations/_patch.py +20 -0
- azure/ai/evaluation/_common/rai_service.py +578 -69
- azure/ai/evaluation/_common/raiclient/__init__.py +34 -0
- azure/ai/evaluation/_common/raiclient/_client.py +128 -0
- azure/ai/evaluation/_common/raiclient/_configuration.py +87 -0
- azure/ai/evaluation/_common/raiclient/_model_base.py +1235 -0
- azure/ai/evaluation/_common/raiclient/_patch.py +20 -0
- azure/ai/evaluation/_common/raiclient/_serialization.py +2050 -0
- azure/ai/evaluation/_common/raiclient/_version.py +9 -0
- azure/ai/evaluation/_common/raiclient/aio/__init__.py +29 -0
- azure/ai/evaluation/_common/raiclient/aio/_client.py +130 -0
- azure/ai/evaluation/_common/raiclient/aio/_configuration.py +87 -0
- azure/ai/evaluation/_common/raiclient/aio/_patch.py +20 -0
- azure/ai/evaluation/_common/raiclient/aio/operations/__init__.py +25 -0
- azure/ai/evaluation/_common/raiclient/aio/operations/_operations.py +981 -0
- azure/ai/evaluation/_common/raiclient/aio/operations/_patch.py +20 -0
- azure/ai/evaluation/_common/raiclient/models/__init__.py +60 -0
- azure/ai/evaluation/_common/raiclient/models/_enums.py +18 -0
- azure/ai/evaluation/_common/raiclient/models/_models.py +651 -0
- azure/ai/evaluation/_common/raiclient/models/_patch.py +20 -0
- azure/ai/evaluation/_common/raiclient/operations/__init__.py +25 -0
- azure/ai/evaluation/_common/raiclient/operations/_operations.py +1238 -0
- azure/ai/evaluation/_common/raiclient/operations/_patch.py +20 -0
- azure/ai/evaluation/_common/raiclient/py.typed +1 -0
- azure/ai/evaluation/_common/utils.py +505 -27
- azure/ai/evaluation/_constants.py +147 -0
- azure/ai/evaluation/_converters/__init__.py +3 -0
- azure/ai/evaluation/_converters/_ai_services.py +899 -0
- azure/ai/evaluation/_converters/_models.py +467 -0
- azure/ai/evaluation/_converters/_sk_services.py +495 -0
- azure/ai/evaluation/_eval_mapping.py +87 -0
- azure/ai/evaluation/_evaluate/_batch_run/__init__.py +10 -2
- azure/ai/evaluation/_evaluate/_batch_run/_run_submitter_client.py +176 -0
- azure/ai/evaluation/_evaluate/_batch_run/batch_clients.py +82 -0
- azure/ai/evaluation/_evaluate/_batch_run/code_client.py +18 -12
- azure/ai/evaluation/_evaluate/_batch_run/eval_run_context.py +19 -6
- azure/ai/evaluation/_evaluate/_batch_run/proxy_client.py +47 -22
- azure/ai/evaluation/_evaluate/_batch_run/target_run_context.py +18 -2
- azure/ai/evaluation/_evaluate/_eval_run.py +32 -46
- azure/ai/evaluation/_evaluate/_evaluate.py +1809 -142
- azure/ai/evaluation/_evaluate/_evaluate_aoai.py +992 -0
- azure/ai/evaluation/_evaluate/_telemetry/__init__.py +5 -90
- azure/ai/evaluation/_evaluate/_utils.py +237 -42
- azure/ai/evaluation/_evaluator_definition.py +76 -0
- azure/ai/evaluation/_evaluators/_bleu/_bleu.py +80 -28
- azure/ai/evaluation/_evaluators/_code_vulnerability/__init__.py +5 -0
- azure/ai/evaluation/_evaluators/_code_vulnerability/_code_vulnerability.py +119 -0
- azure/ai/evaluation/_evaluators/_coherence/_coherence.py +40 -4
- azure/ai/evaluation/_evaluators/_common/__init__.py +2 -0
- azure/ai/evaluation/_evaluators/_common/_base_eval.py +430 -29
- azure/ai/evaluation/_evaluators/_common/_base_multi_eval.py +63 -0
- azure/ai/evaluation/_evaluators/_common/_base_prompty_eval.py +269 -12
- azure/ai/evaluation/_evaluators/_common/_base_rai_svc_eval.py +74 -9
- azure/ai/evaluation/_evaluators/_common/_conversation_aggregators.py +49 -0
- azure/ai/evaluation/_evaluators/_content_safety/_content_safety.py +73 -53
- azure/ai/evaluation/_evaluators/_content_safety/_hate_unfairness.py +35 -5
- azure/ai/evaluation/_evaluators/_content_safety/_self_harm.py +26 -5
- azure/ai/evaluation/_evaluators/_content_safety/_sexual.py +35 -5
- azure/ai/evaluation/_evaluators/_content_safety/_violence.py +34 -4
- azure/ai/evaluation/_evaluators/_document_retrieval/__init__.py +7 -0
- azure/ai/evaluation/_evaluators/_document_retrieval/_document_retrieval.py +442 -0
- azure/ai/evaluation/_evaluators/_eci/_eci.py +6 -3
- azure/ai/evaluation/_evaluators/_f1_score/_f1_score.py +97 -70
- azure/ai/evaluation/_evaluators/_fluency/_fluency.py +39 -3
- azure/ai/evaluation/_evaluators/_gleu/_gleu.py +80 -25
- azure/ai/evaluation/_evaluators/_groundedness/_groundedness.py +230 -20
- azure/ai/evaluation/_evaluators/_groundedness/groundedness_with_query.prompty +30 -29
- azure/ai/evaluation/_evaluators/_groundedness/groundedness_without_query.prompty +19 -14
- azure/ai/evaluation/_evaluators/_intent_resolution/__init__.py +7 -0
- azure/ai/evaluation/_evaluators/_intent_resolution/_intent_resolution.py +196 -0
- azure/ai/evaluation/_evaluators/_intent_resolution/intent_resolution.prompty +275 -0
- azure/ai/evaluation/_evaluators/_meteor/_meteor.py +89 -36
- azure/ai/evaluation/_evaluators/_protected_material/_protected_material.py +22 -4
- azure/ai/evaluation/_evaluators/_qa/_qa.py +94 -35
- azure/ai/evaluation/_evaluators/_relevance/_relevance.py +100 -4
- azure/ai/evaluation/_evaluators/_relevance/relevance.prompty +154 -56
- azure/ai/evaluation/_evaluators/_response_completeness/__init__.py +7 -0
- azure/ai/evaluation/_evaluators/_response_completeness/_response_completeness.py +202 -0
- azure/ai/evaluation/_evaluators/_response_completeness/response_completeness.prompty +84 -0
- azure/ai/evaluation/_evaluators/_retrieval/_retrieval.py +39 -3
- azure/ai/evaluation/_evaluators/_rouge/_rouge.py +166 -26
- azure/ai/evaluation/_evaluators/_service_groundedness/_service_groundedness.py +38 -7
- azure/ai/evaluation/_evaluators/_similarity/_similarity.py +81 -85
- azure/ai/evaluation/_evaluators/_task_adherence/__init__.py +7 -0
- azure/ai/evaluation/_evaluators/_task_adherence/_task_adherence.py +226 -0
- azure/ai/evaluation/_evaluators/_task_adherence/task_adherence.prompty +101 -0
- azure/ai/evaluation/_evaluators/_task_completion/__init__.py +7 -0
- azure/ai/evaluation/_evaluators/_task_completion/_task_completion.py +177 -0
- azure/ai/evaluation/_evaluators/_task_completion/task_completion.prompty +220 -0
- azure/ai/evaluation/_evaluators/_task_navigation_efficiency/__init__.py +7 -0
- azure/ai/evaluation/_evaluators/_task_navigation_efficiency/_task_navigation_efficiency.py +384 -0
- azure/ai/evaluation/_evaluators/_tool_call_accuracy/__init__.py +9 -0
- azure/ai/evaluation/_evaluators/_tool_call_accuracy/_tool_call_accuracy.py +298 -0
- azure/ai/evaluation/_evaluators/_tool_call_accuracy/tool_call_accuracy.prompty +166 -0
- azure/ai/evaluation/_evaluators/_tool_call_success/__init__.py +7 -0
- azure/ai/evaluation/_evaluators/_tool_call_success/_tool_call_success.py +306 -0
- azure/ai/evaluation/_evaluators/_tool_call_success/tool_call_success.prompty +321 -0
- azure/ai/evaluation/_evaluators/_tool_input_accuracy/__init__.py +9 -0
- azure/ai/evaluation/_evaluators/_tool_input_accuracy/_tool_input_accuracy.py +263 -0
- azure/ai/evaluation/_evaluators/_tool_input_accuracy/tool_input_accuracy.prompty +76 -0
- azure/ai/evaluation/_evaluators/_tool_output_utilization/__init__.py +7 -0
- azure/ai/evaluation/_evaluators/_tool_output_utilization/_tool_output_utilization.py +225 -0
- azure/ai/evaluation/_evaluators/_tool_output_utilization/tool_output_utilization.prompty +221 -0
- azure/ai/evaluation/_evaluators/_tool_selection/__init__.py +9 -0
- azure/ai/evaluation/_evaluators/_tool_selection/_tool_selection.py +266 -0
- azure/ai/evaluation/_evaluators/_tool_selection/tool_selection.prompty +104 -0
- azure/ai/evaluation/_evaluators/_ungrounded_attributes/__init__.py +5 -0
- azure/ai/evaluation/_evaluators/_ungrounded_attributes/_ungrounded_attributes.py +102 -0
- azure/ai/evaluation/_evaluators/_xpia/xpia.py +20 -4
- azure/ai/evaluation/_exceptions.py +24 -1
- azure/ai/evaluation/_http_utils.py +7 -5
- azure/ai/evaluation/_legacy/__init__.py +3 -0
- azure/ai/evaluation/_legacy/_adapters/__init__.py +7 -0
- azure/ai/evaluation/_legacy/_adapters/_check.py +17 -0
- azure/ai/evaluation/_legacy/_adapters/_configuration.py +45 -0
- azure/ai/evaluation/_legacy/_adapters/_constants.py +10 -0
- azure/ai/evaluation/_legacy/_adapters/_errors.py +29 -0
- azure/ai/evaluation/_legacy/_adapters/_flows.py +28 -0
- azure/ai/evaluation/_legacy/_adapters/_service.py +16 -0
- azure/ai/evaluation/_legacy/_adapters/client.py +51 -0
- azure/ai/evaluation/_legacy/_adapters/entities.py +26 -0
- azure/ai/evaluation/_legacy/_adapters/tracing.py +28 -0
- azure/ai/evaluation/_legacy/_adapters/types.py +15 -0
- azure/ai/evaluation/_legacy/_adapters/utils.py +31 -0
- azure/ai/evaluation/_legacy/_batch_engine/__init__.py +9 -0
- azure/ai/evaluation/_legacy/_batch_engine/_config.py +48 -0
- azure/ai/evaluation/_legacy/_batch_engine/_engine.py +477 -0
- azure/ai/evaluation/_legacy/_batch_engine/_exceptions.py +88 -0
- azure/ai/evaluation/_legacy/_batch_engine/_openai_injector.py +132 -0
- azure/ai/evaluation/_legacy/_batch_engine/_result.py +107 -0
- azure/ai/evaluation/_legacy/_batch_engine/_run.py +127 -0
- azure/ai/evaluation/_legacy/_batch_engine/_run_storage.py +128 -0
- azure/ai/evaluation/_legacy/_batch_engine/_run_submitter.py +262 -0
- azure/ai/evaluation/_legacy/_batch_engine/_status.py +25 -0
- azure/ai/evaluation/_legacy/_batch_engine/_trace.py +97 -0
- azure/ai/evaluation/_legacy/_batch_engine/_utils.py +97 -0
- azure/ai/evaluation/_legacy/_batch_engine/_utils_deprecated.py +131 -0
- azure/ai/evaluation/_legacy/_common/__init__.py +3 -0
- azure/ai/evaluation/_legacy/_common/_async_token_provider.py +117 -0
- azure/ai/evaluation/_legacy/_common/_logging.py +292 -0
- azure/ai/evaluation/_legacy/_common/_thread_pool_executor_with_context.py +17 -0
- azure/ai/evaluation/_legacy/prompty/__init__.py +36 -0
- azure/ai/evaluation/_legacy/prompty/_connection.py +119 -0
- azure/ai/evaluation/_legacy/prompty/_exceptions.py +139 -0
- azure/ai/evaluation/_legacy/prompty/_prompty.py +430 -0
- azure/ai/evaluation/_legacy/prompty/_utils.py +663 -0
- azure/ai/evaluation/_legacy/prompty/_yaml_utils.py +99 -0
- azure/ai/evaluation/_model_configurations.py +26 -0
- azure/ai/evaluation/_safety_evaluation/__init__.py +3 -0
- azure/ai/evaluation/_safety_evaluation/_generated_rai_client.py +0 -0
- azure/ai/evaluation/_safety_evaluation/_safety_evaluation.py +917 -0
- azure/ai/evaluation/_user_agent.py +32 -1
- azure/ai/evaluation/_vendor/rouge_score/rouge_scorer.py +0 -4
- azure/ai/evaluation/_vendor/rouge_score/scoring.py +0 -4
- azure/ai/evaluation/_vendor/rouge_score/tokenize.py +0 -4
- azure/ai/evaluation/_version.py +2 -1
- azure/ai/evaluation/red_team/__init__.py +22 -0
- azure/ai/evaluation/red_team/_agent/__init__.py +3 -0
- azure/ai/evaluation/red_team/_agent/_agent_functions.py +261 -0
- azure/ai/evaluation/red_team/_agent/_agent_tools.py +461 -0
- azure/ai/evaluation/red_team/_agent/_agent_utils.py +89 -0
- azure/ai/evaluation/red_team/_agent/_semantic_kernel_plugin.py +228 -0
- azure/ai/evaluation/red_team/_attack_objective_generator.py +268 -0
- azure/ai/evaluation/red_team/_attack_strategy.py +49 -0
- azure/ai/evaluation/red_team/_callback_chat_target.py +115 -0
- azure/ai/evaluation/red_team/_default_converter.py +21 -0
- azure/ai/evaluation/red_team/_evaluation_processor.py +505 -0
- azure/ai/evaluation/red_team/_mlflow_integration.py +430 -0
- azure/ai/evaluation/red_team/_orchestrator_manager.py +803 -0
- azure/ai/evaluation/red_team/_red_team.py +1717 -0
- azure/ai/evaluation/red_team/_red_team_result.py +661 -0
- azure/ai/evaluation/red_team/_result_processor.py +1708 -0
- azure/ai/evaluation/red_team/_utils/__init__.py +37 -0
- azure/ai/evaluation/red_team/_utils/_rai_service_eval_chat_target.py +128 -0
- azure/ai/evaluation/red_team/_utils/_rai_service_target.py +601 -0
- azure/ai/evaluation/red_team/_utils/_rai_service_true_false_scorer.py +114 -0
- azure/ai/evaluation/red_team/_utils/constants.py +72 -0
- azure/ai/evaluation/red_team/_utils/exception_utils.py +345 -0
- azure/ai/evaluation/red_team/_utils/file_utils.py +266 -0
- azure/ai/evaluation/red_team/_utils/formatting_utils.py +365 -0
- azure/ai/evaluation/red_team/_utils/logging_utils.py +139 -0
- azure/ai/evaluation/red_team/_utils/metric_mapping.py +73 -0
- azure/ai/evaluation/red_team/_utils/objective_utils.py +46 -0
- azure/ai/evaluation/red_team/_utils/progress_utils.py +252 -0
- azure/ai/evaluation/red_team/_utils/retry_utils.py +218 -0
- azure/ai/evaluation/red_team/_utils/strategy_utils.py +218 -0
- azure/ai/evaluation/simulator/_adversarial_scenario.py +6 -0
- azure/ai/evaluation/simulator/_adversarial_simulator.py +187 -80
- azure/ai/evaluation/simulator/_constants.py +1 -0
- azure/ai/evaluation/simulator/_conversation/__init__.py +138 -11
- azure/ai/evaluation/simulator/_conversation/_conversation.py +6 -2
- azure/ai/evaluation/simulator/_conversation/constants.py +1 -1
- azure/ai/evaluation/simulator/_direct_attack_simulator.py +37 -24
- azure/ai/evaluation/simulator/_helpers/_language_suffix_mapping.py +1 -0
- azure/ai/evaluation/simulator/_indirect_attack_simulator.py +56 -28
- azure/ai/evaluation/simulator/_model_tools/__init__.py +2 -1
- azure/ai/evaluation/simulator/_model_tools/_generated_rai_client.py +225 -0
- azure/ai/evaluation/simulator/_model_tools/_identity_manager.py +12 -10
- azure/ai/evaluation/simulator/_model_tools/_proxy_completion_model.py +100 -45
- azure/ai/evaluation/simulator/_model_tools/_rai_client.py +101 -3
- azure/ai/evaluation/simulator/_model_tools/_template_handler.py +31 -11
- azure/ai/evaluation/simulator/_model_tools/models.py +20 -17
- azure/ai/evaluation/simulator/_simulator.py +43 -19
- {azure_ai_evaluation-1.0.1.dist-info → azure_ai_evaluation-1.13.5.dist-info}/METADATA +378 -27
- azure_ai_evaluation-1.13.5.dist-info/RECORD +305 -0
- {azure_ai_evaluation-1.0.1.dist-info → azure_ai_evaluation-1.13.5.dist-info}/WHEEL +1 -1
- azure/ai/evaluation/_evaluators/_multimodal/__init__.py +0 -20
- azure/ai/evaluation/_evaluators/_multimodal/_content_safety_multimodal.py +0 -132
- azure/ai/evaluation/_evaluators/_multimodal/_content_safety_multimodal_base.py +0 -55
- azure/ai/evaluation/_evaluators/_multimodal/_hate_unfairness.py +0 -100
- azure/ai/evaluation/_evaluators/_multimodal/_protected_material.py +0 -124
- azure/ai/evaluation/_evaluators/_multimodal/_self_harm.py +0 -100
- azure/ai/evaluation/_evaluators/_multimodal/_sexual.py +0 -100
- azure/ai/evaluation/_evaluators/_multimodal/_violence.py +0 -100
- azure/ai/evaluation/simulator/_tracing.py +0 -89
- azure_ai_evaluation-1.0.1.dist-info/RECORD +0 -119
- {azure_ai_evaluation-1.0.1.dist-info → azure_ai_evaluation-1.13.5.dist-info/licenses}/NOTICE.txt +0 -0
- {azure_ai_evaluation-1.0.1.dist-info → azure_ai_evaluation-1.13.5.dist-info}/top_level.txt +0 -0
|
@@ -6,12 +6,15 @@ import copy
|
|
|
6
6
|
import json
|
|
7
7
|
import time
|
|
8
8
|
import uuid
|
|
9
|
-
from typing import Any, Dict, List, Optional, cast
|
|
9
|
+
from typing import Any, Dict, List, Optional, cast, Union
|
|
10
10
|
|
|
11
11
|
from azure.ai.evaluation._http_utils import AsyncHttpPipeline, get_async_http_client
|
|
12
|
-
from azure.ai.evaluation._user_agent import
|
|
13
|
-
from azure.core.exceptions import HttpResponseError
|
|
12
|
+
from azure.ai.evaluation._user_agent import UserAgentSingleton
|
|
13
|
+
from azure.core.exceptions import HttpResponseError, ServiceResponseError
|
|
14
14
|
from azure.core.pipeline.policies import AsyncRetryPolicy, RetryMode
|
|
15
|
+
from azure.ai.evaluation._common.onedp._client import ProjectsClient as AIProjectClient
|
|
16
|
+
from azure.ai.evaluation._common.onedp.models import SimulationDTO
|
|
17
|
+
from azure.ai.evaluation._common.constants import RAIService
|
|
15
18
|
|
|
16
19
|
from .._model_tools._template_handler import TemplateParameters
|
|
17
20
|
from .models import OpenAIChatCompletionsModel
|
|
@@ -40,15 +43,15 @@ class SimulationRequestDTO:
|
|
|
40
43
|
headers: Dict[str, str],
|
|
41
44
|
payload: Dict[str, Any],
|
|
42
45
|
params: Dict[str, str],
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
templateKey: str,
|
|
47
|
+
templateParameters: Optional[TemplateParameters],
|
|
45
48
|
):
|
|
46
49
|
self.url = url
|
|
47
50
|
self.headers = headers
|
|
48
51
|
self.json = json.dumps(payload)
|
|
49
52
|
self.params = params
|
|
50
|
-
self.
|
|
51
|
-
self.templateParameters =
|
|
53
|
+
self.templateKey = templateKey
|
|
54
|
+
self.templateParameters = templateParameters
|
|
52
55
|
|
|
53
56
|
def to_dict(self) -> Dict:
|
|
54
57
|
"""Convert the DTO to a dictionary.
|
|
@@ -89,6 +92,7 @@ class ProxyChatCompletionsModel(OpenAIChatCompletionsModel):
|
|
|
89
92
|
self.tkey = template_key
|
|
90
93
|
self.tparam = template_parameters
|
|
91
94
|
self.result_url: Optional[str] = None
|
|
95
|
+
self.simulation_id: Optional[str] = kwargs.pop("simulation_id", "")
|
|
92
96
|
|
|
93
97
|
super().__init__(name=name, **kwargs)
|
|
94
98
|
|
|
@@ -110,7 +114,7 @@ class ProxyChatCompletionsModel(OpenAIChatCompletionsModel):
|
|
|
110
114
|
async def get_conversation_completion(
|
|
111
115
|
self,
|
|
112
116
|
messages: List[Dict],
|
|
113
|
-
session: AsyncHttpPipeline,
|
|
117
|
+
session: Union[AsyncHttpPipeline, AIProjectClient],
|
|
114
118
|
role: str = "assistant", # pylint: disable=unused-argument
|
|
115
119
|
**request_params,
|
|
116
120
|
) -> dict:
|
|
@@ -141,7 +145,7 @@ class ProxyChatCompletionsModel(OpenAIChatCompletionsModel):
|
|
|
141
145
|
|
|
142
146
|
async def request_api(
|
|
143
147
|
self,
|
|
144
|
-
session: AsyncHttpPipeline,
|
|
148
|
+
session: Union[AsyncHttpPipeline, AIProjectClient],
|
|
145
149
|
request_data: dict,
|
|
146
150
|
) -> dict:
|
|
147
151
|
"""
|
|
@@ -162,13 +166,14 @@ class ProxyChatCompletionsModel(OpenAIChatCompletionsModel):
|
|
|
162
166
|
proxy_headers = {
|
|
163
167
|
"Authorization": f"Bearer {token}",
|
|
164
168
|
"Content-Type": "application/json",
|
|
165
|
-
"User-Agent":
|
|
169
|
+
"User-Agent": UserAgentSingleton().value,
|
|
166
170
|
}
|
|
167
171
|
|
|
168
172
|
headers = {
|
|
169
173
|
"Content-Type": "application/json",
|
|
170
174
|
"X-CV": f"{uuid.uuid4()}",
|
|
171
175
|
"X-ModelType": self.model or "",
|
|
176
|
+
"x-ms-client-request-id": self.simulation_id,
|
|
172
177
|
}
|
|
173
178
|
# add all additional headers
|
|
174
179
|
headers.update(self.additional_headers) # type: ignore[arg-type]
|
|
@@ -181,51 +186,101 @@ class ProxyChatCompletionsModel(OpenAIChatCompletionsModel):
|
|
|
181
186
|
headers=headers,
|
|
182
187
|
payload=request_data,
|
|
183
188
|
params=params,
|
|
184
|
-
|
|
185
|
-
|
|
189
|
+
templateKey=self.tkey,
|
|
190
|
+
templateParameters=self.tparam,
|
|
186
191
|
)
|
|
187
192
|
|
|
188
193
|
time_start = time.time()
|
|
189
194
|
full_response = None
|
|
190
195
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
+
if isinstance(session, AIProjectClient):
|
|
197
|
+
sim_request_dto = SimulationDTO(
|
|
198
|
+
headers=headers,
|
|
199
|
+
params=params,
|
|
200
|
+
json=json.dumps(request_data),
|
|
201
|
+
template_key=self.tkey,
|
|
202
|
+
template_parameters=self.tparam,
|
|
203
|
+
)
|
|
204
|
+
response_data = session.red_teams.submit_simulation(sim_request_dto, headers=headers, params=params)
|
|
205
|
+
operation_id = response_data["location"].split("/")[-1]
|
|
206
|
+
|
|
207
|
+
request_count = 0
|
|
208
|
+
flag = True
|
|
209
|
+
while flag:
|
|
210
|
+
try:
|
|
211
|
+
response = session.red_teams.operation_results(operation_id, headers=headers)
|
|
212
|
+
except Exception as e:
|
|
213
|
+
from types import SimpleNamespace # pylint: disable=forgotten-debug-statement
|
|
214
|
+
|
|
215
|
+
response = SimpleNamespace(status_code=202, text=str(e), json=lambda: {"error": str(e)})
|
|
216
|
+
if isinstance(response, dict):
|
|
217
|
+
response_data = response
|
|
218
|
+
flag = False
|
|
219
|
+
break
|
|
220
|
+
if not isinstance(response, SimpleNamespace) and response.get("object") == "chat.completion":
|
|
221
|
+
response_data = response
|
|
222
|
+
flag = False
|
|
223
|
+
break
|
|
224
|
+
else:
|
|
225
|
+
request_count += 1
|
|
226
|
+
sleep_time = RAIService.SLEEP_TIME**request_count
|
|
227
|
+
await asyncio.sleep(sleep_time)
|
|
228
|
+
else:
|
|
229
|
+
# Retry policy for POST request to RAI service
|
|
230
|
+
service_call_retry_policy = AsyncRetryPolicy(
|
|
231
|
+
retry_on_exceptions=[ServiceResponseError],
|
|
232
|
+
retry_total=7,
|
|
233
|
+
retry_backoff_factor=10.0,
|
|
234
|
+
retry_backoff_max=180,
|
|
235
|
+
retry_mode=RetryMode.Exponential,
|
|
196
236
|
)
|
|
197
237
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
self.result_url, headers=proxy_headers
|
|
238
|
+
response = None
|
|
239
|
+
async with get_async_http_client().with_policies(retry_policy=service_call_retry_policy) as retry_client:
|
|
240
|
+
try:
|
|
241
|
+
response = await retry_client.post(
|
|
242
|
+
url=self.endpoint_url, headers=proxy_headers, json=sim_request_dto.to_dict()
|
|
243
|
+
)
|
|
244
|
+
except ServiceResponseError as e:
|
|
245
|
+
self.logger.error("ServiceResponseError during POST request to rai svc after retries: %s", str(e))
|
|
246
|
+
raise
|
|
247
|
+
|
|
248
|
+
# response.raise_for_status()
|
|
249
|
+
if response.status_code != 202:
|
|
250
|
+
raise HttpResponseError(
|
|
251
|
+
message=f"Received unexpected HTTP status: {response.status_code} {response.text()}",
|
|
252
|
+
response=response,
|
|
253
|
+
)
|
|
254
|
+
response_data = response.json()
|
|
255
|
+
|
|
256
|
+
self.result_url = cast(str, response_data["location"])
|
|
257
|
+
retry_policy = AsyncRetryPolicy( # set up retry configuration
|
|
258
|
+
retry_on_status_codes=[202], # on which statuses to retry
|
|
259
|
+
retry_total=7,
|
|
260
|
+
retry_backoff_factor=10.0,
|
|
261
|
+
retry_backoff_max=180,
|
|
262
|
+
retry_mode=RetryMode.Exponential,
|
|
224
263
|
)
|
|
225
264
|
|
|
226
|
-
|
|
265
|
+
# initial 15 seconds wait before attempting to fetch result
|
|
266
|
+
# Need to wait both in this thread and in the async thread for some reason?
|
|
267
|
+
# Someone not under a crunch and with better async understandings should dig into this more.
|
|
268
|
+
await asyncio.sleep(15)
|
|
269
|
+
time.sleep(15)
|
|
270
|
+
|
|
271
|
+
async with get_async_http_client().with_policies(retry_policy=retry_policy) as exp_retry_client:
|
|
272
|
+
token = await self.token_manager.get_token_async()
|
|
273
|
+
proxy_headers = {
|
|
274
|
+
"Authorization": f"Bearer {token}",
|
|
275
|
+
"Content-Type": "application/json",
|
|
276
|
+
"User-Agent": UserAgentSingleton().value,
|
|
277
|
+
}
|
|
278
|
+
response = await exp_retry_client.get( # pylint: disable=too-many-function-args,unexpected-keyword-arg
|
|
279
|
+
self.result_url, headers=proxy_headers
|
|
280
|
+
)
|
|
281
|
+
response.raise_for_status()
|
|
282
|
+
response_data = response.json()
|
|
227
283
|
|
|
228
|
-
response_data = response.json()
|
|
229
284
|
self.logger.info("Response: %s", response_data)
|
|
230
285
|
|
|
231
286
|
# Copy the full response and return it to be saved in jsonl.
|
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
3
|
# ---------------------------------------------------------
|
|
4
4
|
import os
|
|
5
|
-
from typing import Any
|
|
5
|
+
from typing import Any, Dict, List
|
|
6
6
|
from urllib.parse import urljoin, urlparse
|
|
7
|
+
import base64
|
|
8
|
+
import json
|
|
7
9
|
|
|
8
10
|
from azure.ai.evaluation._exceptions import ErrorBlame, ErrorCategory, ErrorTarget, EvaluationException
|
|
9
11
|
from azure.ai.evaluation._http_utils import AsyncHttpPipeline, get_async_http_client, get_http_client
|
|
10
12
|
from azure.ai.evaluation._model_configurations import AzureAIProject
|
|
11
|
-
from azure.ai.evaluation._user_agent import
|
|
13
|
+
from azure.ai.evaluation._user_agent import UserAgentSingleton
|
|
12
14
|
from azure.core.pipeline.policies import AsyncRetryPolicy, RetryMode
|
|
13
15
|
|
|
14
16
|
from ._identity_manager import APITokenManager
|
|
@@ -57,9 +59,11 @@ class RAIClient: # pylint: disable=client-accepts-api-version-keyword
|
|
|
57
59
|
# add a "/" at the end of the url
|
|
58
60
|
self.api_url = self.api_url.rstrip("/") + "/"
|
|
59
61
|
self.parameter_json_endpoint = urljoin(self.api_url, "simulation/template/parameters")
|
|
62
|
+
self.parameter_image_endpoint = urljoin(self.api_url, "simulation/template/parameters/image")
|
|
60
63
|
self.jailbreaks_json_endpoint = urljoin(self.api_url, "simulation/jailbreak")
|
|
61
64
|
self.simulation_submit_endpoint = urljoin(self.api_url, "simulation/chat/completions/submit")
|
|
62
65
|
self.xpia_jailbreaks_json_endpoint = urljoin(self.api_url, "simulation/jailbreak/xpia")
|
|
66
|
+
self.attack_objectives_endpoint = urljoin(self.api_url, "simulation/attackobjectives")
|
|
63
67
|
|
|
64
68
|
def _get_service_discovery_url(self):
|
|
65
69
|
bearer_token = self.token_manager.get_token()
|
|
@@ -144,7 +148,7 @@ class RAIClient: # pylint: disable=client-accepts-api-version-keyword
|
|
|
144
148
|
headers = {
|
|
145
149
|
"Authorization": f"Bearer {token}",
|
|
146
150
|
"Content-Type": "application/json",
|
|
147
|
-
"User-Agent":
|
|
151
|
+
"User-Agent": UserAgentSingleton().value,
|
|
148
152
|
}
|
|
149
153
|
|
|
150
154
|
session = self._create_async_client()
|
|
@@ -166,3 +170,97 @@ class RAIClient: # pylint: disable=client-accepts-api-version-keyword
|
|
|
166
170
|
category=ErrorCategory.UNKNOWN,
|
|
167
171
|
blame=ErrorBlame.USER_ERROR,
|
|
168
172
|
)
|
|
173
|
+
|
|
174
|
+
async def get_image_data(self, path: str) -> Any:
|
|
175
|
+
"""Make a GET Image request to the given url
|
|
176
|
+
|
|
177
|
+
:param path: The url of the image
|
|
178
|
+
:type path: str
|
|
179
|
+
:raises EvaluationException: If the Azure safety evaluation service is not available in the current region
|
|
180
|
+
:return: The response
|
|
181
|
+
:rtype: Any
|
|
182
|
+
"""
|
|
183
|
+
token = self.token_manager.get_token()
|
|
184
|
+
headers = {
|
|
185
|
+
"Authorization": f"Bearer {token}",
|
|
186
|
+
"Content-Type": "application/json",
|
|
187
|
+
"User-Agent": UserAgentSingleton().value,
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
session = self._create_async_client()
|
|
191
|
+
params = {"path": path}
|
|
192
|
+
async with session:
|
|
193
|
+
response = await session.get(
|
|
194
|
+
url=self.parameter_image_endpoint, params=params, headers=headers
|
|
195
|
+
) # pylint: disable=unexpected-keyword-arg
|
|
196
|
+
|
|
197
|
+
if response.status_code == 200:
|
|
198
|
+
return base64.b64encode(response.content).decode("utf-8")
|
|
199
|
+
|
|
200
|
+
msg = (
|
|
201
|
+
"Azure safety evaluation service is not available in your current region, "
|
|
202
|
+
+ "please go to https://aka.ms/azureaistudiosafetyeval to see which regions are supported"
|
|
203
|
+
)
|
|
204
|
+
raise EvaluationException(
|
|
205
|
+
message=msg,
|
|
206
|
+
internal_message=msg,
|
|
207
|
+
target=ErrorTarget.RAI_CLIENT,
|
|
208
|
+
category=ErrorCategory.UNKNOWN,
|
|
209
|
+
blame=ErrorBlame.USER_ERROR,
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
async def get_attack_objectives(
|
|
213
|
+
self, risk_categories: List[str], application_scenario: str = None, strategy: str = None
|
|
214
|
+
) -> Any:
|
|
215
|
+
"""Get the attack objectives based on risk categories and application scenario
|
|
216
|
+
|
|
217
|
+
:param risk_categories: List of risk categories to generate attack objectives for
|
|
218
|
+
:type risk_categories: List[str]
|
|
219
|
+
:param application_scenario: Optional description of the application scenario for context
|
|
220
|
+
:type application_scenario: str
|
|
221
|
+
:param strategy: Optional attack strategy to get specific objectives for
|
|
222
|
+
:type strategy: str
|
|
223
|
+
:return: The attack objectives
|
|
224
|
+
:rtype: Any
|
|
225
|
+
"""
|
|
226
|
+
# Create query parameters for the request
|
|
227
|
+
if application_scenario:
|
|
228
|
+
raise NotImplementedError("Application scenario is not supported yet")
|
|
229
|
+
|
|
230
|
+
params = {
|
|
231
|
+
"api-version": "2022-11-01-preview",
|
|
232
|
+
"riskTypes": ",".join(risk_categories),
|
|
233
|
+
"lang": "en", # Default to English
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
# Add strategy parameter if provided
|
|
237
|
+
if strategy:
|
|
238
|
+
params["strategy"] = strategy
|
|
239
|
+
|
|
240
|
+
try:
|
|
241
|
+
# Make the request using the existing get method
|
|
242
|
+
result = await self.get(self.attack_objectives_endpoint)
|
|
243
|
+
# from collections import defaultdict
|
|
244
|
+
# counts_by_risk = defaultdict(int)
|
|
245
|
+
# for item in result:
|
|
246
|
+
# target_harms = item.get("Metadata", {}).get("TargetHarms", [])
|
|
247
|
+
# if not target_harms:
|
|
248
|
+
# # No risk type specified
|
|
249
|
+
# counts_by_risk["empty"] += 1
|
|
250
|
+
# else:
|
|
251
|
+
# for harm in target_harms:
|
|
252
|
+
# # Use "empty" if the risk type field is missing
|
|
253
|
+
# risk_type = harm.get("RiskType", "") or "empty"
|
|
254
|
+
# counts_by_risk[risk_type] += 1
|
|
255
|
+
return result
|
|
256
|
+
except Exception:
|
|
257
|
+
# If the API fails or isn't implemented yet, return a mock response
|
|
258
|
+
# This is temporary until the API endpoint is fully implemented
|
|
259
|
+
return [
|
|
260
|
+
{
|
|
261
|
+
"metadata": {"lang": "en", "target_harms": [{"risk-type": "violence", "risk-subtype": ""}]},
|
|
262
|
+
"messages": [{"role": "user", "content": "Risky content"}],
|
|
263
|
+
"modality": "text",
|
|
264
|
+
"source": ["source"],
|
|
265
|
+
}
|
|
266
|
+
]
|
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
3
|
# ---------------------------------------------------------
|
|
4
4
|
|
|
5
|
-
from typing import Dict, List, Optional, TypedDict, cast
|
|
6
|
-
|
|
5
|
+
from typing import Dict, List, Optional, TypedDict, cast, Union
|
|
6
|
+
from ast import literal_eval
|
|
7
7
|
from typing_extensions import NotRequired
|
|
8
8
|
|
|
9
9
|
from azure.ai.evaluation._model_configurations import AzureAIProject
|
|
10
|
+
from azure.ai.evaluation._common.onedp._client import ProjectsClient as AIProjectClient
|
|
11
|
+
from azure.ai.evaluation.simulator._adversarial_scenario import AdversarialScenario
|
|
10
12
|
|
|
11
13
|
from ._rai_client import RAIClient
|
|
12
14
|
|
|
@@ -56,6 +58,7 @@ class TemplateParameters(TypedDict):
|
|
|
56
58
|
category: NotRequired[str]
|
|
57
59
|
target_population: NotRequired[str]
|
|
58
60
|
topic: NotRequired[str]
|
|
61
|
+
jailbreak_string: NotRequired[str]
|
|
59
62
|
|
|
60
63
|
|
|
61
64
|
class _CategorizedParameter(TypedDict):
|
|
@@ -144,15 +147,18 @@ class AdversarialTemplate:
|
|
|
144
147
|
|
|
145
148
|
class AdversarialTemplateHandler:
|
|
146
149
|
"""
|
|
147
|
-
|
|
150
|
+
Initialize the AdversarialTemplateHandler.
|
|
148
151
|
|
|
149
|
-
:param azure_ai_project: The Azure AI project
|
|
150
|
-
|
|
151
|
-
:
|
|
152
|
-
:
|
|
152
|
+
:param azure_ai_project: The Azure AI project, which can either be a string representing the project endpoint
|
|
153
|
+
or an instance of AzureAIProject. It contains subscription id, resource group, and project name.
|
|
154
|
+
:type azure_ai_project: Union[str, AzureAIProject]
|
|
155
|
+
:param rai_client: The RAI client or AI Project client used for fetching parameters.
|
|
156
|
+
:type rai_client: Union[~azure.ai.evaluation.simulator._model_tools.RAIClient, ~azure.ai.evaluation._common.onedp._client.AIProjectClient]
|
|
153
157
|
"""
|
|
154
158
|
|
|
155
|
-
def __init__(
|
|
159
|
+
def __init__(
|
|
160
|
+
self, azure_ai_project: Union[str, AzureAIProject], rai_client: Union[RAIClient, AIProjectClient]
|
|
161
|
+
) -> None:
|
|
156
162
|
self.azure_ai_project = azure_ai_project
|
|
157
163
|
self.categorized_ch_parameters: Optional[Dict[str, _CategorizedParameter]] = None
|
|
158
164
|
self.rai_client = rai_client
|
|
@@ -161,8 +167,10 @@ class AdversarialTemplateHandler:
|
|
|
161
167
|
if self.categorized_ch_parameters is None:
|
|
162
168
|
categorized_parameters: Dict[str, _CategorizedParameter] = {}
|
|
163
169
|
util = ContentHarmTemplatesUtils
|
|
164
|
-
|
|
165
|
-
|
|
170
|
+
if isinstance(self.rai_client, RAIClient):
|
|
171
|
+
parameters = await self.rai_client.get_contentharm_parameters()
|
|
172
|
+
elif isinstance(self.rai_client, AIProjectClient):
|
|
173
|
+
parameters = literal_eval(self.rai_client.red_teams.get_template_parameters())
|
|
166
174
|
|
|
167
175
|
for k in parameters.keys():
|
|
168
176
|
template_key = util.get_template_key(k)
|
|
@@ -175,17 +183,29 @@ class AdversarialTemplateHandler:
|
|
|
175
183
|
|
|
176
184
|
template_category = collection_key.split("adv_")[-1]
|
|
177
185
|
|
|
186
|
+
# Handle both qa_enterprise and qa_documents mapping to qa
|
|
187
|
+
if template_category in ["qa_enterprise", "qa_documents"]:
|
|
188
|
+
template_category = "qa"
|
|
189
|
+
|
|
178
190
|
plist = self.categorized_ch_parameters
|
|
179
191
|
ch_templates = []
|
|
192
|
+
|
|
180
193
|
for key, value in plist.items():
|
|
194
|
+
# Skip enterprise templates for ADVERSARIAL_QA
|
|
195
|
+
if collection_key == AdversarialScenario.ADVERSARIAL_QA.value and "enterprise" in key:
|
|
196
|
+
continue
|
|
197
|
+
# Skip non-enterprise templates for ADVERSARIAL_QA_DOCUMENTS
|
|
198
|
+
if collection_key == AdversarialScenario.ADVERSARIAL_QA_DOCUMENTS.value and "enterprise" not in key:
|
|
199
|
+
continue
|
|
200
|
+
|
|
181
201
|
if value["category"] == template_category:
|
|
182
202
|
params = value["parameters"]
|
|
183
203
|
for p in params:
|
|
184
204
|
p.update({"ch_template_placeholder": "{{ch_template_placeholder}}"})
|
|
185
205
|
|
|
186
206
|
template = AdversarialTemplate(template_name=key, text=None, context_key=[], template_parameters=params)
|
|
187
|
-
|
|
188
207
|
ch_templates.append(template)
|
|
208
|
+
|
|
189
209
|
return ch_templates
|
|
190
210
|
|
|
191
211
|
def get_template(self, template_name: str) -> Optional[AdversarialTemplate]:
|
|
@@ -12,6 +12,8 @@ from abc import ABC, abstractmethod
|
|
|
12
12
|
from collections import deque
|
|
13
13
|
from typing import Deque, Dict, List, Optional, Union
|
|
14
14
|
from urllib.parse import urlparse
|
|
15
|
+
from azure.ai.evaluation._common.onedp._client import ProjectsClient as AIProjectClient
|
|
16
|
+
from ._rai_client import RAIClient
|
|
15
17
|
|
|
16
18
|
from azure.ai.evaluation._exceptions import ErrorBlame, ErrorCategory, ErrorTarget, EvaluationException
|
|
17
19
|
from azure.ai.evaluation._http_utils import AsyncHttpPipeline
|
|
@@ -78,7 +80,7 @@ class LLMBase(ABC):
|
|
|
78
80
|
async def get_completion(
|
|
79
81
|
self,
|
|
80
82
|
prompt: str,
|
|
81
|
-
session: AsyncHttpPipeline,
|
|
83
|
+
session: Union[AsyncHttpPipeline, AIProjectClient],
|
|
82
84
|
**request_params,
|
|
83
85
|
) -> dict:
|
|
84
86
|
"""
|
|
@@ -100,7 +102,7 @@ class LLMBase(ABC):
|
|
|
100
102
|
async def get_all_completions(
|
|
101
103
|
self,
|
|
102
104
|
prompts: List[str],
|
|
103
|
-
session: AsyncHttpPipeline,
|
|
105
|
+
session: Union[AsyncHttpPipeline, AIProjectClient],
|
|
104
106
|
api_call_max_parallel_count: int,
|
|
105
107
|
api_call_delay_seconds: float,
|
|
106
108
|
request_error_rate_threshold: float,
|
|
@@ -120,7 +122,7 @@ class LLMBase(ABC):
|
|
|
120
122
|
async def get_conversation_completion(
|
|
121
123
|
self,
|
|
122
124
|
messages: List[dict],
|
|
123
|
-
session: AsyncHttpPipeline,
|
|
125
|
+
session: Union[AsyncHttpPipeline, AIProjectClient],
|
|
124
126
|
role: str,
|
|
125
127
|
**request_params,
|
|
126
128
|
) -> dict:
|
|
@@ -274,7 +276,7 @@ class OpenAICompletionsModel(LLMBase):
|
|
|
274
276
|
async def get_conversation_completion(
|
|
275
277
|
self,
|
|
276
278
|
messages: List[dict],
|
|
277
|
-
session: AsyncHttpPipeline,
|
|
279
|
+
session: Union[AsyncHttpPipeline, AIProjectClient],
|
|
278
280
|
role: str = "assistant",
|
|
279
281
|
**request_params,
|
|
280
282
|
) -> dict:
|
|
@@ -304,7 +306,7 @@ class OpenAICompletionsModel(LLMBase):
|
|
|
304
306
|
async def get_all_completions( # type: ignore[override]
|
|
305
307
|
self,
|
|
306
308
|
prompts: List[Dict[str, str]],
|
|
307
|
-
session: AsyncHttpPipeline,
|
|
309
|
+
session: Union[AsyncHttpPipeline, AIProjectClient],
|
|
308
310
|
api_call_max_parallel_count: int = 1,
|
|
309
311
|
api_call_delay_seconds: float = 0.1,
|
|
310
312
|
request_error_rate_threshold: float = 0.5,
|
|
@@ -372,7 +374,7 @@ class OpenAICompletionsModel(LLMBase):
|
|
|
372
374
|
self,
|
|
373
375
|
request_datas: List[dict],
|
|
374
376
|
output_collector: List,
|
|
375
|
-
session: AsyncHttpPipeline,
|
|
377
|
+
session: Union[AsyncHttpPipeline, AIProjectClient],
|
|
376
378
|
api_call_delay_seconds: float = 0.1,
|
|
377
379
|
request_error_rate_threshold: float = 0.5,
|
|
378
380
|
) -> None:
|
|
@@ -433,7 +435,7 @@ class OpenAICompletionsModel(LLMBase):
|
|
|
433
435
|
|
|
434
436
|
async def request_api(
|
|
435
437
|
self,
|
|
436
|
-
session: AsyncHttpPipeline,
|
|
438
|
+
session: Union[AsyncHttpPipeline, AIProjectClient],
|
|
437
439
|
request_data: dict,
|
|
438
440
|
) -> dict:
|
|
439
441
|
"""
|
|
@@ -476,11 +478,12 @@ class OpenAICompletionsModel(LLMBase):
|
|
|
476
478
|
time_start = time.time()
|
|
477
479
|
full_response = None
|
|
478
480
|
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
481
|
+
if isinstance(session, AIProjectClient):
|
|
482
|
+
response_data = session.red_teams.submit_simulation(request_data, headers, params)
|
|
483
|
+
else:
|
|
484
|
+
response = await session.post(url=self.endpoint_url, headers=headers, json=request_data, params=params)
|
|
485
|
+
response.raise_for_status()
|
|
486
|
+
response_data = response.json()
|
|
484
487
|
|
|
485
488
|
self.logger.info(f"Response: {response_data}")
|
|
486
489
|
|
|
@@ -533,7 +536,7 @@ class OpenAIChatCompletionsModel(OpenAICompletionsModel):
|
|
|
533
536
|
async def get_conversation_completion(
|
|
534
537
|
self,
|
|
535
538
|
messages: List[dict],
|
|
536
|
-
session: AsyncHttpPipeline,
|
|
539
|
+
session: Union[AsyncHttpPipeline, AIProjectClient],
|
|
537
540
|
role: str = "assistant",
|
|
538
541
|
**request_params,
|
|
539
542
|
) -> dict:
|
|
@@ -544,7 +547,7 @@ class OpenAIChatCompletionsModel(OpenAICompletionsModel):
|
|
|
544
547
|
----------
|
|
545
548
|
messages: List of messages to query the model with.
|
|
546
549
|
Expected format: [{"role": "user", "content": "Hello!"}, ...]
|
|
547
|
-
session: AsyncHttpPipeline object to query the model with.
|
|
550
|
+
session: Union[AsyncHttpPipeline, AIProjectClient] object to query the model with.
|
|
548
551
|
role: Not used for this model, since it is a chat model.
|
|
549
552
|
request_params: Additional parameters to pass to the model.
|
|
550
553
|
"""
|
|
@@ -560,7 +563,7 @@ class OpenAIChatCompletionsModel(OpenAICompletionsModel):
|
|
|
560
563
|
async def get_completion(
|
|
561
564
|
self,
|
|
562
565
|
prompt: str,
|
|
563
|
-
session: AsyncHttpPipeline,
|
|
566
|
+
session: Union[AsyncHttpPipeline, AIProjectClient],
|
|
564
567
|
**request_params,
|
|
565
568
|
) -> dict:
|
|
566
569
|
"""
|
|
@@ -569,7 +572,7 @@ class OpenAIChatCompletionsModel(OpenAICompletionsModel):
|
|
|
569
572
|
Parameters
|
|
570
573
|
----------
|
|
571
574
|
prompt: Prompt str to query model with.
|
|
572
|
-
session: AsyncHttpPipeline object to use for the request.
|
|
575
|
+
session: Union[AsyncHttpPipeline, AIProjectClient] object to use for the request.
|
|
573
576
|
**request_params: Additional parameters to pass to the request.
|
|
574
577
|
"""
|
|
575
578
|
messages = [{"role": "system", "content": prompt}]
|
|
@@ -583,7 +586,7 @@ class OpenAIChatCompletionsModel(OpenAICompletionsModel):
|
|
|
583
586
|
async def get_all_completions(
|
|
584
587
|
self,
|
|
585
588
|
prompts: List[str], # type: ignore[override]
|
|
586
|
-
session: AsyncHttpPipeline,
|
|
589
|
+
session: Union[AsyncHttpPipeline, AIProjectClient],
|
|
587
590
|
api_call_max_parallel_count: int = 1,
|
|
588
591
|
api_call_delay_seconds: float = 0.1,
|
|
589
592
|
request_error_rate_threshold: float = 0.5,
|